/***************************************************************************
 * This javascript has the functions and variables that are used in all of 
 * the page checks
 **************************************************************************/
 
function isBlank(checkThis){
	//this line will get rid of all white space
	checkThis = checkThis.replace(/\s+/g ,"");		
	//alert("checking if this is blank: " + checkThis);
	retval = (checkThis == "" || checkThis == null || checkThis == " ")?true: false;		
	return retval;
}


/************************************************************ 
 * This function will check to see if a string is in our format
 *  $? [:digit:]* ("." [:digit:] [:digit:])?
 *  if the value is good, then return true, else false
 ***********************************************************/
function checkFormat(sortMe){
	//error check - see if there is anything bad in there....it will save the longest match, so we can compare it to what it should be
	retError = sortMe.match(/(\$)?([0-9])*([^0-9])*([0-9])*(\.([^0-9])*([0-9])*([^0-9])*([0-9])*)?([^0-9])*/);
	//retError = sortMe.match(/(\$)?([0-9])*([^0-9])*([0-9])*(\.([0-9])*([^0-9])*([0-9])*)?([^0-9])*/);
	//this re will find res in our format
	retval = sortMe.match(/(\$)?([0-9])*(\.[0-9][0-9])?/);			
	//we get arrays back and the first one is the longest match, so by comparing the two...if they match
	retBool = (retval[0] == retError[0])?true: false;
	
	//okay ..... let's do one to cover 103.7 conversion to 103.70 if it's false;
	if(!retBool){
		retError = sortMe.match(/(\$)?([0-9])*([^0-9])*([0-9])*(\.([^0-9])*([0-9])*)?([^0-9])*/);
		retval = sortMe.match(/(\$)?([0-9])*(\.[0-9])?/);
		retBool = (retval[0] == retError[0])?true: false;
	}					
	return retBool;
	
}

/***********************************************************
 * Date validator - this takes advantage of the select boxes....
 * checks to make sure it is a valid date:
 *********************************************************/
function checkDate(mon, day, year){
	mon = parseInt(mon);
	day = parseInt(day);
	year = parseInt(year);
	arrayVals = new Array(2);
	
	arrayVals[1] = " ";
	
	dayFebMax = 28;
	//since our base check is already handled with the pull-downs, we just need to check the maxes

	//leap year check
	if( ( (year % 100) != 0 ) && ( (year % 4) == 0) ){	
		dayFebMax = 29;	
	}
	
	//special check
	if( mon == 2){	
		if( day <= dayFebMax){
			arrayVals[0] = true;
		}
		else{
			arrayVals[0] = false;
			arrayVals[1] = "February only has " + dayFebMax + " days for the year you have selected. Please select a day within that period.";			
		}			
	}
	//30 day check
	else if( (mon == 4) || (mon == 6) || (mon == 9) || (mon == 11) ){
		if( day <= 30){
			arrayVals[0] = true;
		}
		else{
			arrayVals[0] = false;
			arrayVals[1] = "There are only 30 days in this month. Please select a day within that period.";
		}
	}
	//31 day else case - we really don't need to do any error checking here......there are 31 days max for checkboxes
	else{
		arrayVals[0] = true;
	}
	
	//return an array with error message and value;
	return arrayVals;
}

//returns true if something is checked
function ctRadio(checkThisName){
	for(k =0; k < checkThisName.length; k++){
		if(checkThisName[k].checked == true){
			return true;
		}
	}
	return false;
}

//returns true if object passed is a decimal or integer number; false if not.
// 2 params: first is the value; second is the field name, can be left blank if you don't want it to update the field 
function isNum( passedVal, fieldName){
	//kill all white space
	retState = false;
	new_value = "";		
	passedVal = passedVal.replace(/\s+/g ,"");
	isInteger = ( passedVal.search(/\./) == -1)?1: 0; 
	if(isInteger){
		// this should just be a solid digit .. like 4, 3, 2, etc.
		retState = isInt(passedVal); 
	}
	else{
		blocks = passedVal.split(".");
		//there can only be at most two blocks ..any more and we don't have a valid decimal			
		if(blocks.length < 3){					
			if(isBlank(blocks[0]) && isInt(blocks[1])){				
				new_value = "0." + blocks[1];
				retState = true;
			}
			else if( isInt(blocks[0]) && isBlank(blocks[1])){
				new_value =  blocks[0] + ".0";				
				retState = true;
			}
			else if( isInt(blocks[0]) && isInt(blocks[1])){				
				retState = true;
			}
			else{}			
			//update the field is desired
			//commented out; causing errors (MY 20080711)
			/*if(!isBlank(fieldName) && retState){
				fieldName += ".value = " + new_value;			
				eval(fieldName);
			}*/			
		}										
	}				
	return retState;
}

//this one only checks for integers ... use isNum if you want to check for decimals as well
function isInt(passedNum){
		passedNum = passedNum.replace(/\s+/g ,"");		
		nonDigits = passedNum.search(/\D/);	 		
		if(nonDigits >= 0 || isBlank(passedNum) )
			return false;
		else
			return true;
}
/***********************************************/
//			Dynamically Expanding Textareas
/***********************************************/
function expTextArea() {
	allTxt = document.getElementsByTagName("textarea");
	for (x=0;x<allTxt.length; x++){
		contentTxt = allTxt[x].value;
		colsTxt = allTxt[x].cols;
		arrtxt = contentTxt.split('\n');
		rowsTxt = arrtxt.length;
		for (i=0;i<arrtxt.length;i++){
			rowsTxt += parseInt(arrtxt[i].length/colsTxt);
		}
		if (rowsTxt>allTxt[x].maxrows) {
			allTxt[x].rows = allTxt[x].maxrows;
		}
		else if (rowsTxt<3)
		{
			allTxt[x].rows = 3;
			}
		else{
			allTxt[x].rows = rowsTxt;
		}
	}
}

/***********************************************/
//		Trims Emails onblur and calls emailCheck
/***********************************************/
function trimEmail(element) {
   var varElm = eval(element);
   var temp = varElm.value;
   var obj = /^(\s*)([\W\w]*)(\b\s*$)/;
   if (obj.test(temp)) { temp = temp.replace(obj, '$2'); }
   var obj = / +/g;
   temp = temp.replace(obj, " ");
   if (temp == " ") { temp = ""; }
   varElm.value = temp;
   return emailCheck(temp);
}
