
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 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)
			{
				document.getElementById("invUser").value="invalid";
				document.form1.email.focus();				
				document.getElementById("errorInfo").innerHTML="Ingresar una dirección de e-mail válida.";
				//alert('Ingresar una dirección de e-mail válida.');
			}
			else if(resp.indexOf("too many rows")!=-1)
			{
				username = document.getElementById("userid");
				if(username.value == '')
				{
					document.getElementById("invUser").value="too";
					document.form1.user_id.focus();
					//alert('Ingrese el nombre de usuario para identificar su cuenta de IDrive.');
					document.getElementById("errorInfo").innerHTML="Ingrese el nombre de usuario para identificar su cuenta de IDrive.";
				}				
			}
			else
			{
				username =  resp.substring(0,(resp.indexOf("<<")));
				document.getElementById("userid").value=username;				
				document.getElementById("invUser").value="";					
			}			
		}
		else
		{
			//alert("Algún problema en respuesta");
		}
	}

}

function displayAlert(){
//document.getElementById('email').focus();
document.form1.email.focus();
}

STREAMNET.idrive.emailVerify = function(thisForm)
{

	document.getElementById("errorInfo").innerHTML="";
	var emailID = trim(thisForm.email.value);

	if(!isValidEmailId(emailID))
	{
		setTimeout('displayAlert()',0);   // Calling this method as on IE6 and IE7 document.form1.email.focus(); was not working
		document.form1.invUser.value = 'invalid';
		document.form1.email.focus();
		document.getElementById("errorInfo").innerHTML="Ingresar una dirección de e-mail válida.";
	}
	else
	{
		document.form1.invUser.value = '';
		var data = "email="+emailID;
		STREAMNET.Ajax.makeRequest("/idrivee/jsp/validateIdriveFeedBackDetails.jsp",data,true,"POST",STREAMNET.idrive.getEmailResponse);
	}

}

STREAMNET.idrive.emailVerifySyn = function(thisForm)
{
	document.getElementById("errorInfo").innerHTML="";
	var emailID = trim(thisForm.email.value);
	var data = "email="+emailID;
	STREAMNET.Ajax.makeRequest("/idrivee/jsp/validateIdriveFeedBackDetails.jsp",data,false,"POST",STREAMNET.idrive.getEmailResponse);

}

/*****************************************/

function IDSupport_local(thisForm) 
{  

	thisForm.email.value = trim(thisForm.email.value);
   thisForm.user_id.value = trim(thisForm.user_id.value);
  // document.getElementById("errorInfo").innerHTML="";

  if(!isValidEmailId(thisForm.email.value))
   {
      thisForm.invEmail.value = "";
	  alert('Ingresar una dirección de e-mail válida.');
      thisForm.email.focus();
      return(false);
   }
   else if(thisForm.user_id.value!='' && invalidUserNameNew(thisForm.user_id.value))
	{
	   alert('El nombre de usuario puede tener solamente los caracteres a-z, 0-9 y _ (Guión Abajo).');
	   thisForm.user_id.focus();
	   return false;

	}
	else if((thisForm.invaliduser.value=="too") && isBlank(thisForm.user_id.value)) 
   {  
	  alert("Ingrese el nombre de usuario para identificar su cuenta de IDrive");
      thisForm.user_id.focus();
	  return false; 
   }
    else if((thisForm.invaliduser.value=="too") && (invalidUserNameNew(thisForm.user_id.value)))
   {
	  alert('El nombre de usuario puede tener solamente los caracteres a-z, 0-9 y _ (Guión Abajo).');
      thisForm.user_id.focus();
	  return false;
   } 
   else if( (thisForm.phone.value!='') && (invalidSecurityPhNum(thisForm.phone.value)))
   {
	   thisForm.phone.focus();
	   alert('El número telefónico puede tener únicamente 0-9 x X - +.');
	   return(false);
   }
    
 /** else if((thisForm.os.value=='choose'))
   {
     alert('Please select the OS');
   //  thisForm.os_others.focus();
     thisForm.os.focus();
     return (false);   
   }
   */

 /**  else if((thisForm.os.value=='Others') && (thisForm.os_others.value==''))
   {
     alert('Please specify the OS');
     //thisForm.os_others.focus();
     thisForm.os_others.focus();
     return (false);   
   }
   */
   else if( (thisForm.program.value=='') || (thisForm.program.value=='choose') )
	{
	   alert('Choose your Program');
	   thisForm.program.focus();
	   return (false);   
	}
	
	else if( (thisForm.program.value=='Others') && (thisForm.otherinfo.value=='') )
	{
	   thisForm.otherinfo.focus();
	   alert('Ingresar sus dudas técnicas / comentarios.');
	   return (false);   
	}
	else{	  
		 STREAMNET.idrive.emailVerifySyn(thisForm);
		if(thisForm.invaliduser.value==""){
		return true;
		}else{
		return false ;
		}	  
	}  
	
	return true ;
	
}


function checkField(){
	if(document.form1.userid.value!=''){
	document.getElementById("errorInfo").innerHTML="";
	document.form1.invaliduser.value = '';
	}
}

function invalidUserNameNew(Str)
 {
	 var alloweduser = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_@.";

	 for(var count = 0; count < Str.length; count++)
	 {
		if(alloweduser.indexOf(Str.substring(count, count + 1)) == -1)
		{
			return(true);
		}
	 };

 }

