var defaultEmptyOK = false;

// decimal point character differs by language and culture
var decimalPointDelimiter = ".";

// whitespace characters
var whitespace = " \t\n\r";

// Created By Daniel
// For Opening in a new Pop Up Link
function opennew(link) {
   window.open(link , '', 'menubr=no,resizable=no,toolbar=no,scrollbars=yes,status=no,width=800,height=600,screenX=0,screenY=0');
}

function makeArray(n) {
//*** BUG: If I put this line in, I get two error messages:
//(1) Window.length can't be set by assignment
//(2) daysInMonth has no property indexed by 4
//If I leave it out, the code works fine.
//   this.length = n;
   for (var i = 1; i <= n; i++) {
      this[i] = 0
   } 
   return this
}

var daysInMonth = makeArray(12);
daysInMonth[1] = 31;
daysInMonth[2] = 29;   // must programmatically check this
daysInMonth[3] = 31;
daysInMonth[4] = 30;
daysInMonth[5] = 31;
daysInMonth[6] = 30;
daysInMonth[7] = 31;
daysInMonth[8] = 31;
daysInMonth[9] = 30;
daysInMonth[10] = 31;
daysInMonth[11] = 30;
daysInMonth[12] = 31;

 function setSelectedData(elementName, itemSelected){
   var thisElement = elementName;
   if (elementName != null){
     for (i = 0; i < thisElement.length; i++){
       if (thisElement.options[i].value == itemSelected){
        thisElement.selectedIndex = i;
        i = thisElement.length;
       }
     }
   }       
 }

 function setSelectedText(elementName, itemSelected){
   var thisElement = elementName;
   if (elementName != null){
     for (i = 0; i < thisElement.length; i++){
       if (thisElement.options[i].text == itemSelected){
        thisElement.selectedIndex = i;
        i = thisElement.length;
       }
     }
   }       
 }


 function checkRadioButton(elementName,val){
   if (elementName != null){
     if (elementName.length != null){
	   for (i = 0; i < elementName.length; i++){
	     if (elementName[i].value == val){
		   elementName[i].checked = true;
		   break;
		 }
	   }
	 }else if (elementName.value == val){
	   elementName.checked = true;
	 }
   }
 }

function checkFrames(){
  if (parent.frames[2] == null){
     self.location.href = "adm_login.jsp";
  }else{ 
     if (parent.frames[2].name != "centent"){
        self.location.href = "adm_login.jsp";
     }
  }
}

//Check valid page..
function checkValidEntry(){
 if (parent.frames[2].name != "centent")
   self.location.href = "adm_login.jsp";
 else
   parent.location.href = "adm_login.jsp"; 
}    

// Check whether string s is empty.
function isEmpty(s)
{   
	return ((s == null) || (s.length == 0))
}

function isIntegerInRange (s, a, b)
{   if (isEmpty(s)) 
       if (isIntegerInRange.arguments.length == 1) return defaultEmptyOK;
       else return (isIntegerInRange.arguments[1] == true);

    // Catch non-integer strings to avoid creating a NaN below,
    // which isn't available on JavaScript 1.0 for Windows.
    if (!isInteger(s, false)) return false;

    // Now, explicitly change the type to integer via parseInt
    // so that the comparison code below will work both on 
    // JavaScript 1.2 (which typechecks in equality comparisons)
    // and JavaScript 1.1 and before (which doesn't).
    var num = parseInt (s, 10);
    return ((num >= a) && (num <= b));
}


function isYear (s)
{   if (isEmpty(s)) 
       if (isYear.arguments.length == 1) return defaultEmptyOK;
       else return (isYear.arguments[1] == true);
    if (!isNonnegativeInteger(s)) return false;
    return ((s.length == 2) || (s.length == 4));
}

function isMonth (s)
{   if (isEmpty(s)) 
       if (isMonth.arguments.length == 1) return defaultEmptyOK;
       else return (isMonth.arguments[1] == true);
    return isIntegerInRange (s, 1, 12);
}

function isDay (s)
{   if (isEmpty(s)) 
       if (isDay.arguments.length == 1) return defaultEmptyOK;
       else return (isDay.arguments[1] == true);   
    return isIntegerInRange (s, 1, 31);
}


function isDate (year, month, day)
{   // catch invalid years (not 2- or 4-digit) and invalid months and days.
    if (! (isYear(year, false) && isMonth(month, false) && isDay(day, false))) return false;

    // Explicitly change type to integer to make code work in both
    // JavaScript 1.1 and JavaScript 1.2.
    var intYear = parseInt(year, 10);
    var intMonth = parseInt(month, 10);
    var intDay = parseInt(day, 10);

    // catch invalid days, except for February
    if (intDay > daysInMonth[intMonth]) return false; 

    if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) return false;

    return true;
}

function checkEmail (theField, emptyOK)
{   if (checkEmail.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else if (!isEmail(theField.value, false)) 
       return warnInvalid (theField, iEmail);
    else return true;
}

function isEmail (s)
{   if (isEmpty(s)) 
       if (isEmail.arguments.length == 1) return defaultEmptyOK;
       else return (isEmail.arguments[1] == true);
   
    // is s whitespace?
    if (isWhitespace(s)) return false;
    
    // there must be >= 1 character before @, so we
    // start looking at character position 1 
    // (i.e. second character)
    var i = 1;
    var sLength = s.length;


	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(s)) 
		return true;
	else 
		return false;
	
}

function isFloat (s)

{   var i;
    var seenDecimalPoint = false;

    if (isEmpty(s)) 
       if (isFloat.arguments.length == 1) return defaultEmptyOK;
       else return (isFloat.arguments[1] == true);

    if (s == decimalPointDelimiter) return false;

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);

        if ((c == decimalPointDelimiter) && !seenDecimalPoint) seenDecimalPoint = true;
        else if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}

function isDigit(myValue){
  var inputRange = "0123456789";
  var myNewVal;
  var rtnVal = true;
  var i;
  myNewVal = myValue.toUpperCase();
  for (i=0; i < myNewVal.length; i++){
	 if (inputRange.indexOf(myNewVal.charAt(i)) == -1){
	   rtnVal = false;
	   break;
	 }   
  }
  
  return rtnVal;
}

function isSymbol(myValue){
  var inputRange = ",-@.()&~'";
  var myNewVal;
  var rtnVal = true;
  var i;
  myNewVal = myValue.toUpperCase();
  for (i=0; i < myNewVal.length; i++){
	 if (inputRange.indexOf(myNewVal.charAt(i)) == -1){
	   rtnVal = false;
	   break;
	 }   
  }
  
  return rtnVal;
}
// Returns true if string s is empty or 
// whitespace characters only.

function isWhitespace (s)

{   var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}

function isInteger (s)

{   var i;

    if (isEmpty(s)) 
       if (isInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isInteger.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);

        if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}

function isSignedInteger (s)

{   if (isEmpty(s)) 
       if (isSignedInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isSignedInteger.arguments[1] == true);

    else {
        var startPos = 0;
        var secondArg = defaultEmptyOK;

        if (isSignedInteger.arguments.length > 1)
            secondArg = isSignedInteger.arguments[1];

        // skip leading + or -
        if ( (s.charAt(0) == "-") || (s.charAt(0) == "+") )
           startPos = 1;    
        return (isInteger(s.substring(startPos, s.length), secondArg))
    }
}

function isNonnegativeInteger (s)
{   var secondArg = defaultEmptyOK;

    if (isNonnegativeInteger.arguments.length > 1)
        secondArg = isNonnegativeInteger.arguments[1];

    // The next line is a bit byzantine.  What it means is:
    // a) s must be a signed integer, AND
    // b) one of the following must be true:
    //    i)  s is empty and we are supposed to return true for
    //        empty strings
    //    ii) this is a number >= 0

    return (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s, 10) >= 0) ) );
}

function isLetter (c)
{   return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
}

function daysInFebruary (year)
{   // February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (  ((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0) ) ) ? 29 : 28 );
}

function isAlphanumeric (s)

{   var i;

    if (isEmpty(s)) 
       if (isAlphanumeric.arguments.length == 1) return defaultEmptyOK;
       else return (isAlphanumeric.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-alphanumeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number or letter.
        var c = s.charAt(i);

        if (! (isLetter(c) || isDigit(c) ) )
        return false;
    }

    // All characters are numbers or letters.
    return true;
}

function isChineseCharacters(str){
  var isChineseCharacters = true;
  var alphabetCounter = 0;
  for (i = 0; i < str.length; i++){
	if (isLetter(str.charAt(i))){
	  alphabetCounter+= 1;
	  if (alphabetCounter > 5){
		isChineseCharacters = false;
		break;
      }
    }
  }	
  return isChineseCharacters;  
}

function isFullEnglishCharacters(str){
  var isEnglishCharacters = true;
  var notAlphabetCounter = 0;
  
  for (var i = 0; i < str.length; i++){
	if ( !( isLetter(str.charAt(i)) || isWhitespace(str.charAt(i)) ||  isDigit(str.charAt(i)) || isSymbol(str.charAt(i))) ){
		isEnglishCharacters = false;
		break;
    }
  }	
  return isEnglishCharacters; 	
}

function isOverlimitChineseCharacter(formElement, errorMsg, maxLength) {
		isOverLimit = false;
        var countMe = formElement.value;
        var escapedStr = encodeURI(countMe)
       if (escapedStr.indexOf("%") != -1) {
            var count = escapedStr.split("%").length -1
           if (count == 0) count++  //perverse case; can't happen with real UTF-8
            var tmp = escapedStr.length - (count * 3)
            count = count +(count/3)+ tmp
        } else {
            count = escapedStr.length
        }
    	if (count > maxLength)
    	{
    		alert(errorMsg);
    		formElement.focus();
    		isOverLimit = true;
    	}else {
    		isOverLimit = false;
    	} 
    	return isOverLimit;
}