// JavaScript Document
window.onload = init;

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

function init(){
	
	tacookie = new Cookie("visitordata");
	
	//let's make sure cookies are enabled
	if(tacookie.enabled){
		
		//now let's check to see if they've been here before..
		if(tacookie.ip){
			//visitor is returning ..
		}else{
			//first time visitor ..
		}
		
		//get timestamp
		var timestamp = new Date().getTime();
		
		if(!tacookie.pages){
			//first page
			tacookie.pages + location.href + "@" + timestamp + "|";  //stores each page visited asscotiated with the time visited
		}else{
			//subsequent pages
			tacookie.pages += location.href + "@" + timestamp + "|";  //stores each page visited asscotiated with the time visited
		}
		
		//if we haven't already stored browser info do so now
		// ! this info is only grabbed once !
		var browser = "";
		if(!tacookie.browser){
			for(var propname in navigator){
				if(typeof navigator[propname] == "function" || typeof navigator[propname] == "object") continue; 
				browser += encodeURIComponent(propname + "|" + navigator[propname]);	
			}
			tacookie.browser = browser; 
			
			tacookie.ip = getIp(); //getIp() method is an inline script in header.php, the ip address returned is provided by php's $_SERVER array
		}
		
		/* Debug output 
		data = "";
		for(var prop in tacookie){
			if(typeof tacookie[prop] == "function") continue;
			//alert("prestorage... " + prop);
			data += "prop : " + prop + "\n";
		}
		*/
		
		//finally store the cookie data
		tacookie.store(90); //we'll store it for 90 days
	}else{
		// !-- Cookies are not enabled --!
		//alert("You currently have cookies disabled in your browser, elements of this site may not work correctly. If you'd like to ensure that they do, please enable cookies in your browser");
	}
	
	
	// Begin Form Validation Handler
	var forms = document.getElementsByTagName("form");
	var op_msg = '';//string that will hold output message
	var err_msg = "";
	if(forms.length > 0){
		for(var i=0; i< forms.length;i++);
		{	
			var fields = forms[i-1].length - 1; //minus 1 for the submit button	
			forms[i-1].onsubmit = function(){
				var valid = true;
				var errorEl = document.getElementById("error_msg");
				errorEl.style.display = "none";
				for(var j=0; j<fields; j++){
					if(forms[i-1][j].value == "" && forms[i-1][j].className == "reqd"){
						err_msg = "*Some fields were not completed.<br />";
						forms[i-1][j].style.backgroundColor = "#ffffe8";
						valid = false;
					}
					else{
						forms[i-1][j].style.backgroundColor = "#ffffff";
					}
				}//ends for loop
				var hphone = document.getElementById("hphone");
				var cphone = document.getElementById("cphone");
				
				//foreach next step of validation check to see if it already failed first
				if(valid) valid = emailVal();
				if(valid) valid = passVal();
				
				if(!valid){
					errorEl.innerHTML = err_msg;
					errorEl.style.display = "block";
				}
				return valid;
			}//ends  onsubmit function
		}//ends while loop
	}
} //Init is finished 


function planVal(){
	var errorEl = document.getElementById("error");
	var selected = -1;
	for(var i = document["joinForm"]["plan"].length-1; i > -1; i--){
		if(document["joinForm"]["plan"][i].checked) {
			selected = i; i = -1;	//setting i to -1 stops the loop
		}
	}
	if(selected = -1){
		errorEl.innerHTML = "Please choose a plan\n";
		errorEl.style.display = "block";
		return false;
	}else return true;
}

function numVal(field){
	var PhoneMsgEl = document.getElementById("email_phone_msg");
	if(field.name == "hphone" || field.name == "cphone"){
		//check length 10 digits
		if(field.value.length != 10){
			PhoneMsgEl.style.color = "red";
			PhoneMsgEl.innerHTML = "&nbsp;Phone number(s) must contain 10 digits";
			return false;
		}else{ PhoneMsgEl.innerHTML = "";}
		
		if(/^\d+$/.test(field.value) == false){
			PhoneMsgEl.style.color = "red";
			PhoneMsgEl.innerHTML = "&nbsp;Phone number(s) must only contain numbers";
			return false;
		}else{ PhoneMsgEl.innerHTML = "";}
	}
	return true;
}

function emailVal() {
	var email = document.getElementById("email");
	var EmailMsgEl = document.getElementById("email_phone_msg");
	if(email.value == ''){
		//do nothing because it has already been highlighted
		return false;
	}
	else if(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email.value)){
	return true;
	}
	EmailMsgEl.style.color = "red";
	op_msg = "&nbsp;Invalid Email";
	EmailMsgEl.innerHTML = op_msg;
	return false;
}

function passVal(){
	//start join page's password confirmation
	var pass = document.getElementById("pass");
	var confirm_pass = document.getElementById("confirm_pass");
	var msgEl = document.getElementById("pass_msg");
	var valid = true;
	var illegalChars = /\W/; // allow only letters and numbers

	//first check to see if they match
	if(pass.value != confirm_pass.value){
		msgEl.style.color = "red";
		op_msg = "&nbsp;Passwords do not Match";
		msgEl.innerHTML = op_msg;
		return false;
	}
	//check if they're empty
	if(pass.value == '' || confirm_pass == ''){
		return false;
	}
	if(pass.value.length < 6){
		msgEl.style.color = "red";
		op_msg = "&nbsp;Password is too short";
		msgEl.innerHTML = op_msg;
		return false;
	}
	if(illegalChars.test(pass.value) == true){
		msgEl.style.color = "red";
		op_msg = "&nbsp;Password can only contain numbers and letters";
		msgEl.innerHTML = op_msg;
		return false;
	}	
	//else clear messages and return true
	msgEl.style.color = "#666666";
	msgEl.innerHTML = "&nbsp;Ok";
	return true;
}
	
function recordPlanSelection(plan){
	tacookie.planSelection = plan;
	
	tacookie.store(90);
}