//****************************************************************
// Sale Order Popup
// Customer Popup
var P_CUSTOMER_W    = 970;
var P_CUSTOMER_H    = 680;

// Customer Popup
var P_PRODUCT_W    = 820;
var P_PRODUCT_H    = 680;

//****************************************************************

var iCallID;
var arrProvinceTh = new Array();

function InitialServices()
{
	service.useService("../ScriptCallService/proxy.asmx?WSDL","proxy");
}

function ServiceFindByCode(sServiceName,sFindCode){
	if(service.proxy != null){
		
		iCallID = service.proxy.callService(myResults,sServiceName,convertToUnicode(sFindCode));
	}		
}

function convertToUnicode(source) 
{ 
	result = ''; 
	for (i=0; i<source.length; i++) 
		result += source.charCodeAt(i) + ";"; 
	return result; 
} 

// Comment : This function to show Constant Message From ConfigScript\Page Client Side
 //         : if pnPOS is Null or not send pnPos = 0
 // Para1 : ptMsG is Messagae ( format : XXX ; YYY)
 // Para2 : pnPos is Position to Split Message ( 0 is First , 1 is Second)
 // Return : This function is return Messgae by pnPOS 
 function Show_Message(ptMSG){
    var lang = getCookie("LanguageUse");
    var nPos = 0;
    var tMSG = "";
    
    switch(lang){
        case "th-TH":
            nPos = 1;
        break;
        case "en-US":
            nPos = 0;
        break;
        default:
            nPos = 0;
        break;
    }// end case
    
    tMSG = ptMSG.split('|')
    
    return tMSG[nPos];
}

// Comment  : This function to show message box in client side page
// Param1   : msg is a message from file ConfigMessage.js
// Param2   : pos is to select show message thai / english default is Eng : 0
// Return   : Message by language thai/eng
function GetMessage(msg,type,value1){
    var lang = getCookie("LanguageUse");
    var pos = (lang == "en-US") ? 0 : 1;
    var message = (value1) ? msg.replace("{0}",value1) : msg.replace("'{0}'","");
    
    switch(type){
        case "I": message = MSG_ErrorInput+" \""+message+"\" "+MSG_ErrorEnd; break;
        case "S": message = MSG_ErrorSelect+" \""+message+"\" "+MSG_ErrorEnd; break;
        default: message = message; break;
    }//end switch
    
    return message;
}

//For Format Number to Currency "#,##0.00"
function formatCurrency(num) 
{
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + num + '.' + cents);
}

// Show popup is dialog format
function popupDialog(url,args,w,h){
	/*if(flag == true){
	    var result = window.showModelessDialog(url,args,"center:yes;help:no;status:no;dialogWidth:"+w+"px;dialogHeight:"+h+"px");
	}else{
	    var result = window.showModalDialog(url,args,"center:yes;help:no;status:no;dialogWidth:"+w+"px;dialogHeight:"+h+"px");
	}/**/
	
	var result = window.showModalDialog(url,args,"center:yes;help:no;status:no;dialogWidth:"+w+"px;dialogHeight:"+h+"px");
	
	return result;
}
// Show popup is open Windows format
function popupReport(url,width,height){
	var topPosition = (screen.height) ? (screen.height-height)/2 : 0;
    var leftPosition = (screen.width) ? (screen.width-width)/2 : 0;

	var result = window.open(url,'MainReport','left='+leftPosition+',top='+topPosition+',resizable=no,scrollbars=no,width='+width+';,height='+height+';,toolbar=no',true);
	return result;
}

// Show popup in fullscreen
function popupFullScreen(url){
    window.open(url,"","fullscreen,scrollbars")
}

// Open popup windows / popup Dialog
function popupWindow(url,args,w,h,flag){
    var today = new Date();
    var runTime = today.getSeconds();
    var topPosition = (screen.height) ? (screen.height-h)/2 : 0;
    var leftPosition = (screen.width) ? (screen.width-w)/2 : 0;
    var settings = 'left='+leftPosition+',top='+topPosition+',resizable=no,scrollbars=no,width='+w+',height='+h+',toolbar=no';
    
    if(flag == true){
        var newUrl = (url.indexOf("?") == -1?url = url+"?flag=yes":url+"&flag=yes");
        var result = window.showModalDialog(url,args,"center:yes;help:no;status:no;dialogWidth:"+w+"px;dialogHeight:"+h+"px");
    }else{
        var result = window.open(url,runTime,settings,true);
    }    
    
    return result;
}

// Set Cookie
function setCookie(name,value,expires,path,domain,secure){
    expires = expires * 60*60*24*1000;
    var today = new Date();
    var expires_date = new Date( today.getTime() + (expires) );
    var cookieString = name + "=" +escape(value) + 
       ( (expires) ? ";expires=" + expires_date.toGMTString() : "") + 
       ( (path) ? ";path=" + path : "") + 
       ( (domain) ? ";domain=" + domain : "") + 
       ( (secure) ? ";secure" : ""); 
       
    document.cookie = cookieString; 
}

// Get Cookie
function getCookie(name){
	var start = document.cookie.indexOf(name+"="); 
    var len = start+name.length+1;
    
    if ((!start) && (name != document.cookie.substring(0,name.length))) return null; 
    if (start == -1) return null; 
    
    var end = document.cookie.indexOf(";",len); 
    if (end == -1) end = document.cookie.length; 
    
    return unescape(document.cookie.substring(len,end));
}

// Check is input numeric
function CheckIsNumeric(code){
    if((code < 47 || code > 58)&&(code < 96 || code > 105)
        &&(code != 8 && code != 46 && code != 110 && code != 9))
        return false;
    else
        return true;
}
/*---------------------*/
//Create By Yar,17/07/06
function splitValue(sValue)
{
    var sValue = sValue.split(';');
    return sValue[0];
}

// Check key is not numeric
function CheckFilterNumeric(){
    var re;
    var ch=String.fromCharCode(event.keyCode);
    if (event.keyCode<32){
        return;
    }
    if((event.keyCode<=57)&&(event.keyCode>=48)){
        if (!event.shiftKey){
            return;
        }
    }
    if ((ch=='-') ||(ch=='.')){
        return;
    }
    
    event.returnValue=false;
}

// Function Convert date to date format
// Date is dd/MM/yyyy
function SetDateFormat(sDate,sFormat){
    var lang = getCookie("LanguageUse");		    
    var ar_format = sFormat.split('/');
    var ar_date = null;
    var NewDate = "";		    
    var year = 0;
    
    if(sDate.indexOf("-") == -1){
        ar_date = sDate.split('/');
        NewDate = sFormat.replace("dd", ar_date[0]);
        NewDate = NewDate.replace("MM",ar_date[1]);
        year = parseInt(ar_date[2]);
    }else{
        ar_date = sDate.split('-');
        NewDate = sFormat.replace("dd", ar_date[2]);
        NewDate = NewDate.replace("MM",ar_date[1]);
        year = parseInt(ar_date[0]);
    }

    
    if(lang == "th-TH"){
        year = (year > 2500 ? year : (year + 543));
    }else{
        year = (year > 2500 ? (year-543) : year);
    }
    
    if(ar_format[2]=="yy"){
        NewDate = NewDate.replace("yy", String(year).substring(2));
    }else{
        NewDate = NewDate.replace("yyyy", year);
    }
    
    return NewDate;
}
		
// Check key is not numeric
function CheckFilterNumeric(){
    var re;
    var ch=String.fromCharCode(event.keyCode);
    if (event.keyCode<32){
        return;
    }
    if((event.keyCode<=57)&&(event.keyCode>=48)){
        if (!event.shiftKey){
            return;
        }
    }
    if ((ch=='-') ||(ch=='.')){
        return;
    }
    
    event.returnValue=false;
}

// Set to TextBox Read Only on keypress
function TextBoxReadOnly(){
    var ch = String.fromCharCode(event.keyCode);
    window.focus();
    window.returnValue = false;
    event.returnValue = false;    
}
	
// Set to TextBox Read Only (Property / Attribute)
function SetTextBoxToReadOnly(arrObjName){
    for(var i = 0; i < arrObjName.length; i++){
        document.getElementById(arrObjName[i]).readOnly = true;
    }//end for
}

// Set to TextBox Read Only (Property / Attribute)
function SetTextBoxInfraToReadOnly(arrObjName){
    for(var i = 0; i < arrObjName.length; i++){
        igedit_getById(arrObjName[i]).setReadOnly(true);
    }//end for
}

// ************************************************************************************ //
// Update By    : Tong~Tree
// Update Date  : 26 January 2007

// Trim message
function Trim(TRIM_VALUE){
    if(TRIM_VALUE.length < 1){
        return "";
    }//end
    TRIM_VALUE = RTrim(TRIM_VALUE);
    TRIM_VALUE = LTrim(TRIM_VALUE);
    if(TRIM_VALUE==""){
        return "";
    }else{
        return TRIM_VALUE;
    }//end
}

// Right trim message
function RTrim(VALUE){
    var w_space = String.fromCharCode(32);
    var v_length = VALUE.length;
    var strTemp = "";
    
    if(v_length < 0){
        return "";
    }//end
    
    var iTemp = v_length -1;
    while(iTemp > -1){
        if(VALUE.charAt(iTemp) == w_space){
            //
        }else{
            strTemp = VALUE.substring(0,iTemp +1);
            break;
        }//end if
    
        iTemp = iTemp-1;
    } //End While
    
    return strTemp;
} //End Function

// Left trim message
function LTrim(VALUE){
    var w_space = String.fromCharCode(32);
    if(v_length < 1){
        return "";
    }
    
    var v_length = VALUE.length;
    var strTemp = "";
    var iTemp = 0;

    while(iTemp < v_length){
        if(VALUE.charAt(iTemp) == w_space){
            //
        }else{
            strTemp = VALUE.substring(iTemp,v_length);
            break;
        }//end if
        
        iTemp = iTemp + 1;
    } //End While
    
    return strTemp;
} //End Function

// ************************************************************************************ //

// Create By    : Tong~Tree
// Create On    : 06 February 2007
// Disabled textbox by object name
function SetDisabledTextBox(arrObjName,bFlag){
    for(var i = 0; i < arrObjName.length; i++){
        document.getElementById(arrObjName[i]).disabled = bFlag;
    }//end for
}

// Check validate form
function CheckValidateForm(form){
    var numEl = form.elements.length;
    var oForm = null;
    var msg = "";
                
    for(var i = 0; i < numEl; i++){
        oForm = form.elements[i];
        switch(oForm.type){
            case "text":
            case "textarea":
                if(oForm.title != undefined && oForm.title != ""){
                    msg = "MSG_"+oForm.title.replace(/\s/g,"");
                    if(oForm.value == ""){
                        alert(eval(msg));
                        oForm.focus();
                        return false;
                    }//end
                }//end if                        
                break;
            case "hidden":
                if(oForm.name.indexOf("wcb") != -1){
                    var combo = document.getElementById(oForm.name+"_f");
                    if(combo != null && combo.title != undefined && combo.title != ""){
                        var oCombo = ISGetObject(oForm.name);
                        if(oCombo.Value == ""){
                            msg = "MSG_"+combo.title.replace(/\s/g,"");
                            alert(eval(msg));
                            oCombo.SetFocus();
                            return false;
                        }//end if                                
                    }//end
                }//end if
                break;
        }//end switch
    }//end for                
}

// Create By    : Tree
// Create Date  : 09/03/2007
// Check validated form
function CheckedForm(arrObjectControl,arrMessage){
    var oForm = null;
    
    for(var i = 0; i < arrObjectControl.length; i++){
        oForm = document.getElementById(arrObjectControl[i]);
        
        switch(oForm.type){
            case "textarea":
            case "select-one":
            case "text":
                if(!oForm.value){
                    alert(arrMessage[i]);
                    if(!oForm.disabled)oForm.focus();
                    return false;
                }//end if
                break;
        }//end/**/
    }//end for
    
    return true;
}

// Create By    : Tree
// Create Date  : 14/03/2007
// Get data response from web service
function GetServiceResponse(sParam,sValue,sUrl,sMethodName,oFunction){
    var arrParam = sParam.split(',');
    var arrValue = sValue.split(',');
    var pl = new SOAPClientParameters();
    
    for(var i = 0; i < arrParam.length; i++){
        pl.add(arrParam[i], arrValue[i]);
    }//end for
    
    if(oFunction)
        SOAPClient.invoke(sUrl, sMethodName, pl, true, oFunction);
    else
        SOAPClient.invoke(sUrl, sMethodName, pl, true, eval(sMethodName));
    
    pl = null;
}

// Get filter object number if null return 0
function GetFilterNumber(oNumber){
    var dNumber = 0;
    if(oNumber){
        dNumber = parseFloat(oNumber);
    }
    
    return dNumber;
}

// Get filter object text if null return ""
function GetFilterText(oText){
    var sText = "";
    if(oText){
        sText = oText;
    }
    
    return sText;
}

function getQueryVariable(qName) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    
    for (var i=0;i<vars.length;i++) {
        var pair = vars[i].split("=");
        if (pair[0] == qName) {
            return pair[1];
        }
    }//end for
    
    return "";
}

// Get inner text on label control
function getMessageBox(labelId){
    var message = MSG_Input+""+document.getElementById(labelId).innerText;
    return message;
}

//Create By Golf
//Change Cursor to Wait while page submit
function setHourglass()
{
    document.body.style.cursor = 'wait';
}

