function resetLabelColor(){//change label's color to black when user reset form
        var arrId = document.getElementsByTagName('span');
        for (var i=0; i<arrId.length; i++)
                if (arrId[i].id && arrId[i].id.substring(0,3)=='lbl')
                        arrId[i].style.color='#000000';
}

function chkNumKey(e, obj){
		if (!e) {
			return false;
		}

		if (isNetscape()) {
			return ( (e.charCode ==0) || ((e.charCode>=48) && (e.charCode <= 57)) );
		}else {
			return (e.keyCode>=48 && e.keyCode<=57);
		}
}

function chkYearKey(e, obj){//first digit can only be 1 or 2,  another digit can be 0-9 //dealerform2
	var isNum = chkNumKey(e, obj);
	if (obj.value.length==0){
		if (isNetscape())
			return isNum && (e.charCode==49 || e.charCode==50);
		else
			return isNum && (e.keyCode==49 || e.keyCode==50);
	}else
		return isNum;		
}

function isNetscape() {
	return navigator.appName == "Netscape";
}

function isDot(e) {
	if (!e)
		return false;

	if (isNetscape())
		return e.charCode == 46;
	else
		return e.keyCode == 46;
}

function chkFloatKey(e, obj){
	return e && (chkNumKey(e, obj) || isDot(e));
}

function chkGradeKey(e, obj){ // x.yy used in jobform1.php
	if (!e)
		return false;

	var charCode = (isNetscape())?e.charCode : e.keyCode;

	if (obj.value.length == 1) //.
		return (e.charCode == 0) || isDot(e);
	else 
	if (obj.value.length == 0)// 1-4
		return chkNumKey(e, obj) && (charCode > 48  && charCode <= 52); 
	else 
		return chkNumKey(e, obj);
}


function trim(val){
        return val.replace(/(^[ \r\n]+)|([ \r\n]+$)/g, "");
}

function getInnerText ( element ) {
/*        if (element.innerText) {//ie, opera
                alert('in if '+element.innerText);
                return element.innerText;
        }
        else {//netscape
                var text = new Array, textNode;
                var walker = element.ownerDocument.createTreeWalker( element, NodeFilter.SHOW_TEXT, null, true );
                while ( textNode = walker.nextNode() ) text.push( textNode.nodeValue );
                alert('in else '+ text.join(''));
                return text.join('');
        }
*/
        return element.innerHTML.replace(/<[^>]*>/g, "");
        
        /*upper comment part is similar to this return line, it will get only text (exclude html tag)=element.innerText in ie*/
}


function FormValidator(formobj, fieldRequired){
        this.formobj = formobj;
        this.fieldRequired = fieldRequired;
        
        this.preface_err = "กรุณากรอกข้อมูลดังนี้:";
        this.data_txt = "ข้อมูล";
        this.incorrect_txt = "ไม่ถูกต้อง";
        this.valid_filetype = Array("pdf", "doc", "xls", "jpg", "bmp", "zip");
		this.specialLabel = "บริษัทขอสงวนสิทธิ์ในการมอบสิทธิประโยชน์พิเศษให้กับผู้กรอกข้อมูลในช่องนี้ "; /* คำว่า"นี้" ถ้าอยู่ท้ายประโยคและไม่มีตัวอะไรมาคั่นระหว่างมันกับ " แล้วล่ะก็ มันจะเกิด js error ในหน้าภาษาญี่ปุ่นทันที */


        this.hideErr = false;
        this.errorList = new Array;
        this.execute = execute;
        this.checkTxt =checkTxt;
        this.checkSelect = checkSelect;
        this.checkRadioOrChkbox = checkRadioOrChkbox;
        this.checkDate = checkDate;
        this.raiseError = raiseError;
        this.setLabelRed = setLabelRed;
        this.setLabelBlack = setLabelBlack;
        this.displayErrors = displayErrors;
        this.isEmpty = isEmpty;
        this.checkFileType = checkFileType;
        
        this.haveErrors = haveErrors;
        this.specialAlert = specialAlert;
        this.check = check;

        function execute(){
                for (var i=0; i<this.fieldRequired.length; i++){
                        //if (eval("this.formobj." + this.fieldRequired[i]))
                        if (this.formobj.elements[this.fieldRequired[i]]){
                                //eval("var obj = this.formobj." + this.fieldRequired[i]);
                                var obj = this.formobj.elements[this.fieldRequired[i]];
                        }
                        else{//for tag no have attribute "name"
                                var obj = document.getElementById(this.fieldRequired[i]);
                                if (obj.getAttribute('func'))
                                        eval("this."+obj.getAttribute('func')+"()");
                                continue;
                        }
                        var label = document.getElementById('lbl' + this.fieldRequired[i]);
/*
                        if (obj){
                                switch (obj.type){
                                        case 'text'             :
                                        case 'textarea'        :
                                        case 'password'        :
                                                this.checkTxt(obj, label);
                                                break;
                                        case 'select-one'        :
                                                this.checkSelect(obj, label);
                                                break;
                                        case 'file'             :
                                                this.checkFileType(obj, label);
                                                break;
                                        default:
                                }
                                if (!obj.type){  //for checkbox and radio
                                        this.checkRadioOrChkbox(obj, label);
                                }
                        }
*/
                        this.check(obj, label);                        
                }

        }
        
        function check(obj, lbl) {
          if (obj){
            switch (obj.type){
                    case 'text'             :
                    case 'textarea'        :
                    case 'password'        :
                            this.checkTxt(obj, lbl);
                            break;
                    case 'select-one'        :
                            this.checkSelect(obj, lbl);
                            break;
                    case 'file'             :
                            this.checkFileType(obj, lbl);
                            break;
                    default:
            }
            if (!obj.type){  //for checkbox and radio
                    this.checkRadioOrChkbox(obj, lbl);
            }
          }        
        }

        function raiseError(msg, obj){// set errors
                x = this.errorList.length;
                this.errorList[x] = new Object();
                this.errorList[x].msg = msg;
                this.errorList[x].obj = obj;
        }

        function haveErrors(){
                return (this.errorList.length>0) || this.hideErr;
        }

        function displayErrors(){// how to show errors
                if (!this.hideErr) {
                        alert(this.preface_err+"\n" + this.errorList[0].msg);
						if (this.errorList[0].obj.focus){
	                        this.errorList[0].obj.focus();
						}
                }
        }
        
        function specialAlert(msg, obj){
                if (this.errorList.length == 0){
                        alert(msg);
						if (obj.focus) {
							obj.focus();
						}
                        
                        this.hideErr = true;
                        return;
                }
        }

        function isEmpty(val){
                return (val.match(/^\s+$/) || val=="");
        }

        function isNumPositive(val) { // check integer number
                return val.match(/^([1-9]+[0-9]*|[1-9])$/);
        }

        function isFloatPositive(val) {
//alert('in isfloatpositive ' + val); 
                //return val.match(/^(([1-9]+[0-9]*)|0)\.[0-9][0-9]$/);// check float format= x.xx
		//return val.match(/^(([1-9]+[0-9]*)|0)\.[0-9][0-9]*$/) && (val > 0);// check float format= x.x
//alert('isnumpositive = '+isNumPositive(val));
//alert('check2 = ' + val.match(/^(([1-9]+[0-9]*)|0)\.[0-9][0-9]*$/));
		return isNumPositive(val) || val.match(/^(([1-9]+[0-9]*)|0)\.[0-9][0-9]*$/);
        }
		
		function isHourPerDay(val) {
				return (val > 0 && val <= 24)
		}
        
        function isEmail(val){// check valid email
                return val.match(/^[a-zA-Z0-9]+[a-zA-Z0-9-_.]*@[a-zA-Z0-9-_]+(\.[a-zA-Z0-9-_]+)+$/);
        }

        function setLabelRed(label){// change color of label to red
                label.style.color='#FF0000';
        }

        function setLabelBlack(label){// change color of label to black
                label.style.color='#000000';
        }
        
        function checkTxt(txtObj, label){
                var errFlag = false;

                txtObj.value = trim(txtObj.value);
                if (isEmpty(txtObj.value) || (txtObj.value==trim(this.specialLabel))){
                        this.raiseError(getInnerText(label), txtObj);
                        errFlag = true;//for appearing once for this field, exclude other checking
                }

                if (!errFlag && (txtObj.getAttribute('numPositive'))){
                        txtObj.value = parseInt(txtObj.value);//change value from string to int ex. 001 to 1
                        if (!isNumPositive(txtObj.value)){
                                //this.raiseError(getInnerText(label) + " must be a positive number and more than 0", txtObj);
                                this.specialAlert(this.data_txt + " " + getInnerText(label) + " " + this.incorrect_txt, txtObj);
				errFlag = true;
                        }
                }


                if (!errFlag && (txtObj.getAttribute('floatPositive'))){
			if (parseFloat(txtObj.value))
				txtObj.value = parseFloat(txtObj.value)
			else{
				errFlag = true;	
				this.specialAlert(this.data_txt + " " + getInnerText(label) + " " + this.incorrect_txt, txtObj);
			}
                }

                if (!errFlag && (txtObj.getAttribute('email') && !isEmail(txtObj.value))){
                        this.specialAlert(this.data_txt + " " + getInnerText(label) + " " + this.incorrect_txt, txtObj);
                        errFlag = true;
                }

		if (!errFlag && (txtObj.getAttribute('hourPerDay') && !isHourPerDay(txtObj.value))){
			this.specialAlert(this.data_txt + " " + getInnerText(label) + " " + this.incorrect_txt, txtObj);
                        errFlag = true;
                }
                
                if (errFlag)
                        this.setLabelRed(label);
                else
                        this.setLabelBlack(label);
                        
                return !errFlag;
        }
        
        function checkFileType(fileObj, label){
				var fName = fileObj.value;
                var valid =false;
                var pointPos = fName.lastIndexOf(".");
	
                if (fName=="") {
                        return ;
				}else if (fName!="" && pointPos!=-1) 	{
					var fType = fName.substr(pointPos+1, 3).toLowerCase();
					for(var ct=0; ct<this.valid_filetype.length; ct++)
							if (this.valid_filetype[ct]==fType){
									valid = true;
									break;
							}
				}

                if (valid){
                        this.setLabelBlack(label);
                        return true;
                }else{
                        this.specialAlert(this.data_txt + " " + getInnerText(label) + " " + this.incorrect_txt, fileObj);
                        this.setLabelRed(label);
                        return false;
                }
       }



        function checkSelect(selectObj, label){
                if (selectObj.selectedIndex==-1 || selectObj.options[selectObj.selectedIndex].text=="---select---" || selectObj.value=="-1"){
                        this.raiseError(getInnerText(label), selectObj);
                        this.setLabelRed(label);
                }else {
                        this.setLabelBlack(label);
                }
        }

        function checkRadioOrChkbox(arrObj, label){
                var choose = false;
                for (var jj=0; jj<arrObj.length; jj++)
                        if (arrObj[jj].checked)
                                choose = true;

                if (!choose){
                        this.raiseError(getInnerText(label), arrObj[0]);
                        this.setLabelRed(label);
                }
                else {
                        this.setLabelBlack(label);
                }
        }
        
        function checkDate(dObj, mObj, yObj, label){
                var y = yObj.value;
                var m = mObj.selectedIndex+1;
                var d = dObj.value;
                var errFlag = false;
                
                if (yObj.value.match(/^(24|25)/))
                        y = yObj.value-543;

                if ((m==4 || m==6 || m==9 || m==11) && d > 30){//check day in range of each month
                        errFlag = true;///
                }

                if (!errFlag && m==2){//check leap year
                        if (y%4>0 && d>28){//Feb of "+y+" has only 28 days
                                errFlag = true;
                        }else if (d>29){ //Feb of "+y+ " has only 29 days
                                errFlag = true;
                        }
                }
                
                if (errFlag){
                        this.specialAlert(this.data_txt + " " + getInnerText(label)+ " " + this.incorrect_txt, dObj);
                        this.setLabelRed(label);
                }
                else
                        this.setLabelBlack(label);
        }
}





function FormEnValidator(formobj, fieldRequired){
        this.inheritFrom = FormValidator;
        this.inheritFrom(formobj, fieldRequired);
        
        this.preface_err = "Please complete the following field:";
        this.data_txt = "Invalid ";
        this.incorrect_txt = "";
		this.specialLabel = "We reserve our rights to offer privileges and process the inquiry with complete information only.";
}











/***************************************************************************************************************/
/***** Job *****/
function JobValidator(){
        var form = document.frmJob;
        var fieldRequired = Array("position", "salary", "attch", "name", "age", "gender", "addr", "tel", "email", "tabEdu", "langAbility", "tabWork");

        this.inheritFrom = FormValidator;
        this.inheritFrom(form, fieldRequired);
        this.eduChk = eduChk;
        this.workChk = workChk;

        function eduChk(){//chk for education
                for (var j=1; j<3; j++){
                        this.checkSelect(eval("form.eduLev" + j), document.getElementById("lbleduLev"));
                        this.checkTxt(eval("form.eduName" + j), document.getElementById("lbleduName"));
                        this.checkTxt(eval("form.eduDegree" + j), document.getElementById("lbleduDegree"));
                        this.checkTxt(eval("form.eduMajor" + j), document.getElementById("lbleduMajor"));
                        this.checkTxt(eval("form.eduGpa" + j), document.getElementById("lbleduGpa"));
                        this.checkTxt(eval("form.eduStartYear" + j), document.getElementById("lbleduStartYear"));
                        this.checkTxt(eval("form.eduEndYear" + j), document.getElementById("lbleduEndYear"));
                }
        }
        
        function workChk(){//chk for work history
                for (var i=1; i<3; i++)
                        if (    !(eval("this.isEmpty(form.wkPeriod" + i +".value)") &&
                                eval("this.isEmpty(form.wkName" + i + ".value)") &&
                                eval("this.isEmpty(form.wkPosition" + i + ".value)"))
                           ){
                                eval("this.checkTxt(form.wkPeriod"+ i +", document.getElementById(\"lblwkPeriod"+ i +"\"))");
                                eval("this.checkTxt(form.wkName"+ i +", document.getElementById(\"lblwkName"+ i +"\"))");
                                eval("this.checkTxt(form.wkPosition"+ i +", document.getElementById(\"lblwkPosition"+ i +"\"))");
                        }
        }
}


/***** Insurance *****/
function InsuranceValidator(){
        var form = document.frmInsure;
//	var fieldRequired = Array("custType", "tabPersonal", "tabComp", "addr", "tel", "email", "model", "tabSn", "buyDateX", "buyFrom", "attch");
	var fieldRequired = Array("custType", "tabPersonal", "tabComp", "addr", "tel", "email", "model", "tabSn", "buyDateX", "buyFrom");

        this.inheritFrom = FormValidator;
        this.inheritFrom(form, fieldRequired);
        this.personalChk = personalChk;
        this.compChk = compChk;
        this.buyDateXChk = buyDateXChk;
	this.snChk = snChk;

        function personalChk(){//chk for custType= personal
                if (form.custType[0].checked)
                        this.checkTxt(form.buyer, document.getElementById("lblbuyer"));
                this.checkDate(form.birth_d, form.birth_m, form.birth_y, document.getElementById("lblbirthDate"));
        }

        function compChk(){//chk for custType= company
                if (form.custType[1].checked){
                        this.checkTxt(form.contactx, document.getElementById("lblcontactx"));
                        this.checkTxt(form.company, document.getElementById("lblcompany"));
                }
        }
        
        function buyDateXChk(){
                this.checkDate(form.buy_d, form.buy_m, form.buy_y, document.getElementById("lblbuyDate"));
        }

	function snChk(){
		var snLabel = document.getElementById("lblsn");
		this.checkTxt(form.sn0, snLabel);
		this.checkTxt(form.sn1, snLabel);
		this.checkTxt(form.sn2, snLabel);
		this.checkTxt(form.sn3, snLabel);
		this.checkTxt(form.sn4, snLabel);
		this.checkTxt(form.sn6, snLabel);
		this.checkTxt(form.sn8, snLabel);
		this.checkTxt(form.sn9, snLabel);
		this.checkTxt(form.sn10, snLabel);
		this.checkTxt(form.sn11, snLabel);
		this.checkTxt(form.sn13, snLabel);
		this.checkTxt(form.sn14, snLabel);
		this.checkTxt(form.sn15, snLabel);
	}
}

function InsureItemValidator(){
        var form = document.frmItem;
        var fieldRequired = Array("elecType", "others", "brand", "model", "serial", "description");

        this.inheritFrom = FormValidator;
        this.inheritFrom(form, fieldRequired);
        this.otherTypeChk = otherTypeChk;
        this.descriptionChk = descriptionChk;
        
        function otherTypeChk(){
                if (form.elecType[form.elecType.length-1].checked)
                        this.checkTxt(form.txtOthers, document.getElementById("lblelecType"));
        }
        
        function descriptionChk(){
                if (form.elecType[1].checked)//monitor
                        this.checkTxt(form.desc, document.getElementById("lbldescription"));
        }
}


/***** Quotation *****/
function QuotationValidator(){
        var form = document.frmQuo;
        var fieldRequired = Array("oldCust", "name", "company", "position", "email", "addr", "province", "tel", "tel_office", "tel_mobile", "fax", "sendBy", "tabReqItems");

        this.inheritFrom = FormValidator;
        this.inheritFrom(form, fieldRequired);
        this.reqItemChk = reqItemChk;
        
        function reqItemChk(){
                var table = document.getElementById("tabReqItems");
                if (table.rows.length == 2)
                          this.specialAlert(_QUO_TH_ITEM_ERR, table);
                
                for (var x=1; x<table.rows.length-1; x++)
                        this.checkTxt(table.rows[x].cells[3].getElementsByTagName('input')[0], document.getElementById("lblQty"));
        }
}

function QuotationEnValidator(){
        var form = document.frmQuo;
        var fieldRequired = Array("oldCust", "name", "company", "position", "email", "addr", "country", "tel", "tel_office", "tel_mobile", "fax", "tabReqItems");

        this.inheritFrom = FormEnValidator;
        this.inheritFrom(form, fieldRequired);
        this.reqItemChk = reqItemChk;

        function reqItemChk(){
                var table = document.getElementById("tabReqItems");
                if (table.rows.length == 2)
                        this.specialAlert("Please choose the product item(s)", table);

                for (var x=1; x<table.rows.length-1; x++)
                        this.checkTxt(table.rows[x].cells[3].getElementsByTagName('input')[0], document.getElementById("lblQty"));
        }
}

function QuotationJpValidator(){
		/*use this line  in form
			 		QuotationJpValidator.prototype = new QuotationEnValidator;		
		   instead of  these lines in this position (because it's error)
		           this.inheritFrom = QuotationEnValidator;
			        this.inheritFrom(form, fieldRequired);
		*/
}

function QuotationGrp2Validator() {
   var form = document.frmQuo;
   var fieldRequired = Array("temp", "GAHP_hotQty", "GAHP_readyLevel", "GAHP_hotSystem");

   this.inheritFrom = FormValidator;
   this.inheritFrom(form, fieldRequired);
   this.tempChk = tempChk;
   this.gahpChk = gahpChk;

   function tempChk(){
      var arr = Array("GAHP_tempPrefer", "GAHP_tempInput", "GAHP_tempReturn");
      var err;
    
      for (var i=0; i<arr.length; i++) {
         var obj1 = this.formobj.elements[arr[i] + "0"];
         var obj2 = this.formobj.elements[arr[i] + "1"];
         var obj3 = this.formobj.elements[arr[i] + "2"];
         var label = document.getElementById("lbl" + arr[i]);
            
         err = !(obj1.checked || obj2.checked || obj3.checked); 

         if (err){
            this.raiseError(getInnerText(label), obj1);
            this.setLabelRed(label);
         }
         else
            this.setLabelBlack(label);
      }
      
      if (document.getElementById("GAHP_hotSystem").value > 0) //must check enoughSystem when have hot system > 0 
         this.fieldRequired.push("GAHP_enoughSystem");
      this.fieldRequired.push("tabGahp");      
   }
  
   function gahpChk(){
      var maxIndex = document.getElementById("GAHP_hotSystem").value;
      var arr = Array("_sysType", "_sysTypeOth", "_sysSys", "_sysSysTankTxt", "_sysSize", "_sysQty", "_sysFuel", "_sysFuelOth", "_sysYear");
       
      for (var i=1; i<=maxIndex; i++) {
         var name = "GAHP" + i;
         for (var j=0; j<arr.length; j++) {
               
            var objName = name + arr[j];
            var obj = this.formobj.elements[objName];
            var objLbl;
        
            if ((j == 1) || (j == 3) || (j == 7)) {
               objLbl = "lbl" + name + arr[j-1];
               var prevObj = this.formobj.elements[name + arr[j-1]];  
                             
               switch (j) {
                  case 1 :
                  case 7 :
                     if (prevObj[prevObj.length-1].checked) 
                        this.check(obj, document.getElementById(objLbl));
                     break; 
                  case 3 :  
                     if (prevObj[0].checked)
                        this.check(obj, document.getElementById(objLbl));                      
                     break;
                  default :
               } 
            }
            else {
               objLbl = "lbl" + name + arr[j];
               this.check(obj, document.getElementById(objLbl));
            }
         }     
      }
   }  
}


function QuotationGrp2EnValidator() {
   var form = document.frmQuo;
   var fieldRequired = Array("temp", "GAHP_hotQty", "GAHP_readyLevel", "GAHP_hotSystem");

   this.inheritFrom = FormEnValidator;
   this.inheritFrom(form, fieldRequired);
   this.tempChk = tempChk;
   this.gahpChk = gahpChk;

   function tempChk(){
      var arr = Array("GAHP_tempPrefer", "GAHP_tempInput", "GAHP_tempReturn");
      var err;
    
      for (var i=0; i<arr.length; i++) {
         var obj1 = this.formobj.elements[arr[i] + "0"];
         var obj2 = this.formobj.elements[arr[i] + "1"];
         var obj3 = this.formobj.elements[arr[i] + "2"];
         var label = document.getElementById("lbl" + arr[i]);
            
         err = !(obj1.checked || obj2.checked || obj3.checked); 

         if (err){
            this.raiseError(getInnerText(label), obj1);
            this.setLabelRed(label);
         }
         else
            this.setLabelBlack(label);
      }      
      
      if (document.getElementById("GAHP_hotSystem").value > 0) //must check enoughSystem when have hot system > 0 
         this.fieldRequired.push("GAHP_enoughSystem");
      this.fieldRequired.push("tabGahp");
   }
  
   function gahpChk(){
      var maxIndex = document.getElementById("GAHP_hotSystem").value;
      var arr = Array("_sysType", "_sysTypeOth", "_sysSys", "_sysSysTankTxt", "_sysSize", "_sysQty", "_sysFuel", "_sysFuelOth", "_sysYear");
       
      for (var i=1; i<=maxIndex; i++) {
         var name = "GAHP" + i;
         for (var j=0; j<arr.length; j++) {
               
            var objName = name + arr[j];
            var obj = this.formobj.elements[objName];
            var objLbl;
        
            if ((j == 1) || (j == 3) || (j == 7)) {
               objLbl = "lbl" + name + arr[j-1];
               var prevObj = this.formobj.elements[name + arr[j-1]];  
                             
               switch (j) {
                  case 1 :
                  case 7 :
                     if (prevObj[prevObj.length-1].checked) 
                        this.check(obj, document.getElementById(objLbl));
                     break; 
                  case 3 :  
                     if (prevObj[0].checked)
                        this.check(obj, document.getElementById(objLbl));                      
                     break;
                  default :
               } 
            }
            else {
               objLbl = "lbl" + name + arr[j];
               this.check(obj, document.getElementById(objLbl));
            }
         }     
      }
   }  
}


function QuotationGrp2JpValidator(){
		/*use this line  in form
			 		QuotationJpValidator.prototype = new QuotationEnValidator;		
		   instead of  these lines in this position (because it's error)
		           this.inheritFrom = QuotationEnValidator;
			        this.inheritFrom(form, fieldRequired);
		*/
}







function QuotItemValidator(){
   var form = document.frmReqItem;
	var fieldRequired = Array("type", "model");
		        
   this.inheritFrom = FormValidator;
   this.inheritFrom(form, fieldRequired);
   this.setGahp = setGahp;

   function setGahp(flag){
  	   if (flag == false){
	 	   this.fieldRequired.push("useWith");
      }
   }
}

function QuotItemEnValidator(){
   var form = document.frmReqItem;
   var fieldRequired = Array("type", "model");

   this.inheritFrom = FormEnValidator;
   this.inheritFrom(form, fieldRequired);
   this.setGahp = setGahp;

	function setGahp(flag){
  	   if (flag == false){
  	      this.fieldRequired.push("useWith");
      }
	}
}

function QuotItemJpValidator(){
		/*use this line  in form
			 		QuotItemJpValidator.prototype = new QuotItemEnValidator;		
		   instead of  these lines in this position 
		           this.inheritFrom = QuotItemEnValidator;
			        this.inheritFrom(form, fieldRequired);
		*/
}



/***** PO *****/
function PoForm1Validator(){
        var form = document.frmPo;
//        var fieldRequired = Array("oldCust", "name", "company", "tabNewCust");
		var fieldRequired = Array("oldCust", "name", "company", "position", "email", "addr", "tel", "tel_office", "tel_mobile", "fax", "deliverPlace", "province", "receiver", "taxAddr");

        this.inheritFrom = FormValidator;
        this.inheritFrom(form, fieldRequired);
//        this.newCustChk = newCustChk;

/*        function newCustChk(){//chk for new customer
                if (form.oldCust[1].checked){
                        this.checkTxt(form.position, document.getElementById("lblposition"));
                        this.checkTxt(form.email, document.getElementById("lblemail"));
                        this.checkTxt(form.tel, document.getElementById("lbltel"));
                        this.checkTxt(form.deliverPlace, document.getElementById("lbldeliverPlace"));
                        this.checkTxt(form.receiver, document.getElementById("lblreceiver"));
                        this.checkTxt(form.taxAddr, document.getElementById("lbltaxAddr"));
                }
        }
*/
}

function PoForm1EnValidator(){
        var form = document.frmPo;
        var fieldRequired = Array("oldCust", "name", "company", "position", "email", "addr", "tel", "tel_office", "tel_mobile", "fax", "deliverPlace", "country", "receiver", "taxAddr");

        this.inheritFrom = FormEnValidator;
        this.inheritFrom(form, fieldRequired);
}

function PoForm1JpValidator(){
		/*use this line  in form
			 		PoForm1JpValidator.prototype = new PoForm1EnValidator;		
		   instead of  these lines in this position 
		           this.inheritFrom = PoForm1EnValidator;
			        this.inheritFrom(form, fieldRequired);
		*/
}		

function PoForm2Validator(){
        var form = document.frmPo;
        var fieldRequired = Array("deliverCond", "quotationNo", "tabPoItems");
        
        this.inheritFrom = FormValidator;
        this.inheritFrom(form, fieldRequired);
        this.poItemChk = poItemChk;
        
        function poItemChk(){
                var table = document.getElementById("tabPoItems");
                if (table.rows.length == 2)
                        this.specialAlert(_PO_TH_ITEM_ERR, table);

                for (var x=1; x<table.rows.length-1; x++)
                        this.checkTxt(table.rows[x].cells[3].getElementsByTagName('input')[0], document.getElementById("lblQty"));
        }
}

function PoForm2EnValidator(){
        var form = document.frmPo;
        var fieldRequired = Array("deliverCond", "quotationNo", "tabPoItems", "deliverMethod");

        this.inheritFrom = FormEnValidator;
        this.inheritFrom(form, fieldRequired);
        this.poItemChk = poItemChk;

        function poItemChk(){
                var table = document.getElementById("tabPoItems");
                if (table.rows.length == 2)
                        this.specialAlert("Please choose the product item(s)", table);

                for (var x=1; x<table.rows.length-1; x++)
                        this.checkTxt(table.rows[x].cells[3].getElementsByTagName('input')[0], document.getElementById("lblQty"));
        }
}

function PoForm2JpValidator(){
		/*use this line  in form
					PoForm2JpValidator.prototype = new PoForm2EnValidator;
		   instead of  these lines in this position 
		           this.inheritFrom = PoForm2EnValidator;
			        this.inheritFrom(form, fieldRequired);
		*/
}

function PoItemValidator(){
        var form = document.frmReqItem;
        var fieldRequired = Array("type", "model");

        this.inheritFrom = FormValidator;
        this.inheritFrom(form, fieldRequired);
}

function PoItemEnValidator(){
        var form = document.frmReqItem;
        var fieldRequired = Array("type", "model");

        this.inheritFrom = FormEnValidator;
        this.inheritFrom(form, fieldRequired);
}

function PoItemJpValidator(){
		/*use this line  in form
			 		PoItemJpValidator.prototype = new PoItemEnValidator;		
		   instead of  these lines in this position 
		           this.inheritFrom = PoItemEnValidator;
			        this.inheritFrom(form, fieldRequired);
		*/
}

function PoForm3Validator(){
        var form = document.frmPo;
        var fieldRequired = Array("payType", "tabBank", "deliverId", "others");
        
        this.inheritFrom = FormValidator;
        this.inheritFrom(form, fieldRequired);
        this.bankChk = bankChk;
        this.otherTypeChk = otherTypeChk;
        
        function bankChk(){//for transfer money
                if (form.payType[1].checked)
                        this.checkRadioOrChkbox(form.bankId, document.getElementById("lblbank"));
        }
        
        function otherTypeChk(){
                if (form.deliverId[form.deliverId.length-1].checked)
                        this.checkTxt(form.deliverOth, document.getElementById("lbldeliverId"));
        }
}



/***** Asking for serial number *****/
function AskSnValidator(){
        var form = document.frmAskSn;
        var fieldRequired = Array("name", "addr", "tel", "email", "seller", "Type", "Model", "issueNo");

        this.inheritFrom = FormValidator;
        this.inheritFrom(form, fieldRequired);
}


/***** Asking for service *****/
function AskServiceValidator(){
        var form = document.frmAskService;
        var fieldRequired = Array("name", "addr", "tel", "email", "seller", "Type", "Model", "issueNo", "question");

        this.inheritFrom = FormValidator;
        this.inheritFrom(form, fieldRequired);
}


/***** Contact us *****/
function ContactValidator(){
        var form = document.frmContact;
        var fieldRequired = Array("aimType", "others", "name", "email", "addr", "province", "title", "msg");

        this.inheritFrom = FormValidator;
        this.inheritFrom(form, fieldRequired);
        this.otherTypeChk = otherTypeChk;
        
        function otherTypeChk(){
                if (form.aimType[form.aimType.length-1].checked)
                        this.checkTxt(form.othAim, document.getElementById("lblaimType"));
        }
}

function ContactEnValidator(){
        var form = document.frmContact;
        var fieldRequired = Array("aimType", "others", "name", "email", "addr", "country", "title", "msg");

        this.inheritFrom = FormEnValidator;
        this.inheritFrom(form, fieldRequired);
        this.otherTypeChk = otherTypeChk;

        function otherTypeChk(){
                if (form.aimType[form.aimType.length-1].checked)
                        this.checkTxt(form.othAim, document.getElementById("lblaimType"));
        }
}

function ContactJpValidator(){ 
		var form = document.frmContact;
		var fieldRequired = Array("name", "email", "addr", "country", "title", "msg");

        this.inheritFrom = FormEnValidator;
        this.inheritFrom(form, fieldRequired);
}

/***** Dealer *****/
function DealerForm1Validator(){
        var form = document.frmDealer;
        var fieldRequired = Array("name", "compName", "position", "email", "tel", "tel_mobile", "fax", "addr", "province");
        
        this.inheritFrom = FormValidator;
        this.inheritFrom(form, fieldRequired);
}


function DealerForm1EnValidator(){
        var form = document.frmDealer;
        var fieldRequired = Array("name", "compName", "position", "email", "tel", "tel_mobile", "fax", "addr", "country");

        this.inheritFrom = FormEnValidator;
        this.inheritFrom(form, fieldRequired);
}

function DealerForm1JpValidator(){
		/*use this line  in form
					DealerForm1JpValidator.prototype = new DealerForm1EnValidator;
		   instead of  these lines in this position 
		           this.inheritFrom = DealerForm1EnValidator;
			        this.inheritFrom(form, fieldRequired);
		*/
}


function DealerForm2Validator(){
        var form = document.frmDealer;
        var fieldRequired = Array("compAddr", "busnType", "history", "prevIncome", "empNo", "year", "decisionName", "purchaser");
        
        this.inheritFrom = FormValidator;
        this.inheritFrom(form, fieldRequired);
        this.chkValidInitYear = chkValidInitYear;
        
        function chkValidInitYear(){
                var err = false;
                var label = document.getElementById("lblinitYear");
                var obj = form.initYear;
                
                if (!this.checkTxt(obj, label) || obj.value.length!=4)//check number and 4 digit
                        err = true;

                //check for <= this year
                if (!err){
                        var yearNow = new Date().getFullYear(); //getYear() give different result between ie and ff
                        if (obj.value.charAt(0)=="1"  && parseInt(obj.value)>yearNow)
                                err = true;
                        else if (obj.value.charAt(0)=="2" && parseInt(obj.value)>yearNow + 543)//note->in thai lang web, must check for first digit=1,2 but in eng web check only 1.
                                err = true;
                }
                
                if (err){
                        this.specialAlert(this.data_txt + " " + getInnerText(label) + " " + this.incorrect_txt, obj);
                        this.setLabelRed(label);
                }
                else
                        this.setLabelBlack(label);
        }
}

function DealerForm2EnValidator(){
        var form = document.frmDealer;
        var fieldRequired = Array("compRegName", "compRegAddr", "busnType", "history", "prevIncome", "empNo", "year", "authorizedName", "purchaser");

        this.inheritFrom = FormEnValidator;
        this.inheritFrom(form, fieldRequired);
        this.chkValidInitYear = chkValidInitYear;

        function chkValidInitYear(){
                var err = false;
                var label = document.getElementById("lblinitYear");
                var obj = form.initYear;

                if (!this.checkTxt(obj, label) || obj.value.length!=4)//check number and 4 digit
                        err = true;

                //check for <= this year
                if (!err){
                        var yearNow = new Date().getFullYear(); //getYear() give different result between ie and ff
                        if (obj.value.charAt(0)=="1"  && parseInt(obj.value)>yearNow)
                                err = true;
                        else if (obj.value.charAt(0)=="2" && parseInt(obj.value)>yearNow)
                                err = true;
                }

                if (err){
                        this.specialAlert(this.data_txt + " " + getInnerText(label) + " " + this.incorrect_txt, obj);
                        this.setLabelRed(label);
                }
                else
                        this.setLabelBlack(label);
        }
}

function DealerForm2JpValidator(){
		/*use this line  in form
					DealerForm2JpValidator.prototype = new DealerForm2EnValidator;
		   instead of  these lines in this position 
		           this.inheritFrom = DealerForm2EnValidator;
			        this.inheritFrom(form, fieldRequired);
		*/
}


function DealerForm3Validator(){
        var form = document.frmDealer;
        var fieldRequired = Array("isDealer", "beDealer", "marketArea", "predictAmt", "reasonApply", "partType[]", "deliverId", "others", "pleasedTrain", "noPleased", "showPartPlace");

        this.inheritFrom = FormValidator;
        this.inheritFrom(form, fieldRequired);
        this.beDealerChk = beDealerChk;
        this.otherTypeChk = otherTypeChk;
        this.noPleasedChk = noPleasedChk;
        
        function beDealerChk(){
                if (form.isDealer[1].checked)
                        this.checkTxt(form.dealerDesc, document.getElementById("lblisDealer"));
        }
        
        function otherTypeChk(){
                if (form.deliverId[form.deliverId.length-1].checked)
                        this.checkTxt(form.deliverOth, document.getElementById("lbldeliverId"));
        }
        
        function noPleasedChk(){
                if (form.pleasedTrain[1].checked)
                        this.checkTxt(form.unhappyDesc, document.getElementById("lblpleasedTrain"));
        }
}

function DealerForm3EnValidator(){
        var form = document.frmDealer;
        var fieldRequired = Array("isDealer", "beDealer", "territory", "custDesc", "predictRevenue", "reasonApply", "partType[]", "deliverId", "others", "pleasedTrain", "noPleased", "showPartPlace");

        this.inheritFrom = FormEnValidator;
        this.inheritFrom(form, fieldRequired);
        this.beDealerChk = beDealerChk;
        this.otherTypeChk = otherTypeChk;
        this.noPleasedChk = noPleasedChk;

        function beDealerChk(){
                if (form.isDealer[1].checked)
                        this.checkTxt(form.dealerDesc, document.getElementById("lblisDealer"));
        }

        function otherTypeChk(){
                if (form.deliverId[form.deliverId.length-1].checked)
                        this.checkTxt(form.deliverOth, document.getElementById("lbldeliverId"));
        }

        function noPleasedChk(){
                if (form.pleasedTrain[1].checked)
                        this.checkTxt(form.unhappyDesc, document.getElementById("lblpleasedTrain"));
        }
}

function DealerForm3JpValidator(){
		/*use this line  in form
					DealerForm3JpValidator.prototype = new DealerForm3EnValidator;
		   instead of  these lines in this position 
		           this.inheritFrom = DealerForm3EnValidator;
			        this.inheritFrom(form, fieldRequired);
		*/
}

/******** Policy (kung) *********/
function PolicyValidator(){
	var form = document.frmPolicy;
	var fieldRequired = Array("email", "serial");

	this.inheritFrom = FormValidator;
    this.inheritFrom(form, fieldRequired);
  	this.snChk = snChk;

	function snChk(){
		var snLabel = document.getElementById("lblserial");
		this.checkTxt(form, snLabel);
	}
}
