function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}

function encodeNameandValue(sName, sValue){
	var sParam = encodeURIComponent(sName);
	sParam += "=";
	sParam += encodeURIComponent(sValue);
	return sParam;
}

function createXHR(){
	if(typeof XMLHttpRequest != "undefined"){
		return new XMLHttpRequest();	
	} else if (window.ActiveXObject){
		var aVersions = [ "MSXML2.XMLHttp.6.0","MSXML2.XMLHttp.3.0" ];
		for(var i=0;i < aVersions.length;i++){
			try{
				var oXHR = new ActiveXObject(aVersions[i]);
				return oXHR;
			} catch(oError){
				//nothing to do
			}	
		}
	}
	throw new Error("XMLHttp object could not be created. Sorry");
}

function signupContacts(){
	var aParams = new Array();
	var thecode = document.getElementById("c").value;
	aParams.push(encodeNameandValue("thecode", thecode));
	sBody=aParams.join("&");
	
	var newS = document.getElementById("countdiv");
	newS.innerHTML="<img src=\"images/loadingAnimation.gif\" style=\"width:205px;height:13px\" />";
	
	var oXHR = createXHR();
	oXHR.open("post","./js/signup_contacts.inc.php",true);
	oXHR.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	oXHR.onreadystatechange = function () {
		if(oXHR.readyState == 4) {
			if(oXHR.status == 200 || oXHR.status == 304){
				if(oXHR.responseText == "NOTOK") {
					alert('There was a problem calculating the number of contacts.');
					var newC = document.getElementById("countdiv");
					newC.innerHTML="";
				} else {
					var newC = document.getElementById("countdiv");
					var i = oXHR.responseText.indexOf("~");
					if(i > -1){
						var cts = oXHR.responseText.substring(0,i);
						var ctext = oXHR.responseText.substring(i + 1,oXHR.responseText.length);
						newC.innerHTML=ctext;
						set_plan_options(cts);
					} else {
						newC.innerHTML=oXHR.responseText;
					}
				}
			} else {
				alert('There was a problem.');
				var newC = document.getElementById("countdiv");
				newC.innerHTML="";
			}
		}	
	}
	oXHR.send(sBody);
}

function set_plan_options(c) {
	var optns = 1;
	var selmenu = "<select name=\"packages\" id=\"packages\" onchange=\"toCheckOut(this);\"><option value=\"none\">Choose your plan</option>";
	if(c > 999) {
		optns=2;
	}
	if(c > 4999) {
		optns=3;
	}
	switch(optns){
		case 3:
			selmenu += "<option value=\"0\">Basic Exposure Plan</option><option value=\"1\">Premium Exposure Plan</option>";
			break;
		case 2:
			selmenu += "<option value=\"0\">Basic Exposure Plan</option><option value=\"1\">Premium Exposure Plan</option>";
			break;
		case 1:
		default:
			selmenu += "<option value=\"0\">Basic Exposure Plan</option><option value=\"1\">Premium Exposure Plan</option>";
			break;
	}
	selmenu += "</select>";
	var newC = document.getElementById("selectionsdiv");
	newC.innerHTML=selmenu;
}

function checkPcode(pcode) {
	var aParams = new Array();
	aParams.push(encodeNameandValue("pcode", pcode));
	sBody=aParams.join("&");
	
	var newS = document.getElementById("pcodestatusdiv");
	newS.innerHTML="<img src=\"../images/loadingAnimation.gif\" style=\"width:205px;height:13px\" />";
	
	var oXHR = createXHR();
	oXHR.open("post","./check_p.php",true);
	oXHR.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	oXHR.onreadystatechange = function () {
		if(oXHR.readyState == 4) {
			if(oXHR.status == 200 || oXHR.status == 304){
				if(oXHR.responseText == "NOPCODE") {
					var newC = document.getElementById("pcodestatusdiv");
					newC.innerHTML="";
					return false;
				} else if(oXHR.responseText == "INVALID") {
					var newC = document.getElementById("pcodestatusdiv");
					newC.innerHTML="";
					return false;
				} else {
					var URL = oXHR.responseText;
					window.location.href=URL;
				}
			} else {
				alert('There was a problem creating the request.');
				var newC = document.getElementById("pcodestatusdiv");
				newC.innerHTML="";
				return false;
			}
		}	
	}
	oXHR.send(sBody);	
}

function toCheckOut(){
	var selBox = document.getElementById("packages");
	var seloptn = selBox.options[selBox.selectedIndex].value;
	var rno = document.getElementById("c").value;
	//var pcode = document.getElementById("pcode").value;
	var pcode=trim(rno);
	//alert('pcode length is ' + pcode.length);
	if(pcode.length > 0) {
		if(checkPcode(pcode)) {
			alert('There was a problem redirecting you. Please try again.');
			return false;
		} else {
			var URL = "toco.php?r=" + rno + "&o=" + seloptn + "&pcode=" + pcode;
			window.location.href=URL;
		}
	} else {
		var URL = "toco.php?r=" + rno + "&o=" + seloptn + "&pcode=" + pcode;
		window.location.href=URL;
	}
}


	