// GLOBAL FORM FUNCTIONS
//********************************************************************************************************
// Created: June 12, 2008
// By: Keith Valley
// Purpose: To create one document that can be included in all form pages to for java form verification 
//
//********************************************************************************************************
// 		TOC
/*
1. isBlank (inString)															
2. isWithinRange (inString, rangeMin, rangeMax)
3. trim (string)
4. leftTrim (inString)
5. rightTrim (inString)
6. isNumeric(str)
7. isNumberString(inString)
8. isLetter(c)
9. isEmail(str)

7. cent(amount)
8. round(number)





*/


// whitespace characters
var whitespace = " \t\n\r";

/*
 * Function: isBlank (inString)
 * Purpose: Is the String Blank or empty
 * Arguments: String
 * Returns: boolean
 **/
function isBlank (inString) {
	if (inString == null || inString.length == 0) {
		return true;
	} else {
		return false;
	}
}
//********************************************************************************
//Function: isWithinRange (inString, rangeMin, rangeMax)
//Purpose: Is the String within the range
//Arguments: String, integer, integer
//Returns: boolean
//********************************************************************************
function isWithinRange (inString, rangeMin, rangeMax)  {
	if ((inString == null) || (inString == "")) { 
		return (false);
	}
	if((inString >= rangeMin) && (inString <= rangeMax)) {
		return true;
	} else {
		return false;
	}
}

//********************************************************************************
//Function: trim (string)
//Purpose: Removes the white space at the edges
//Arguments: String
//Returns: String
//********************************************************************************
function trim(sString) 
{ 
  while (sString.substring(0,1) == ' ') 
  { 
    sString = sString.substring(1, sString.length); 
  } 
  while (sString.substring(sString.length-1, sString.length) == ' ') 
  { 
    sString = sString.substring(0,sString.length-1); 
  } 
return sString; 
} 


//********************************************************************************
// Function: isNumeric(str)
// Purpose: is string a numeric (integer or float)
// Arguments: String
// Returns: boolean
//********************************************************************************
function isNumeric(str) {
  var num;

  /* matches 99.999 */
  if (str.indexOf(".") > 0) {
    num = str.split(".");
    if (num.length == 2) {
      for (var i = 0; i < num.length; i++) {
        if (!isNumberString(num[i])) {
	  return false;
	}
      }
      return true;
    } else if (num.length > 2) {
      return false;
    }
    /* matches .999 */
    } else if (str.indexOf(".") == 0) {
      var temp = str.substring(1, str.length);
      if (isNumberString(temp)) {
        return true;
      } else {
	false;
      }
      
    /* matches 999 */
    } else if (isNumberString(str)) {
      return true;
    } else {
    return false;
  }
}

//*******************************************************************************
// Function: isNumberString(inString)
// Purpose: is the String a number (Integer)
// Arguments: String
// Returns: boolean
//*******************************************************************************
function isNumberString(inString)  {
  if (inString.length == 0) { 
    return false;
  }
  var refString = "1234567890";
  for (var count=0; count < inString.length; count++)  {
    var tempChar = inString.substring (count, count + 1);
    if (refString.indexOf(tempChar, 0) == -1) {  
      return false;
    }
  }
  return true;
}

//********************************************************************************
// Function: isLetter(c)
// Purpose: to verify that a character is a letter
// Arguments: a single Character
// Returns: true if character c is an English letter (A .. Z, a..z).
//********************************************************************************
function isLetter (c)
{   
  if (c == "-")
  {
    return true;
    //alert("got here");
  }
  else
  {
   return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
  }
}


/**
 * DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */

function isEmail(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   //alert("Invalid E-mail")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   //alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    //alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    //alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    //alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    //alert("Invalid E-mail ID")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    //alert("Invalid E-mail ID")
		    return false
		 }

 		 return true					
	}



function ValidateForm(){
	var emailID=document.frmSample.txtEmail
	
	if ((emailID.value==null)||(emailID.value=="")){
		alert("Please Enter your Email ID")
		emailID.focus()
		return false
	}
	if (echeck(emailID.value)==false){
		emailID.value=""
		emailID.focus()
		return false
	}
	return true
 }




//****************************************************
// returns the amount in the .99 format
//****************************************************
function cent(amount) {
  amount -= 0;
  return (amount == Math.floor(amount)) ? amount + '.00' : (  (amount*10 == Math.floor(amount*10)) ? amount + '0' : amount);
}

//**********************************************************
// returns a number rounded to 2 decimal places
//**********************************************************
function round(number) {
  var X
  X = (!X ? 2 : X);
  return Math.round(number*Math.pow(10,X))/Math.pow(10,X);
}
