// login.cfm JavaScript

//email regex taken from RegexLib: http://regexlib.com/Search.aspx?k=email
var emailRegex = new RegExp(/^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/);
emailRegex = new RegExp(/^.+@.+\..+$/);

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

var message="";

function clickIE() {if (document.all) {(message);return false;}}
function clickNS(e){
	if (document.layers||(document.getElementById&&!document.all)) {
		if (e.which==2||e.which==3) {(message);return false;}
	}
}
if (document.layers){
	document.captureEvents(Event.MOUSEDOWN);document.onmousedown=clickNS;
}else{
	document.onmouseup=clickNS;document.oncontextmenu=clickIE;
}

document.oncontextmenu=new Function("return false")

function showHideOther(theSelection,other_div){
	var myValue = document.form1.hearAbout.value;
	var myDisplay = "block";
	
	if (document.getElementById){
		// this is the way the standards work
		var style2 = document.getElementById(other_div).style;
		var label	= document.getElementById("hearAboutOtherLabel").style;
		//style2.display = style2.display? "":myDisplay;
	}else if (document.all){
		// this is the way old msie versions work
		var style2 = document.all[other_div].style;
		//style2.display = style2.display? "":myDisplay;
	}else if (document.layers){
		// this is the way nn4 works
		var style2 = document.layers[other_div].style;
		//style2.display = style2.display? "":myDisplay;
	}
	
	//var myInputLabel = document.getElementById("hearAboutOther");
	//myInputLabel.style.color ="0";
	var myInputField = document.getElementById("hearAboutOther");
	myInputField.value ="";
	
	setStyleById("hearAbout","color", "#000000");
	//myInputField.innerHTML = "";
	if(myValue == "other" ){
		style2.display = "block";
		label.innerHTML = "Please specify how you heard about us.";
		//style2.color = "#ff0000";
		//alert("show it");
	}else if(myValue == "printAd"){
		style2.display = "block";
		label.innerHTML = "Which Print Publication?";
	}else if(myValue == "tradeShow"){
		style2.display = "block";
		label.innerHTML = "Which Trade Show?";
	}else if(myValue == "referral"){
		style2.display = "block";
		label.innerHTML = "Who Referred You?";
	}else if(myValue == "retailer"){
		style2.display = "block";
		label.innerHTML = "Which Retailer?";
	}else {
		style2.display = "none";
	}
} // end of function

function validateForm(objForm) {
//alert("objForm.elements.length is: " + objForm.elements.length)
	var i;
	var emptyFields = "";

	for(i=0;i<objForm.elements.length;i++) {
		if (objForm.elements[i].type == "text" || objForm.elements[i].type == "password") {
			//Check text fields...
			var fieldName = objForm.elements[i].name;
			//skip non-required fields
			if((fieldName != "address2")&&(fieldName != "hearAboutOther")&&(fieldName != "fax")&&(fieldName != "dealer")&&(fieldName != "website")){
				if (objForm.elements[i].value.length == 0) {
					emptyFields += " - " + fieldName.toUpperCase() + "\n";
					setStyleById(fieldName,"color", "#ff0000");
				}else{
					setStyleById(fieldName,"color", "#000000");
				}
			}
		} else if (objForm.elements[i].type == "select-one") {
			//Check drop-down...
			
			var fieldName = objForm.elements[i].name;
			//var whichEvent = objForm.city_date.selectedIndex;
			if (fieldName == "state"){
				//require state when country = USA or not selected yet
				if (objForm.country.selectedIndex == 1 || objForm.country.selectedIndex == 0){
					if (objForm.elements[i].selectedIndex == 0){
						emptyFields += " - STATE\n";
						setStyleById(fieldName, "color", "#ff0000");
					}else{
						setStyleById(fieldName, "color", "#000000");
					}
				}else{
					setStyleById(fieldName, "color", "#000000");
				}
			}else if (fieldName == "country"){
				if (objForm.elements[i].selectedIndex == 0){
					emptyFields += " - COUNTRY\n";
					setStyleById(fieldName, "color", "#ff0000");
				}else{
					setStyleById(fieldName, "color", "#000000");
				}
			}else if (fieldName == "title"){
				if (objForm.elements[i].selectedIndex == 0){
					emptyFields += " - TITLE\n";
					setStyleById(fieldName, "color", "#ff0000");
				}else{
					setStyleById(fieldName, "color", "#000000");
				}
			} else if(fieldName == "hearAbout"){
			 	
				if (objForm.elements[i].selectedIndex == 0) {
					emptyFields += " - PLEASE SPECIFY HOW YOU HEARD ABOUT US\n";
					setStyleById("hearAbout","color", "#ff0000");
				} else if(objForm.elements[i].value == "other"){
				
					if (objForm.hearAboutOther.value.length == 0) {
						emptyFields += " - PLEASE SPECIFY HOW YOU HEARD ABOUT US.\n";
						setStyleById("hearAboutOtherLabel","color", "#ff0000");
					}
					else{
						setStyleById("hearAboutOtherLabel","color", "#000000");
					}
				} else if(objForm.elements[i].value == "printAd"){
				
					if (objForm.hearAboutOther.value.length == 0) {
						emptyFields += " - PLEASE SPECIFY THE PRINT PUBLICATION IN WHICH YOU SAW OUR AD.\n";
						setStyleById("hearAboutOtherLabel","color", "#ff0000");
					}else{
						setStyleById("hearAboutOtherLabel","color", "#000000");
					}
				} else if(objForm.elements[i].value == "tradeShow"){
				
					if (objForm.hearAboutOther.value.length == 0) {
						emptyFields += " - PLEASE SPECIFY WHICH TRADE SHOW.\n";
						setStyleById("hearAboutOtherLabel","color", "#ff0000");
					}else{
						setStyleById("hearAboutOtherLabel","color", "#000000");
					}
				} else if(objForm.elements[i].value == "retailer"){
				
					if (objForm.hearAboutOther.value.length == 0) {
						emptyFields += " - PLEASE SPECIFY WHICH RETAILER.\n";
						setStyleById("hearAboutOtherLabel","color", "#ff0000");
					}else{
						//setStyleById(label.name,"color", "#000000");
					}
				} else if(objForm.elements[i].value == "referral"){
				
					if (objForm.hearAboutOther.value.length == 0) {
						emptyFields += " - PLEASE SPECIFY WHO REFERRED YOU.\n";
						setStyleById("hearAboutOtherLabel","color", "#ff0000");
					}else{
						setStyleById("hearAboutOtherLabel","color", "#000000");
					}
				}
			}
		}
	}
	if (emptyFields.length > 0) {
		var strMsg = "All required fields must be filled in. The following fields were left empty:\n\n" + emptyFields;
		alert(strMsg);
		return false;
	} else if (!(objForm.emailAddress.value.match(emailRegex))) {
		//Email address check...
		var strMsg = "Please enter a valid email address.";
		alert(strMsg);
		objForm.emailAddress.focus();
		objForm.emailAddress.select();
		return false;
	}  else {
		return true;
	}
}

function setStyleById(i, p, v) {
	var n = document.getElementById(i);
	n.style[p] = v;
}

function validateLoginForm(objForm) {
	//Email address check...
	if (!(objForm.Email.value.match(emailRegex))) {
		var strMsg = "Please enter a valid email address.";
		alert(strMsg);
		objForm.Email.focus();
		objForm.Email.select();
		return false;
	}
	//password check...
	var testpwd = document.form1.passwordAnswer[0].checked ? true : false;
	var pwd = document.getElementById('Password').value
	pwd = pwd.replace(/ /gi, "");
	if (pwd.length == 0 && testpwd) {
		var strMsg = "Please enter a password.";
		alert(strMsg);
		document.getElementById('Password').focus();
		document.getElementById('Password').select();
		return false;
	}
	return true;
}