function checkradio(formname,elementname)
{

   var x = eval("document." + formname + ".elements" );

			 for (i = 0 ; i < x.length ; i++)    
					 if ( x[i].name == elementname )         
											if ( x[i].type == "radio" )							 
															 if ( x[i].checked ) return true;	 

    return false;

};


function replace(str,target,repl,mode)
{
var comm = "g";
 if ( replace.arguments.length == 3)  
                                   comm = "gi"  
  else if (replace.arguments.length > 3)
                    if ( mode != -1 )                
                                comm = "gi"  
 var tmp = str
 var re  = new RegExp(target,comm); 
 tmp=tmp.replace(re,repl);
 return tmp; 
};

function checkradio(formname,elementname)
{

   var x = eval("document." + formname + ".elements" );
   var cnt;


   for (i = 0 ; i < x.length ; i++)
    {
       if ( x[i].name == elementname )
       {       
						 if (   x[i].type == "radio"   )
							  {
									 if ( x[i].checked ) cnt++;
									 if ( cnt >= 2 ) return true;
							  };    
       };

    };

    return false;

};


  function HTMLEncode(t) {
    var t = t.toString().replace(/&/g, "&amp;").replace(/"/g, "&quot;");
    return(t.replace(/</g, "&lt;").replace(/>/g, "&gt;"));
  };

function HTMLDecode(str)
{
  var tmp,i;
   tmp = str;
   tmp = replace(tmp,"&quot;",String.fromCharCode(34));
   tmp = replace( tmp, "&lt;"  , String.fromCharCode(60) );
   tmp = replace( tmp, "&gt;"  , String.fromCharCode(62) );
   tmp = replace( tmp, "&amp;" , String.fromCharCode(38) );
   tmp = replace( tmp, "&nbsp;", String.fromCharCode(32) );
   for ( i = 1 ; i < 256 ; i++ )
   		  tmp = replace( tmp, "&#" + i + ";", String.fromCharCode( i ) )
   return(tmp);


};

function roundNumber(number,decimals) {
	if(isNaN(number))
	{
		number = "0";
	}
	
	var newString;// The new rounded number
	decimals = Number(decimals);
	if (decimals < 1) {
		newString = (Math.round(number)).toString();
	} else {
		var numString = number.toString();
		if (numString.lastIndexOf(".") == -1) {// If there is no decimal point
			numString += ".";// give it one at the end
		}
		var cutoff = numString.lastIndexOf(".") + decimals;// The point at which to truncate the number
		var d1 = Number(numString.substring(cutoff,cutoff+1));// The value of the last decimal place that we'll end up with
		var d2 = Number(numString.substring(cutoff+1,cutoff+2));// The next decimal, after the last one we want
		if (d2 >= 5) {// Do we need to round up at all? If not, the string will just be truncated
			if (d1 == 9 && cutoff > 0) {// If the last digit is 9, find a new cutoff point
				while (cutoff > 0 && (d1 == 9 || isNaN(d1))) {
					if (d1 != ".") {
						cutoff -= 1;
						d1 = Number(numString.substring(cutoff,cutoff+1));
					} else {
						cutoff -= 1;
					}
				}
			}
			d1 += 1;
		} 
		newString = numString.substring(0,cutoff) + d1.toString();
	}
	if (newString.lastIndexOf(".") == -1) {// Do this again, to the new string
		newString += ".";
	}
	var decs = (newString.substring(newString.lastIndexOf(".")+1)).length;
	for(var i=0;i<decimals-decs;i++) newString += "0";
	//var newNumber = Number(newString);// make it a number if you like
	return newString; // Output the result to the form field (change for your purposes)
}






function roundOff(value, precision)
{
        value = "" + value //convert value to string
        precision = parseInt(precision);

        var whole = "" + Math.round(value * Math.pow(10, precision));

        var decPoint = whole.length - precision;

        if(decPoint != 0)
        {
                result = whole.substring(0, decPoint);
                result += ".";
                result += whole.substring(decPoint, whole.length);
        }
        else
        {
                result = whole;
        }
        return result;
}


function isphone(phoneNumber) {
   var alphavals = new RegExp("[a-z]","gi");
   var numericvals = new RegExp("[0-9]+" , "gi");
   var searchy = phoneNumber.search(alphavals); 
   var searchx = phoneNumber.search(numericvals);
   if ( searchx == -1 ||  phoneNumber.length < 7 || searchy > -1)  return false; 
   return true;
};

function isZipcode(zipcode) {
	if ( zipcode.charAt(0)== 0 || isnumeric(zipcode.substring(0,4)) != true ||  zipcode.length < 6 || isalpha(rmspaces(zipcode.substring(4,6))) != true || rmspaces(zipcode.substring(4,6)).length < 2 )  return false; 
   return true;
};

function checkfield(data)
{
if (rmspaces(data) == "") return false
else return true;
};

function checkfieldn(data,len)
{
if ( (rmspaces(data)).length < len) return false
else return true;
};

function rmspaces(x) {
var leftx = 0;
var rightx = x.length -1;
while ( x.charAt(leftx) == ' ') leftx++;
while ( x.charAt(rightx) == ' ') --rightx;
var q = x.substr(leftx,rightx-leftx + 1); 
if ( (leftx == x.length) && (rightx == -1) ) q ='';
return(q)
};

function isalpha(x)
{
 var flag = true;
 
 for (i = 0 ; i < x.length && flag; i++) 
   if ( parseFloat(x.charAt(i)) >= 0 ||  parseFloat(x.charAt(i)) <= "9" )   
                                                flag = false;
                                              
  return(flag); 

};

function y2k(number) { return (number < 1000) ? number + 1900 : number; }

function isdate (day,month,year)
{

    var today = new Date();
    year = ((!year) ? y2k(today.getYear()):year);
    month = ((!month) ? today.getMonth():month-1);
    if (!day) return false
    var test = new Date(year,month,day);
    if ((y2k(test.getYear()) == year)&&(month == test.getMonth())&&(day == test.getDate()))
        return true;
    else
        return false
}


function dc(fday,fmon,fyear , tday,tmon,tyear)
{
var fromdt;
var todt;

   fm = parseInt(fmon) - 1;
   tm = parseInt(tmon) - 1;
   var fromdt = new Date(fyear,fm,fday);
   var todt = new Date(tyear,tm,tday);
 
 	 if ( todt > fromdt)
 	 		{
	         return 1;
      };
   if (todt <= fromdt)
      {
       return -1;
      }
   else if ( todt == fromdt)
      {
        return 0;
      }
    
     
}



function todate(fday,fmon,fyear , tday,tmon,tyear)
{
var fromdt;
var todt;

   fm = parseInt(fmon) - 1;
   tm = parseInt(tmon) - 1;
   var fromdt = new Date(fyear,fm,fday);
   var todt = new Date(tyear,tm,tday);
 
   if (todt < fromdt)
          return false;
       else
          return true;
}

function isnumeric(data)
 {
 var flag = ( (rmspaces(data)).length > 0);
 var isdot = 0;
  if (flag)
  {
        for (i=0;i<data.length && flag;i++)
    if ( data.charAt(i) > '9' || data.charAt(i) < '0')
       {
         if (data.charAt(i) != '.' || isdot > 0)
            {
              flag = false;
      }
           else
            {
         isdot = 1;
      }
       };
  }
 return flag
  }

function isemail(data){
var flag = false;

	if (  (data.indexOf('@',0)  == -1) || (data.indexOf('\\',0)  != -1) || (data.indexOf('/',0)  != -1) ||!checkfield(data) || ( data.indexOf('.',0)  == -1 ) || ( data.indexOf('@')  == 0 ) || ( data.lastIndexOf('.') < data.lastIndexOf('@')  ) || ( data.lastIndexOf('.') == (data.length - 1)  ) || ( data.lastIndexOf('@') != data.indexOf('@') ) || (data.indexOf(',',0)  != -1) ||  (data.indexOf(':',0)  != -1) || (data.indexOf(';',0)  != -1) )return flag
	else
	{
             var temp = rmspaces(data);
             if (temp.indexOf(' ',0) != -1) flag = true;
             var d3 = temp.lastIndexOf('.') + 4;
             var d4 = temp.substring(0,d3);
             var e2 = temp.length  -  temp.lastIndexOf('.')  - 1 ;
             var i1 = temp.indexOf('@') ;
          if (  (temp.charAt(i1+1) == '.') || ( e2 > 3 ) || ( e2 < 2 ) ) flag = true;
          	return !flag;
        };
};// End Of Function
   
   
function arecharacters(input)
{
  var ch;
  for(var i=0;i<input.length;i++)
  {
    ch=input.charAt(i);
    if(!((ch>='a' && ch<='z') || (ch>='A' && ch<='Z')) )
      return(false);  
  }
  return(true);
}
    
    
function isinteger(data)
 {
   var flag = ( (rmspaces(data)).length > 0);
 
  if (flag)  
        for (i=0 ;i<data.length && flag; i++)
                     if ( data.charAt(i) > '9' || data.charAt(i) < '0')   flag = false;  
                     
  if (flag) 
        if ( parseFloat(data) <= 0 ) flag = false;        
  
      return flag
  };
  
function validate_contract(data)
{
   var tmp;
   
      tmp = data;
      
    if ( !isnumeric(data) )    
      { 
         alert("Contract No has to be Numeric");     
         return(false);
      }; 
      
      if ( data.length != 8)
      {
          alert("Contract No has to be 8 characters in length");     
          return(false);      
      };
      
 return(true);
};

function isalphanum(data)
{
 
  var str = data.toLowerCase();
  var ch;
  var ischar = false;
  var isnum = false;
  
  for( var i=0;i<str.length;i++)
	  {
	      ch=str.charAt(i);
	      if (! (  (ch >='a' && ch<='z') || ( ch >= '0' && ch <= '9') ) )     return(false); 	      
	      if ( ch >='a' && ch<='z' ) ischar = true;
	      if ( ch >='0' && ch<='9' ) isnum = true;	      
    }; 
    
    return(ischar && isnum);

};
function numbersonly(e)
{
	var unicode=e.charCode? e.charCode : e.keyCode
	if (unicode!=8 && unicode!=9 && unicode!=46)
		{			
			if ( unicode<48||unicode>57 ) 
				return false			
		}
}
function alphaonly(e)
{
	var unicode=e.charCode? e.charCode : e.keyCode
	//if (unicode!=8)
	if (unicode!=8 && unicode!=9 && unicode!=46)
		{
			 
			if ((unicode>=65 && unicode<=90) || (unicode>=97 && unicode<=122)) 
				return true
			else
				return false
		}
}


function alphanumbersonly(e)
{
	var unicode=e.charCode? e.charCode : e.keyCode
	if (unicode!=8 && unicode!=9 && unicode!=46)
		{
			if ((unicode>=65 && unicode<=90) || (unicode>=97 && unicode<=122) || (unicode>=48 && unicode<=57)){
				return true				
			}
			else
				return false
		}		
}
function movebackspace(e,objName,objMove)
{
	var unicode=e.charCode? e.charCode : e.keyCode	
	if(unicode == 8)
	{
		var obj = document.getElementById(objName);
		var objM = document.getElementById(objMove);
		if( !checkfield(obj.value) )
		{
				objM.focus();
				objM.value = objM.value.substr(0,1);
				
		}
	}
}
/*
function movebackspace(e,objName,objMove)
{
		var unicode=e.charCode? e.charCode : e.keyCode
		if (unicode!=8 && unicode!=9 && unicode!=46)
		{
			if ((unicode>=65 && unicode<=90) || (unicode>=97 && unicode<=122) || (unicode>=48 && unicode<=57)){
				return true				
			}
			else
				return false
		}		
		else if(unicode == 8)
		{
			var obj = document.getElementById(objName);
			var objM = document.getElementById(objMove);
			if( !checkfield(obj.value) )
			{
					objM.value = objM.value.substr(0,1);
					objM.focus();
			}
		}
}
*/
function capitalize(data)
{
	data.value = 	data.value.toUpperCase();
}

function alphanumericonly(data)
{
	var str = data.toLowerCase();
	var isalphanum = true;
	for( var i=0;i<str.length;i++)
	  {
		ch=str.charAt(i);
		if ((ch>='a' && ch<='z') || (ch>='0' && ch<='9')){ 
			isalphanum = false;
		}else{
			isalphanum = true;
			break;
		}	
	  }	
	 return(isalphanum);
}

function isalphaonly(data)
{
	var str = data.toLowerCase();
	var isalphanum = true;
	for( var i=0;i<str.length;i++)
	  {
		ch=str.charAt(i);
		if ((ch>='a' && ch<='z')){ 
			isalphanum = true;
		}else{
			isalphanum = false;
			break;
		}	
	  }	
	 return(isalphanum);
}

function isnumberonly(data)
{
	var str = data.toLowerCase();
	var isalphanum = true;
	for( var i=0;i<str.length;i++)
	  {
		ch=str.charAt(i);
		if ((ch>='0' && ch<='9')){ 
			isalphanum = true;
		}else{
			isalphanum = false;
			break;
		}	
	  }	
	 return(isalphanum);
}

function numbersonlywithdot(e)
{
	var unicode=e.charCode? e.charCode : e.keyCode
	if (unicode!=8)			
		{
			if (unicode == 46) 
			return true;
			
			if (unicode<48||unicode>57) 
			return false
		}
}
function replaceSubstring(inputString, fromString, toString) {
   // Goes through the inputString and replaces every occurrence of fromString with toString
   var temp = inputString;
   if (fromString == "") {
      return inputString;
   }
   if (toString.indexOf(fromString) == -1) { // If the string being replaced is not a part of the replacement string (normal situation)
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } else { // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
      var midStrings = new Array("~", "`", "_", "^", "#");
      var midStringLen = 1;
      var midString = "";
      // Find a string that doesn't exist in the inputString to be used
      // as an "inbetween" string
      while (midString == "") {
         for (var i=0; i < midStrings.length; i++) {
            var tempMidString = "";
            for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
            if (fromString.indexOf(tempMidString) == -1) {
               midString = tempMidString;
               i = midStrings.length + 1;
            }
         }
      } // Keep on going until we build an "inbetween" string that doesn't exist
      // Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + midString + toTheRight;
      }
      // Next, replace the "inbetween" string with the "toString"
      while (temp.indexOf(midString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(midString));
         var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } // Ends the check to see if the string being replaced is part of the replacement string or not
   return temp; // Send the updated string back to the user
} // Ends the "replaceSubstring" function