/**
* Common javascript validaion functions
* Copyright (C) 2006 - wiantech.com
*
*/
function pr(obj){
    s = "";
    for( v in obj){
        s = s + "<br>" + v;
    }
    document.write(s);
}

function errshow(a,b,c){
    alert("Wiantech Test:: Javascript Erro \n" + a + "\n" + b + "\n Line No:" + c);
}
//onerror = errshow;

function getobj(_id){
    var obj;
    obj = document.getElementById(_id);
    if(  null == obj ){
        obj = document.all(_id)
    }
    return obj;
}

function testRegx(strValue, strMatchPattern){
    //RETURNS:  True if valid, otherwise false.
    var objRegExp = new RegExp( strMatchPattern);

    //check if string matches pattern
    return objRegExp.test(strValue);
}

function limittext(object, psize){
    if( null != object ){
        size = object.value.length;
        if( size > psize){
            object.value = object.value.substr(0,psize);
        }
    }
}

function textareaMaxsize(objName, psize)
{
    object = document.getElementById(objName);
    object.onchange = new Function ( " limittext(this,"+ psize +");" );//{ "this," + psize + ", limittext(object,
}

function on_click_toggle_all(bval, prefix, formname){
//set checked for check box
    for(xchk in document.forms[formname].elements){
        obj = document.forms[formname].elements[xchk];
        if( "checkbox" == obj.type ){
            if( 0 == (obj.id).indexOf(prefix) ){
                obj.checked = bval;
            }
        }
    }
}

/* String Functins */
function ltrim(str){
    str = this != window ? this : str;
    return str.replace(/^\s+/,'');
}

function rtrim(str){
    str = this != window ? this : str;
    return str.replace(/\s+$/,'');
}

function trim(str){
    str = this != window ? this : str;
    return str.replace(/^\s+/,'').replace(/\s+$/,'');
}

/*String Regex Functions*/

function isEmail(str){
    str = this != window ? this : str;
    var regx = /[A-Za-z0-9_\-.]+\@[A-Za-z0-9_\-.]+\.([A-Za-z_]{2,4})/;
    return regx.test(str);
}
function isNotEmail(str){
    str = this != window ? this : str;
    return !str.isEmail();
}


function isNumber(str){
    str = this != window ? this : str;
    return ! isNaN(str);
}
function isNotNumber(str){
    str = this != window ? this : str;
    return  isNaN(str);
}

function isEmpty(str){
    str = this != window ? this : str;
    str= str.replace(/^\s+/,'').replace(/\s+$/,'');
    return (str.length == 0) ;
}
function isNotEmpty(str){
    str = this != window ? this : str;

    return !str.isEmpty();
}

function reverseStr(str){
    str = this != window ? this : str;
    var outp = "";
        for (i = 0; i <= str.length; i++) {
        outp = str.charAt (i) + outp
    }
    return outp;
}

var curr_date_label = "";
function isNotDate(str){
  str = this != window ? this : str;
  return ! str.isDate();
}
function isDate(str){
  str = this != window ? this : str;
  arr = str.split("-");
  if( arr.length  != 3 ){
      alert(curr_date_label + " Invalid Date");
      return false;
  }

  if( isNaN(arr[0]) || isNaN(arr[1]) || isNaN(arr[2]) ){
    alert(curr_date_label + " invalid Date");
    return false;
  }
 //format mm-dd-yyyy : No chnge

 //Format dd-mm-yyyy Start
 tmp = arr[1];
 arr[1] = arr[0];
 arr[0] = tmp;
//Format dd-mm-yyyy End

  if( arr[2].length !=4 ){
      alert(curr_date_label + " Invalid Year");
      return false;
  }else if ( arr[2] <= 1970){
      alert(curr_date_label + " Invalid Year");
      return false;
  }else if( !((arr[0] <=12 ) && (arr[0] >=1 )) ){
      alert(curr_date_label + " Invalid Month");
      return false;
  }else{
    ret = true;
    if( !((arr[1] <=31 ) && (arr[1] >=1 )) ){
        ret = false;
    }else if( (arr[0]==4) || (arr[0]==6) || (arr[0]==9) || (arr[0]==11) ){
       ret = ( arr[1] != 31);
    }else if( arr[0] ==2 ){
       if( arr[1] >28 ){
          ret =  (arr[1]==29) && ((arr[2] % 4)==0);
       }
    }
  }
  if( !ret ){ alert(curr_date_label + " Invalid Day"); }
  return ret;
}

//---------------------------
function trim_form(obj){
    for(i=0; i<obj.elements.length; i++){
         ctype =  obj.elements[i].type.toLowerCase();
         if( (ctype == "text") || (ctype == "textarea") ){
              obj.elements[i].value = obj.elements[i].value.trim();
         }
    }
}

function inputs(obj){
    s = "";
    for(i=0; i<obj.elements.length; i++){
        //prn(obj.elements[i].);
        if( obj.elements[i].nodeName == 'INPUT' ){
           s += "\r\n obj = document.getElementById('" + obj.elements[i].id + "');";
        }
    }
    document.writeln(s);
}
/*
function chk_empty(arr_ctl){
  for(ctl in arr_ctl){
    obj = document.getElementById(ctl);
    if( null == obj ){ alert( ctl + " Not Found"); return false; }
    if( "" == obj.value ){
        alert( arr_ctl[ctl] + " Cant be empty.");
        obj.focus();
        return false;
    }
 }
 return true;
}
function chk_empty_sel(arr_ctl){
  for(ctl in arr_ctl){
       obj = document.getElementById(ctl);
       if( null == obj ){ alert( ctl + " Not Found"); return false; }
       if( (obj.value == -1) || (obj.value == '') ){
         alert( "Please Select " + arr_ctl[ctl] );
         obj.focus();
         return false;
       }
 }
 return true;
}

function chk_is_date(arr_ctl){
  for(ctl in arr_ctl){
   curr_date_label = arr_ctl[ctl];
   obj = document.getElementById(ctl);
   if( obj.value.is_Date() == false ){
       obj.focus();
       curr_date_label = "";
       return false;
   }
 }
 curr_date_label = "";
 return true;
}

function chk_isEmail(arr_ctl){
  for(ctl in arr_ctl){
    obj = document.getElementById(ctl);
    if( ! obj.value.isEmail() ){
        alert( arr_ctl[ctl] + " is Not a valid Email"   );
        obj.focus();
        return false;
    }
 }
 return true;
}


function chkExt(msg, fileName, ext){

    if( null == fileName ){
        alert(fileName);
        return true;
    }

    fileName = fileName.trim();
    ext      = ext.trim();

    arrExt = ext.split(",");
    len    = fileName.length;
    if( "" == fileName ){
        return true; // ignore the Empty control
    }

    if( len > 4 ) {
        dotAt = len - ( fileName.reverseStr().indexOf(".") );
        fileExt = fileName.substring(dotAt, len);
        fileExt = fileExt.toLowerCase();
        if( "" != fileExt ){
             for(xExt in arrExt){
                if( arrExt[xExt].toLowerCase() == fileExt ){
                    return true;
                }
             }
         }
    }file:///opt/lampp/htdocs/hemant/intvo/admin/jsScript_2.js
    alert(msg + " is not allowed type file.");
    return false;
}

function isId(str){
//must be only 8 char long
//start with alphabet and other have number _
    str = this != window ? this : str;
    if( (8 <= str.length) & (60 >= str.length) ){
        return false; //Alert len must be 8
    }
    if(! testRegx(str, "^(([a-zA-Z])+([a-zA-Z0-9_])*)$" ) ){
        return false;// No Valid Format
    }
    return true;
}

function isName(str){
//start with alphabet and other have space number _
    str = this != window ? this : str;

    if(! testRegx(str, "^(([a-zA-Z])+([a-zA-Z0-9 _])+)$" ) ){
        return false;// No Valid Format
    }
    return true;
}

function isAddress(str){
// None empty include chara space numbres _ , . - &
    str = this != window ? this : str;
    if(! testRegx(str, "^([a-zA-Z0-9 \r\n_,\-\.&/])+$" ) ){
        return false;// No Valid Format
    }
    return true;
}

function isPhone(str){
//+9846...
//132465
//no space
    str = this != window ? this : str;
    if(! testRegx(str, "^([\+]){0,1}\([0-9])+$" ) ){
        return false;// No Valid Format
    }
    return true;
}

function isZip(str){
//start with alpha num , have space -
    str = this != window ? this : str;
    //if(! testRegx(str, "^([a-zA-Z0-9])+\([a-zA-Z0-9 \-])*$" ) ){
    if(! testRegx(str, "^([a-zA-Z0-9 \-])*([0-9])+([a-zA-Z0-9 \-])*$" ) ){

        return false;// No Valid Format
    }
    return true;
}

*/


String.prototype.ltrim = ltrim;
String.prototype.rtrim = rtrim;
String.prototype.trim = trim;

String.prototype.isEmpty = isEmpty;
String.prototype.isNotEmpty = isNotEmpty;

String.prototype.isEmail = isEmail;
String.prototype.isNotEmail = isNotEmail;

String.prototype.isNumber = isNumber;
String.prototype.isNotNumber = isNotNumber;

String.prototype.isDate = isDate;
String.prototype.isNotDate = isNotDate;

String.prototype.reverseStr = reverseStr;
/*
String.prototype.isId = isId;
String.prototype.isName = isName;
String.prototype.isAddress= isAddress;
String.prototype.isZip= isZip;
String.prototype.isPhone = isPhone;
*/
