// check if input is numeric  // not in use    syed 
/*function isNumeric(str){
  for (var i=0; i<str.length; i++){
    var oneChr=str.charAt(i);
    if (oneChr<"0" || oneChr>"9"){
      return false;
    }
  }
  return true;
}*/
// format Amount
function amtFormat(inAmt){
  inAmt=inAmt+".00";
  return inAmt;
}

// general purpose function to see if an input value has been
// entered at all
function isEmpty(inputStr) {
	if (inputStr == null || inputStr == "") {
		//alert("Please Enter the value and submit again.");
		return true
	}
	return false
}

//----------- check if input is numeric  
//if Number is  in #.00 format-------------

function isNumeric(expr){

	if(isNaN(parseInt(expr,10))){
		alert("This should be numeric.");
		return false
		}
	else
		if(isNaN(expr)){
			alert("This should be numeric.");
			return false
		}
		else
			if(eval(parseInt(expr,10))<0){
				alert("This should be greater than zero.");
				return false
				}	
			else
				return true;
	}

function isNumeric2(expr, name){
	
	if(isNaN(parseInt(expr,10))){
		alert("This "+ name + " should be numeric.");
		return false
		}
	else
		if(isNaN(expr)){
			alert("This "+ name + " should be numeric.");
			return false
		}
		else
			if(eval(parseInt(expr,10))<=0){
				alert("This "+ name + " should be greater than zero.");
				return false
				}	
			else
				return true;
	}

//-------- Function to remove spaces between Numbers
function Remove_space(istr){
  var ostr="";
  for (var i=0; i<istr.length; i++){
    var oneChr=istr.charAt(i);
    if (oneChr!=" "){
		ostr=ostr+oneChr;
    }
  }
  return ostr;
}

//----- this function returns Number in ##.00 format removing thousand seprator ----------------- 

function FormatNumber(expr, decpl){
	//expr=Remove_space(expr);
	//expr=removeThous(expr);
	
	var st1=expr;
	var cm=st1.indexOf(".")
	if(cm!=-1) st1=expr.substring(0, cm)+expr.substring(cm+1, expr.length);
	
	if(parseInt(st1,10)>0){	
		var str=""+Math.round(eval(expr)*Math.pow(10,decpl));
		
		while (str.length<=decpl){
		       str="0"+str;
		       }
		
		var decpt=str.length-decpl;
		return str.substring(0, decpt)+"."+str.substring(decpt, str.length);
		
	}
	else  //0
		return "0.00";
	
}


// general purpose function to see if a suspected numeric input
// is a positive integer
function isPosInteger(inputVal) {
	inputStr = inputVal.toString()
	for (var i = 0; i < inputStr.length; i++) {
		var oneChar = inputStr.charAt(i)
		if (oneChar < "0" || oneChar > "9") {
			return false
		}
	}
	return true
}

// general purpose function to see if a suspected numeric input is a positive integer <> "-"
//(***** kasa modified *****)
function chkNumeric(inputVal) {
	inputStr = inputVal.toString()
	for (var i = 0; i < inputStr.length; i++) {
		var oneChar = inputStr.charAt(i)
		if (oneChar != "-") {
			if (oneChar < "0" || oneChar > "9") { return false; }
		}
	}
	return true;
}

// general purpose function to see if a suspected numeric input is a positive integer <> "."
//(***** kasa modified *****)
function chkNumericButPeriod(inputVal) {
	inputStr = inputVal.toString()
	for (var i = 0; i < inputStr.length; i++) {
		var oneChar = inputStr.charAt(i)
		if (oneChar != ".") {
			if (oneChar < "0" || oneChar > "9") { return false; }
		}
	}
	return true;
}
// ***


// general purpose function to see if a suspected numeric input
// is a positive or negative integer

function isInteger(inputVal) {
	inputStr = inputVal.toString()
	for (var i = 0; i < inputStr.length; i++) {
		var oneChar = inputStr.charAt(i)
		if (i == 0 && oneChar == "-") {
			continue
		}
		if (oneChar < "0" || oneChar > "9") {
			return false
		}
	}
	return true
}

// general purpose function to see if a suspected numeric input
// is a positive or negative number
function isNumber(inputVal) {
	oneDecimal = false
	inputStr = inputVal.toString()
	for (var i = 0; i < inputStr.length; i++) {
		var oneChar = inputStr.charAt(i)
		if (i == 0 && oneChar == "-") {
			continue
		}
		if (oneChar == "." && !oneDecimal) {
			oneDecimal = true
			continue;
		}
		if (oneChar < "0" || oneChar > "9") {
			return false
		}
	}
	return true
}

// Search and replace with another string

function replaceString(iStr, searStr, repStr )
	{
		var oStr = "";
		oStr = iStr.replace(searStr, repStr);
		return(oStr);  
}


// function to determine if value is in acceptable range
// for this application
function inRange(inputStr) {
	num = parseInt(inputStr)
	if (num < 1 || num > 586 && num < 596 || num > 599 && num < 700 || num > 728) {
		return false
	}
	return true
}

// Master value validator routine
function isValid(inputStr) {
	if (isEmpty(inputStr)) {
		alert("Please enter a number into the field before clicking the button.")
		return false
	} else {
		if (!isNumber(inputStr)) {
			alert("Please make sure entries are numbers only.")
			return false
		} else {
			if (!inRange(inputStr)) {
				alert("Sorry, the number you entered is not part of our database.  Try another three-digit number.")
				return false
			}
		}
	}
	return true
}

//------------------------
function Data_Val(inputStr){
	inputStr=Remove_space(inputStr);
	inputStr=removeThous(inputStr);
	if(!isEmpty(inputStr)&& isNumeric(inputStr)){
		return true;
	}
	else return false;   	
}
//---------------------- 



// date field validation (called by other validation functions )

function val_Date(inputStr){
	
	var OutStr="";
	inputStr=Remove_space(inputStr);

	if(!isEmpty(inputStr)){  
 		
 		while (inputStr.indexOf("-") != -1) {
			inputStr = replaceString(inputStr, "-","/")
		}
		
		var delim1 = inputStr.indexOf("/")
		var delim2 = inputStr.lastIndexOf("/")
		if (delim1 != -1 && delim1 == delim2) {
		// there is only one delimiter in the string
			alert("The date entry is not in an acceptable format.\n\n You can enter dates in the following formats: mmddyyyy, mm/dd/yyyy, or mm-dd-yyyy.  (If the month or day data is not required, enter \'01\' in the appropriate location.)")
			return false
		}
		
		if (delim1 != -1) {
		// there are delimiters; extract component values
		var mm = parseInt(inputStr.substring(0,delim1),10)
		var dd = parseInt(inputStr.substring(delim1 + 1, delim2),10)
		var yyyy = parseInt(inputStr.substring(delim2 + 1, inputStr.length),10)
		} 
		else {
		// there are no delimiters; extract component values
		var mm = parseInt(inputStr.substring(0,2),10)
		var dd = parseInt(inputStr.substring(2,4),10)
		var yyyy = parseInt(inputStr.substring(4,inputStr.length),10)
		}
		if (isNaN(mm) || isNaN(dd) || isNaN(yyyy)) {
		// there is a non-numeric character in one of the component values
		alert("The date entry is not in an acceptable format.\n\nYou can enter dates in the following formats: mm/dd/yyyy, or mm-dd-yyyy.")
		return false
		}
		
		// validate year, allowing for checks between year ranges
		// passed as parameters from other validation functions
		if (yyyy > 100&& (yyyy<1951||yyyy>2050)){
			alert("Year must be entered between the range of 1951 to 2050.")
			
			return false;	
		}
		
		if (yyyy < 100) {
			// entered value is two digits, which we allow for 1930-2029
			if (yyyy >= 51) {
				yyyy += 1900
			} else {
				yyyy += 2000
			}
		}
		
		if (mm < 1 || mm > 12) {
		// month value is not 1 thru 12
		alert("Months must be entered between the range of 01 (January) and 12 (December).")
		return false
		}
		if (dd < 1 || dd > 31) {
			// date value is not 1 thru 31
			alert("Days must be entered between the range of 01 and a maximum of 31 (depending on the month and year).")
			return false
		}
		
		
		if (mm == 1 || mm == 3|| mm == 5|| mm == 7|| mm == 8|| mm == 10 || mm == 12) {
		// month value is not 1 thru 12
			if (dd < 1 || dd > 31) {
			// date value is not 1 thru 31
				alert("Days must be entered between the range of 01 and a maximum of 31 (for this month).")
				return false
			}
		}
		if (mm == 4 || mm == 6|| mm == 9|| mm == 11) {
		// month value is not 1 thru 12
			if (dd < 1 || dd > 30) {
			// date value is not 1 thru 30
				alert("Days must be entered between the range of 01 and a maximum of 30 (for this month).")
				return false
			}
		}
		
		
		if (mm == 2 ) {
			if (yyyy%4 == 0){
				if (dd < 1 || dd > 29) {
			// date value is not 1 thru 29
				alert("Days must be entered between the range of 01 and a maximum of 29 (for this month and Year).")
				return false
				}
			}
			else{
				if (dd < 1 || dd > 28) {
				// date value is not 1 thru 28
				alert("Days must be entered between the range of 01 and a maximum of 28 (for this month and Year).")
				return false
				}
			}
		}

		
		if (dd < 10) dd=""+'0'+dd
		if (mm < 10) mm=""+'0'+mm

		OutStr=mm+'/'+dd+'/'+yyyy;
		return(OutStr);
		
	}
	else
	return false;
}


// date field validation (called by other validation functions )
// modified by kasa for the input of "mm/yyyy" and return "mm/01/yyyy" format as dd=01 
function val_Date_mmyyyy(inputStr){
	
	var OutStr="";
	inputStr=Remove_space(inputStr);

	if(!isEmpty(inputStr)){  
 		
 		while (inputStr.indexOf("-") != -1) {
			inputStr = replaceString(inputStr, "-","/")
		}
		
		var delim1 = inputStr.indexOf("/")
		if (delim1 != -1) {
		// there are delimiters; extract component values
			var mm = parseInt(inputStr.substring(0,delim1),10)
			var yyyy = parseInt(inputStr.substring(delim1 + 1, inputStr.length),10)
		} 
		else {
		// there are no delimiters; extract component values
			var mm = parseInt(inputStr.substring(0,2),10)
			var yyyy = parseInt(inputStr.substring(2,inputStr.length),10)
		}
		
		if ( isNaN(mm) || isNaN(yyyy) ){
		// there is a non-numeric character in one of the component values
			alert("The date entry is not in an acceptable format.\n\nYou can enter dates in the following formats: mm/yyyy, or mm-yyyy.")
			return false
		}
		
		// validate year, allowing for checks between year ranges
		// passed as parameters from other validation functions
		if (yyyy>100 && (yyyy<2001||yyyy>2050)){
			alert("Year must be entered between the range of 2001 to 2050.")
			return false;	
		}
		if (yyyy<100){	
		// entered value is two digits, which we allow for 2001-2049
			yyyy += 2000
		}

		if (mm<1 || mm>12){		
		// month value is not 1 thru 12
			alert("Months must be entered between the range of 01 (January) and 12 (December).")
			return false
		}
		if (mm<10) mm=""+'0'+mm

		OutStr=mm+'/01/'+yyyy;
		//alert(OutStr);	
		return(OutStr);
	}
	else
	return false;
}


//-----------------------
//---------removeThousand seperator ie "1,000"

function removeThous(istr)
{
	var ostr=istr;
	// if the number has three seperator ie "1,000,000"
	for (var i=0; i<=2; i++)
	{
		var cm=ostr.indexOf(",")
		if(cm==-1){
			ostr=ostr;
			break;
			}
		else
			{
			ostr=ostr.substring(0, cm)+ostr.substring(cm+1, ostr.length);
			}  
	}
	return ostr;
}

//----------------------------------
function formatThousand(value)
{
	var string = "" + value;
	var fixedString = "";
	var wLength = string.length;
	var wDecimalStr = "";
	var wNewLength = wLength;
	var wString = string;
	for (var i=0; i<=wLength; i++)
	{
		var wChar = string.charAt(i);
		if (wChar == ".")
		{
			wDecimals = wLength-i;
			wDecimalStr = string.substring(wLength-wDecimals, wLength);
			wString = string.substring(0, wLength-wDecimals);
			wNewLength = wString.length;
			//alert(wNewLength);
		}
	}
	var k = 1;
	var l = 1;
	var wNum;
	var wRem;
	var wIntNum;
	wNum = wNewLength/3;
	wRem = wNewLength % 3
	wIntNum = parseInt(wNum);

	if (wIntNum > 1)
	{
		for (var j=1; j<=wIntNum; j++)
		{	
			k = j * 3;
			if (j != wIntNum){
				if (j==1){
					fixedString = fixedString + wString.substring(wRem, k+wRem) + ",";
					}
				else{
					fixedString = fixedString + wString.substring(l+wRem, k+wRem) + ",";
					}
			}
			else
				fixedString = fixedString + wString.substring(l+wRem, k+wRem);
				//alert(fixedString);
			l = k;
		}
		if (wRem != 0)
			fixedString = wString.substr(0,wRem) + "," + fixedString + wDecimalStr;
		else
			fixedString = fixedString + wDecimalStr;
	}
	else
	{
		if (wNum > 1)
			fixedString = wString.substr(0,wRem) + "," + wString.substring(wRem, 3+wRem) + wDecimalStr;
		else
			fixedString = value;
	}
	//alert("total"+fixedString);
	return fixedString;
}





function isCheckDigit(istr)
	{
	var i;
	var sLength = istr.length;
	for (i = 0; i < sLength; i++)
		{
			var c = istr.charAt(i);
			
			if (!isDigit(c))
				return false;
		}
	return true;
	}
	
	
//-----------
function isDigit(c)
	{ 
	if ((c >= 0) && (c <= 9))
			return true;
		else
		{
			if (c != ".")return false;
			else return true;
				
		}	
	}

//---------------
function check_Email(wEmail)
{
	if ( (wEmail.indexOf("'") != -1) 
		   ||(wEmail.indexOf('"') != -1)  
		   ||(wEmail.indexOf(' ') != -1)  
		   ||(wEmail.indexOf(';') != -1)
		   ||(wEmail.indexOf(':') != -1)
		   ||(wEmail.indexOf(',') != -1)  
		   ||(wEmail.indexOf('<') != -1)  
		   ||(wEmail.indexOf('>') != -1)  
		   ||(wEmail.indexOf('(') != -1)  
		   ||(wEmail.indexOf(')') != -1)  
		   ||(wEmail.indexOf('[') != -1)  
		   ||(wEmail.indexOf(']') != -1)  
		   ||(wEmail.indexOf('=') != -1)
		   ||(wEmail.indexOf('/') != -1)
		   ||(wEmail.indexOf('\\') != -1)
		   ||(wEmail.indexOf('`') != -1) 
		   ||(wEmail.indexOf('~') != -1)
		   ||(wEmail.indexOf('!') != -1)  
		   )
		{
			alert ("Invalid E-mail Address");
			return false;
		}
		if ( wEmail.indexOf("@") == -1)
		{
			alert("Invalid E-mail Address - No Domain Specified");
			return false;
		}
		if ( wEmail.lastIndexOf(".") < wEmail.indexOf("@") )
		{
			alert("Invalid E-mail Address - Invalid Domain Specified");
			return false;
		}

		var sDomains = "/.com/.org/.gov/.net/.edu/.mil/.us/.cc/.ws/.tv/.ca/.jp/";

		var sEmail = wEmail.toLowerCase();
		var nLength = sEmail.length;

		if ( ( sDomains.indexOf("/" + sEmail.substr(nLength - 3) + "/") < 0 )
		   &&( sDomains.indexOf("/" + sEmail.substr(nLength - 4) + "/") < 0 ) )
		{
			alert("Invalid E-mail Address - Unsupported Domain Specified");
			return false;
		}
			
		if ( nLength >= 15 && sEmail.substr(nLength - 15) == "freeservers.com" )
		{
			alert("Invalid E-mail Address - Unsupported Domain Specified");
			return false;
		}
	return true;	
		
}



