var httpObject = null;
var testresults;
var aa=false;
function show_other(selval)
{
	 if(selval == 'other')
	    document.getElementById('security_question').style.display='';
	 else
	    document.getElementById('security_question').style.display='none';
}
function checkemail(str)
{
	//var str=document.validation.emailcheck.value;
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if (filter.test(str))
		testresults=true;
	else{
		alert("Please input a valid email address!");
		testresults=false;
	}
	return (testresults);
}
function emptyValidation(entered,name) 
{  
  var value = entered.value; 
  if(!value)// empty string is false  
  { 
    alert("Required: Please enter "+name);  
    return false;  
  }  
  else 
  	return true;  
} 
function getHTTPObject()
{
	if (window.ActiveXObject) 
		return new ActiveXObject("Microsoft.XMLHTTP");
	else if (window.XMLHttpRequest) 
		return new XMLHttpRequest();
	else {
		alert("Your browser does not support AJAX.");
		return null;
	}
}
// Change the value of the outputText field
function setOutput()
{
	if(httpObject.readyState == 4)
	{
		if(httpObject.responseText==1){
			document.getElementById('availityText').innerHTML = "Username is available.";
			//document.register.Submit.disabled=false;
			//aa=true;
		}
		else if(httpObject.responseText==0){
			document.getElementById('availityText').innerHTML ="<br>Username is not available, please choose another one.";
			//document.register.Submit.disabled=true;
			//aa=false;
		}
		else{			
			document.getElementById('availityText').innerHTML =httpObject.responseText;
			//aa=false;
		}
	}
}
// Implement business logic
function checkuser()
{
	httpObject = getHTTPObject();
	if (httpObject != null) 
	{
		httpObject.open("GET", "checkuser.php?Email="+document.getElementById('Email').value, true);
		httpObject.send(null);
		httpObject.onreadystatechange = setOutput;
	}
}

function checkusernameunique()
{	
	httpObject = getHTTPObject();
	if (httpObject != null) 
	{
		httpObject.open("GET", "checkuser.php?Username="+document.getElementById('Username').value+"&CPID="+document.getElementById('CPID').value, true);
		httpObject.send(null);
		httpObject.onreadystatechange = setOutput;	
	}
}
function updateMeter()
{
	p = document.getElementById("password").value;
	
	var maxWidth = "200";
	var nScore = this.calcStrength(p);

 	var nRound = Math.round(nScore * 2);
	if (nRound > 100) {
		nRound = 100;
	}

	var scoreWidth = (maxWidth / 100) * nRound;
	
	document.getElementById("scoreBar").style.width = scoreWidth+"px"
}
function calcStrength(p)
{
	var intScore = 0;
	intScore += p.length;
			
	if(p.length > 0 && p.length <= 4) {
		intScore += p.length;
	}
	else if (p.length >= 5 && p.length <= 7) {
		intScore += 6;
	}
	else if (p.length >= 8 && p.length <= 15) {
		intScore += 12;
	}
	else if (p.length >= 16) {
		intScore += 18;
	}
	if (p.match(/[a-z]/)) {
		intScore += 1;
	}
	if (p.match(/[A-Z]/)) {
		intScore += 5;
	}
	if (p.match(/\d/)) {
		intScore += 5;
	}
	if (p.match(/.*\d.*\d.*\d/)) {
		intScore += 5;
	}
	if (p.match(/[!,@,#,$,%,^,&,*,?,_,~]/)) {
		intScore += 5;
	}
	if (p.match(/.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~]/)) {
		intScore += 5;
	}
	if (p.match(/(?=.*[a-z])(?=.*[A-Z])/)) {
		intScore += 2;
	}
	if (p.match(/(?=.*\d)(?=.*[a-z])(?=.*[A-Z])/)) {
		intScore += 2;
	}
	if (p.match(/(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!,@,#,$,%,^,&,*,?,_,~])/)) {
		intScore += 2;
	}
	return intScore;
}        
