// JavaScript Document
/**
 * DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail ID")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

 		 return true					
	}


var xmlHttp
// Removes leading whitespaces
function LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

// Removes ending whitespaces
function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

// Removes leading and ending whitespaces
function trim( value ) {
	
	return LTrim(RTrim(value));
	
}

function check_avail(str)
{
	if(trim(str)=='')
	{
		return false;
	}
	
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	} 
	var url="check_avail.php";
	url=url+"?q="+str;
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
} 
function stateChanged() 
{ 
	if (xmlHttp.readyState==4)
	{
		var dtxt=xmlHttp.responseText;
		if(trim(dtxt)=="true")
		{
			document.getElementById("myans").style.backgroundColor="#49AD49";
			document.getElementById("myans").innerHTML="You can use <b>"+document.form1.nickname.value+"</b> as your member ID.";
		}
		else
		{
			document.getElementById("myans").style.backgroundColor="#FF0000";
			document.getElementById("myans").innerHTML="This member ID is currently used. Please choose another one";
			document.form1.nickname.focus();
		}
	}
}

function check_power(val)
{
	if(document.form1.pass[0].value=='')
	{
		document.getElementById("mypass2").innerHTML='<span style="background-color: red; color: white">Please Fill Password</span>';
		//document.form1.pass[0].focus();
		return false;
	} else
	{
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	} 
	var url="check_password_strength.php";
	url=url+"?q="+val;
	xmlHttp.onreadystatechange=checkPassword;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	}
}

function checkPassword()
{
	if (xmlHttp.readyState==4)
	{
		var dtxt=xmlHttp.responseText;
		document.getElementById("mypass").innerHTML=dtxt;
	}
}

function match_pass()
{
	if(document.form1.pass[0].value!='')
	{
		if(document.form1.pass[0].value!=document.form1.pass[1].value)
		{
			document.getElementById("mypass2").innerHTML='<span style="background-color: red; color: white">Password Not Matched With Above</span>';
			//document.form1.pass[0].focus();
			return false;
		}
		else
		{
			document.getElementById("mypass2").innerHTML='<span style="background-color: #49AD49; color: white">Password Matched</span>';
			return true;
		}
	}
	
}

function blank_email()
{
	if(document.form1.email[0].value=='')
	{
		document.getElementById("email_msg").innerHTML='<span style="background-color: red; color: white">Please Fill Email Address</span>';
		//document.form1.email[0].focus();
		return false;
	}
}




function match_email()
{
	if(document.form1.email[0].value!='')
	{
		if(document.form1.email[0].value!=document.form1.email[1].value)
		{
			document.getElementById("email_msg").innerHTML='<span style="background-color: red; color: white">Email Not Matched With Above</span>';
			//document.form1.email[1].focus();
			return false;
		}
		else
		{
			document.getElementById("email_msg").innerHTML='<span style="background-color: #49AD49; color: white">Email Matched</span>';
			return true;
		}
	}
}

function GetXmlHttpObject()
{
	var xmlHttp=null;
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}


function check_form()
{
		if(document.form1.first_name.value=='')
		{
			alert('Please Fill First Name!');
			document.form1.first_name.focus();
			return false;
		}
	
		if(document.form1.last_name.value=='')
		{
			alert('Please Fill Last Name!');
			document.form1.last_name.focus();
			return false;
		}
	
	/*	if(document.form1.address1.value=='')
		{
			alert('Please Fill Street Address1!');
			document.form1.address1.focus();
			return false;
		}
	*/	
		//if(document.form1.address2.value=='')
		//{
		//	alert('Please Fill Street Address2!');
		//	document.form1.address2.focus();
		//	return false;
		//}
	
/*		if(document.form1.city.value=='')
		{
			alert('Please fill City!');
			document.form1.city.focus();
			return false;
		}
		
		if(!isNaN(document.form1.city.value))
		{
			alert('City name should not be numeric!');
			document.form1.city.focus();
			return false;
		}
		
		if(document.form1.zip.value=='')
		{
			alert('Please Fill Pincode/Zipcode!');
			document.form1.zip.focus();
			return false;
		}
		
		if(!isNaN(document.form1.state.value) || document.form1.state.value=='')
		{
			alert('Please Fill State!');
			document.form1.state.focus();
			return false;
		}	
	
*/		
		
		if(isNaN(document.form1.phone1.value) || document.form1.phone1.value=='')
		{
			alert('Please Fill Phone Number Using Numbers!');
			document.form1.phone1.focus();
			return false;
		}
	
		if(document.form1.email[0].value=='')
		{
			alert('Please Fill Email');
			document.form1.email[0].focus();
			return false;
		}
		
		if (echeck(document.form1.email[0].value)==false)
		{
			//alert('Invalid Email ID')
			document.form1.email[0].value=""
			document.form1.email[0].focus()
			return false
		}
		
		
		if(document.form1.email[1].value=='')
		{
			alert('Please Fill Verify Email');
			document.form1.email[1].focus();
			return false;
		}
		
		if (echeck(document.form1.email[1].value)==false)
		{
			//alert('Invalid Verification Email ID')
			document.form1.email[1].value=""
			document.form1.email[1].focus()
			return false
		}
		
		if(document.form1.email[0].value!=document.form1.email[1].value)
		{
			alert('Email Address Not Verified');
			document.form1.email[0].focus();
			return false;
		}
		
				
	
		if(document.form1.nickname.value=='' )
		{
			alert('Johareez User ID Empty');
			document.form1.nickname.focus();
			return false;
		} 
			
		if(document.form1.nickname.value.length < 6 )
		{
			alert('Johareez User ID Should Have Atleast 6 Charactor');
			document.form1.nickname.focus();
			return false;
		}
	
		if(document.form1.pass[0].value=='')
		{	
			alert('Please Fill Password');
			document.form1.pass[0].focus();
			return false;
		}
		
		if(document.form1.pass[1].value=='')
		{
			alert('Please Fill Verify Password');
			document.form1.pass[1].focus();
			return false;
		}

		if(document.form1.pass[0].value!=document.form1.pass[1].value)
		{
			alert('Password Not Verified ! Re-enter same values in both password field.');
			document.form1.pass[0].focus();
			return(false);
		}				 
	
		if(document.form1.pass[0].value.length < 6 )
		{
			alert("Minimum Length for Password is 6 Charactor");
			document.form1.pass[0].focus();
			return false;
		}
		
		if(document.form1.agree.checked==false)
		{
			alert("You must agree with terms and condition.");
			document.form1.agree.focus();
			return false;
		}
		
		if(document.form1.captcha_input.value=="")
		{
			alert("Please enter the verification code");
			document.form1.captcha_input.focus();
			return false;
		}
}
