﻿// JScript File
//1. clear/set default text on form field
function clearText(which, txt) {
    if(which.value == txt) which.value="";
}

function setText(which, txt) {
    if(which.value == "") { which.value = txt; }
}

function submitForm(btn) {
	finished = false;

	while (!finished) {
		btn = btn.parentNode;
		if (btn == undefined) {
			break;
		} 
		if (btn.submit!=undefined) {
			finished = true;
			break;
		}
	}
	theForm = document.profile;
	var s = 0;

	var requiredFields = new Array();
	requiredFields = formRequired.split(',');

	for (var i=0; i<requiredFields.length; i++) {
		var thisField = new Array();
		var thisFieldName, thisFieldType;
		thisField = requiredFields[i].split(':');
		s += checkField(thisField[0], thisField[1], theForm, thisField[2])
	}
	if (s == 0) {
		theForm.submit();
	}
	return;
}


function alertError(fieldName) {
   
    if(fieldName =="sex") { document.getElementById("genderlabel").className = 'fieldError'; }
    else { 
    	if(fieldName=="birthdayY" || fieldName=="birthdayM" || fieldName=="birthdayD") { document.getElementById("birthdaylabel").className = 'fieldError'; }
   	    document.getElementById('f_'+fieldName).className = 'fieldBoxError'; 
	}
}

function UnalertError(fieldName) {
    
    if(fieldName == "preferred" || fieldName =="country") {
        if(document.getElementById('f_'+fieldName)) document.getElementById('f_'+fieldName).className = 'fieldSelNormal'; }
    else if(fieldName =="sex") { document.getElementById("genderlabel").className = 'fieldNormal'; }
    else if(fieldName == "birthday") { 
        document.getElementById("birthdaylabel").className = 'fieldNormal'; 
	    document.getElementById("f_birthdayY").className = 'fieldNormal';
	    document.getElementById("f_birthdayM").className = 'fieldNormal';
	    document.getElementById("f_birthdayD").className = 'fieldNormal';
    } else { 
        if(document.getElementById('f_'+fieldName)) document.getElementById('f_'+fieldName).className = 'fieldNormal';  
    }
}

function checkField(fieldName, fieldType, theForm, required) {
	theField = eval("theForm."+fieldName);
	
	UnalertError(fieldName);
	
	switch (fieldType) {
		case 'text':
			return checkTextField(fieldName, theField, required);
			break;
		case 'select':
			return checkTextField(fieldName, theField, required);
			break;
		case 'date':
			if(fieldName == "birthday") {
				var a,b,c = 0;
				theFieldY = eval("theForm."+fieldName+"Y"); a = checkTextField(fieldName+"Y", theFieldY, required);
				theFieldM = eval("theForm."+fieldName+"M"); b = checkTextField(fieldName+"M", theFieldM, required);
				theFieldD = eval("theForm."+fieldName+"D"); c = checkTextField(fieldName+"D", theFieldD, required);
				return (a || b || c);
				
			} else { 
				return checkTextField(fieldName, theField, required);
			}
			break;
		case 'radio':
			return checkBoxField(fieldName, theField, required);
			break;
		case 'check':
			return checkBoxField(fieldName, theField, required);
			break;
		case 'email':
			return checkEmailField(fieldName, theField, required);
			break;
		case 'phone':
			return checkPhoneField(fieldName, theField, required);
			break;
		case 'radio':
			return checkRadioField(fieldName, theField, required);
			break;
		
		default:
			return checkTextField(fieldName, theField, required);
	}
}

function checkRadioField(fieldName, theField, required) {
	var j=0;
	if(theField!=null && required == 1) {
	for (i=0; i<theField.length; i++) {
		if (theField[i].checked) j++
	}
	if (j==0) { 
		alertError(fieldName);
	    return true
	}
	}
	return false
}

function checkBoxField(fieldName, theField, required) {
	if (theField!=null && required==1) {
		var j=0;
		for (var i=0; i<theField.length; i++) {
			if (theField[i].checked) j++
		}
		if (j==0) { 
			alertError(fieldName);
			return true
		}
	}
	return false;
}

function checkTextField(fieldName, theField, required) {
	if (theField!=null && required==1 && (Trim(theField.value)=="" || checkForHTML(theField.value) != -1 
	    || theField.value==l_fname || theField.value==l_lname
	    || theField.value == l_add1 || theField.value == l_add2 || theField.value == l_add3 
	    || theField.value == l_city || theField.value == l_state || theField.value == l_pc)) {
		alertError(fieldName);
		return true;
	}
	return false;
}

function checkEmailField(fieldName, theField, required) {
	if (theField!=null && required==1 && (Trim(theField.value)=="" || !isMail(theField.value))){
		alertError(fieldName);
		return true
	}
	return false
}

function checkPhoneField(fieldName, theField, required) {
	if (theField!=null && required==1 && (Trim(theField.value)=="" || !isValidPhone(theField.value))){
	    alertError(fieldName);
		return true
	}
	return false
}

function HighLightRequired(list) {
    var orders = list.split(",");
    
    for(var i=0;i<orders.length; i++) { 
       var field = orders[i].split(":");
       if(field[2]==1) {  //only for required fields
       var y = document.getElementsByName(field[0])[0];
       if(field[0] == "birthday") { document.profile.birthdayY.className = 'requireddropdown';document.profile.birthdayM.className = 'requireddropdown';document.profile.birthdayD.className = 'requireddropdown';
       } else if(field[0] =="country" || field[0] =="preferred") y.className="requireddropdown";
       else if(field[0] != "sex" ) y.className = 'requiredfield';
       }
    }
    //for IE and remove yellow background from Google ToolBar
    setListeners();

}

function isMail(strInputMail) {
	return(new RegExp(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/).test(strInputMail));
}

function isValidPhone(phonenum) {
	var regu	= "^([0-9(+]+)+([0-9-.() ]*)+([0-9)]+)$"
	var reg		= new RegExp(regu);
	return (phonenum.search(reg) > -1);
	return true;
}

/**
* Method to see if a string contains HTML
*
*   @params string String to check for HTML
*   @returns position of first match, -1 if no match
*/

function checkForHTML(string) {
    var regEx = /<(.|\n)+?>/;
    var result = string.search(regEx); 
    return(result);
}

function Trim(strInput) {
	return strInput.replace(/(^\s*)|(\s*$)/g,"");
}

function ShowZip(v) {
	window.open("/master/welcome/_incs/zipcode.asp?version="+v,"_blank","scrollbars=yes, resizable=no, width=316, height=250");
}

function setListeners(){
  if(navigator.appName =="Microsoft Internet Explorer") {
    inputList = document.getElementsByTagName("INPUT");
    for(i=0;i<inputList.length;i++){
      inputList[i].attachEvent("onpropertychange",restoreStyles);
      inputList[i].style.backgroundColor = "";
	}
    selectList = document.getElementsByTagName("SELECT");
    for(i=0;i<selectList.length;i++){
      selectList[i].attachEvent("onpropertychange",restoreStyles);
      selectList[i].style.backgroundColor = "";
    }

  } else {
    inputList = document.getElementsByTagName("INPUT");
    for(i=0;i<inputList.length;i++){
      inputList[i].addEventListener("onpropertychange",restoreStyles,false);
      inputList[i].style.backgroundColor = "";
	}
    selectList = document.getElementsByTagName("SELECT");
    for(i=0;i<selectList.length;i++){
      selectList[i].addEventListener("onpropertychange",restoreStyles,false);
      selectList[i].style.backgroundColor = "";
    }
}
}

function restoreStyles(){
    if(event.srcElement.style.backgroundColor != "")
      event.srcElement.style.backgroundColor = "";
}
