﻿function FindPath(type)
{  
   var str = location;  
   var start=str.toString().toLowerCase().indexOf("/vn/");
   
   if (start==-1)
   {
      start=str.toString().toLowerCase().indexOf("/en/");
   }
   
   if (type=='image')
   {
      if (start==-1)
          return ''; 
      else   
          return '../'; 
   }
   else
   {
      if (start!=-1)
          return ''; 
      else   
          return type + '/'; 
   }
}

/* Air Tracking */

var prefix = new Array();
var codes = new Array();
prefix[1] = '390';
codes[1] = 'A3';
prefix[2] = '001';
codes[2] = 'AA';
prefix[3] = '057';
codes[3] = 'AF';
prefix[4] = '124';
codes[4] = 'AH';
prefix[5] = '027';
codes[5] = 'AS';
prefix[6] = '105';
codes[6] = 'AY';
prefix[7] = '055';
codes[7] = 'AZ';
prefix[8] = '236';
codes[8] = 'BD';
prefix[9] = '997';
codes[9] = 'BG';
prefix[10] = '695';
codes[10] = 'BR';
prefix[11] = '657';
codes[11] = 'BT';
prefix[12] = '106';
codes[12] = 'BW';
prefix[13] = '999';
codes[13] = 'CA';
prefix[14] = '230';
codes[14] = 'CM';
prefix[15] = '048';
codes[15] = 'CY';
prefix[16] = '006';
codes[16] = 'DL';
prefix[17] = '118';
codes[17] = 'DT';
prefix[18] = '053';
codes[18] = 'EI';
prefix[19] = '071';
codes[19] = 'ET';
prefix[20] = '607';
codes[20] = 'EY';
prefix[21] = '108';
codes[21] = 'FI';
prefix[22] = '023';
codes[22] = 'FX';
prefix[23] = '126';
codes[23] = 'GA';
prefix[24] = '072';
codes[24] = 'GF';
prefix[25] = '061';
codes[25] = 'HM';
prefix[26] = '075';
codes[26] = 'IB';
prefix[27] = '096';
codes[27] = 'IR';
prefix[28] = '635';
codes[28] = 'IY';
prefix[29] = '771';
codes[29] = 'J2';
prefix[30] = '957';
codes[30] = 'JJ';
prefix[31] = '201';
codes[31] = 'JM';
prefix[32] = '115';
codes[32] = 'JU';
prefix[33] = '643';
codes[33] = 'KM';
prefix[34] = '229';
codes[34] = 'KU';
prefix[35] = '020';
codes[35] = 'LH';
prefix[36] = '080';
codes[36] = 'LO';
prefix[37] = '724';
codes[37] = 'LX';
prefix[38] = '114';
codes[38] = 'LY';
prefix[39] = '239';
codes[39] = 'MK';
prefix[40] = '129';
codes[40] = 'MP';
prefix[41] = '077';
codes[41] = 'MS';
prefix[42] = '050';
codes[42] = 'OA';
prefix[43] = '064';
codes[43] = 'OK';
prefix[44] = '988';
codes[44] = 'OZ';
prefix[45] = '214';
codes[45] = 'PK';
prefix[46] = '079';
codes[46] = 'PR';
prefix[47] = '566';
codes[47] = 'PS';
prefix[48] = '656';
codes[48] = 'PX';
prefix[49] = '081';
codes[49] = 'QF';
prefix[50] = '070';
codes[50] = 'RB';
prefix[51] = '281';
codes[51] = 'RO';
prefix[52] = '580';
codes[52] = 'RU';
prefix[53] = '318';
codes[53] = 'S3';
prefix[54] = '421';
codes[54] = 'S7';
prefix[55] = '555';
codes[55] = 'SU';
prefix[56] = '065';
codes[56] = 'SV';
prefix[57] = '217';
codes[57] = 'TG';
prefix[58] = '235';
codes[58] = 'TK';
prefix[59] = '370';
codes[59] = 'TL';
prefix[60] = '037';
codes[60] = 'US';
prefix[61] = '738';
codes[61] = 'VN';
prefix[62] = '932';
codes[62] = 'VS';
prefix[63] = '870';
codes[63] = 'VV';
prefix[64] = '463';
codes[64] = 'ZP';
prefix[65] = '950';
codes[65] = 'XS';
function SetPFX(AwbForm){
    var code = AwbForm.Carrier.value;
    for(var i = 0; i < codes.length; i++){
        if(code == codes[i]){
           AwbForm.Pfx.value = prefix[i];
           break;
        }
    }
}

function GetPFX(code){
    for(var i = 0; i < codes.length; i++){
        if(code == codes[i]){
           return prefix[i];
        }
    }
}

function loadTracking(AwbForm, id) {

    if (AwbForm.Carrier.value == '') {
        alert('No carrier selected. Please choose one!');
        return;
    }
    

    if (AwbForm.Pfx.value == '') {
        alert('Prefix value is empty. Please enter valid one!');
        return;
    } else {
        if (isNaN(AwbForm.Pfx.value)) {
            alert('Invalid input prefix. Must be numeric value!');
            return;
        } else {
            var code, inList;
            inList = false;
            for (var i = 0; i < AwbForm.Carrier.length; i++) {
                code = AwbForm.Carrier[i].value;
                if (AwbForm.Pfx.value == GetPFX(code)) {
                    inList = true;
                    break;
                }
            }

            if (inList) {
                if (AwbForm.Pfx.value != GetPFX(AwbForm.Carrier.value)) {
                    alert('Select carrier is not match prefix. Please enter valid one!');
                    return;
                }
            } else {
                alert('Prefix value is not in carrier\'s  list . Please choose "..More carrier" for further information!');
                return;
            }
        }
    }

    if (AwbForm.Shipment.value == '') {
        alert('Shipment value is empty. Please enter valid one!');
        return;
    } else {
        if (isNaN(AwbForm.Shipment.value)) {
            if (AwbForm.Shipment.value.indexOf(' ') != -1) {
                alert('Invalid input shipment. No white space allow!')
            } else {
                alert('Invalid input shipment. Must be numeric value!')
            }
            return;
        } else {
            if (AwbForm.Shipment.value.length < 8) {
                alert('Invalid input shipment. Must be 8 characters length!')
                return;
            }
        }
    }

    if (id == 'td_frame')
        document.getElementById("ld").style.display = "";
    else
        document.getElementById("ld1").style.display = "";

    AwbForm.btnTrk.disabled = true;
    document.getElementById(id).style.display = "none";
    
    // code for IE
    if (window.ActiveXObject) {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    // code for Mozilla, etc.
    else if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    }

    if (xmlhttp != null) {
        //var param = 'Carrier=' + AwbForm.Carrier.value + '&Pfx=' + AwbForm.Pfx.value + '&Shipment=' + AwbForm.Shipment.value;
        xmlhttp.open("GET", AwbForm.Carrier.value+"/"+AwbForm.Pfx.value+"/"+AwbForm.Shipment.value+"/WebRes.aspx", true);
        xmlhttp.onreadystatechange = function() { state_sendTracking(AwbForm, id) };
        xmlhttp.send(null);
    } else {
        if (id == 'td_frame')
            document.getElementById("ld").style.display = "none";
        else
            document.getElementById("ld1").style.display = "none";
        alert("Your browser does not support XMLHTTP.");
        AwbForm.btnTrk.disabled = false;
    }
}

function state_sendTracking(AwbForm, id) {
    if (xmlhttp.readyState == 4) {
        // if "OK"
        if (xmlhttp.status == 200) {
            var comment = xmlhttp.responseText;

            if (comment == '0') {
                if (id == 'td_frame')
                    document.getElementById("ld").style.display = "none";
                else
                    document.getElementById("ld1").style.display = "none";
                AwbForm.btnTrk.disabled = false;
                alert("Can't not connect remote server at this time. \r\n Please try agian later! \r\n or search on ..More carrier.");
                return;
            }

            var div = document.createElement('div');
            div.innerHTML = comment;
            //var oTable = div.getElementsByTagName('table')[1];
            //var addRow, addCell, newRow, newCell;

            var vTable = document.getElementById(id);
            vTable.appendChild(div);
            
            //alert(comment);
            
            //vTable.innerHTML = comment;
            
//            try {
//                while (vTable.rows.length > 1)
//                    vTable.deleteRow(vTable.rows.length - 1);


//                for (var i = 0; i < oTable.rows.length; i++) {
//                    addRow = oTable.rows[i];
//                    newRow = vTable.insertRow(i);
//                    newRow.style.backgroundColor = oTable.rows[i].getAttribute('bgcolor');
//                    addCell = addRow.getElementsByTagName("td");
//                    for (var j = 0; j < addCell.length; j++) {
//                        newCell = newRow.insertCell(j);
//                        newCell.innerHTML = addCell[j].innerHTML;
//                        newCell.colSpan = addCell[j].colSpan;
//                        if (addCell[j].getAttribute('bgcolor') != '')
//                            newCell.style.backgroundColor = addCell[j].getAttribute('bgcolor');

//                        if (addCell[j].className != '')
//                            newCell.className = addCell[j].className;
//                        newCell.width = addCell[j].width;
//                    }
//                }
//            }
//            catch (e) {
//                alert(e.description);
//            }

            if (id == 'td_frame')
                document.getElementById("ld").style.display = "none";
            else
                document.getElementById("ld1").style.display = "none";

            AwbForm.btnTrk.disabled = false;
            vTable.style.display = "";
        }
        else {
            if (id == 'td_frame')
                document.getElementById("ld").style.display = "none";
            else
                document.getElementById("ld1").style.display = "none";

            AwbForm.btnTrk.disabled = false;
            alert("Problem retrieving XML data");
        }
    }
}

/*********** Email Sender **********/


function sendMail(lang) {

    var HoTen, Tel, Mail, Sub, tBody, k, sError, imgLoad;
    HoTen = document.aspnetForm.e_name;
    Tel = document.aspnetForm.e_tel;
    Mail = document.aspnetForm.e_mail;
    Sub = document.aspnetForm.e_sub;
    tBody =document.aspnetForm.e_cont;
    imgLoad = document.getElementById("imgLoad_"+lang);
    
    k = 0;
    sError = "";

    if (HoTen.value == "") {
        if (lang == "vn")
            sError += "- Xin vui lòng điền họ tên của bạn. \r\n";
        else
            sError += "- Please fill in your full name. \r\n";
    }

    if (Sub.value == "") {
        if (lang == "vn")
            sError += "- Xin vui lòng nhập chủ đề. \r\n";
        else
            sError += "- Please enter your subject. \r\n";

    }

    if (Tel.value != "") {
        k = k + 1;
    }

    if (Mail.value != "") {
        k = k + 1;
    }

    if (Mail.value == "") {
        if (lang == "vn")
            sError += "- Xin vui lòng nhập địa chỉ email của bạn. \r\n";
        else
            sError += "- Please fill in your email address. \r\n";

        Mail.focus;

    } else {

        var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

        if (!filter.test(Mail.value)) {
            if (lang == "vn")
                sError += "- Nhập địa chỉ mail không đúng định dạng!\r\n";
            else
                sError += "- Invalid email address format!\r\n";

            Mail.focus;
        }
    }

    if (k == 0) {
        if (lang == "vn")
            sError = sError + "- Xin vui lòng nhập địa chỉ, hoặc số điện thoại hoặc địa chỉ email để chúng tôi có thể phản hồi thông tin đến bạn.\r\n";
        else
            sError = sError + "- Please enter at least your address, telephone number or email address for contacting.\r\n";
    }

    if (sError == '') {
        if (tBody.value == "") {
            if (lang == "vn")
                sError += "- Xin vui lòng nhập nội dung.\r\n";
            else
                sError += "- Please enter your content.\r\n";

            tBody.focus;
        } else {
            if (tBody.value.length > 2000) {
                if (lang == "vn")
                    sError += "- Xin vui lòng nhập nội dung ít hơn 2000 ký tự.\r\n"
                else
                    sError += "- Your content must be less than 2000 characters .\r\n";

                tBody.focus;
            }
        }
    }

    if (sError == "") {
        imgLoad.style.display = '';
        var param = 'txtTen=' + HoTen.value + '&txtTel=' + Tel.value + '&txtMail=' + Mail.value + '&txtTitle=' + Sub.value + '&txt_message=' + Url.encode(tBody.value);
        sendMailItem(lang, param, imgLoad);
    } else {
        sError += ' '; 
        alert(sError);
    }
}

function sendMailItem(lang, em, imgLoad) {
    // code for Mozilla, etc.
       // code for IE
    if (window.ActiveXObject) {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    // code for Mozilla, etc.
    else if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    }
    
    if (xmlhttp != null) {
        try {
              xmlhttp.open("POST", "Post/M/WebRes.aspx", true);
              xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
              xmlhttp.onreadystatechange = function() { state_sendMail(imgLoad) };
              xmlhttp.send(em);           
        } catch (ex) {
            if (imgLoad != '')
                imgLoad.style.display = 'none';
        }
    } else {
        alert("Your browser does not support XMLHTTP.");
        if (imgLoad != '')
            imgLoad.style.display = 'none';
    }
}

function state_sendMail(imgLoad) 
{
    if (xmlhttp.readyState == 4) 
    {
        if (xmlhttp.status == 200) 
        {
            var comment = xmlhttp.responseText;
            if (imgLoad != '')
              imgLoad.style.display = 'none';
            
            alert(comment);  
        }
        else 
        {
            alert("Problem retrieving XML data");
            if (imgLoad != '')
                imgLoad.style.display = 'none';
        }
    }
    else
    {
        if (imgLoad != '')
          imgLoad.style.display = 'none';
    }
}

/**
*
*  URL encode / decode
*  http://www.webtoolkit.info/
*
**/

var Url = {

    // public method for url encoding
    encode: function(string) {
        return escape(this._utf8_encode(string));
    },

    // public method for url decoding
    decode: function(string) {
        return this._utf8_decode(unescape(string));
    },

    // private method for UTF-8 encoding
    _utf8_encode: function(string) {
        string = string.replace(/\r\n/g, "\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if ((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode: function(utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while (i < utftext.length) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if ((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i + 1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i + 1);
                c3 = utftext.charCodeAt(i + 2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

}

/********** End Url Encode ************/
