// JavaScript Document
function DisableText()
{
	var count = document.forms[0].elements.length;
	for (i=0; i<count; i++) 
	{
		var element = document.forms[0].elements[i]; 
		if (document.form1.check1.checked == true) 
		{ 
			if (element.type == "text") 
			{ 
				element.disabled=true; 
			} 
		} 
		else 
		{ 
			element.disabled=false; 
		}
	}
}

function validate()
{
	var frm = document.registerfrm;
	var count = frm.elements.length;
	var count1 = frm.elements.length;
	var i;
	if(count > 0)
	{
		//alert(count);
		count = count-3;
		for(i=0;i<count;i++)
		{
			var element = frm.elements[i]; 
			if(element.type=="radio" || element.type=="checkbox")
			{
				var tot_size = document.getElementById(i+"_size").innerHTML;	
				i = parseInt(i) + parseInt(tot_size)-1;
			}
			if(i>0)
				var k = i;
			else
				k = 0;
			var input_type = document.getElementById(k+"_input_type").innerHTML;
			//if(input_type=="date")
			//	i = i+3;
			if(i<count)	
			{
				var err_msg = document.getElementById(k).innerHTML;
				var valid_type = document.getElementById(k+"_validation").innerHTML;
				//alert(i+"=>"+element.name+"=>"+input_type+"=>"+element.value);
				if(input_type!="date" && element.value=="" && element.type!="radio" && element.type!="checkbox")	
				{
					alert("Please enter "+err_msg);
					element.focus();
					return false;
				}
				
				if(input_type=="password")
				{
					if(element.value.search(" ")>-1)
					{
						 alert("Space is not allowed in "+err_msg);
						 element.focus();
						 return false;
					}
					if(element.value.length <6 || element.value.length > 16)
					{
						alert("Please enter 6-16 characters only for "+err_msg);
						element.focus();
						return false;
					}
					if(element.name=="confirm_pwd")
					{
						if(element.value=="")
						{
							alert("Please enter confirm "+err_msg);
							element.focus();
							return false;
						}
						if(element.value.search(" ")>-1)
						{
							 alert("Space is not allowed in "+err_msg);
							 element.focus();
							 return false;
						}
						if(frm.elements[i-1].value!=element.value)
						{
							alert("Both passwords does not match.\n");
							element.focus();
							return false;
						}
					}
				}
				
				if(valid_type!="" && valid_type!="none" && input_type!="date")
				{
					if(valid_type=="number")
					{
						if(!numbersonly(element.value))
						{
							alert("Please enter numbers only.");
							element.focus();
							return false;
						}
					}
					if(valid_type=="alphabets")
					{
						if(!isAllCharacters(element.value))
						{
							alert("Please enter alphabets only.");
							element.focus();
							return false;
						}
					}
					if(valid_type=="number_alpha")
					{
						if(!isNumAlpha(element.value))
						{
							alert("Please enter numbers and alphabets only.");
							element.focus();
							return false;
						}
					}
					if(valid_type=="email")
					{
						if(!isValidEmail(element.value))
						{
							//alert("Please enter numbers and alphabets only.");
							element.focus();
							return false;
						}
					}
					if(valid_type=="text")
					{
						//return true;
					}
				}//end if none
			}
		}//end for
	}//end if
	
}//end validate function

// For existing username check. AJAX functions

var xmlHttp;
function checkuser()
{  
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	} 
	var username = document.getElementById("usrname").value;
	var url = "checkuser.php";
	url = url+"?username="+username;
	url = url+"&e="+Math.random();
	//alert(url);
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}
function stateChanged() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		var content = xmlHttp.responseText;
		if(!content.match("greentxt1"))
		{
			document.getElementById("usrname").value = "";
		}
		document.getElementById("usermsg").innerHTML = content ;
	} 
} 

function GetXmlHttpObject()
{ 
	var objXMLHttp=null;
	if (window.XMLHttpRequest)
	{
		objXMLHttp=new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	return objXMLHttp;
}
function checkimage()
{
	var val = document.frm_imgup;
	if(val.file.value=="")
	{
		alert("Please select image.");	
		val.file.focus();
		return false;
	}
}
function logincheck()
{
	var val = document.frm_login;
	if(val.uname.value=="")
	{
		alert("Please enter user name.\n");
		val.uname.focus();
		return false;
	}
	if(val.pwd.value=="")
	{
		alert("Please enter password.\n");
		val.pwd.focus();
		return false;
	}
}

function valid(act)
{
	var val = document.registerfrm;	
	var pass = val.password.value;
	if(act == "add")
	{
		if(val.username.value=="")
		{
			alert("Please enter user name .\n");
			val.username.focus();
			return false;
		}	
	}
	if(val.username.value.search(" ")>-1)
	{
	     alert("Space is not allowed in username.\n");
		 val.username.focus();
		 return false;
	}
	
	//password checking
	if((act=="edit" && pass!="") || act=="add")
	{
		if(pass=="")
		{
			alert("Please enter password.\n");
			val.password.focus();
			return false;
		}
		if(pass.search(" ")>-1)
		{
			 alert("Space is not allowed in password.\n");
			 val.password.focus();
			 return false;
		}
		if(pass.length <6 || pass.length > 16)
		{
			alert("Please enter 6-16 characters only for password.\n");
			val.password.focus();
			return false;
		}
		if(val.repassword.value=="")
		{
			alert("Please enter confirm password.\n");
			val.repassword.focus();
			return false;
		}
		if(val.repassword.value.search(" ")>-1)
		{
			 alert("Space is not allowed in repassword.\n");
			 val.repassword.focus();
			 return false;
		}
		if(val.password.value!=val.repassword.value)
		{
			alert("Both passwords does not match.\n");
			val.repassword.focus();
			val.repassword.select();			
			return false;
		}
	}
	//end password check
	
	if(val.email.value=="")
	{
		alert("Please enter email address.\n");
		val.email.focus();
		return false;
	}
	if(!val.gender[0].checked && !val.gender[1].checked)
	{
		alert("Please select gender.\n");
		val.gender[0].focus();
		return false;
	}
	if(val.city.value=="")
	{
		alert("Please enter city.\n");
		val.city.focus();
		return false;
	}
	if(val.city.value.search(" ")>-1)
	{
	     alert("Space is not allowed in city.\n");
		 val.city.focus();
		 return false;
	}
	if(val.country.value=="")
	{
		alert("Please select country.\n");
		val.country.focus();
		return false;
	}
	if(val.description.value=="")
	{
		alert("Please enter description.\n");
		val.description.focus();
		return false;
	}
	if(val.file.value=="")
	{
		alert("Please uplode profile photo.");
		val.file.focus();
		return false;
		
	}
    
	if(!val.trm.checked)
	{
		alert("Please agrree terms and condition");
		val.trm.focus();
		return false;
	}
	
    if(act=="add")
	{
		if(val.s_code.value=="")
		{
			alert("Please enter security code.\n");
			val.s_code.focus();
			return false;
		}
	}
	if(isValidEmail(val.email.value)==false)
	{
		alert("Please enter valid email address.");
		val.email.focus();
		val.email.select();			
		return false;
	}
}


//function for checking email address in forgot password page
function emailcheck()
{
	var val = document.frm_forgotpass;
	if(val.email.value=="")
	{
		alert("Please enter email address.\n");
		val.email.focus();
		return false;
	}
	if(isValidEmail(val.email.value)==false)
	{
		//alert("Please enter valid email address.");
		val.email.focus();
		return false;
	}

}

//function for checking email address and name in news letter page
function emailcheck()
{
	var val = document.frm_newsletter;
	if(val.nl_username.value=="")
	{
		alert("Please enter name.\n");
		val.nl_username.focus();
		return false;
	}
	if(val.email.value=="")
	{
		alert("Please enter email address.\n");
		val.email.focus();
		return false;
	}
	if(isValidEmail(val.email.value)==false)
	{
		//alert("Please enter valid email address.");
		val.email.focus();
		return false;
	}

}


function isAllCharacters(objValue)

{

		var characters="' -abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ."

		var tmp

		var lTag

		lTag = 0

		temp = (objValue.length)

		for (var i=0;i<temp;i++)

		{

			tmp=objValue.substring(i,i+1)

			if (characters.indexOf(tmp)==-1)

			{

				lTag = 1

			}

		}

		if(lTag == 1)

			return false

		else

			return true

}
function isNumAlpha(objValue)

{

		var characters="' -abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.0123456789"

		var tmp

		var lTag

		lTag = 0

		temp = (objValue.length)

		for (var i=0;i<temp;i++)

		{

			tmp=objValue.substring(i,i+1)

			if (characters.indexOf(tmp)==-1)

			{

				lTag = 1

			}

		}

		if(lTag == 1)

			return false

		else

			return true

}

function isValidEmail(emailStr)

{

		if(emailStr=="")

		{

				//alert("Please enter email id.");

				return false;

				

		}

			/* The following pattern is used to check if the entered e-mail address

			   fits the user@domain format.  It also is used to separate the username

			   from the domain. */

			var emailPat=/^(.+)@(.+)$/

			/* The following string represents the pattern for matching all special

			   characters.  We don't want to allow special characters in the address. 

			   These characters include ( ) < > @ , ; : \ " . [ ]    */

			//var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"

			var specialChars="\\(\\)<>@,`';:~!#$%^&*+=|{}?\\\\\\\"\\.\\[\\]"

			

			/* The following string represents the range of characters allowed in a 

			   username or domainname.  It really states which chars aren't allowed. */

			var validChars="\[^\\s" + specialChars + "\]"

			/* The following pattern applies if the "user" is a quoted string (in

			   which case, there are no rules about which characters are allowed

			   and which aren't; anything goes).  E.g. "sg cricket"@disney.com

			   is a legal e-mail address. */

			var quotedUser="(\"[^\"]*\")"

			/* The following pattern applies for domains that are IP addresses,

			   rather than symbolic names.  E.g. sg@[123.124.233.4] is a legal

			   e-mail address. NOTE: The square brackets are required. */

			var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/

			/* The following string represents an atom (basically a series of

			   non-special characters.) */

			var atom=validChars + '+'

			/* The following string represents one word in the typical username.

			   For example, in sg.sg@somewhere.com, sg and sg are words.

			   Basically, a word is either an atom or quoted string. */

			var word="(" + atom + "|" + quotedUser + ")"

			// The following pattern describes the structure of the user

			var userPat=new RegExp("^" + word + "(\\." + word + ")*$")

			/* The following pattern describes the structure of a normal symbolic

			   domain, as opposed to ipDomainPat, shown above. */

			var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")





			/* Finally, let's start trying to figure out if the supplied address is

			   valid. */



			/* Begin with the coarse pattern to simply break up user@domain into

			   different pieces that are easy to analyze. */

			var matchArray=emailStr.match(emailPat)

			if (matchArray==null) {

			  /* Too many/few @'s or something; basically, this address doesn't

			     even fit the general mould of a valid e-mail address. */

				alert("Email address seem to be invalid(check @ and .'s)")

				return false

			}

			var user=matchArray[1]

			var domain=matchArray[2]



			// See if "user" is valid 

			if (user.match(userPat)==null) {

			    // user is not valid

			    alert("The username doesn't seem to be valid.")

			    return false

			}



			/* if the e-mail address is at an IP address (as opposed to a symbolic

			   host name) make sure the IP address is valid. */

			var IPArray=domain.match(ipDomainPat)

			if (IPArray!=null) {

			    // this is an IP address

				  for (var i=1;i<=4;i++) {

				    if (IPArray[i]>255) {

				        alert("Destination IP address is invalid!")

					return false

				    }

			    }

			    return true

			}



			// Domain is symbolic name

			var domainArray=domain.match(domainPat)

			if (domainArray==null) {

			alert("The domain name doesn't seem to be valid.")

			    return false

			}


			var atomPat=new RegExp(atom,"g")

			var domArr=domain.match(atomPat)

			var len=domArr.length

			if (domArr[domArr.length-1].length<2 || 

			    domArr[domArr.length-1].length>5) {

			   // the address must end in a two letter or three letter word.

			   alert("The address must end in a three-letter domain, or two letter country.")

			   return false

			}




			// Make sure there's a host name preceding the domain.

			if (len<2) {

			   var errStr="This address is missing a hostname!"

			   alert(errStr)

			   return false

			}



			// If we've gotten this far, everything's valid!

			return true;

}

function isWhitespace(str)

{    var i;

	 var flag

 	 if (isEmpty(str)) return true;	

	    for (i = 0; i < str.length; i++)

 	   {   

 	       // Check that current character isn't whitespace.

 	       var c = str.charAt(i);



		   if (whitespace.indexOf(c) == -1)

		   		return false

 	   }	

 	   // All characters are whitespace.

		    return true;

			

			alert("Please Enter Username");

}
function numbersonly(e,a)

{	// var docf=document.frmRegister.d;

	var unicode=e.charCode? e.charCode : e.keyCode

	

	if (unicode!=8)

	{ 

		if (unicode<48||unicode>57) //if not a number

		{

			return false ;//disable key press

		}

	}

}

