var use_msg_th = "th";
var use_msg_en = "en";
var use_lang_msg = use_msg_en;
var msg_required = "";
var errors_required_th = "กรุณาระุบุุ";
var errors_required_en = " is required. \n";
var confirm_add_th ="ต้องการบันทึกข้อมูล";
var confirm_add_en = "Do you want to insert data?";
var confirm_update_th = "ต้องการแก้ไขข้อมูล";
var confirm_update_en = "Do you want to update data?";
var confirm_delete_th = "ต้องการลบข้อมูล";
var confirm_delete_en = "Do you want to delete data?";
var confirm_cancel_th = "ต้องการยกเลิกการทำรายการ";
var confirm_cancel_en = "Do you want to cancel?";
var confirm_choose_record_th = "กรุณาระบุข้อมูลที่ต้องการ";
var confirm_choose_record_en = "Please choose record.";

var warning_can_not_add_th = "คุณไม่สามารถทำการเพิ่มข้อมูลได้";
var warning_can_not_update_th = "คุณไม่สามารถทำการแก้ไขข้อมูลได้";
var warning_can_not_delete_th = "คุณไม่สามารถทำการลบข้อมูลได้";
var warning_can_not_export_th = "คุณไม่สามารถทำการ Export ข้อมูลได้";

function validForm(theArrayObj , theArrayMsg , theNotAlertMsg){
	msg_alert = "";
	setThis = null;
	
	if(theArrayObj.length > 0){
		for(icount=0; icount<theArrayObj.length; icount++){
			if(!checkWithMsg(theArrayObj[icount])){
				
				if(use_lang_msg == use_msg_en){
					msg_alert += theArrayMsg[icount] + errors_required_en;
				}else{
					msg_alert += errors_required_th + theArrayMsg[icount] + " \n";
				}
			
				if(setThis == null) 
					setThis = theArrayObj[icount];
					
			}
		}
		
	} else {
		
		if(!checkWithMsg(theArrayObj)){
		
			if(use_lang_msg == use_msg_en){
				msg_alert += theArrayMsg + errors_required_en;
			}else{
				msg_alert += errors_required_th + theArrayMsg + " \n";
			}
			
			if(theArrayObj.type == "text" || theArrayObj.type == "textarea" || theArrayObj.type == "hidden")
				theArrayObj.value = "";
			
			if(theArrayObj.type != "hidden")
				theArrayObj.focus();
			
		}
	}
	
	if(msg_alert != ""){
		
		if(theNotAlertMsg != null && theNotAlertMsg){
			// nothing
		} else {
			alert(msg_alert);
		}

		if(setThis != null){
			if(setThis.length > 0) {
				if(setThis[0].type == "radio")
					setThis[0].focus();
			} else {
				if(setThis.type == "text" || setThis.type == "textarea" || setThis.type == "hidden")
					setThis.value = "";

				if(setThis.type != "hidden")
					setThis.focus();
			}
		} 
	
		return false;
	} 
	
	return true;
}


function checkWithMsg(theObjForm){
	
	if(theObjForm == null)
		return false;
	
	if(theObjForm.length > 0){
		// radio
		if(theObjForm[0].type == "radio")
			return checkRadio(theObjForm);
		
		// checkbox
		if(theObjForm[0].type == "checkbox")
			return checkBoxArray(theObjForm);
	
	} 
		// text box
		if(theObjForm.type == "text" || theObjForm.type == "textarea" ||  theObjForm.type == "hidden"){
			if(trimString(theObjForm.value) == "")
				return false;
			
			return true;
		}
		
		// select box
		else if(theObjForm.type == "select-one" || theObjForm.type == "select-multiple"){
			if(trimString(theObjForm.value) == "")
				return false;
			
			return true;
		
		}
		
		// one radio
		else if(theObjForm.type == "radio"){
			return checkRadio(theObjForm);
		}
		
		// one check box
		else if(theObjForm.type == "checkbox"){
			return checkBoxArray(theObjForm);
		}
	
	return true;
	
}

function calculate_age(theDateStart , theDateEnd){
	age = 0;
	if(theDateStart == "" || theDateEnd == "")
		return "";
	
	arr_from = theDateStart.value.split("/");
	arr_to = theDateEnd.value.split("/");

	if(arr_from == null || arr_from.length < 3 || arr_from[2].length < 4)
		return "";
	if(arr_to == null || arr_to.length < 3 || arr_to[2].length < 4)
		return "";

	year_from = getInt(arr_from[2]);
	year_to = getInt(arr_to[2]);
	
	age = year_to - year_from;
	
	if(age < 0)
		return "";

	return age;
}

function isNumeric(theEvent,theValueObj){  
  if(theEvent.keyCode==46){
    posPoint=theValueObj.indexOf(".");
    if(posPoint>-1) 
      theEvent.keyCode=0;
  }else{
    if(theEvent.keyCode<48||theEvent.keyCode>57){
       theEvent.keyCode=0;
     }else{
        posPoint=theValueObj.indexOf(".");
    	if(posPoint>-1){ 
    	  len=theValueObj.substr(posPoint+1,theValueObj.length).length;
    	  if(len==3)
      			theEvent.keyCode=0;
      	}

     }
  }
}

function isInteger(theEvent){
	 if(theEvent.keyCode<48||theEvent.keyCode>57){
       theEvent.keyCode=0;
     }
}

function isLetters(theEvent,theValueObj){	
	if(theEvent.keyCode >=48 && theEvent.keyCode <=57 ){
		theEvent.keyCode = 0;
	}
}

// greaterThanDec("99.999" , 10 , 8) = true
// greaterThanDec("100" , 10 , 8) = false
function greaterThanDec(theValue , thePrecision , theScale){
	result = false;
	theValue = getFloat(outComma(theValue));
	MaxDecimal = Math.pow(10 , thePrecision - theScale) - Math.pow(10 , - theScale);

	if(theValue > MaxDecimal)
		result = true;
		
	return result;
}

function checkMaxLength(theObj,theMaxValue){		
		var lengthValue = trimString(theObj.value).length;		
		if(lengthValue > 0){		
			var maxValue=  parseInt(theMaxValue);		
			if(lengthValue > maxValue){		
				alert(" The information can not be greater than "+ maxValue + " characters.");			
				var strValue = theObj.value.substring(0,(maxValue-1));			
				theObj.value =strValue;
				theObj.focus();
			}
		}else{
			theObj.value="";
			theObj.focus();
		}	
}
function checkMinLength(theObj,theMinValue){		
		var lengthValue = trimString(theObj.value).length;	
		if(lengthValue >0){
			var minValue=  parseInt(theMinValue);		
			if(lengthValue < minValue){			
				alert(" The information can not be less than "+ minValue + " characters.");	
				theObj.value = trimString(theObj.value);
			}	
		}else{
			theObj.value='';
			theObj.focus();
		}
}
function trimString(sInString) {
  sInString = sInString.replace( /^\s+/g, "" );// strip leading
  return sInString.replace( /\s+$/g, "" );// strip trailing
}
function outComma(theValueObj){
   realnumber = "";
   var valueObj = trimString(theValueObj);
   if(valueObj.length >0){
	   for(k=0;k<valueObj.length;k++){
	      if (valueObj.substr(k,1) != ","){
	         realnumber = realnumber + valueObj.substr(k,1);
	      }
	   }
   }
   return realnumber;
} 
function getFloat(theValueObj){	
		if(theValueObj == "" || theValueObj == "."){
		    return 0.00;
		}else{
			return parseFloat(theValueObj);
		}
}
function getInt(theValueObj){	
		if(theValueObj == ""){
		    return 0;
		}else{
			return parseInt(theValueObj);
		}
}

function checkRadio(ObjRadio){
	result = false;
	
	if(ObjRadio.length > 1){
		for(icount_radio = 0; icount_radio < ObjRadio.length; icount_radio++)	
			if(ObjRadio[icount_radio].checked)
				return true;
	} else {
		return ObjRadio.checked?true:false;
	}

	return result;
}

function commaSplit(theValueObj) {
theValueObj = outComma(theValueObj);
var txtNumber = '' + theValueObj;
/*
if (isNaN(txtNumber) || txtNumber == "") {
fieldName.select();
fieldName.focus();
}*/
/*else {*/
	var rxSplit = new RegExp('([0-9])([0-9][0-9][0-9][,.])');
	var arrNumber = txtNumber.split('.');
	arrNumber[0] += '.';
	do {
		arrNumber[0] = arrNumber[0].replace(rxSplit, '$1,$2');
	} while (rxSplit.test(arrNumber[0]));
	if (arrNumber.length > 1) {
		return arrNumber.join('');
	}else {
		return arrNumber[0].split('.')[0];
    }
   /*}*/
}
function getDot(theValueObj){
	if(getInt(theValueObj) > 0){
	idx =-1;
	idx = theValueObj.indexOf(".");
	if(idx == -1){
	theValueObj = theValueObj+".00";
	}else{
		afterdot = theValueObj.substring(idx,theValueObj.length);
		if(afterdot.length == 2)
			theValueObj = theValueObj + "0";
	}
	}
	return theValueObj;	
}
function keepPoint(theValueObj,theValuePoint){

	returnResult=theValueObj;
	if(returnResult !=""){
	if(getFloat(returnResult) > 0 ) {
	    theValueObj=theValueObj+"";
	    posPoint=theValueObj.indexOf(".");
	    if(posPoint>-1){ 
	    	len=theValueObj.substr(posPoint+1,theValueObj.length).length;
	    	if(len>theValuePoint){
	    		if (theValuePoint == 0) {
	    	  		returnResult=theValueObj.substr(0,posPoint);
				}else{
	      			tmp=theValueObj.substr(posPoint,theValueObj.length).substr(theValuePoint+1,1);
	      			if(eval(tmp)>4){
	      			   	dec = parseFloat(theValueObj.substr(posPoint+1,theValuePoint))+1;
	      			   	dec = dec + "";
	      			   	if (dec.length <= theValuePoint) {
	      			   		for (k = dec.length;k < theValuePoint;k++)
	      			   			dec = "0" + dec;
	      			   		returnResult=theValueObj.substr(0,posPoint+1) + dec;
	      			   	} else {      			   		
	      			   		returnResult=(parseFloat(theValueObj.substr(0,posPoint))+1) + "." + dec.substr(1,theValuePoint);
						}
					}else{
	      			   returnResult=theValueObj.substr(0,posPoint+theValuePoint+1);
					}
				}	
	    	} else {
	      		for(k=len;k<theValuePoint;k++) {
	      			theValueObj += "0";
	      		}
	      		returnResult=theValueObj;
	    	}
	   	} else {
			if (theValuePoint > 0) {
				theValueObj += ".";
				for(k=0;k<theValuePoint;k++) {
					theValueObj += "0";
				}
			}
			returnResult=theValueObj;
	   	}    
	    if (theValuePoint !=0 ) {
	    	posPoint=returnResult.indexOf(".");
	    	if(posPoint<0){ 
	    		returnResult += ".";
				for(k=0;k<theValuePoint;k++) {
					returnResult += "0";
				}
	    	}
	    }
    } 
    }
    	
    return returnResult;      
}
function upperCase(theEvent){
	keynum = theEvent.keyCode;
	if (keynum >= 97 && keynum <= 122) {
		theEvent.keyCode = keynum-32;
	}
}
function checkPercent(theObj){
	if(theObj.value !=""){
		theObj.value = parseFloat(theObj.value);	
		if(parseFloat(theObj.value)>100){
      		alert("Percent can not more than 100 %");      
      		theObj.focus();
      		return false;
    	}else{
    		return true;
    	}
    }
}
function compareGreater(theFirstValue,theSecondValue){
	if(eval(theFirstValue) > eval(theSecondValue) ){
		return true;
	}else{
		return false;
	}
}

function checkduplicate(theObjArray){
	Obj2 = theObjArray;
	result = false;
	
	if(theObjArray.length > 1){
		for(i = 0; i < theObjArray.length; i++){
		  if(trimString(theObjArray[i].value) != "") {
			for(k = i+1; k < Obj2.length; k++){
				if(theObjArray[i].value == Obj2[k].value){
					alert("duplicate record");
					theObjArray[k].focus();
					return result;
				}
			}
		  }
		}
	} else {
		result = true;
	}

	result = true;
	return result;
}

function checkNotGreaterThan(theValueObj, theMaximum){	
	tmp_str = new String(theMaximum);
	if(trimString(theValueObj.value) != ''){
		var value = outComma(theValueObj.value);	
		//theValueObj.value = parseFloat(value);
		if(parseFloat(value) > parseFloat(theMaximum)){		
			alert('The value must less than '+ tmp_str);
			theValueObj.focus();
			return false;
		}else{
			return true;
		}
	}
}

function checkNotLessThan(theValueObj, theMinimum){ 
	if(trimString(theValueObj.value) != ''){
		var value = outComma(theValueObj.value);
		//theValueObj.value = parseFloat(value);
		if(parseFloat(value) < eval(theMinimum)){		
			alert('The value must more than '+ theMinimum);
			theValueObj.focus();
			return false;
		}else{
			return true;
		}
	}
}

function confirmUpdate(){
	
	if(use_lang_msg == use_msg_en){
		
		if(confirm(confirm_update_en)){
			return true;
		} 
		
	}else{
		
		if(confirm(confirm_update_th)){
			return true;
		}
	
	}
	
	return false;
	
}


function confirmAdd(){
	
	if(use_lang_msg == use_msg_en){
		
		if(confirm(confirm_add_en)){
			return true;
		} 
		
	}else{
		
		if(confirm(confirm_add_th)){
			return true;
		}
	
	}
	
	return false;

}

function confirmDelete(){
	
	if(use_lang_msg == use_msg_en){
	
		if(confirm(confirm_delete_en)){
			return true;
		} 
		
	}else{
		
		if(confirm(confirm_delete_th)){
			return true;
		}

	}
	
	return false;

}

function confirmCancel(){
	
	if(use_lang_msg == use_msg_en){
	
		if(confirm(confirm_cancel_en)){
			return true;
		} 
		
	}else{
		
		if(confirm(confirm_cancel_th)){
			return true;
		}

	}
	
	return false;

}

function validStickerObj(theTypeObj , theYearObj , theNumberObj , theCheckDigitObj , theMsgAlert){

	if(theMsgAlert != null && theMsgAlert != ""){
		w_msgAlert = theMsgAlert;
		
	} else {
		w_msgAlert = "ค่า check digit ไม่ถูกต้อง";
	
	}

	w_valid = validSticker(theTypeObj.value , theYearObj.value , theNumberObj.value , theCheckDigitObj.value);
	
	if(!w_valid){
		alert(w_msgAlert);
		window.focus();
		theCheckDigitObj.focus();
		return false;

	} else {
		return true;
	}
}

// Ex. checkSticker("1" , "7" , "10004500" , "1");
// Ex. return true;
function validSticker(theType , theYear , theNumber , theCheckDigit){

	check_digit = getStickerCheckDigit(theType , theYear , theNumber);

	if(check_digit == getInt(theCheckDigit)){
		return true;
	} else {
		alert("true digit is " + check_digit);
		return false;
	}
}

// Ex. getStickerCheckDigit("1" , "7" , "10004500");
// Ex. return 1;
function getStickerCheckDigit(theType , theYear , theNumber){
	data_str = theType + theYear + theNumber;
	data_int = getInt(data_str);
	return data_int % 7;
}

function alertNoPermission(theMode){	

	if(theMode != null && theMode != ""){
		if(theMode == "add")
			alert(warning_can_not_add_th);
		
		else if(theMode == "update")
			alert(warning_can_not_update_th);
		
		else if(theMode == "delete")
			alert(warning_can_not_delete_th);
		
		else if(theMode == "export")
			alert(warning_can_not_export_th);
		
		else if(theMode == "special")
			alert(warning_can_not_export_th);
	}
}

//
// function String roundStandard(float theValue , String theRoundType)
//
function roundStandard(theValue , theRoundType){
/*
	U = ปัดขึ้น
	N = ปัดตามหลักสากล
	D = ปัดลง
	X = ไม่ปัด

	*** X , D is same case , process by Puangpetch-s
*/

	switch(theRoundType){
		case "U" : return Math.ceil(theValue);
		case "N" : return Math.round(theValue);
		case "D" : 
		case "X" : return Math.floor(theValue);
		default : return getFloat(theValue);
	}
}

//
// function float setScale(float theValue , int theScale , String theRoundType)
//
function setScale(theValue , theScale , theRoundType){

	w_str = new String(theValue);
	w_indexpoint = w_str.indexOf(".");
	w_strnumber = "";

	// step 1 : remove "."
	if(w_indexpoint > 0){
		for(i=0; i<w_str.length; i++)
			if(w_str.substr(i,1) != ".")
				w_strnumber += w_str.substr(i,1);

	} else {
		return theValue;
	}

	// step 2 : add "0"
	for(i=w_strnumber.length; i<theScale; i++)
		w_strnumber += "0";

	// step 3 : move "."
	w_tmp = w_strnumber.substr(0 , w_indexpoint + theScale);
	w_tmp+= ".";
	w_tmp+= w_strnumber.substr(w_indexpoint + theScale , w_strnumber.length - w_indexpoint + theScale);
	
	// step 4 : calculate roundStandard
	w_result = roundStandard(getFloat(w_tmp) , theRoundType);

	// step 5 : move "."
	w_resultStr = new String(w_result);
	w_result2 = "";
	for(i=0; i<w_resultStr.length; i++){
		w_tmp = w_resultStr.substr(i, 1);
		if(i == w_indexpoint)
			w_result2 += "."; 
		w_result2 += w_tmp;
	}

	return w_result2;
}

function checkEmail(e) {
	var ok = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";

	for(i=0; i < e.length ;i++){
		if(ok.indexOf(e.charAt(i))<0){
			return false;
		}
	}
	return true;
}
