/***************************************************************************
	//Company		: netBIOS technologies
	//Site 			: dcmatbusinessschool.com
	//Page 			: Alumni User Registration
	//Created By 	: Geo.V.L
	//Created On 	: 6th April,2009
	//Modified By   : Gireesh C.S
	//Modified on   : 7th April,2009

*****************************************************************************/
function createRequestObject()
{	var request_o; 
	var browser = navigator.appName; 
	if(browser == "Microsoft Internet Explorer")
		request_o = new ActiveXObject("Microsoft.XMLHTTP");
	else		
		request_o = new XMLHttpRequest();	
	return request_o; 
	
}
var http = createRequestObject(); 
/********************************************Functions calling from php pages****************************************************/
//Function for Admin user name availability checking
function checkusername(username)
{	
	if(echeck(username)){
		http.open('get', 'ajax_username_email_check.php?action=username&username='+username);
		http.onreadystatechange = handleAlumniUserNameAvailability;
		http.send(null);
	}
	else{
		document.getElementById('username').value="";
		document.getElementById('username_div').innerHTML = "<span style='color:#FF0000;font-size:11px;'>E-mail is not valid</span>";
	}
}	

function checkFacUserName(username)
{	
	if(username!="")
	{
		if(username.length > 4)
		{
		http.open('get', '../php/ajax_username_email_check.php?action=facultyusername&username='+username);
		http.onreadystatechange = handleFacUserNameAvailability;
		http.send(null);
		}
		else
		{
		document.getElementById('username_div').innerHTML="Username must contain minimum 5 characters";	
		//document.getElementById('username').focus();
		//setTimeout("document.getElementById('username').focus();",1);
		//setTimeout("document.getElementById('username').select();",1);
		}
	}
}

/****************************************End of functions calling from php pages************************************************/
//Retun Function for Admin username availability checking
function handleFacUserNameAvailability()
{	
	if(http.readyState == 4)
	{	
		var response=http.responseText;
		if(response==0)
		{
			document.getElementById('username').value="";
			//document.getElementById('username').focus();
			setTimeout("document.getElementById('username'').focus();",1);
			setTimeout("document.getElementById('username'').select();",1);
			document.getElementById('username_div').innerHTML = "<img src='images/false.png' width='15' height='15'><span style='color:#E9594D;font-size:9px;font-weight:bold;'>&nbsp;Username not Available</span>";
		}
		else if(response==1)
		{
			document.getElementById('username_div').innerHTML = "<img src='images/true.png' width='15' height='15'><span style='color:#4BB426;font-size:9px;font-weight:bold;'>&nbsp;Username Available</span>";	
		}
		
	}
}

function handleAlumniUserNameAvailability()
{	
	if(http.readyState == 4)
	{	
		var response=http.responseText;
		if(response==0)
		{
			document.getElementById('username').value="";
			//document.getElementById('username').focus();
			setTimeout("document.getElementById('username').focus();",1);
			setTimeout("document.getElementById('username').select();",1);
			document.getElementById('username_div').innerHTML = "<img src='images/false.png' width='13' height='13'><span style='color:#E9594D;font-size:9px;font-weight:bold;'>&nbsp;Email address not available</span>";
		}
		else if(response==1)
		{
			document.getElementById('username_div').innerHTML = "<img src='images/true.png' width='13' height='13'><span style='color:#4BB426;font-size:9px;font-weight:bold;'>&nbsp;Email address is available</span>";	
		}
		
	}
}
function checkemail(email,uid)
{	

	if(email!="")
	{
		if(echeck(email)!=false)
		{
			var url =  'ajax_username_email_check.php?action=email&email='+email+'&uid='+uid;
			http.open('get',url);
			http.onreadystatechange = handleAlumniEmailAvailability;
			http.send(null);
		}
		else
		{
			document.getElementById('email_div').innerHTML="<img src='images/false.png' width='15' height='15'>&nbsp;<span style='color:#FF0000;font-size:11px;'>Enter a valid email address</span>";	
		}
	}
	else{
		document.getElementById('email_div').innerHTML="<span  style='color:#FF0000;font-size:11px;'>Enter  email address</span>";	
	}
}

function checkloginname(loginemail,uid)
{	
	if(loginemail!="")
	{
		if(echeck(loginemail)!=false)
		{
			var url =  'ajax_quiz_email_check.php?action=email&email='+loginemail+'&uid='+uid;
			http.open('get',url);
			http.onreadystatechange = handleLoginnameAvailability;
			http.send(null);
		}
		else
		{
			document.getElementById('email_div').innerHTML="<img src='images/false.png' width='15' height='15'>&nbsp;<span style='color:#FF0000;font-size:11px; font-family=Ariel;'>Enter a valid email address</span>";	
		}
	}
	else{
		document.getElementById('email_div').innerHTML="<span  style='color:#FF0000;font-size:11px;'>Enter email address</span>";	
	}
}
function handleLoginnameAvailability()
{	
	if(http.readyState == 4)
	{	
		var response=http.responseText;
		if(response==0)
		{
			document.getElementById('login_name').value="";

			//document.getElementById('email').focus();
			document.getElementById('email_div').innerHTML = "<span  style='color:#FF0000;font-size:11px;'>Email not available</span>";
		}
		else if(response==1)
		{
			document.getElementById('email_div').innerHTML = "<span style='color:#FF0000;font-size:11px;'>Email is available</span>";	
		}
	}
}

function handleAlumniEmailAvailability()
{	
	if(http.readyState == 4)
	{	
		var response=http.responseText;
		if(response==0)
		{
			document.getElementById('pers_email').value="";
			document.getElementById('login_name').value="";

			//document.getElementById('email').focus();
			document.getElementById('email_div').innerHTML = "<img src='images/false.png' width='15' height='15'>&nbsp;<span  style='color:#E9594D;font-size:9px;font-weight:bold;'>Email already choosen by someone. Please use another email</span>";
		}
		else if(response==1)
		{
			document.getElementById('email_div').innerHTML = "<img src='images/true.png' width='15' height='15'>&nbsp;<span style='color:#4BB426;font-size:9px;font-weight:bold;'>Email Id is valid and available</span>";	
		}
	}
}

//Email validation
function echeck(str) 
{
		str=trimSpaces(str);
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1)
		{
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
		{
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
		{
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1)
		 {
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
		 {
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1)
		 {
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1)
		 {
		    return false
		 }

 		 return true					
}

//Trim space
function trimSpaces(stringValue) {
	// Checks the first occurance of spaces and removes them
	for(i = 0; i < stringValue.length; i++) {
		if(stringValue.charAt(i) != " ") {
			break;
		}
	}
	if(i > 0) {
		stringValue = stringValue.substring(i);
	}
	
	// Checks the last occurance of spaces and removes them
	strLength = stringValue.length - 1;
	for(i = strLength; i >= 0; i--) {
		if(stringValue.charAt(i) != " ") {
			break;
		}
	}
	if(i < strLength) {
		stringValue = stringValue.substring(0, i + 1);
	}
	
	// Returns the string after removing leading and trailing spaces.
	return stringValue;
}