/**
* Filename.......: amhValidation.js
* Project........: amh Framework
* Last Modified..: $Date: 2000-02-18
* version........: 2
		-validating empty string to 0 in numeric checks

* Copyright......: 2008 bob Hadaya
*
*
*/
function validateRequired(str, warning, min, max) {
	
	switch(str.type){
		case 'text':
		case 'textarea':
		case 'file':
		case 'password':
			return validateRequiredText(str, warning, min, max);
		case 'select-one':	
			return validateRequiredSelectOne(str, warning);
		case 'select-multiple':
			return validateRequiredSelectMultiple(str, warning);
		case 'radio':
			return validateRequiredChecked(str, warning);
		case 'checkbox':
			return validateRequiredCheckedBox(str, warning);
	}
	return true;
}

//--------------------
function validateRequiredText(str, warning, min, max) {
	if (!min) { min = 1; }
	if (!max) {	max = 65535; }

	if (!str.value || str.value.length < min || str.value.length > max) {
		alert(unescape(warning));
		str.focus();
		str.select();
		return false;
	}
	return true;
}
//--------------------
function validateRequiredSelectOne(str, warning) {
// first option not valid

var si = str.selectedIndex;
	if (si > 0) {
		return true;
	}
	alert(unescape(warning));
	str.focus();
	//str.select();
	return false;
}
//--------------------
function validateRequiredSelectMultiple(str, warning) {
	var numOptions = str.options.length;
	lastSelected=-1;
	for(loop=numOptions-1;loop>=0;loop--) {
		if(str.options[loop].selected) {
			lastSelected = loop;
			value = str.options[loop].value;
			break;
		}
	}
	if(lastSelected < 0 || trim(value).length == 0) {
		alert(unescape(warning));
		return false;
	}
	return true;
}

//----------------------------
function validateRequiredCheckedBox(str,warning){

	if (!str.checked) {
			alert(unescape(warning));
		return false;
	}
	return true;
	
}
//----------------------------------
function validateRequiredChecked(str, warning)	{
	//alert("bob");
	isChecked=-1;
	if (!isArray(str))	{
alert(str.name + '\n' + str.type);
		if (str.checked) {
			isChecked=1;
		}
	}	else	{
		for (loop=0;loop < str.length;loop++) {
			alert(str[loop].checked);
			if (str[loop].checked) {
				isChecked=loop;
				break; // only one needs to be checked
			}
		}
	}
	if (isChecked < 0) {
		alert(unescape(warning));
		return false;
	}
	return true;
}





//--------------------
function validateEmail(email) {
	if (!email.value) {
		return true;
	}

	var re_mail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z])+$/;
	if (!re_mail.test(email.value)) {
		alert('Incorrect email address');
		email.focus();
		email.select();
		return false;
	}
	return true;
}
//--------------------
function validateMatch(var1, var2, msg) {
	if (var1.value != var2.value) {
		alert(unescape(msg));
		var1.focus();
		var1.select();
		return false;
	}
	return true;
}

//--------------------
function validateNumeric(str,  warning)
   //  check for valid numeric strings	
   {
	
   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;
   var strString = str.value;

   if (strString.length == 0) {
		blnResult = false;
		return true; //  assume empty string as acceptable
   }

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   if (blnResult)	{
		return true;   
   }	else	{
		alert(unescape(warning));
		str.focus();
		str.select();
		return false;
   }
}
//----------- added by bob hadaya-----

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

//-------------------------------
function trim(TXT)
    {
    	return TXT.replace(/(^\s+)|(\s+$)/g,"");
}
//------------------------------
function isDecimal(txtfld){
var txt=trim(txtfld);

var reg=/^-?\d*\.?\d*$/;
return( reg.test(txt));
}
//-------------------------
//chk if an object is an array or not.
function isArray(obj) {
	//returns true is it is an array
	if (obj.constructor.toString().indexOf('Array') == -1)	{
		return false;
	}	else	{
		return true;
	}
}
//--------------------------------------
function validateInteger(str,  warning){
	//alert(str);
	var txt=trim(str.value);
	var reg=/^-?\d+$/; // positive only /^\d*$/
	if ( !isEmpty(txt) &&  reg.test(txt)  ){
		return true;
	}else{
		alert(unescape(warning));
		str.focus();
		str.select();
		return false;
		
	}
}

//---------------------------------------
function MakeTen(expr,decimal){
     var str="" + Math.round(eval(expr) * Math.pow(10,decimal))
     while (str.length <= decimal){
        str = "0" + str
     }
     var decpoint = str.length - decimal
     return str.substring(0,decpoint) + "." + str.substring(decpoint,str.length);
}
//---------------------------------
function validateComparison(str,warning,comp, compvalue){
	
	
	var v;
	if (isDecimal(str.value)){
			if(isEmpty(str.value))v=0; // assume 0
			 else 
			 	v=parseFloat(str.value);
			compvalue=parseFloat(compvalue);
			//alert(str.value+' '+comp+' '+compvalue);
			//alert('v='+ v +' comp='+compvalue);
		switch (comp){
		 case 'eq': if (v == compvalue) return true;break;
		 case 'ne': if (v != compvalue) return true;break;
		 case 'gt': if (v > compvalue) return true;break;
		 case 'ge': if (v >= compvalue) return true;break;
		 case 'lt': if (v < compvalue) return true;break;
		 case 'le': if (v <= compvalue) return true;break;	
		}
	}
	//false
	alert(unescape(warning));
		str.focus();
		//str.select();
		return false;
}

