// ming-on@rocketmail.com
// Ming

//-----------------------------------------------------------------------------------------
// Check for un-allow char < > " '
//-----------------------------------------------------------------------------------------
function NotAllowChar(str) {
for(var j=0; j<str.length; j++) {
 var temp = str.charAt(j);
 if(temp=="<" || temp==">" || temp=="\"" || temp=="'"){
  return false;
  break;
  }
 }

return true;
}

//-----------------------------------------------------------------------------------------
// Check only numeric 0, 1, 2,.......9
//-----------------------------------------------------------------------------------------
function isNumeric(str) {
for(var j=0; j<str.length; j++) {
  var temp = str.charAt(j);
  if(!((temp=="0") || (temp=="1") || (temp=="2") || (temp=="3") || (temp=="4") || (temp=="5") || (temp=="6") || (temp=="7") || (temp=="8") || (temp=="9"))){
    return false;
    break;
    }

  // if j = last term, it means true - all char are digits!! Great!!
  // str.length-1 since j starts from 0!
  if(j == str.length-1)
    return true;
    }
}
/* Check only numeric 0, 1, 2,.......9, ' ', '-' */
/*
function isTelephone(str) {
for(var j=0; j<str.length; j++) {
  var temp = str.charAt(j);
  if(!((temp=="0") || (temp=="1") || (temp=="2") || (temp=="3") || (temp=="4") || (temp=="5") || (temp=="6") || (temp=="7") || (temp=="8") || (temp=="9") || (temp==" ") || (temp=="-"))){
    return false;
    break;
    }

// if j = last term, it means true - all char are digits!! Great!!
// str.length-1 since j starts from 0!
  if(j == str.length-1)
    return true;
    }
}
*/
/*Check valid telephone number format: 0-9 or - only*/
function isTelephone(input){
  var re1 = new RegExp("^\\d{1}[\\d\-]+$");
  result = re1.test(input);
  if(result==true){
    return true;
  }else{
    return false;
  }
}

/*Check valid email address format: A-Z,a-z,0-9,-,_ characters only with english letter starts first.
input - Input string, shouldsplit - TRUE/FALSE
\.{0,1}[\\w]{0,4}*/
function checkemailformat(input,shouldsplit){
   //modified by tony on 4/6/2008
  var re1=/^[a-zA-Z0-9][\w\-\.]*@[\w\-]+(\.[\w\-]+){1,4}$/;  //check general format of a email address (max 4 parts after '@')
  var re3=/[\-\_]$/;  //check the tail of email address should not be special character, not allowed

  /*Specify to split the input into array or not. Use for mutliple email address separated by "," */
  if(shouldsplit==true){
    var input = input.split(",");
  }
  /* Process multiple checking if condition if input is an array*/
  if(isArray(input)==true){
    for(var i=0;i<input.length;i++){
      if(re1.test(input[i])&&!re3.test(input[i])){
      }else{
        return false;
      }
    }
  }else{
    /* Process single checking if input is a string*/
    if(re1.test(input)&&!re3.test(input)){
    }else{
      return false;
    }
  }
  return true;
}

/*Check if argument obj is an array or not.*/
function isArray(obj){
   if (obj.constructor.toString().indexOf("Array") == -1)
      return false;
   else
      return true;
}

/*Check valid email address format: A-Z,a-z,0-9,-,_ characters only with english letter starts first.*/
/*function checkemailformat(input){
  var re1 = new RegExp("^[A-Za-z]{1}[\\w\-]*@[\\w\-]+.[\\w\-]{2,}$");
  result = re1.test(input);
  if(result==true){
    return true;
  }else{
    return false;
  }
}*/
/*Check for specific occurrences of a character*/
function checkcharacter(input,character){
  var count = 0;
  for(var i=0; i<input.length; i++) {
    var temp = input.charAt(i);
    if(temp==character){
      count++;
    }  
  }
  if(count>5){
    return false;
  }else{
    return true;
  }  
}


//-----------------------------------------------------------------------------------------
// 顯示個error message出來，再setfocus去特別field到，再set errState=true
//-----------------------------------------------------------------------------------------
function error(elem,text)
{
alert(text);
elem.focus();
errState=true;
}


//-----------------------------------------------------------------------------------------
// Check for text length of a textfield
//-----------------------------------------------------------------------------------------
function validLength(item, len) {
  // changed by tracy
  //return (item.length >= len);
  //alert('item'+item);
  if (item.length<len) return false;
  var s=item;
  for(var i =0; i<s.length;i++)
  {
    var c = s.charAt(i);
    /*Check for whitespace*/
    if(!(/\s/.test(c))==true) return true;
  }
  return false;
  
}



//-----------------------------------------------------------------------------------------
// Check for correct email address
//-----------------------------------------------------------------------------------------
 function gjfValidEmail(item) {
 //alert('window.RegExp'+window.RegExp);
//  var alertEmailAddrNotEntered         ="Sorry, you must enter your
//  email address.";
//  var alertInvalidCharFoundInEmailAddr ="Sorry, this email address seems
//  wrong.";
//  var alertAtSignMissingInEmailAddr    ="Sorry, this email address seems
//  wrong.";
//  var alertBlankSpaceFoundInEmailAddr  ="Sorry, this email address seems
//  wrong.";
  var invalidEmailChars = "\"|&;<>!*\\";

  email = item

   if (! gjfValidateAsciiData (email)) {
     //alert(alertInvalidCharFoundInEmailAddr);
     return false
   }

   var invalidChars = invalidEmailChars;
   for (var i = 0; i < invalidChars.length; i++) {
     if (email.indexOf(invalidChars.charAt(i)) != -1) {
       //alert(alertInvalidCharFoundInEmailAddr);
       return false;
     }
   }

    if (email == ""){
     //alert(alertEmailAddrNotEntered);
     return false;
   }
   if (email.indexOf("@") == -1){
     //alert(alertAtSignMissingInEmailAddr);
     return false;
   }

   if (email.indexOf(" ") != -1){
     //alert(alertBlankSpaceFoundInEmailAddr);
     return false;
   }
    
   if (window.RegExp) {
     var reg1str = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)";
     var reg2str =
"^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$";

     var reg1 = new RegExp (reg1str);
     var reg2 = new RegExp (reg2str);

     if (reg1.test(email) || !reg2.test(email)) {
       //alert(alertInvalidCharFoundInEmailAddr);
       return false;
     }
   }   
   return true;
 }

 function gjfValidateAsciiData(urstr) {
 //use by gjfValidEmail
   var i;
   var result;

   for (i=0; i<urstr.length; i++) {
     if ((urstr.charAt(i) < " ") || (urstr.charAt(i) > "~"))
       return false;
   }
   return true;
 }
 
 function CheckPositiveValue(oStr) {
  if(isNaN(oStr)){
    return false;
  }else{
    if(parseInt(oStr)<0){
      return false;
    }
  }
  return true;
 }
 
function CheckRadio(oObject , oText)
{
  var i=oObject.length;
  for (var j=0;j<i;j++) {
          if (oObject[j].checked) 
          {         
            return false;
          }
  }
  return true;
}
 
function CheckSelected(oObject) {
  
  if (oObject.options[oObject.selectedIndex].value=='' || oObject.options[oObject.selectedIndex].value==-1){
    return true;
  }
  return false;
}



