// JavaScript Document

//######################################################################################################################################
// Function to check the user name is correctly entered or not
//######################################################################################################################################
//alert ("ok");
function usernamecheck(obj)
{
	if(obj.value =="")
			{
				alert ("Please enter a Username");
				obj.focus();
				return false;
			}
		   else
		   {
				var usr=obj.value;
				var ltr,i;
				
				if(usr.length < 5 )
				{
					alert("Enter minimum 5 characters for the Username ");
					obj.focus();
					return false;
				}
				
				
				for (i=0;i<usr.length;i++)
				{
				ltr = usr.substring(i,i+1);
	//checking whether there are spaces in the username
					if (ltr==' ')
					{
					alert("Invalid Username - Space not allowed ");
					obj.focus();
					return false;
					}
			   }
	//checking whether the first character is an alphabetic character
				if((usr.substring(0,1)<"a" || usr.substring(0,1)>"z") && (usr.substring(0,1)<"A" || usr.substring(0,1)>"Z"))
				{
					alert("The User Name should begin with an alphabetic character.");
					obj.focus();
					return false;
				}
	//checking whether the username contains a special character
				for (var i = 1; i < usr.length; i++) 
				{
					var ch = usr.substring(i, i + 1);
					if ( ((ch < "a" || "z" < ch) && (ch < "A" || "Z" < ch)) && (ch < "0" || "9" < ch) && (ch != '_')) 
					{
						alert("The User Name field  accepts only letters,numbers & underscore.\n\nPlease re-enter your User Name.");
						obj.focus();
						return false;
					}
				}
			   
			}
return true;
}
//######################################################################################################################################
//Function to check the password field
//######################################################################################################################################
function passwordcheck(obj)
{
	if(obj.value =="")
		{
			alert ("Please enter a Password");
			obj.focus();
			return false;
		}
		else
       {
			var pwd=obj.value;
			var ltr,i;

//checking whether the password field contain spaces			

			for (i=0;i<pwd.length;i++)
			{
			ltr = pwd.substring(i,i+1);

				if (ltr==' ')
				{
				alert("Invalid Password - Space not allowed ");
				obj.focus();
				return false;
				}
//checking the length of the password
				if (pwd.length<5)
				{
				alert("Enter minimum 5 characters for the Password");
				obj.focus();
				return false;				
				}	
				if (pwd.length>12)
				{
				alert("Password not to exceed 12 characters");
				obj.focus();
				return false;				
				}			   
		   }
		}
		
	return true;	   
}
//######################################################################################################################################

//######################################################################################################################################
//Function to check the confirm password and both the passwords are equal or not 
//######################################################################################################################################
function confirmpasswordcheck(obj,obj1)
 {
 if(obj.value =="")
		{
			alert ("Please enter Confirm Password");
			obj.focus();
			return false;
		}
		else
       {
			var cpwd=obj.value;
			var ltr,i;
			
//checking whether the confirm password field contain spaces				
			for (i=0;i<cpwd.length;i++)
			{
			ltr = cpwd.substring(i,i+1);

				if (ltr==' ')
				{
				alert("Invalid Confirm Password - Space not allowed\n \nThe Confirm Password and Password must be same ");
				obj.focus();
				return false;
				}
//checking the length of the confirm password			
				if (cpwd.length<5)
				{
				alert("Enter minimum 5 characters for the Confirm Password\n \nThe Confirm Password and Password must be same ");
				obj.focus();
				return false;				
				}	
				if (cpwd.length>12)
				{
				alert("Password should not exceed 12 characters");
				obj.focus();
				return false;				
				}		   
		   }
		}	
		
		//checking whether the password and confirm password is same
		if (obj1.value!=obj.value)
		{
			alert ("Password and Confirm Password are not matching. Please  re-enter");
			obj.focus();
			return false;
		}

	return true;
		
 }
 //#####################################################################################################################################
 
 //#####################################################################################################################################
 //Function to check wheather any of the field is empty
 //#####################################################################################################################################

 function emptycheck(obj,cmd)
  {
 // alert("OKD");
   if(trim(obj.value) =="") //trim function isa an userdefined function defined at end of this file
   {
			alert ("Please enter " + cmd);
			obj.focus();
			return false;
		}
		return true;
  }
  
//#####################################################################################################################################
function combocheck(obj,cmd,defaultvalue)
 {
  var f=defaultvalue;
  if(obj.value==f)
			{
				alert("Please select " +cmd)
				obj.focus();
				return false;
					
			} 
			return true;
 }
 //#####################################################################################################################################
 
 //#####################################################################################################################################
 //Function to check the Email 
 //#####################################################################################################################################
function emailcheck(obj)
{
  if(obj.value=="")
		{
			alert ("Please enter E-mail");
			obj.focus();
			return false;
		}
		
		if(obj.value!="")
		{ 
			if(obj.value.indexOf('@')==-1 || obj.value.indexOf('.')==-1)
			{
			alert("Invalid E-Mail");
			obj.focus();
			return false;
			}
		var em=obj.value;
		var ltr,i,c=0;
		for (i=0;i<em.length;i++)
			{
			ltr = em.substring(i,i+1);
				if(ltr=='@')
					{
						c=c+1;
						lgth1=i;
						if(i<1)
							{
								alert("Invalid E-Mail,email address must not begin with '@'");
								obj.focus();
								return false;
						  }
						if(c>1)
							{	
									alert("Invalid E-Mail,Only one '@' is allowed");
									obj.focus();
									return false;
							}
				   }
			
					if(ltr=='.')
						{
							lgth2=i;
							if(i<1)
								{
									alert("Invalid E-Mail,email address must not begin with '.'");
									obj.focus();
									return false;
							   }
						}
				}
				
			if((lgth2-lgth1)<2)
					{	
						alert("Invalid E-Mail,There should be some domain name between '@' and '.'");
						obj.focus();
						return false;
					}
			
			if((em.length-lgth2)<=1)
					{	
						alert("Invalid E-Mail,No character detected after the '.'");
						obj.focus();
						return false;
					}	
				
		var em=obj.value	
		for (var i = 0; i < em.length; i++) 
			{
				
				var ch = em.substring(i, i + 1);
				if (ch == " ")
				 	{
						alert("Spaces not allowed.\n\nPlease re-enter your Email.");
						obj.focus();
						return false;
					}
						if ( ((ch < "a" || "z" < ch) && (ch < "A" || "Z" < ch)) && (ch < "0" || "9" < ch) && (ch != '_') && (ch != '@') && (ch != '.') && (ch != '-')) 
							{
								alert("The Email field  does not accept special characters.\n\nPlease re-enter your Email.");
								obj.focus();
								return false;
							}
				
			}
		}
		
	return true;	
}
//######################################################################################################################################
//#####################################################################################################################################
 // Funcation to check  the URL
 //#####################################################################################################################################
  function urlcheck(obj)
   {
     if(obj.value=="")
		{ 
			alert("Please enter your web address");
			obj.focus();
			return false;
		}	
    if(obj.value!="")
		{ 
			
		var em=obj.value;
		var ltr,i,len,c=0;
		
		ltr = em.substring(0,4)
		len=em.length;
		entr=em.substring(len-4,len)
		/*if((entr==".com")||(entr==".com"))
				{
				
				} 
		else
				{
				alert ("Invalid url. Please enter the URL in the form www.name.com");
				obj.focus();
				return false;
				}  */
		if((ltr=="www.")||(ltr=="WWW."))
				{
				
				} 
		else
				{
				alert ("Invalid url. Please enter the URL in the form www.name.com");
				obj.focus();
				return false;
				}  
		
		}
    return true;
   }
  //####################################################################################################################################
 
 //function used for right trimmmming the variable
function rtrim(argvalue) {

  while (1) {
    if (argvalue.substring(argvalue.length - 1, argvalue.length) != " ")
      break;
    argvalue = argvalue.substring(0, argvalue.length - 1);
  }

  return argvalue;
}

//function used to trimm left side spaces
function ltrim(argvalue) {

  while (1) {
    if (argvalue.substring(0, 1) != " ")
      break;
    argvalue = argvalue.substring(1, argvalue.length);
  }

  return argvalue;
}

//function for trimming both right and left spaces
function trim(argvalue) {
  var tmpstr = ltrim(argvalue);

  return rtrim(tmpstr);

}

