function ge(e) {
	return document.getElementById(e);
}

function notifyValid (elid) {
	var inputVal  = ge(elid).value;
	var notifier  = ge(elid+'Notify');
	var anyString = /[a-zA-Z0-9]{2}/;
	var validName = /^[a-zA-Z\s\-\.]{2,}$/;
	var optString = /[a-zA-Z0-9]{0,}/;
	var selectStr = /[^0]/;
	var zipString = /^\d{5}$/;
	var canadaZIP = /^\D{1}\d{1}\D{1}\-?\s?\d{1}\D{1}\d{1}$/;
	var phoneStr  = /^[\(]?\d{3}[\)\-\.\s]*\d{3}[\-\.\s]?\d{4}$/;
	var intlPhStr = /^[0-9\s\-+]{7,}$/;
	var taxidSsn  = /^[0-9\s\-]{9,11}$/;
	var emailStr  = /^[a-zA-Z0-9._%+-]+\@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
	var	checkPass = ge('pass').value;
	var checkInit = termAgree;
	
	switch (elid) {
		case "fname":
		case "lname":
		case "city":
		case "paytocity":
			var stringToMatch = inputVal.search(validName);
			break;
		case "title":
		case "otherstate":
		case "otherpaytostate":
			var stringToMatch = inputVal.search(validName);
			if (inputVal.length>0) {
				if (stringToMatch == -1) {
					notifier.className = "notifyInvalid";
					notifier.innerHTML = "X";
				}
				else {
					notifier.className = "notifyValid";
					notifier.innerHTML = "ok";
				}
			}
			else {
				notifier.className = "notifyValid";
				notifier.innerHTML = "ok";						
			}
			return;
			break;
		case "address2":
		case "im":
		case "paytoaddress2":
		case "comment":
			var stringToMatch = inputVal.search(optString);
			break;
		case "country":
		case "state":
		case "province":
		case "paytocountry":
		case "paytostate":
		case "paytoprovince":
		case "taxclass":
		case "challengequestion":
			var stringToMatch = inputVal.search(selectStr);
			break;
		case "improto":
			if (ge('im').value != '') {
				var stringToMatch = inputVal.search(selectStr);	
			}
			break;
		case "taxid_ssn":
		   var stringToMatch = inputVal.search(taxidSsn);
		   break;
		case "zip":
			switch (ge('country').value) {
				case "1":
					var stringToMatch = inputVal.search(zipString);
					break;
				case "2":
					var stringToMatch = inputVal.search(canadaZIP);
					break;
				default:
					var stringToMatch = inputVal.search(anyString);
			}
			break;
		case "phone":
			switch (ge('country').value) {
				case "1":
				case "2":
					var stringToMatch = inputVal.search(phoneStr);
					break;
				default:
					var stringToMatch = inputVal.search(intlPhStr);
			}
			break;
		case "fax":
			switch (ge('country').value) {
				case "1":
				case "2":
					var stringToMatch = inputVal.search(phoneStr);
					break;
				default:
					var stringToMatch = inputVal.search(intlPhStr);					
			}
			if (inputVal.length>0) {
				if (stringToMatch == -1) {
					notifier.className = "notifyInvalid";
					notifier.innerHTML = "X";
				}
				else {
					notifier.className = "notifyValid";
					notifier.innerHTML = "ok";
				}
			}
			else {
				notifier.className = "notifyValid";
				notifier.innerHTML = "ok";						
			}
			return;
			break;
		case "email":
			var stringToMatch = inputVal.search(emailStr);
			ge('uname').value = inputVal;
			break;
		case "paytozip":
			switch (ge('paytocountry').value) {
				case "1":
					var stringToMatch = inputVal.search(zipString);
					break;
				case "2":
					var stringToMatch = inputVal.search(canadaZIP);
					break;
				default:
					var stringToMatch = inputVal.search(anyString);
			}
			break;
		case "uname":
			if (inputVal.length<5) {
				var stringToMatch = inputVal.search('ABCDEF');
			}
			else {
				checkUsername();
			}
			break;
		case "pass2":
		   var stringToMatch = inputVal.search(checkPass);
			break;
		case "initials":
			var inputVal      = ge(elid).value.toUpperCase();
			var stringToMatch = inputVal.search(checkInit);
			break;
		default:
			var stringToMatch = inputVal.search(anyString);
	}
	if (stringToMatch == -1) {
		notifier.className = "notifyInvalid";
		notifier.innerHTML = "X";
	}
	else {
		notifier.className = "notifyValid";
		notifier.innerHTML = "ok";
	}			
	
}

function showRow(row) {
	ge(row).style.display = "";
}

function hideRow(row) {
	ge(row).style.display = "none";
}

function changeCountry(selection) {
	var cSelect = ge(selection);
	var country = cSelect.options[cSelect.selectedIndex].value;
	switch (selection) {
		case "country":
			switch (country) {
				case "1":
					hideRow('provinceTr'); hideRow('otherstateTr'); showRow('stateTr');
					break;
				case "2":
					hideRow('stateTr'); hideRow('otherstateTr'); showRow('provinceTr');
					break;
				default:
					hideRow('stateTr'); hideRow('provinceTr'); showRow('otherstateTr');
			}
			break;
		case "paytocountry":
			switch (country) {
				case "1":
					hideRow('paytoprovinceTr'); hideRow('otherpaytostateTr'); showRow('paytostateTr');
					showRow('taxclassTr'); showRow('taxid_ssnTr');
					break;
				case "2":
					hideRow('paytostateTr'); hideRow('otherpaytostateTr'); showRow('paytoprovinceTr');
					hideRow('taxclassTr'); hideRow('taxid_ssnTr');
					break;
				default:
					hideRow('paytostateTr'); hideRow('paytoprovinceTr'); showRow('otherpaytostateTr');
					hideRow('taxclassTr'); hideRow('taxid_ssnTr');
			}
			break;			
	}
}

function sameas() {
	var copyFields = new Array('country', 'address1', 'address2', 'city', 'state', 'province', 'zip');
	if (ge('saa').checked == true) {
		for (i=0;i<copyFields.length;i++) {
			var copyFrom = ge(copyFields[i]);
			var copyTo   = ge('payto'+copyFields[i]);
			copyTo.value = copyFrom.value;
			notifyValid('payto'+copyFields[i]);
		}
		ge('otherpaytostate').value = ge('otherstate').value;
		changeCountry('paytocountry');
		if (ge('paytocountry').value == '1') {
			ge('taxclass').focus();
		}
		else {
			ge('paytoname').focus();
			notifyValid('otherpaytostate');
		}
	}
}

function checkStrength() {
	var str = 0;
	var pw  = ge('pass').value;
	var lc  = /[a-z]/;
	var uc  = /[A-Z\!\@\#\$\%\^\&\*\(\)]/;
	var num = /[0-9]/;
	if (pw.search(lc)!=-1) str++;
	if (pw.search(uc)!=-1) str++;
	if (pw.search(num)!=-1)	str++;
	var strength = "weak";
	ge('passNotify').className = "notifyInvalid";
	if (pw.length>0) {
		if (pw.length>6) {
			if (str==2) {
				ge('passNotify').className = "notifyValid";
				var strength = "medium";
			}
			if (str==3) {
				ge('passNotify').className = "notifyValid";
				var strength = "STRONG";
			}
		}
		ge('passNotify').innerHTML = strength;
	}
}

var cStatus   = 2;
var uStatus   = 0;

function httpObject() {
	try {
		req = new XMLHttpRequest();
	}
	catch (err1) {
		try {
			req = new ActiveXObject("Msxml12.XMLHTTP");
		}
		catch (err2) {
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (err3) {
				req = false;
			}
		}
	}
	return req;
}

var http = httpObject();

function handleCaptcha() {
	if (http.readyState == 4) {
		cStatus = http.responseText;
		if (cStatus != 1) {
			alert('The security code that you entered is incorrect. Please try again');
			var randomnumber = Math.floor(Math.random()*1001)
			ge("captchaimage").src = "/CAPTCHA/captcha.php?reloadimage="+randomnumber;
			document.appform.captchainput.value = "";
			document.appform.captchainput.focus();
			return false;
		}
		hideRow('step4');
		ge('bullet4').className = "inactivestepID";
		ge('header4').className = "inactivestep";
		showRow('step5');
		ge('bullet5').className = "activestepID";
		ge('header5').className = "activestep";
		window.scrollTo(0,0);
	}
}

function checkCaptcha() {
   var rnd = Math.random() * 99999999;
	var url       = '/CAPTCHA/captchavalidator.php?value=';
	var captcha   = ge('captchainput').value;
	var required  = ['uname', 'pass', 'pass2', 'challengequestion', 'challengeanswer'];
	var fieldName = ['Username', 'Password', 'Password Verification', 'Question', 'Answer'];
	for (i=0;i<required.length;i++) {
	   if (ge(required[i]+'Notify').className == "notifyInvalid") {
			alert('Please make a correction to the '+fieldName[i]+' field before proceeding to the next page.');
			ge(required[i]).focus();
			return false;
		}
	}	
	http.open("GET", url + captcha, true);
	http.onreadystatechange = handleCaptcha;
	http.send("");
}
function handleUsername() {
	if (http.readyState == 4) {
		uStatus = parseInt(http.responseText);
		if (uStatus != 1) {
			ge('unameNotify').className = "notifyInvalid";
			ge('unameNotify').innerHTML = "TAKEN"; 
		}
		else {
			ge('unameNotify').className = "notifyValid";
			ge('unameNotify').innerHTML = "ok";
		}
	}
}

function checkUsername() {
   var rnd = Math.random() * 99999999;
	var url = '/usernamevalidator.php?u=';
	var captcha = ge('uname').value;
	http.open("GET", url + captcha + '&' + rnd, true);
	http.onreadystatechange = handleUsername;
	http.send("");
}

var termAgree = "FN";

function getInitials() {
	var fFullName   = ge('fname').value;
	var fInitial    = fFullName.charAt(0).toUpperCase();
	var fRestOfName = fFullName.substring(1);
	ge('fnameinitial').innerHTML   = fInitial;
	ge('completedfname').innerHTML = fRestOfName;
	var lFullName   = ge('lname').value;
	var lInitial    = lFullName.charAt(0).toUpperCase();
	var lRestOfName = lFullName.substring(1);
	ge('lnameinitial').innerHTML   = lInitial;
	ge('completedlname').innerHTML = lRestOfName;
	termAgree = ge('fnameinitial').innerHTML+ge('lnameinitial').innerHTML;
}

var confirmClose = false;
window.onbeforeunload = verifyClose;
function verifyClose() {
	if (confirmClose) {
		return "Any data you've entered will be lost.";
	}
}

function changePage(page, validate) {
	if (validate==1) {
		switch (page) {
			case 2:
				notifyValid('uname');
				var optional = new Array('title', 'address2', 'fax', 'im');
				for (i=0;i<optional.length;i++) {
					if (ge(optional[i]).value == '') {
						ge(optional[i]+'Notify').className = "notifyValid";
						ge(optional[i]+'Notify').innerHTML = "ok";	
					}
					ge('improtoNotify').className = "notifyValid";
					ge('improtoNotify').innerHTML = "ok";	
				}
				switch (ge('country').value) {
					case "0":
					case "1":
						var required  = new Array('fname', 'lname', 'title', 'country', 'address1', 'address2', 'city', 'state', 'zip', 'phone', 'email', 'improto');
						var fieldName = new Array('First Name', 'Last Name', 'Title', 'Country', 'Address', '2nd Address', 'City', 'State', 'ZIP Code', 'Phone', 'Email', 'Instant Messenger Protocol');
						break;
					case "2":
						var required  = new Array('fname', 'lname', 'title', 'country', 'address1', 'address2', 'city', 'province', 'zip', 'phone', 'email', 'improto');
						var fieldName = new Array('First Name', 'Last Name', 'Title', 'Country', 'Address', '2nd Address', 'City', 'Province', 'Post Code', 'Phone', 'Email', 'Instant Messenger Protocol');
						break;
					default:
						var required  = new Array('fname', 'lname', 'title', 'country', 'address1', 'address2', 'city', 'otherstate', 'zip', 'phone', 'email', 'improto');
						var fieldName = new Array('First Name', 'Last Name', 'Title', 'Country', 'Address', '2nd Address', 'City', 'State/Territory', 'Post Code', 'Phone', 'Email', 'Instant Messenger Protocol');
				}
				break;
			case 3:
				switch (ge('paytocountry').value) {
					case "0":
					case "1":
						var required  = new Array('company', 'paytocountry', 'paytoaddress1', 'paytoaddress2', 'paytocity', 'paytostate', 'paytozip', 'taxclass', 'taxid_ssn');
						var fieldName = new Array('Company', 'Country', 'Address', '2nd Address', 'City', 'State', 'ZIP Code', 'Tax Classification', 'Tax ID/SSN');
						break;
					case "2":
						var required  = new Array('company', 'paytocountry', 'paytoaddress1', 'paytoaddress2', 'paytocity', 'paytoprovince', 'paytozip');
						var fieldName = new Array('Company', 'Country', 'Address', '2nd Address', 'City', 'Province', 'Post Code');
						break;
					default:
						var required  = new Array('company', 'paytocountry', 'paytoaddress1', 'paytoaddress2', 'paytocity', 'otherpaytostate', 'paytozip');
						var fieldName = new Array('Company', 'Country', 'Address', '2nd Address', 'City', 'State/Territory', 'Post Code');
				}
				break;
			case 4:
				if (ge('comment').value == '') {
					ge('commentNotify').className = 'notifyValid';
					ge('commentNotify').innerHTML = 'ok';
				}
				var required  = new Array('url', 'sitecat', 'sitemktg', 'comment');
				var fieldName = new Array('Web Site', 'Site Category', 'Marketing Method', 'Comments');
				break;
			case 5:
				getInitials();
				checkCaptcha();
				return false; exit();
				break;
			case 6:
				var required = new Array('initials');
				var fieldName = new Array();
				if (ge('initialsNotify').className == "notifyValid") {
					confirmClose = false;
					document.appform.submit();
				}
				else {
					return false;
				}
				break;
		}
		for (i=0;i<required.length;i++) {
			if ((ge(required[i]+'Notify').className == "notifyInvalid")||(ge(required[i]+'Notify').className == "notifyEmpty")) {
//			if (ge(required[i]+'Notify').className == "notifyInvalid") {
				alert('Please make a correction to the '+fieldName[i]+' field before proceeding to the next page.');
				ge(required[i]).focus();
				return false;
			}
		}
	}
	for (j=0;j<6;j++) {
		hideRow('step'+j);
		ge('bullet'+j).className = "inactivestepID";
		ge('header'+j).className = "inactivestep";
	}
	if (page < 6) {
	   showRow('step'+page);
	   ge('bullet'+page).className = "activestepID";
	   ge('header'+page).className = "activestep";
	   window.scrollTo(0,0);
	}
}

function beginApplication() {
	//confirmClose = true;
	changePage(1,0);
}
