//---------------------------- Functions to check availability of sponsor name USING AJAX ----------------------


var xmlHttp;
function checkAvailability_sponsor(changed)
{
	if (changed == "typing") { // onChange	
		if (document.getElementById("usernamestatus").innerHTML.indexOf(" is available") == -1) {
			// user is changing a non-validated name, let them do it without interference
			return;
		} else {
			// user is changing a validated name, let them know they need to check it again
			inputBoxStatus("error", document.getElementById("UN"));
			document.getElementById("usernamestatus").parentNode.innerHTML = "<div id='usernamestatus' class='statuserror'>Username changed</div><a href='javascript:checkAvailability_sponsor();' id='checkusername' >Check Availability</a>";
			return;
		}
	} else if (document.getElementById("usernamestatus").innerHTML.indexOf(" is available") != -1) {
		// user is blurring a validated name, don't bother checking
		return;
	}
	
	var str = document.NewAccount_Form.UN.value;
	str=trimAll(str); 	
	
	var UNL = document.NewAccount_Form.UN.value.length;
	
	if(UNL<5)
	{
		inputBoxStatus("error", document.getElementById("UN"));
		document.getElementById("usernamestatus").parentNode.innerHTML = "<div id='usernamestatus' class='statuserror'>Username must contain at least 5 characters</div><a href='javascript:checkAvailability_sponsor();' id='checkusername'>Check Availability</a>";
	}
	else
	{
		xmlHttp=GetXmlHttpObject()
		if (xmlHttp==null)
		{
			alert("Browser does not support HTTP Request");
			return;
		} else {
			inputBoxStatus("error", document.getElementById("UN"));
			document.getElementById("usernamestatus").parentNode.innerHTML = "<div id='usernamestatus' class='statuserror'>Checking...</div>";
		}
	
		var url="http://" + window.location.hostname + "/sponsors/check.php";
		url = url+"?UN="+str;
		url = url+"&sid="+Math.random();
		xmlHttp.onreadystatechange = stateChanged_sponsor;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	}

}


function stateChanged_sponsor() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	 {
	 	// usernamestatus is id of div element used to display output echo coming from php file
	 	if (xmlHttp.responseText.indexOf(" is available") != -1) {
			inputBoxStatus("ok", document.getElementById("UN"));
	 		document.getElementById("usernamestatus").parentNode.innerHTML = xmlHttp.responseText;
	 	} else {
			inputBoxStatus("error", document.getElementById("UN"));
	 		document.getElementById("usernamestatus").parentNode.innerHTML = xmlHttp.responseText + "<a href='javascript:checkAvailability_sponsor();' id='checkusername'>Check Availability</a>";
		 }            
	 } 
}

//---------------------------- Functions to check availability of sponsor name USING AJAX  ENDS  HERE----------------------


//---------------------------- Functions to check availability of user name USING AJAX ----------------------

function checkAvailability_user(changed)
{
	if (changed == "typing") { // onChange	
		if (document.getElementById("usernamestatus").innerHTML.indexOf(" is available") == -1) {
			// user is changing a non-validated name, let them do it without interference
			return;
		} else {
			// user is changing a validated name, let them know they need to check it again
			inputBoxStatus("error", document.getElementById("UN"));
			document.getElementById("usernamestatus").parentNode.innerHTML = "<div id='usernamestatus' class='statuserror'>Username changed</div><a href='javascript:checkAvailability_user();' id='checkusername' >Check Availability</a>";
			return;
		}
	} else if (document.getElementById("usernamestatus").innerHTML.indexOf(" is available") != -1) {
		// user is blurring a validated name, don't bother checking
		return;
	}
	
	
	var str = document.NewAccount_Form.UN.value;
	str=trimAll(str); 	
	
	var UNL = document.NewAccount_Form.UN.value.length;
	
	if(UNL<5)
	{
		inputBoxStatus("error", document.getElementById("UN"));
		document.getElementById("usernamestatus").parentNode.innerHTML = "<div id='usernamestatus' class='statuserror'>Username must contain at least 5 characters</div><a href='javascript:checkAvailability_user();' id='checkusername'>Check Availability</a>";
	}
	else
	{
		xmlHttp=GetXmlHttpObject()
		if (xmlHttp==null)
		{
			alert("Browser does not support HTTP Request");
			return;
		} else {
			inputBoxStatus("error", document.getElementById("UN"));
			document.getElementById("usernamestatus").parentNode.innerHTML = "<div id='usernamestatus' class='statuserror'>Checking...</div>";
		}
	
		var url="http://" + window.location.hostname + "/users/check.php";
		url = url+"?UN="+str;
		url = url+"&sid="+Math.random();
		xmlHttp.onreadystatechange = stateChanged_user;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	}

}


function stateChanged_user() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	 {
	 	// usernamestatus is id of div element used to display output echo coming from php file
	 	if (xmlHttp.responseText.indexOf(" is available") != -1) {
			inputBoxStatus("ok", document.getElementById("UN"));
	 		document.getElementById("usernamestatus").parentNode.innerHTML = xmlHttp.responseText;
	 	} else {
			inputBoxStatus("error", document.getElementById("UN"));
	 		document.getElementById("usernamestatus").parentNode.innerHTML = xmlHttp.responseText + "<a href='javascript:checkAvailability_user();' id='checkusername'>Check Availability</a>";
		 }             
	 } 
}
//---------------------------- Functions to check availability of user name USING AJAX  ENDS  HERE----------------------


//---------------------------- Functions to check availability of country USING AJAX ----------------------
/*
function checkAvailability_country()
{
	var country = document.getElementById("country").value;
	var str = document.NewAccount_Form.country.value;
	country = trimAll(country);
	
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		//alert("Browser does not support HTTP Request");
		return;
	} else {
		//inputBoxStatus("error", document.getElementById("UN"));
		//document.getElementById("usernamestatus").parentNode.innerHTML = "<div id='usernamestatus' class='statuserror'>Checking...</div>";
	}

	var url="http://" + window.location.hostname + "/users/check2.php";
	url = url+"?C="+country;
	url = url+"&sid="+Math.random();
	xmlHttp.onreadystatechange = stateChanged_country;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);

}


function stateChanged_country() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	 {
	 	if (xmlHttp.responseText == "Signup OK") {
	 		document.getElementById("countrystatus").parentNode.innerHTML = "<div id='countrystatus' class='success'></div>";
	 	} else {
	 		document.getElementById("countrystatus").parentNode.innerHTML = "<div id='countrystatus' class='statuserror'>" + xmlHttp.responseText + "</div>";
			 $('#submitWindow').jqmShow();
		 }             
	 } 
}
*/
//---------------------------- Functions to check availability of country USING AJAX  ENDS  HERE----------------------


//---------------------------- Functions to get notification email USING AJAX ----------------------

function notificationSignup()
{
	var email = document.getElementById("listemail").value;
	email = trimAll(email);
	var country = document.getElementById("country").value;
	country = trimAll(country);
	
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		$("#notify").submit(); // give up on ajax and just submit
		// Browser does not support HTTP Request
		return;
	}

	var url="http://" + window.location.hostname + "/texters/notify.php";
	url = url+"?EM="+email;
	url = url+"&C="+country;
	url = url+"&sid="+Math.random();
	xmlHttp.onreadystatechange = stateChanged_notificationSignup;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);

}


function stateChanged_notificationSignup() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	 {
	 	if (xmlHttp.responseText == "OK") {
	 		$("#loading").fadeTo(150, 0, function() {
				$('#loading').addClass('complete csuccess').html("Thanks. We'll send you an email as soon as YouTXT is ready.").animate({opacity:1}, 250);
			});
	 		
	 	} else if (xmlHttp.responseText == "NoNeed") {
	 		$("#loading").fadeTo(150, 0, function() {
				$('#loading').addClass('complete cnoneed').html("Thanks, but you have already registered your email address for this notifcation.").animate({opacity:1}, 250);
			});
			
	 	} else if (xmlHttp.responseText == "Error") {
	 		$("#loading").fadeTo(150, 0, function() {
				$('#loading').addClass('complete cerror').html("There was an error processing your request.").animate({opacity:1}, 250);
			});
			
	 	} else {
			$('#emailerror').addClass('error').html(xmlHttp.responseText).hide();
	 		$("#loading").fadeTo(150, 0, function() {
				$('#loading').hide().css({"opacity": 0});
				$('#notify').show().animate({opacity:1}, 250, function() {
					$('#emailerror').fadeIn(250);
				});
			});
			
		 }             
	 } 
}

//---------------------------- Functions to get notification email USING AJAX  ENDS  HERE----------------------


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;
}


//------------------ String Trim Functions -----------------------------

function trimAll(sString) 
{
	while (sString.substring(0,1) == ' ')              // left trim
	{
		sString = sString.substring(1, sString.length);
	}
	
	while (sString.substring(sString.length-1, sString.length) == ' ')       // right trim
	{
		sString = sString.substring(0,sString.length-1);
	}

	return sString;
}

//---------------------------- Functions to check availability of user name USING AJAX  ENDS  HERE----------------------
