//---- forms.js ----
/*
/	Forms Management, Graphic & Print Services
/	Form Functions
/	Created by: Ken Godwin
/	Date: 01-25-02
*/

//passField: any text or textarea field
//alertMsg: (OPTIONAL) the message to display in an alert dialog box, ie "Name field cannot be blank."
//fieldValue: (OPTIONAL) the message to display in the passField, ie "ENTER NAME HERE"
//METHOD CALL: any event or function
function setSelected(passField, alertMsg, fieldValue){
	if(alertMsg)alert(alertMsg);
	if(passField.type.indexOf("select") < 0){
		if(fieldValue) passField.value = fieldValue;
	}
	passField.focus();
	if(passField.type.indexOf("select") < 0) passField.select();
}

//passField: any text field
//tabTo: the field to select upon passField reaching maxChar size
//maxChar:  the maximum number of characters you want entered in passField
//METHOD CALL: onKeyUp="autoTab(fieldName, nextFieldName, maxChar)"
//REQUIRES: setSelected function	if(!maxChar) maxChar = passField.size;
function autoTab(passField, tabTo, maxChar){
	if(passField.value.length >= maxChar){
		setSelected(tabTo, "", "")
	}
}

function isValidZip(zipField){
	if(zipField.value != ""){
		if(zipField.value.length == 5){
			if(isNum(zipField.value)) return true;
			else return false;
		}
		if(zipField.value.length == 10){
			if(zipField.value.substring(5,6) == "-"){
				if(!isNum(zipField.value.substring(0,5))) return false;
				if(!isNum(zipField.value.substring(6,9))) return false;
				return true;
			}
		}
	}
	return false;
}

function isValidDate(m,d,y){
	if(d > 30){
		if(m == 4 || m == 6 || m == 9 || m == 11){
			alert(monName[m-1]	+ " cannot have more than 30 days. Please correct the due date.");
			document.form1.job_due_date_mm.focus();
			return false;
		}
	}
	if(m == 2 && d > 29){
		alert(monName[m-1] + " cannot have more than 29 days. Please correct the due date.");
		document.form1.job_due_date_mm.focus();
		return false;
	}
	return true;
}

function isNum(passValue){
	var valid = "0123456789";
	var temp;
	for (var i=0; i<passValue.length; i++){
		temp = "" + passValue.substring(i, i+1);
		if (valid.indexOf(temp) == -1) return false;
	}
	return true;
}

function isEmail(str) {
  // are regular expressions supported?
  var supported = 0;
  if (window.RegExp) {
    var tempStr = "a";
    var tempReg = new RegExp(tempStr);
    if (tempReg.test(tempStr)) supported = 1;
  }
  if (!supported)
    return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
  return (!r1.test(str) && r2.test(str));
}

function optionsTime(returnMe){
// Writes option tags for time select fields
	returnData=""
	for(i=1;i<3;i++){
	var ampm
		for(t=0;t<12;t++){
			(i==1)?ampm=" AM":ampm=" PM";
			(t==0)?tm=12:tm=t;
			if(returnMe == null || returnMe != 1){
				document.write("<option " + (i==1 && t==8?"SELECTED":"") + ">" + tm + ":00" + ampm + "</option>" +
									"<option>" + tm + ":30" + ampm + "</option>")
			}
			else{
				returnData = returnData + "<option " + (i==1 && t==8?"SELECTED":"") + ">" + tm + ":00" + ampm + "</option>" +
												  "<option>" + tm + ":30" + ampm + "</option>"
			}
		}
	}
	if(returnMe == 1){
		return returnData;
	}
}

function replaceChars(passString, charOut, charAdd) {
//Replaces instances of charOut with charAdd in the passString. Returns resulting string.
	tempString = "" + passString; // temporary holder
	while (tempString.indexOf(charOut)>-1) {
		pos= tempString.indexOf(charOut);
		tempString = "" + (tempString.substring(0, pos) + charAdd + tempString.substring((pos + charOut.length), tempString.length));
	}
	return tempString;
}

