// dopeblend.com 2010



//////////////////////////////////////////////////////////////////////////////////////////////////////////////////



//Load the script.

window.onload = initValidate;

//Validate user inputs and create cookies.

function initValidate() {

	var nameCookie = new Cookie('SSTname', 365, 'name', /[;\s]*SSTname\d*\=/g);

	document.forms['contact'].name.onblur = function() {

		nameCookie.create();

	}

	document.forms['contact'].name.onkeyup = function() {

		if (nameCookie.keycode()) {

			nameCookie.insert(nameCookie.locate());

		}

	}

	var snameCookie = new Cookie('SSTsname', 365, 'sname', /[;\s]*SSTsname\d*\=/g);

	document.forms['contact'].sname.onblur = function() {

		snameCookie.create();

	}

	document.forms['contact'].sname.onkeyup = function() {

		if (snameCookie.keycode()) {

			snameCookie.insert(snameCookie.locate());

		}

	}

	var sneedCookie = new Cookie('SSTsneed', 365, 'sneed', /[;\s]*SSTsneed\d*\=/g);

	document.forms['contact'].sneed.onblur = function() {

		sneedCookie.create();

	}

	document.forms['contact'].sneed.onkeyup = function() {

		if (sneedCookie.keycode()) {

			sneedCookie.insert(sneedCookie.locate());

		}

	}

	var btimeCookie = new Cookie('SSTbtime', 365, 'btime', /[;\s]*SSTbtime\d*\=/g);

	document.forms['contact'].btime.onblur = function() {

		btimeCookie.create();

	}

	document.forms['contact'].btime.onkeyup = function() {

		if (btimeCookie.keycode()) {

			btimeCookie.insert(btimeCookie.locate());

		}

	}

	var emailCookie = new Cookie('SSTemail', 365, 'email', /[;\s]*SSTemail\d*\=/g);

	document.forms['contact'].email.onblur = function() {

		emailCookie.create();

	}

	document.forms['contact'].email.onkeyup = function() {

		if (emailCookie.keycode()) {

			emailCookie.insert(emailCookie.locate());

		}

	}

	var hphoneValidate = new Validate('hphone')

	var hphoneCookie = new Cookie('SSTphone', 365, 'hphone', /[;\s]*SSTphone\d*\=/g);

	document.forms['contact'].hphone.onblur = function() {

		hphoneValidate.phone();

		hphoneCookie.create();

	}

	document.forms['contact'].hphone.onkeyup = function() {

		if (hphoneCookie.keycode()) {

			hphoneCookie.insert(hphoneCookie.locate());

		}

	}

	var cellValidate = new Validate('cell');

	var cellCookie = new Cookie('SSTphone', 365, 'cell', /[;\s]*SSTphone\d*\=/g);

	document.forms['contact'].cell.onblur = function() {

		cellValidate.phone();

		cellCookie.create();

	}

	document.forms['contact'].cell.onkeyup = function() {

		if (cellCookie.keycode()) {

			cellCookie.insert(cellCookie.locate());

		}

	}

	var wnoValidate = new Validate('wno');

	var wnoCookie = new Cookie('SSTphone', 365, 'wno', /[;\s]*SSTphone\d*\=/g);

	document.forms['contact'].wno.onblur = function() {

		wnoValidate.phone();

		wnoCookie.create();

	}

	document.forms['contact'].wno.onkeyup = function() {

		if (wnoCookie.keycode()) {

			wnoCookie.insert(wnoCookie.locate());

		}

	}

	var saddrCookie = new Cookie('SSTsaddr', 365, 'saddr', /[;\s]*SSTsaddr\d*\=/g);

	document.forms['contact'].saddr.onblur = function() {

		saddrCookie.create();

	}

	document.forms['contact'].saddr.onkeyup = function() {

		if (saddrCookie.keycode()) {

			saddrCookie.insert(saddrCookie.locate());

		}

	}

	var cityCookie = new Cookie('SSTcity', 365, 'city', /[;\s]*SSTcity\d*\=/g);

	document.forms['contact'].city.onblur = function() {

		cityCookie.create();

	}

	document.forms['contact'].city.onkeyup = function() {

		if (cityCookie.keycode()) {

			cityCookie.insert(cityCookie.locate());

		}

	}

	var zipValidate = new Validate('zip');

	var zipCookie = new Cookie('SSTzip', 365, 'zip', /[;\s]*SSTzip\d*\=/g);

	document.forms['contact'].zip.onblur = function() {

		zipValidate.zip();

		if(zipValidate.tutorLocations()) {

			alert("Congratulations! We have tutors in your area.");

		}

		/*else {

			alert("Sorry, we have no tutors in your area.");

		}*/

		zipCookie.create();

	}

	document.forms['contact'].zip.onkeyup = function() {

		if (zipCookie.keycode()) {

			zipCookie.insert(zipCookie.locate());

		}

	}

}



//////////////////////////////////////////////////////////////////////////////////////////////////////////////////



//Create Validate object

function Validate(inputId) {

	this.input = document.getElementById(inputId);

}

//Validate method: Validates phone numbers

Validate.prototype.phone = function() {

	var phoneRegExp = /^[\(]?\d{3}[\)\.-]?[\s]?\d{3}[\s\.-]?\d{4}$/

	//No number has been entered and it is ok to leave the field.

	if (this.input.value == "") {

		return;

	}

	//A number has been entered but it does not match a correct format.

	else if (!phoneRegExp.test(this.input.value)) {

		alert("Please enter your number in a correct format.\r\n(for example: (123) 465-7890 or 123-456-7890)");

		this.input.focus();

		this.input.select();

	}

}

//Validate method: Validates zip codes

Validate.prototype.zip = function() {

	var zipRegExp1 = /^\d{5}[-\s]?(\d{4})?$/

	//A number has been entered but it does not match a correct format.

	if (this.input.value == "") {

		return;

	}

	//No number has been entered and it is ok to leave the field.

	else if (!zipRegExp1.test(this.input.value)) {

		alert("Pleae enter your zip code in a correct format.\r\n(for example: 12345 or 12345-6789)");

		this.input.focus();

		this.input.select();

	}

}

//Validate method: Validates that there are tutors available in the user's zip code.

Validate.prototype.tutorLocations = function() {

	//Only use the first five digits of the zipcode.

	var zipSS = this.input.value.substring(0,5);

	//Determine which array to search based upon the user's entry.

	if (zipSS >= 85000 && zipSS <= 86599) {

		if(Validate.prototype.tutorAvailability(zipSS, arizona)) {

			return true;

		}

	}

	else if (zipSS >= 90000 && zipSS <= 96199) {

		if(Validate.prototype.tutorAvailability(zipSS, california)) {

			return true;

		}

	}

	else if (zipSS >= 80000 && zipSS <= 81699) {

		if(Validate.prototype.tutorAvailability(zipSS, colorado)) {

			return true;

		}

	}

	else if (zipSS >= 89000 && zipSS <= 89899) {

		if(Validate.prototype.tutorAvailability(zipSS, nevada)) {

			return true;

		}

	}

	else if (zipSS >= 97000 && zipSS <= 97999) {

		if(Validate.prototype.tutorAvailability(zipSS, oregon)) {

			return true;

		}

	}

	else if (zipSS >= 75000 && zipSS <= 79999) {

		if(Validate.prototype.tutorAvailability(zipSS, texas)) {

			return true;

		}

	}

	else if (zipSS >= 84000 && zipSS <= 84799) {

		if(Validate.prototype.tutorAvailability(zipSS, utah)) {

			return true;

		}

	}

	else if (zipSS >= 98000 && zipSS <= 99499) {

		if(Validate.prototype.tutorAvailability(zipSS, washington)) {

			return true;

		}

	}

}



Validate.prototype.tutorAvailability = function(inputValue, state) {

	for (i = 0; i < state.length; i++) {

		if(inputValue == state[i]) {

			return true;

		}

	}

}



////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



//Create Cookie object.

function Cookie(name, days, inputId, cookieRegExp1) {

	this.name = name;

	this.days = days;

	this.input = document.getElementById(inputId);

	this.cookieRegExp1 = cookieRegExp1;

}

//Cookie method: Creates user data cookie

Cookie.prototype.create = function() {

	//Create the cookie expiration date based on number of 'days' entered.

	if (this.days) {

		var date = new Date();

		date.setTime(date.getTime() + (this.days * 24 * 60 * 60 * 1000));

		var expires = "; expires=" + date.toGMTString();

	}

	//No 'days' have been specifies, delete cookie when browser closes.

	else {

		var expires = "";

	}

	cookieValue = document.cookie.match(this.input.value);

	//Exit the function if: 1)The user entry matches a cookie value. 2)There is no entry.

	if (this.input.value == cookieValue || this.input.value == "") {

		return false;

	}

	//If there is no match, create a new cookie.

	else {

		//Create a unique cookie name.

		var x = new Date();

		var y = x.getTime();

		var number = y.toString();

		var cookieName = this.name + number;

		document.cookie = cookieName + "=" + this.input.value + expires + "; path=/";

	}

}

//Cookie method: Inserts cookie value into input field.

Cookie.prototype.insert = function(returnValue) {

	//If the no matching cookie could be located and the return value id null, exit the function.

	if (returnValue == null) {

		return;

	}

	//If a matching cookie was found, insert the return value as selected text.

	else {

		//For non IE browsers.

		if (this.input.setSelectionRange) {

			this.input.value = this.input.value + returnValue;

			this.input.setSelectionRange(this.input.value.length - returnValue.length, this.input.value.length);

		}

		//For IE browser.

		else if (this.input.createTextRange) {

			this.input.value = this.input.value + returnValue;

			var range = this.input.createTextRange();

			range.moveStart("character", this.input.value.length - returnValue.length);

			range.moveEnd("character", this.input.value.length);

			range.select();

		}

		this.input.focus();

	}

}

//Cookie method: Locates cookie and returns its value.

Cookie.prototype.locate = function() {

	//Create a regular expression out of the user input value to insert into the match() function.

	var cookieRegExp2 =  new RegExp('^' + this.input.value + '?');

	//Split the cookie to create an array.

	var cs_1 = document.cookie.split(';');

	//Loop through the array to create an array of each index.

	for (var i = 0; i < cs_1.length; i++) {

		//Remove the cookie's name so that all we have left is the value.

		var cs_2 = cs_1[i].replace(this.cookieRegExp1, '');

		//Find matching text from user input in cookie array.

		if (this.input.value.toLowerCase() == cs_2.toLowerCase().match(cookieRegExp2)) {

			//Create a string value.

			var cs_3 = cs_2.substring(this.input.value.length);

			return cs_3;

		}

	}

}

//Cookie method: Assigns specific keycodes to onkeyup event.

Cookie.prototype.keycode = function() {

	if (event.keyCode >= 48 && event.keyCode <= 90 || event.keyCode >= 96 && event.keyCode <= 111 || event.keyCode >= 186 && event.keyCode <= 222) {

		return true;

	}

}



/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
