
STREAMNET.idrive = {};

function trim(Str)
{
   if (!(isBlank(Str)))
   {
  while(''+Str.charAt(0)==' ')
  Str=Str.substring(1,Str.length);
  while(''+Str.charAt(Str.length-1)==' ')
  Str=Str.substring(0,Str.length-1);
  return(Str);
   }
   else
   { return ('');
   };
}

function isBlank(Str)
 {
   while(''+Str.charAt(0)==' ')
   Str=Str.substring(1,Str.length);
   while(''+Str.charAt(Str.length-1)==' ')
   Str=Str.substring(0,Str.length-1);
   if (Str == '')
   { return(true);
   }
   else
   { return(false);
   };
 }

 function isValidEmailId(email)
 {
	if (email == null)
	{
		return(false);
	}
	else if(email == '')
	{
		return(false) ;
	}
	else if ( (trim(email).indexOf('@') == -1) ||
			  (trim(email).indexOf('@') == 0) ||
			  (trim(email).indexOf('@') == (trim(email).length-1) ||  trim(email).indexOf('@',(trim(email).indexOf('@')+1))!=-1))
	{
		return(false);
	}
	else if (hasSpecChar(email))
	{
		return(false) ;
	}
	else
	{
		return(true) ;
	};
 }

 function trim(Str)
 {
   if (!(isBlank(Str)))
   {
	  while(''+Str.charAt(0)==' ')
	  Str=Str.substring(1,Str.length);
	  while(''+Str.charAt(Str.length-1)==' ')
	  Str=Str.substring(0,Str.length-1);
	  return(Str);
   }
   else
   { return ('');
   };
 }

 function hasSpecChar(Str)
 {
	var alloweduser = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_@.-";

	 for(var count = 0; count < Str.length; count++)
	 {
		if(alloweduser.indexOf(Str.substring(count, count + 1)) == -1)
		{
		  return(true);
		}

	 };
	 return(false);
 }


 STREAMNET.idrive.getEmailResponse = function(http_request)
{


//alert('getting called.....');
if(http_request.readyState == 4)
	{
		if(http_request.status == 200)
		{
			//alert('http_request.responseText....'+http_request.responseText);
			
			var resp = http_request.responseText;
			var emailRef = '';
			var userRef = '';
			var userIdRef = '';
			var username = '';

			if(resp.indexOf("invalid")!=-1)
			{
				emailRef = document.getElementById("invEmail");
				emailRef.value = 'true';
				document.form1.email.focus();
				alert('Enter a valid email address.');
			}
			else if(resp.indexOf("too many rows")!=-1)
			{
				
				emailRef = document.getElementById("invEmail");
				emailRef.value = '';
				
				username = document.getElementById("userid");
				if(username.value == '')
				{
					userRef = document.getElementById("invUser");
					document.form1.user_id.focus();
					userRef.value = 'too';
					alert('Enter the username to identify your IDrive account');
				}
				//alert('userRef..'+userRef.value);
			}
			else
			{
				
				//alert('resp...'+resp);
				username =  resp.substring(0,(resp.indexOf("<<")));
				userIdRef  = document.getElementById("userid");
				userIdRef.value = username;

				emailRef = document.getElementById("invEmail");
				emailRef.value = '';

				userRef = document.getElementById("invUser");
				userRef.value = '';
			}

			
		}
		else
		{
			//alert("Some problem in response");
		}
	}

}

STREAMNET.idrive.emailVerify = function(thisForm)
{
			var emailID = thisForm.email.value;
		  emailID = emailID.replace(/^\s+|\s+$/g, '');
	
	if(!isValidEmailId(emailID))
	{
		document.form1.validateEmail.value = 'true';
		document.form1.email.focus();
		alert('Enter a valid email address');
	}
	else
	{
		document.form1.validateEmail.value = '';
		var data = "email="+emailID;
		STREAMNET.Ajax.makeRequest("/idrivee/jsp/validateIdriveFeedBackDetails.jsp",data,true,"POST",STREAMNET.idrive.getEmailResponse);
	}

}

STREAMNET.idrive.emailVerifySyn = function(thisForm)
{
var emailID = thisForm.email.value;
emailID = emailID.replace(/^\s+|\s+$/g, '');

	var data = "email="+emailID;

	STREAMNET.Ajax.makeRequest("/idrivee/jsp/validateIdriveFeedBackDetails.jsp",data,false,"POST",STREAMNET.idrive.getEmailResponse);

}





