var why = "";

function isEmpty(str) {
	if (trim(str) == "") {
		return true;
	}
	return false;
}

function isSelectEmpty(str) {
	if (trim(str) == "" || trim(str) == 0) {
		return true;
	}
	return false;
}

function trim(str) {
	s = str.replace(/^(\s)*/, '');
	s = s.replace(/(\s)*$/, '');
	return s;
}


function fmt00(x){
 // fmt00: Tags leading zero onto numbers 0 - 9.
 // Particularly useful for displaying results from Date methods.
 //
 if (parseInt(x) < 0) var neg = true;
 if (Math.abs(parseInt(x)) < 10){
  x = "0"+ Math.abs(x);
 }
 if (neg) x = "-"+x;
 return x;
}

function isDate(dateStr,bOutputDate) {
		
	var datePat = /^(\d{1,2})(\/|\.)(\d{1,2})(\/|\.)(\d{2,4})$/;
	var matchArray = dateStr.match(datePat); // is the format ok?
	
	if (matchArray == null) return false;
	
	if (matchArray[2]=='/' && matchArray[4]=='/') {
		day = matchArray[3];
		month = matchArray[1];
	} else {
		day = matchArray[1];
		month = matchArray[3];
	}
	year = matchArray[5];
	if (year.length==2)  year = '20' + year; 
	
	if (month < 1 || month > 12) return false;
	
	if (day < 1 || day > 31) return false;
	
	if ((month==4 || month==6 || month==9 || month==11) && day==31) return false;
	
	if (month == 2) { // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day > 29 || (day==29 && !isleap)) return false;
	}
	if (bOutputDate) return new Date(year,month-1,day);
	else return true; // date is valid
}

function checkWholeForm(form, requiredFields, errorFields) {
	if (arguments.length < 3) {
		var errorFields = new Array();
	}
	var errorText = "";
	
	var fieldsToCheck = new Array();
	fieldsToCheck = requiredFields.split("|");

	for (var i=0;i<fieldsToCheck.length;i++) {
		var tempTable =  new Array();
		tempTable = fieldsToCheck[i].split(":");
		fieldsToCheck[i] = tempTable;
		
		var formField = form[fieldsToCheck[i][0]];
		
		if (fieldsToCheck[i][1]=="select") {
			if (isSelectEmpty(formField.value)) {
				why += fieldsToCheck[i][2] + '\n';
				errorFields[i] = formField;
			};
		}
		if (fieldsToCheck[i][1]=="input") {
			if (isEmpty(formField.value)) {
				why += fieldsToCheck[i][2] + '\n';
				errorFields[i] = formField;
			};
		}
		if (fieldsToCheck[i][1]=="date") {
			if (!isDate(formField.value)) {
				why += fieldsToCheck[i][2] + '\n';
				errorFields[i] = formField;
			};
		}
		
		var img = "../interface/spacer.gif";
		formField.style.backgroundColor = '#fff';
		
		if (fieldsToCheck[i][3]!=null) {
			if (document.getElementById(fieldsToCheck[i][3])!=null) {
				document.getElementById(fieldsToCheck[i][3]).src = img;
			}
		} else if (document.getElementById(formField.name + '_indicator')!=null) {
			document.getElementById(formField.name + '_indicator').src = img;
		}
	}

	var firstErrorID = "";
	var firstFlag = false;
	
	for (var i=0;i<fieldsToCheck.length;i++) {
		var formField = form[fieldsToCheck[i][0]];
		if (errorFields[i]!=null) {
			if (!firstFlag) {
				firstErrorID = formField;
				firstFlag = true;
			}
			var img = "../interface/alert.gif";
			formField.style.backgroundColor = '#ffd2d2';
			if (fieldsToCheck[i][3]!=null) {
				if (document.getElementById(fieldsToCheck[i][3])!=null) {
					document.getElementById(fieldsToCheck[i][3]).src = img;
				}
			} else if (document.getElementById(formField.name + '_indicator')!=null) {
				document.getElementById(formField.name + '_indicator').src = img;
			}
		}
	}

	if (why != "") {
		if (firstErrorID!="") {
			firstErrorID.focus();
		}
		return false;
	}
	return true;
}


function checkPhone(form, phoneFieldsData) {
	var isok = true;

	if (phoneFieldsData != '') {
		var argumentsData = new Array();
		argumentsData = phoneFieldsData.split("|");

		var errorMessage = argumentsData[1];
		var phoneFields = new Array();
		phoneFields = argumentsData[0].split(":");
	
		if (form[phoneFields[1]].value != '' || form[phoneFields[2]].value != '') {
			if (form[phoneFields[1]].value == '' || form[phoneFields[2]].value == '') {
				isok = false;
			}
			else if (form[phoneFields[0]].value.length + form[phoneFields[1]].value.length + form[phoneFields[2]].value.length < 6) {
				isok = false;
			}
			else if (!isNumeric(form[phoneFields[0]].value) || !isNumeric(form[phoneFields[1]].value)) {
				isok = false;
			}
		}

		if (!isok) {
			var img = "../interface/alert.gif";
			var backgroundColor = '#ffd2d2';
			for (var i=0; i<phoneFields.length; i++) {
				document.getElementById(phoneFields[i]).style.backgroundColor = backgroundColor;
			}
			if (document.getElementById('telpriv_indicator') != null) {
				document.getElementById('telpriv_indicator').src = img;
			}
			why += errorMessage + '\n';
		}
		else {
			var img = "../interface/spacer.gif";
			var backgroundColor = '#fff';
			for (var i=0; i<phoneFields.length; i++) {
				document.getElementById(phoneFields[i]).style.backgroundColor = backgroundColor;
			}
			if (document.getElementById('telpriv_indicator') != null) {
				document.getElementById('telpriv_indicator').src = img;
			}
		}
	}

	return isok;
}


function isNumeric(value) {
  if (value == null || !value.toString().match(/^[-]?\d*\.?\d*$/)) return false;
  return true;
}

