function getXmlHttpRequestObject(){
	try{
		// Firefox, Opera 8.0+, Safari
		return new XMLHttpRequest();
	}
	catch(e){
		// Internet Explorer
		try{
			return new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e){
			try{
				return new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e){
				alert("Your browser does not support AJAX!");
				return false;
			}
		}//end catch
	}//end catch
}//end function

var xmlRequest = getXmlHttpRequestObject();

function checkUserName(fieldTest,fieldName,type){
	var nameField = document.getElementById(fieldTest).value;
	var phpPath = "";
	switch(type){
		case "1"://from admin/setting
			phpPath = "../../../scripts/ajax-username.php?name="+nameField+"&sid="+Math.random();
		break;
		case "2"://from frontend
			phpPath = "../scripts/ajax-username.php?name="+nameField+"&sid="+Math.random();
		break;
	}//end switch
	xmlRequest.open("GET",phpPath,true);
	xmlRequest.onreadystatechange = function(){
		if(xmlRequest.readyState == 4 && xmlRequest.status == 200) { // Complete
			var returnXML = xmlRequest.responseXML;
			var elements = returnXML.getElementsByTagName("element");
			var returnResult = elements[0].getElementsByTagName("id")[0].firstChild.data;
			
			if(returnResult == "empty"){				
				nameField.value = "";
				document.getElementById(fieldName).value = "";
				document.getElementById("showusername").innerHTML = "";
			}
			else if(returnResult == "0"){	
			
				document.getElementById(fieldName).value = nameField;
				document.getElementById("showusername").innerHTML = nameField;
			}
			else{
				window.alert("User Name is duplicate");
				nameField = "";				
				document.getElementById(fieldName).value = "";
				document.getElementById("showusername").innerHTML = "";
			}
		}//end if
		
	}//end function
	xmlRequest.send(null);
}//end function


var xmlRequest = getXmlHttpRequestObject();

function checkForget(fieldEmail,type){
	
	var emailField = document.getElementById(fieldEmail).value;
	var phpPath = "";
	switch(type){
		case "1"://from admin/setting
			phpPath = "../../../scripts/ajax-forget.php?email="+emailField+"&sid="+Math.random();
		break;
		case "2"://from frontend
			phpPath = "../scripts/ajax-forget.php?email="+emailField+"&sid="+Math.random();
		break;
	}//end switch
	xmlRequest.open("GET",phpPath,true);
	xmlRequest.onreadystatechange = function(){
		if(xmlRequest.readyState == 4 && xmlRequest.status == 200) { // Complete
			var returnXML = xmlRequest.responseXML;
			var elements = returnXML.getElementsByTagName("element");
			
			var returnResult = elements[0].getElementsByTagName("id")[0].firstChild.data;
			
			if(returnResult == "empty"){				
				nameField.value = "";
				//document.getElementById(fieldName).value = "";
				document.getElementById(fieldEmail).value = "";
			}
			else if(returnResult == "0"){	

				window.alert("Email is incorrect");
				nameField = "";				
				//document.getElementById(fieldUser).value = "";
				document.getElementById(fieldEmail).value = "";
				
			}
			else{
			
				//document.getElementById(fieldName).value = nameField;
				//document.getElementById("showusername").innerHTML = nameField;
			}
		}//end if
		
	}//end function
	xmlRequest.send(null);
	
}//end function

var xmlRequest = getXmlHttpRequestObject();

function checkDuplicateEmail(fieldEmail,type){

	var emailField = document.getElementById(fieldEmail).value;
	var phpPath = "";
	switch(type){
		case "1"://from admin/setting
			phpPath = "../../../scripts/ajax-duplicateEmail.php?email="+emailField+"&sid="+Math.random();
		break;
		case "2"://from frontend
			phpPath = "../scripts/ajax-duplicateEmail.php?email="+emailField+"&sid="+Math.random();
		break;
	}//end switch
	xmlRequest.open("GET",phpPath,true);
	xmlRequest.onreadystatechange = function(){
		if(xmlRequest.readyState == 4 && xmlRequest.status == 200) { // Complete
			var returnXML = xmlRequest.responseXML;
			var elements = returnXML.getElementsByTagName("element");
			
			var returnResult = elements[0].getElementsByTagName("id")[0].firstChild.data;
			
			if(returnResult == "empty"){				
				document.getElementById(fieldEmail).value = "";
			}
			else if(returnResult != "0"){	
				window.alert("Email is duplicate");	
				document.getElementById(fieldEmail).value = "";
			
			}
			else{
			
				document.getElementById(fieldEmail).value = emailField;
				//document.getElementById("showusername").innerHTML = nameField;
			}
		}//end if
		
	}//end function
	xmlRequest.send(null);
	
}//end function
