/******************************************************************************************************************************
 *                                                                                                                            *
 *  All the JavaScript codes in this file are writed by Fints                                                                 *
 *  You MAY NOT redistribute these codes (for example to a web site) without written permission from the original author.     *
 *  Failure to do so is a violation of copyright laws.                                                                        *
 *  If you have any question, please send us an email or visit our web site. Thank you!                                       *
 *  E-mail: zjzmzy@163.com        Homepage: http://www.fints.cn                                                               *
 *  (c)Copy Right Fints 2004.  All Right Reserved.                                                                            *
 *                                                                                                                            *
 ******************************************************************************************************************************/

// Open a small window
function OpenWin(URL,W,H)
{
  var windowW = W;
  var windowH = H;
  var windowX = Math.ceil( (window.screen.width-windowW)/2 );
  var windowY = Math.ceil( (window.screen.height-windowH)/2 );
  var feature = "fullscreen=no,toolbar=no,directions=no,location=no,status=no,menubar=no,scrollbar=no,resizable=no,overflow:hidden";
  
  vbWin = window.open(URL,"win",feature);
  vbWin.resizeTo( Math.ceil(windowW), Math.ceil(windowH) );
  vbWin.moveTo( Math.ceil(windowX), Math.ceil(windowY) );
}

// Count the character
function UpdateSize(fld_From, fld_To)
{
  var iMaxChar = 1000; // the max number of the chars
	var from_elm = document.getElementById(fld_From);
	if (from_elm) {	
		var to_elm = document.getElementById(fld_To);
		if (to_elm) {
		  var iLen = from_elm.value.length;
		  if (iLen>iMaxChar) {
		      alert("Sorry!\nYou have inputed "+iLen+" characters, but the number is no more than "+iMaxChar+" characters!");
		      return false;		  
		  }
		  to_elm.innerText = iMaxChar-iLen; // the rest number
		}
	}
}

// Press Ctrl+Enter or Alt+s to post the form speedily!
function CtrlEnter()
{
	if((event.ctrlKey && window.event.keyCode == 13) || (event.altKey && window.event.keyCode == 83))
	{	  
	  if ( Check() )
	  {
		  window.document.FORM.submit();
		}
	}
}

// Upload file
function Upload( sRoot )
{
  if ( document.getElementById("chkUpload").checked )
  {
    var URL = "/include/upload.asp?fileroot="+sRoot;
    OpenWin(URL,400,200);
  }
}

// check out the Account
function IsValidAccount( sAccount )
{
	regexp_value = new RegExp( '^\\w+$' );
	if ( !sAccount.match( regexp_value ) )
		return false;
	
	return true;
}

// check out E-mail
function IsValidEmail( sEmail )
{
	regexp_value = new RegExp( '^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$' );
	if ( !sEmail.match( regexp_value ) )
		return false;
	
	return true;
}

// check out Url
function IsValidUrl( sUrl )
{
	regexp_value = new RegExp( '^((http|https|ftp|rtsp|mms)(:\/\/))*(\\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))+$' );
	if ( !sUrl.match( regexp_value ) )
		return false;
	
	return true;
}

// check out the mobile number
function IsValidMobile( sMb )
{
	regexp_value = new RegExp( '^13\\d{9}$' );
	if ( sMb.length != 11 || !sMb.match( regexp_value ) )
		return false;
	
	return true;
}

// check out the Telephone Number
// the form of the sPhone is (917)-922-1035, (86025)84592610
function IsValidPhone( sPhone )
{
	//regexp_value = new RegExp( '^(\(\\d+\)(-\\d+)+)|(\\d+)$'' );
	regexp_value = /^((\(\d+\)(-?\d+)+)|(\d+))$/
	if ( !sPhone.match( regexp_value ) ) 
		return false;
	
	return true;
}

// check out the String
// Judge whether each char in the InString is in the RefString or not
function IsVaildString(InString,RefString)
{
  var c;
	if (InString.length==0) return (false);
	for (var Count=0; Count < InString.length; Count++)  
	{
		c= InString.substring(Count, Count+1);
		if (RefString.indexOf(c,0)==-1)  
		  return (false);
	}
	return (true);
}

// check out the Date
function IsValidDate(Y,M,D)
{
  var aMonthDays = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
  var lLeap = false;
  
  if ( (Y%4==0) && ( (Y%100!=0) || (Y%400==0) ) )
    lLeap = true;
  if ( (D<1) || (D>31) || (M<1) || (M>12) || (Y<1900) )
    return false;
  if ( D>aMonthDays[M-1] && !( (M==2) && (D>28) ) )
    return false;
  if ( !lLeap && (M==2) && (D>28) )
    return false;
  if ( lLeap && (M==2) && (D>29) )
    return false;
  
  return true;
}

// validate zip
// Input: ZIP = document.all.txtZip;
function IsValidZip(ZIP)
{
  var valid = "0123456789-";
  var iCntHyphen = 0;

  if (ZIP.value.length !=5 && ZIP.value.length!=10) {
    alert("Please enter your 5 digit or 5 digit+4 zip code.");
    ZIP.focus();
    return false;
  }

  for (var i=0; i < ZIP.value.length; i++) {
    temp = "" + ZIP.value.substring(i, i+1);
    if (temp == "-") 
      iCntHyphen++;
      
    if (valid.indexOf(temp) == "-1") {
      alert("Invalid characters in zip code. Please try again.");
      ZIP.focus();
      return false;
    }
  }

  if ( (iCntHyphen > 1) || ((ZIP.value.length==10) && ""+ZIP.value.charAt(5)!="-")) {
    alert("The hyphen should be used like this '12345-6789'. Please try again.");
    ZIP.focus();
    return false;
  }
}