/* ************************************************************
Standard cliend side data validation and interface manipulation
function

Developer:  Philip Schmitz
Date Updated:  1/3/2009
************************************************************ */

/* ************************************************************
this function Will compare 2 string values and return whether 
they are the same or different
************************************************************ */
function strCompare(str1,str2){
	if(str1==str2){
		return 0;
	}
	if(str1>str2){
		return 1;
	}
	if(str1<str2){
		return -1;
	}
}


/* ************************************************************
this function Will take a value and determine if the 
element is emptry
************************************************************ */
function isValueEmpty(value){
	if(value==null||value==""||value==" "||value=="  "||value=="   "||value=="    "||value=="     ")
		return 1;
	else
		return 0;
}

/* ************************************************************
this function Will take a form field name and determine if the 
element is emptry
************************************************************ */
function isEmpty(element){
	if(element.value==null||element.value==""||element.value==" "||element.value=="  "||element.value=="   "||element.value=="    "||element.value=="     ")
		return 1;
	else
		return 0;	
}

/* ************************************************************
this function will take a value and determine if it is a 
POSITIVE Integer (NO DECIMALS)
************************************************************ */
function isPosInt(element){
	elementString=element.toString();
	for(var i=0;i<elementString.length;i++){
		var elementChar=elementString.charAt(i);
		if(elementChar<"0" || elementChar>"9"){
			return 0;
		}
	}
	return 1;
}

/* ************************************************************
this function will take an a value and determine if it is a
valid zipcode format

12345
12345-1234

NOTE:  does not check for length reqquirements
************************************************************ */
function isZipCode(element){
	elementString=element.toString();
	for(var i=0;i<elementString.length;i++){
		var elementChar=elementString.charAt(i);
		if(!(elementChar == "-" || elementChar == " ") && elementChar<"0" || elementChar>"9"){
			return 0;
		}
	}
	return 1;
}

/* ************************************************************
this function will take a value and determine if its is a 
POSITIVE number.  It allows for decimals.
************************************************************ */
function isPosNumCh(element){
	if(isNaN(element)){
		return 0;
	}
	else{
		if(element <0){
			return 0;
		}
	}
	return 1;
}

/* ************************************************************
this function will take a string value and determine if it is
a POSITIIVE number.  it allows for decimals
************************************************************ */
function isPosNum(element){
	if(isNaN(element)){
		return 0;
	}
	else{
		elementString=element.toString();
		if (elementString.indexOf('.') == -1) {
			for(var i=0;i<elementString.length;i++){
				var elementChar=elementString.charAt(i);
				if(elementChar<"0" || elementChar>"9"){
					return 0;
				}
			}
		}
		else{
			elementString=elementString.substring(0,elementString.indexOf('.')-1)
			for(var i=0;i<elementString.length;i++){
				var elementChar=elementString.charAt(i);
				if(elementChar<"0" || elementChar>"9"){
					return 0;
				}
			}
		}
	}
	return 1;
}

/* ************************************************************
this function will take an array of fields and will determine
if each field passed in the array has a value in it and will
generate a javascript error for each field that is not populated

This function also auto submits form if all fields are filled
out which differentiates it from isAllFilled
************************************************************ */
function checkPreSubmit(fields,formObj){
	var carryOn = 1;
	if(isArray(fields)){
		for(var i = 0;i<fields.length;i++){
			if(isEmpty(fields[i][0])){
				alert("Please enter a value for "+fields[i][1]+".");
				carryOn = 0;
			}
		}
	}
	if(carryOn){
		formObj.submit();
	}
}

/* ************************************************************
this function will take an array of fields and will determine
if each field passed in the array has a value in it and will
generate a javascript error for each field that is not populated
************************************************************ */
function isAllFilled(fields){
	var carryOn = 1;
	if(isArray(fields)){
		for(var i = 0;i<fields.length;i++){
			if(isEmpty(fields[i][0])){
				alert("Please enter a value for "+fields[i][1]+".");
				carryOn = 0;
			}
		}
	}
	if(carryOn){
		return 1;
	}
} 

/* ************************************************************
this function is used to determine if a value (year,month,day)
is a valid date according to the calendar year
************************************************************ */
function isDate(year,month,day){
	// month argument must be in the range 1 - 12
	month = month - 1; // javascript month range : 0- 11
	//var tempDate = new Date(year,month,day);
	var tempDate = new Date();
	tempDate.setFullYear(year,month,day);
	//alert(tempDate);
	//alert(tempDate.getYear());
	//year-1900 is work arround for firefox which displays years after 2000 as 100 (eg 2008 is 108)
	if((year == tempDate.getYear()||(year-1900) == tempDate.getYear()) && (month == tempDate.getMonth()) && (day == tempDate.getDate())){
		return true;
	}
	else{
		return false;
	}
}

/* ************************************************************
this function returns true if the typeof function is not
equal to undefined and fale if it is undefined
************************************************************ */
function defined(o){
	return typeof(o) != "undefined";
}

/* ************************************************************
this function is used to determine if an object is an array
this is particularly useful for checking if cleint side
collections (eg sets of checkboxes) are a collection or
a single instance
************************************************************ */
function isArray(o){

	// If these conditions aren't met, it certainly isn't an Array
	if(o == null || typeof(o) != "object" || typeof(o.length)!= "number"){
		return false;
	}
	
	// Check to see if the object is an instance of the window's Array object
	if(defined(Array) && defined(o.constructor) && o.constructor==Array){
		return true;
	}
	
	// It might be an array defined from another window object - check to see if it has an Array's methods
	if(typeof(o.join) == "function" && typeof(o.sort) == "function" && typeof(o.reverse) == "function"){
		return true;
	}
	
	// As a last resort, let's see if index [0] is defined
	return (o.length==0 || defined(o[0]));
	
}