﻿/**
 * Validates form elements.
 */
function validateForm(form) {
    var result = true;
    var elements = form.elements;
    for (var i = 0; (i < elements.length) && result; ++i) {
        var element = elements[i];
        var invalidBackgroundColor = "#DF9FC1";
        var nextSibling = element.nextSibling;

        if ((nextSibling != null) && (nextSibling.nodeType == 1) && (nextSibling.tagName == "A")) {
            
            //validate the field is not null
            if ((nextSibling.className == "nonempty") && (Trim(element.value) == "")) {
                alert("" + nextSibling.firstChild.nodeValue + " 請勿輸入空白.");
                result = false;
            }

            //validate the field is not null nor too long
            if (nextSibling.className == "description") {
                if (Trim(element.value) == "") {
                    alert("" + nextSibling.firstChild.nodeValue + " 請勿輸入空白.");
                    result = false;
                }
                else if (element.value.length > 5000) {
                    alert("" + nextSibling.firstChild.nodeValue + " 請勿輸入超過5000個字元.");
                    result = false;
                }
            }
            
            if (nextSibling.className == "description2") {
                if (Trim(element.value) != "" && element.value.length > 1000) {
                    alert("" + nextSibling.firstChild.nodeValue + " 請勿輸入超過1000個字元.");
                    result = false;
                }
            }

            //no trainling blank character
            if ((nextSibling.className == "generalname") && !isGeneralName(element.value)) {
                alert("" + nextSibling.firstChild.nodeValue + " cannot begin/end with a blank character.");
                result = false;
            }

            //validate the field is not null or http://
            if ((nextSibling.className == "http") && ((Trim(element.value) == "") || (Trim(element.value) == "http://"))) {
                alert("" + nextSibling.firstChild.nodeValue + " 請勿輸入空白.");
                result = false;
            }
            
            //the img format must JPG、GIF、BMP
            if ((nextSibling.className == "imgformat") && (Trim(element.value) != "")) {
                var str,j;
                j=element.value.length;
                str=element.value.substring(j-4,j);
                str=str.toUpperCase();
                if (str!=".JPG" && str!="JPEG" && str!=".GIF" && str!=".BMP"){
                   alert("" + nextSibling.firstChild.nodeValue + " 限jpg、jpeg、gif、bmp四種格式.");
                   result = false;
                }   
            }
            
            //the img format must JPG、GIF、BMP
            if ((nextSibling.className == "imgformat_must")) {
                var str,j;
                j=element.value.length;
                str=element.value.substring(j-4,j);
                str=str.toUpperCase();
                if (str!=".JPG" && str!="JPEG" && str!=".GIF" && str!=".BMP"){
                   alert("" + nextSibling.firstChild.nodeValue + " 限jpg、jpeg、gif、bmp四種格式.");
                   result = false;
                }   
            }

            //validate the field is mail format
            if ((nextSibling.className == "mailformat" && Trim(element.value) != "")) {
                var regstr = /^[\w-]+(\.[\w-]*)*@([\w-]+\.)+[\w-]{2,4}$/;
                if (!regstr.test(element.value)) {
                    alert("" + nextSibling.firstChild.nodeValue + " 請輸入正確的 E-Mail 格式.");
                    result = false;
                }
            }
            
            //validate the field is mail format
            if ((nextSibling.className == "mailformat_must" )) {
                var regstr = /^[\w-]+(\.[\w-]*)*@([\w-]+\.)+[\w-]{2,4}$/;
                if (!regstr.test(element.value)) {
                    alert("" + nextSibling.firstChild.nodeValue + " 請輸入正確的 E-Mail 格式.");
                    result = false;
                }
            }

            //validate the field is URL format
            if ((nextSibling.className == "urlformat") && (Trim(element.value) != "")) {
                var regstr = /^(http|https):\/\/([\w-]+\.)+[\w-]{2,4}(:\d+)?(\/[\w- .\/\?%&=]*)?$/;
                if (!regstr.test(element.value)) {
                    alert("" + nextSibling.firstChild.nodeValue + " 請輸入正確的 網址 格式，範列：http://www.esun.com.tw.");
                    result = false;
                }
            }
            if ((nextSibling.className == "urlformat_must")) {
                var regstr = /^(http|https):\/\/([\w-]+\.)+[\w-]{2,4}(:\d+)?(\/[\w- .\/\?%&=]*)?$/;
                if (!regstr.test(element.value)) {
                    alert("" + nextSibling.firstChild.nodeValue + " 請填寫 網址 格式，範列：http://www.esun.com.tw.");
                    result = false;
                }
            }
            //validate the field is date format
            if ((nextSibling.className == "dateformat") && (Trim(element.value) != "")) {
                var reg  =  /^[0-9]{4}\/[0-9]{1,2}\/[0-9]{1,2}(\s[0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2})?$/;
                result = (reg.test(element.value));
                if (!reg.test(element.value)) {
                    alert("" + nextSibling.firstChild.nodeValue + " 值不正確. 範例： 2005/7/3.");
                    result = false;
                }
            }
            
            if ((nextSibling.className == "dateformat_must" )) {
                var reg  =  /^[0-9]{4}\/[0-9]{1,2}\/[0-9]{1,2}(\s[0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2})?$/;
                result = (reg.test(element.value));
                if (!reg.test(element.value)) {
                    alert("" + nextSibling.firstChild.nodeValue + " 不得為空值並且格式如： 2005/7/3.");
                    result = false;
                }
            }
            
            //validate the field is date format
            if ((nextSibling.className == "letter")) {
                var reg  =  /^[a-zA-Z0-9_]+?$/;
                result = (reg.test(element.value));
                if (!reg.test(element.value)) {
                    alert("" + nextSibling.firstChild.nodeValue + " format is not a correct letter format, bounding a-z,A-Z,0-9,_. For example abc123_. ");
                    result = false;
                }
                else if(element.value.length < 3) {
                    alert("" + nextSibling.firstChild.nodeValue + " is too short. It should at least contain 3 characters. ");
                    result = false;
                }                
                else if(!isNaN(element.value.charAt(0)) || element.value.charAt(0) == "_") {
                    alert("" + nextSibling.firstChild.nodeValue + " format is incorrect, the first character must be letter.");
                    result = false;
                }
            }

            //validate the field is zip format
            if ((nextSibling.className == "zipcode")) {                
                var reg  = /^[+-]?[*0-9]+(\-[0-9]+)?$/;                
                if (!reg.test(element.value)) {
                    alert("" + nextSibling.firstChild.nodeValue + " format is not a correct zip code format. For example 12345-6789, or 12345.");
                    result = false;
                }
            }

			//統一編號譣証,不能是空白"
			if ((nextSibling.className == "vat")) {
			   var vat = element.value;
			   var tbNum = new Array(1,2,1,2,1,2,4,1);
               var temp = 0;
               var total = 0;
               if(vat=="") {
	             alert("請先輸入營利事業統一編號!");
	            result = false;
	            }
               else if(!vat.match(/^\d{8}$/)) {
	             alert("營利事業統一編號長度不夠或格式有誤!請注意營利事業統一編號格式為八碼數字!");
	            result = false;
	            }
             else{
             for(var i = 0; i < tbNum.length ;i ++){
                 temp = vat.charAt(i) * tbNum[i];
                 total += Math.floor(temp/10)+temp%10;
             }
             if(total%10==0 || (total%10==9 && vat.charAt(6)==7)) alert("營利事業統一編號正確!");
             else alert("營利事業統一編號錯誤!");
			 result = false;
               }	
			}
			//統一編號譣証"
			if ((nextSibling.className == "vat2")) {
			   var vat = element.value;
			   var tbNum = new Array(1,2,1,2,1,2,4,1);
               var temp = 0;
               var total = 0;
			   if (vat != "") {
               if(!vat.match(/^\d{8}$/)) {
	             alert("營利事業統一編號長度不夠或格式有誤!!");
	            result = false;
	            }
             else{
             for(var i = 0; i < tbNum.length ;i ++){
                 temp = vat.charAt(i) * tbNum[i];
                 total += Math.floor(temp/10)+temp%10;
             }
             if(total%10!=0 || (total%10!=9 && vat.charAt(6)!=7)) alert("營利事業統一編號錯誤!");
			 result = false;
               }	
			}
			}
						
			
			//手機驗証必需數字且大於4位元不能是空白"
            if ((nextSibling.className == "mobile")) {                
                var reg = /^[0-9]*$/;
                //var strLength = element.value.Length;
                result = (reg.test(element.value));
                if (!reg.test(element.value) || element.value == "" || element.value.length < 10) {
                    alert("" + nextSibling.firstChild.nodeValue + " 格式不正確");
                    result = false;
                }
            }
			
			//手機驗証必需數字且大於4位元"
            if ((nextSibling.className == "mobile2") && element.value != "") {            
                var reg = /^[0-9]*$/;
                //var strLength = element.value.Length;
                result = (reg.test(element.value));
                if (!reg.test(element.value) || element.value.length < 10) {
                    alert("" + nextSibling.firstChild.nodeValue + " 格式不正確");
                    result = false;
                }
            }
			
			//電話驗証必需英文數字且大於7位元"
            if ((nextSibling.className == "phone") && element.value != "") {                
                var reg = /^[0-9]*$/;
                //var strLength = element.value.Length;
                result = (reg.test(element.value));
                if (!reg.test(element.value) || element.value.length < 7) {
                    alert("" + nextSibling.firstChild.nodeValue + " 格式不正確");
                    result = false;
                }
            }
			
			//電話驗証必需英文數字且大於7位元,而且不能為空白"
            if ((nextSibling.className == "phone2")) {                
                var reg = /^[0-9]*$/;
                //var strLength = element.value.Length;
                result = (reg.test(element.value));
                if (!reg.test(element.value) || element.value.length < 7 || element.value == "") {
                    alert("" + nextSibling.firstChild.nodeValue + " 格式不正確");
                    result = false;
                }
            }
			
			//帳號驗証必需英文數字且大於4位元"
            if ((nextSibling.className == "checkaccount")) {                
                var reg = /[0-9]|[a-z]|[A-Z]|-/;
                //var strLength = element.value.Length;
                result = (reg.test(element.value));
                if (!reg.test(element.value) || element.value == "" || element.value.length < 4) {
                    alert("" + nextSibling.firstChild.nodeValue + " 格式不正確");
                    result = false;
                }
            }

			//密碼驗証 必需英文數字且大於4位元"
            if ((nextSibling.className == "checkpassword")) {                
                var reg = /[0-9]|[a-z]|[A-Z]|-/;
                //var strLength = element.value.Length;
                result = (reg.test(element.value));
                if (!reg.test(element.value) || element.value == "" || element.value.length < 4) {
                    alert("" + nextSibling.firstChild.nodeValue + "格式不正確");
                    result = false;
                }
            }

            //身份字號驗證
            if ((nextSibling.className == "checkID")) {
				var IDCARD = element.value;
				var tab = "ABCDEFGHJKLMNPQRSTUVXYWZIO";
				var A1 = new Array (1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3 );
                var A2 = new Array (0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5 );
                var Mx = new Array (9,8,7,6,5,4,3,2,1,1);
				
				j = tab.indexOf( IDCARD.charAt(0) );							            				
     
	            sum = A1[j] + A2[j]*9;

                for ( j=1; j<10; j++ ) {
                v = parseInt( IDCARD.charAt(j) );
                if ( isNaN(v) ) result = false;
                sum = sum + v * Mx[j];
                }			
                if ( sum % 10 != 0 || IDCARD == "" || IDCARD.length != 10 || j == -1) {
                  alert("" + nextSibling.firstChild.nodeValue + " 格式不正確.");
                  result = false;
                }
			}

            //validate the field is Code format
            if ((nextSibling.className == "code")) {                
                var reg  =  /^[0-9]+(\-[0-9]+)?$/;
                result = (reg.test(element.value));
                if (!reg.test(element.value)) {
                    alert("" + nextSibling.firstChild.nodeValue + " format is not a correct Code format.");
                    result = false;
                }
            }
            
            //validate the field which is "SSN/Tax ID"
            if ((nextSibling.className == "ssn")) {                
                var reg = /[0-9]|[a-z]|[A-Z]|-/;
                var regNegative = /[\.]|[_]/;
                //var strLength = element.value.Length;
                result = (reg.test(element.value) && !regNegative.test(element.value));
                if (!reg.test(element.value) || regNegative.test(element.value) || element.value == "") {
                    alert("The format of " + nextSibling.firstChild.nodeValue + " is not correct.");
                    result = false;
                }
            }  
			
			
            
            //validate the field is float format
            if ((nextSibling.className == "floatformat")) {
                var regfloat  = /^[0-9]+(\.[0-9]+)?$/;
                if (!regfloat.test(element.value)) {
                    alert("" + nextSibling.firstChild.nodeValue + " field requires a positive float!");
                    return false;
                }
                else if (element.value <= 0) {
                    alert("" + nextSibling.firstChild.nodeValue + " field should be more than 0!");
                    return false;
                }
            }
            
            //validate the field is money format
            if ((nextSibling.className == "moneyformat")) {
                var regfloat  = /^[0-9]+(\.[0-9]+)?$/;
                var letter = element.value;
                if (letter.substr(0,1) == "$") {
                    letter = letter.substr(1,letter.length-1);
                }
                else{
                    alert("" + nextSibling.firstChild.nodeValue + " field must be a money format. For example $1.00!");
                    return false;
                }
                
                if (!regfloat.test(letter)) {
                    alert("" + nextSibling.firstChild.nodeValue + " field must be a money format. For example $1.00!");
                    return false;
                }
                else if (letter <= 0) {
                    alert("" + nextSibling.firstChild.nodeValue + " field should be more than 0!");
                    return false;
                }
            }

            //validate the field is int format
            if ((nextSibling.className == "intformat")) {
                var regint  = /^[0-9]*$/;
                if (!regint.test(element.value)) {
                    alert("" + nextSibling.firstChild.nodeValue + " 請輸入數字!");
                    result = false;
                }
		    }

            //validate the field is int format
            if ((nextSibling.className == "intformat_must")) {
                var regint  = /^[0-9]*$/;
                if (!regint.test(element.value) || (Trim(element.value) == "")) {
                    alert("" + nextSibling.firstChild.nodeValue + " 請輸入正整數!");
                    result = false;
                }
                else {                
                	if (element.value<0) {
                	    alert("" + nextSibling.firstChild.nodeValue + " 請輸入大於等於 0 的正整數!");
                	    result = false;
                	}
                }
            }
			
            //validate the field is int format
            if ((nextSibling.className == "n_intformat" )) {
                var regint  = /^([0-9]*)?$/;
                if (!regint.test(element.value) && Trim(element.value) == "") {
                    alert("" + nextSibling.firstChild.nodeValue + " 請輸入正整數。");
                    result = false;
                }
                else {                
                    if (element.intlength) {
                        var intlength = parseInt(element.intlength);
                        var max  = Math.pow(2,intlength);
                        if (parseFloat(element.value) > max-1) {
                            alert("" + nextSibling.firstChild.nodeValue + " is too big, please input it again!");                                                        
                            result = false;
                        }
                    }
                }
            }
	    //validate the field is int format
            if ((nextSibling.className == "full_intformat")) {
                var regint  = /^-?[0-9]+$/;
                if (!regint.test(element.value)) {
                    alert("" + nextSibling.firstChild.nodeValue + " field requires a positive integer!");
                    result = false;
                }
                else {                
                    if (element.intlength) {
                        var intlength = parseInt(element.intlength);
                        var max  = Math.pow(2,intlength);
                        if (parseFloat(element.value) > max-1) {
                            alert("" + nextSibling.firstChild.nodeValue + " is too big, please input it again!");                                                        
                            result = false;
                        }
                    }
                }
            }
        }
        if (!result){
          element.style.backgroundColor = invalidBackgroundColor;
          element.focus();
          element.onkeyup = function(){
            this.style.backgroundColor  = "";
          }
        }
    }
    return result;
}

function validateCheckBoxGroup(name, message){
  var isValid = false;
  var elements = document.getElementsByName(name);
  for (var i=0; i < elements.length; i++){
    var element = elements(i);
    if (element.checked){
      isValid = true;
      break;
    }
  }
  if (!isValid){
    alert(message);
    return false;
  }
  else{
    return true;
  }
}

/**
 * Checks whether a character is blank (space, tab, or return).
 */
function isBlankCharacter(ch) {
    return (ch == ' ') || (ch == '\t') || (ch == '\r') || (ch == '\n');
}

/**
 * Checks whether a string is nonempty and has blank trailing (space, tab, or return).
 */
function isGeneralName(name) {
    return (name.length > 0) && !isBlankCharacter(name.charAt(0)) && !isBlankCharacter(name.charAt(name.length - 1));
}

/**
 * Deletes left space.
 */
function LTrim(str) {
    var whitespace = new String(" \t\n\r");
    var s = new String(str);
    if (whitespace.indexOf(s.charAt(0)) != -1) {
        var j = 0, i = s.length;
        while (j < i && whitespace.indexOf(s.charAt(j)) != -1) {
            j++;
        }
        s = s.substring(j, i);
    }
    return s;
}

/**
 * Deletes right space.
 */
function RTrim(str) {
    var whitespace = new String(" \t\n\r");
    var s = new String(str);
    if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
        var i = s.length - 1;
        while ((i >= 0) && (whitespace.indexOf(s.charAt(i)) != -1)) {
            i--;
        }
        s = s.substring(0, i+1);
    }
    return s;
}

/**
 * Deletes left and right space.
 */
function Trim(str) {
    return RTrim(LTrim(str));
}

/**
 * Gets year, month, day array by parsing date string.
 */
function getYMDDate(date, separator, sequence) {
    var dateArray = date.split(separator);
    var yearIndex = sequence.indexOf("y");
    var monthIndex = sequence.indexOf("m");
    var dayIndex = sequence.indexOf("d");
    var ymdArray = new Array(3);

    ymdArray[0] = dateArray[yearIndex];
    ymdArray[1] = dateArray[monthIndex];
    ymdArray[2] = dateArray[dayIndex];

    return ymdArray;
}



function $JsRequestQuerystring(){
  var Url=window.location.href;
  var u,g,StrBack='';
  if(arguments[arguments.length-1]=="#"){
    u=Url.split("#");
  }else{
    u=Url.split("?");
  }
  if(u.length==1){
    g='';
  }else{
    g=u[1];
  }
  if(g!=''){
     gg=g.split("&");
     var MaxI=gg.length;
     str = arguments[0]+"=";
     for(i=0;i<MaxI;i++){
        if(gg[i].indexOf(str)==0) {
          StrBack=gg[i].replace(str,"");
          break;
        }
     }
  }
  return StrBack;
}

 var FunDir  = $JsRequestQuerystring("FunDir");
 var FunCode = $JsRequestQuerystring("FunCode");
 var FunCodeDelUrl = FunCode.substring(0,FunCode.length-2)+"0D.asp";
 var FunCodeAddUrl = FunCode.substring(0,FunCode.length-2)+"1A.asp";
 var FunCodeUpdUrl = FunCode.substring(0,FunCode.length-2)+"2E.asp";

function Submit_Del(){
	var x = document.form1.D1.length;
	var n = 0;
	if (isNaN(x) == true){	
	  if (document.form1.D1.checked) n++;
	}else{
	  for (var i=0;i<x;i++){
	    if (document.form1.D1[i].checked) n++;
	  }
	}
	if (n != 0){	
	  delr = confirm("確定要刪除所選取的資料？");
	  if(delr == true){
	    document.form1.Button_Delete.disabled=true;
	    document.form1.target = 'iFrm';
	    document.form1.action = '../'+ FunDir +'/'+ FunCodeDelUrl;
	    document.form1.submit();
	    document.form1.target = '_self';
	    document.form1.action = location.href;
	  }
	}else{	
	  alert("請勾選需刪除之資料");
	}
} 

function Mgr_Del_File(intID,strTableName,strFieldName,strFileName){
  var intID,strTableName,strFieldName,strFileName;
  document.all.iFrm.src="../../inc/Function/F_DelFile.asp?intID="+intID+"&strTableName="+strTableName+"&strFieldName="+strFieldName+"&strFileName="+strFileName;
          //window.open("../../inc/Function/F_DelFile.asp?intID="+intID+"&strTableName="+strTableName+"&strFieldName="+strFieldName+"&strFileName="+strFileName,'','');
}

function Mgr_Del_File_Public(strPublicTableName,strFolderName,intID,strTableName,strFileName,img){
  var strPublicTableName,strFolderName,intID,strTableName,strFileName,img;
  document.all.iFrm.src="../../inc/Function/F_DelFilePublic.asp?strPublicTableName="+strPublicTableName+"&strFolderName="+strFolderName+"&intID="+intID+"&strTableName="+strTableName+"&strFileName="+strFileName+"&img="+img;
          //window.open("../../inc/Function/F_DelFilePublic.asp?strPublicTableName="+strPublicTableName+"&intID="+intID+"&strTableName="+strTableName+"&strFileName="+strFileName,'','');
}

//列表select all用
function selectAll(){
   if (document.form1.D1){
      var x=document.form1.D1.length;
      if (document.form1.Sel_All.checked){
         if (isNaN(x) == true){
            document.form1.D1.checked=true;
         }else{	
            for(i=0;i<x;i++){
               document.form1.D1[i].checked=true;
            }
         }
      }else{
         if (isNaN(x) == true){
            document.form1.D1.checked=false;
         }else{
            for(i=0;i<x;i++){
               document.form1.D1[i].checked=false;
            }
         }
      } 
   }
}