function isaPosNum(s) {
	  if(s.indexOf(".") > 0)
	      return false;
	  else 
              return (parseInt(s) > 0);
}

function isaNum(s) {
	  if(s.indexOf(".") > 0)
	      return false;
	  else 
              return !isNaN(parseInt(s));

}

function isaDigit(s) {
     if(s.length != 1) return false;  
     var i = parseInt(s);
     if(isNaN(i)) return false;
     if(i < 0) return false;
     if(i > 9) return false;
     return true;
}

function isaPosNum2(s) {
        var ret = true;

	for(var i = 0 ; i < s.length; i ++) {
		if(!isaDigit(s.charAt(i))) {
		    ret = false;
                    break;
	        }
	}
	return ret;
}

function trim(inStr) {	
	  var begin = -1;
          var end = -1;

	  for( var i=0; i<inStr.length; i++) {
                if(inStr.charAt(i) != " ") {
                       begin = i;
		       break;
	        }
          }
          for( var i= inStr.length - 1; i >= 0; i--) {
                if(inStr.charAt(i) != " ") {
                       end = i;
		       break;
	        }
          }

	  if(begin == -1 && end == -1) return "";
          
	  else return inStr.substring(begin,end + 1);
}

function qty_check_auto(item, min, max) {
          var returnVal = false;
          if (!isaPosNum2(item.value)) 
               if (min == -999 && max == -999)   
                    alert("Please enter an integer for " + item.name);
               else if (min == -999)
		    alert("Please enter an integer for " + item.name + "  less than " + max );
               else if (max == -999)
		    alert("Please enter an integer for " + item.name + "  more than " + min );
               else
		    alert("Please enter an integer for " + item.name + "  between " + min + " and " + max );
          else if (min != -999 && parseInt(item.value) < min) 
                    alert("Please enter an integer for " + item.name + " greater than " + min);
          else if (max != -999 && parseInt(item.value) > max) 
                    alert("Please enter an integer for " + item.name + " less than " + max);
          else 
                    returnVal = true;
          return returnVal;
}

function qty_check(item, min, max, alertMessage) {
          var returnVal = false;
          if (!isaPosNum2(item.value)) 
               if (min == -999 && max == -999)   
                    alert(alertMessage);
               else if (min == -999)
		    alert(alertMessage);
               else if (max == -999)
		    alert(alertMessage);
               else
		    alert(alertMessage);
          else if (min != -999 && parseInt(item.value) < min) 
                    alert(alertMessage);
          else if (max != -999 && parseInt(item.value) > max) 
                    alert(alertMessage);
          else 
                    returnVal = true;
          return returnVal;
}

function empty_check_auto(item) {
	 var str = trim(item.value);
	 if(str.length == 0) {
	    alert(item.name + " cannot be empty")
	    return false;
         }
	 else
	    return true;
}

function empty_check(item, alertMessage) {
	 var str = trim(item.value);
	 if(str.length == 0) {
	    alert(alertMessage)
	    return false;
         }
	 else
	    return true;
}

function noSelection_check_auto(item) {
	 var num = 0;
         for (var i = 0; i < item.options.length; i++) {
		if(item.options[i].selected)
		      num ++;
         }	

	 if(num == 0) {
	    alert("Please at least select one in " + item.name);
	    return false;
         }
	 else
	    return true;
}

function noSelection_check(item, alertMessage) {
	 var num = 0;
         for (var i = 0; i < item.options.length; i++) {
		if(item.options[i].selected)
		      num ++;
         }	

	 if(num == 0) {
	    alert(alertMessage);
	    return false;
         }
	 else
	    return true;
}


function validateEmail(emailAddress,alertMessage) {
     emailAddress = trim(emailAddress);
     var ret = true;
     if(emailAddress.indexOf("@") < 0) ret = false;
     else {
        arrayofStrings = emailAddress.split("@");
        if(arrayofStrings.length != 2) ret = false;
        else if(arrayofStrings[0].indexOf(" ") >= 0) ret = false;
        else if(arrayofStrings[1].indexOf(" ") >= 0) ret = false;
     }
     if(!ret)  alert(alertMessage);
     return ret;
}

function validateEmailSimple(emailAddress,alertMessage) {
     emailAddress = trim(emailAddress);
     var ret = true;   
     if(emailAddress.indexOf(" ") >= 0) ret = false;     
     if(!ret)  alert(alertMessage);
     return ret;
}

function validateZipCode(zipCode,alertMessage) {
     zipCode = trim(zipCode);
     var ret = true;

     if(zipCode.indexOf("-") < 0) {
	if(zipCode.length != 5)
	    ret = false;
        else if(!isaPosNum2(zipCode))
            ret = false;
           
     } else {
        arrayofStrings = zipCode.split("-");
        if(arrayofStrings.length != 2) ret = false;
        else if(arrayofStrings[0].length != 5) ret = false;
	else if(!isaPosNum2(arrayofStrings[0])) ret = false;
        else if(arrayofStrings[1].length != 4) ret = false;
	else if(!isaPosNum2(arrayofStrings[1])) ret = false;
        
     }
     if(!ret)  alert(alertMessage);
     return ret;
}
