
function createOrder(f) {
	var strOrder = "";
	for (i=0; i < f.sortOrder.length; i++) {
		strOrder = strOrder + f.sortOrder[i].value + ",";
	}
	document.form1.strSortValues.value = strOrder;
	f.submit();
}

function swapOptions(obj,i,j) {
	var o = obj.options;
	var i_selected = o[i].selected;
	var j_selected = o[j].selected;
	var temp = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
	var temp2= new Option(o[j].text, o[j].value, o[j].defaultSelected, o[j].selected);
	o[i] = temp2;
	o[j] = temp;
	o[i].selected = j_selected;
	o[j].selected = i_selected;
}

function moveOptionUp(obj) {
	// If > 1 option selected, do nothing
	var selectedCount=0;
	for (i=0; i<obj.options.length; i++) {
		if (obj.options[i].selected) {
			selectedCount++;
			}
		}
	if (selectedCount > 1) {
		return;
		}
	// If this is the first item in the list, do nothing
	var i = obj.selectedIndex;
	if (i == 0) {
		return;
		}
	swapOptions(obj,i,i-1);
	obj.options[i-1].selected = true;
}

function moveOptionDown(obj) {
	// If > 1 option selected, do nothing
	var selectedCount=0;
	for (i=0; i<obj.options.length; i++) {
		if (obj.options[i].selected) {
			selectedCount++;
			}
		}
	if (selectedCount > 1) {
		return;
		}
	// If this is the last item in the list, do nothing
	var i = obj.selectedIndex;
	if (i == (obj.options.length-1)) {
		return;
		}
	swapOptions(obj,i,i+1);
	obj.options[i+1].selected = true;
}

function isIE5() {
	var agt			= navigator.userAgent.toLowerCase();
	var is_major	= parseInt(navigator.appVersion);
	var is_minor	= parseFloat(navigator.appVersion);
	var is_ie		= ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
	var is_ie3		= (is_ie && (is_major < 4));
	var is_ie4		= (is_ie && (is_major == 4) && (agt.indexOf("msie 4")!=-1) );
	var is_ie5up	= (is_ie && !is_ie3 && !is_ie4);
    
	if (is_ie5up) {
		return true;
	} else {
		return false;
	}
}

function openEditor(field) {
	var newWin;
	newWin = window.open("../editor/default.asp?field="+field,"","height=500,width=550,toolbar=no,menubar=no,top=50,left=50");
}

function confirmDelete(idToDelete,filename) {
	var msgStr = "Are you sure you want to delete this?";
	if (confirm(msgStr)) {
		window.location = "../delete/" + filename + "?id=" + idToDelete;
	}
}

function confirmMultiDelete() {
	var msgStr = "Are you sure you want to delete selected records?";
	if (confirm(msgStr)) {
		return true;	
	} else {
		return false;
	}
}

function prepopulateDropdown(field,currValue) {
	for (i=0; i < document.form1.elements[field].length; i++) {
		if (document.form1.elements[field].options[i].value == currValue) {
			document.form1.elements[field].options[i].selected = true;
		}
	}
}
function prepopulateRadio(field,currValue) {
	for (i=0; i < document.form1.elements[field].length; i++) {
		if (document.form1.elements[field][i].value == currValue) {
			document.form1.elements[field][i].checked = true;
		}
	}
}
function prepopulateCheckbox(field,currValue) {
	if (document.form1.elements[field].value == currValue || currValue == 'on') {
		document.form1.elements[field].checked = true;
	}
}
function prepopulateText(field,currValue) {
	document.form1.elements[field].value = currValue;
}

function openCalendar(fieldToPopulate,valueToPrepopulate) {
	var newWin;
	newWin = window.open("../popupCal.asp?field=" + fieldToPopulate + "&value=" + valueToPrepopulate,"Popup","height=300,width=300,top=200,left=400");
}

function formSort(field,order) {
	document.form_sort.sort.value = field;
	document.form_sort.by.value = order;
	form_sort.submit();
}

function redirMenu(url) {
	if (url != "") {
		document.location = url;
	}
}

// based on script by Sandeep V. Tamhankar (stamhankar@hotmail.com) 
function verifyEmail(emailStr) {
	var emailPat = /^(.+)@(.+)$/;
	var specialChars = "\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
	var validChars = "\[^\\s" + specialChars + "\]";
	var quotedUser = "(\"[^\"]*\")";
	var ipDomainPat = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom = validChars + '+';
	var word = "(" + atom + "|" + quotedUser + ")";
	var userPat = new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat= new RegExp("^" + atom + "(\\." + atom +")*$");

	var matchArray = emailStr.match(emailPat);

	if (matchArray == null) { return false;}

	var user = matchArray[1]
	var domain = matchArray[2]
	if (user.match(userPat) == null) { return false; }

	var IPArray=domain.match(ipDomainPat)
	if (IPArray != null) {
		for (var i=1; i<=4; i++) {
	    		if (IPArray[i] > 255) {
	        		return false;
	    		}
    	}
   		return true;
	}

	var domainArray = domain.match(domainPat)
	if (domainArray == null) {
		return false;
	}

	var atomPat = new RegExp(atom,"g")
	var domArr = domain.match(atomPat)
	var len = domArr.length
	if (domArr[domArr.length-1].length < 2 || domArr[domArr.length-1].length > 3) {
		return false;
	}

	if (len < 2) { return false; }
	
   	return true;
}

function verifySSN(inputStr) {
	var matchArr = ssn.match(/^(\d{3})-?\d{2}-?\d{4}$/);
	var numDashes = ssn.split('-').length - 1;
	
	if (matchArr == null || numDashes == 1) {
		return false;
	} else if (parseInt(matchArr[1],10) == 0) {
		return false;
	} else {
		return true;  
	}
}

// based on script by Brian Swalwell 
function verifyZip(field) {
	var valid = "0123456789-";
	var hyphencount = 0;

	if (field.length != 5 && field.length != 10) {
		return false;
	}

	for (var i=0; i < field.length; i++) {
		temp = "" + field.substring(i, i+1);
		if (temp == "-") hyphencount++;
		if (valid.indexOf(temp) == "-1") {
			return false;
		}
		if ((hyphencount > 1) || ((field.length==10) && ""+field.charAt(5)!="-")) {
			return false;
		}
	}
	
	return true;
}

function verifyPhone1(strInput) {
	//for format (xxx) yyy-zzzz
	var noErrors = true;
	var charNumbers = new Array(1,2,3,6,7,8,10,11,12,13)

	if (strInput.charAt(0) != "(" || strInput.charAt(4) != ")" || strInput.charAt(5) != " " || strInput.charAt(9) != "-") {
		noErrors = false;
	}

	for (i=0; i < charNumbers.length; i++) {
		if (isNaN(strInput.charAt(charNumbers[i]))) {
			noErrors = false;
		}
	}

	return noErrors;
}

function verifyPhone2(strInput) {
	//for format xxx-yyy-zzzz
	var noErrors = true;
	var charNumbers = new Array(0,1,2,4,5,6,8,9,10,11)

	if (strInput.charAt(3) != "-" || strInput.charAt(7) != "-") {
		noErrors = false;
	}

	for (i=0; i < charNumbers.length; i++) {
		if (isNaN(strInput.charAt(charNumbers[i]))) {
			noErrors = false;
		}
	}

	return noErrors;
}

function verifyMMDDYYYY(objName) {
	var datefield = objName;
	if (checkMMDDYYYY(objName) == false) {
		return false;
	} else {
		return true;
   }
}

function checkMMDDYYYY(objName) {
	var strDatestyle = "US"; //United States date style
	var strDate;
	var strDateArray;
	var strDay;
	var strMonth;
	var strYear;
	var intday;
	var intMonth;
	var intYear;
	var booFound = false;
	var datefield = objName;
	var strSeparatorArray = new Array("-"," ","/",".");
	var intElementNr;
	var err = 0;

	strDate = datefield.value;
	if (strDate.length < 1) {
		return true;
	}
	for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) {
		if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) {
			strDateArray = strDate.split(strSeparatorArray[intElementNr]);
		if (strDateArray.length != 3) {
			err = 1;
			return false;
		}else {
			strDay = strDateArray[0];
			strMonth = strDateArray[1];
			strYear = strDateArray[2];
		}
		booFound = true;
	   }
	}
	if (booFound == false) {
		if (strDate.length > 5) {
			strDay = strDate.substr(0, 2);
			strMonth = strDate.substr(2, 2);
			strYear = strDate.substr(4);
	   }
	}
	if (parseInt(strYear) && strYear.length == 2) {
	  strYear = '20' + strYear;
	}
	// US style
	if (strDatestyle == "US") {
		strTemp = strDay;
		strDay = strMonth;
		strMonth = strTemp;
	}
	intday = parseInt(strDay, 10);
	if (isNaN(intday)) {
		err = 2;
		return false;
	}
	intMonth = parseInt(strMonth, 10);
	if (isNaN(intMonth)) {
		for (i = 0;i<12;i++) {
			if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) {
				intMonth = i+1;
				strMonth = strMonthArray[i];
				i = 12;
			}
		}
		if (isNaN(intMonth)) {
			err = 3;
			return false;
	   }
	}
	intYear = parseInt(strYear, 10);
	if (isNaN(intYear)) {
		err = 4;
		return false;
	}
	if (intMonth>12 || intMonth<1) {
		err = 5;
		return false;
	}
	if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) {
		err = 6;
		return false;
	}
	if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) {
		err = 7;
		return false;
	}
	if (intMonth == 2) {
	if (intday < 1) {
		err = 8;
		return false;
	}
	if (LeapYear(intYear) == true) {
		if (intday > 29) {
			err = 9;
			return false;
		}
	} else {
		if (intday > 28) {
			err = 10;
			return false;
		}
	}
	}
	return true;
	}
	function LeapYear(intYear) {
	if (intYear % 100 == 0) {
		if (intYear % 400 == 0) { return true; }
	}
	else {
		if ((intYear % 4) == 0) { return true; }
	}
	return false;
}

