function replaceEntities(str) {
    var entityMap = {
        '\xa1': '&iexcl;',
        '\xa4': '&curren;',
        '\xa7': '&sect;',
        '\xa9': '&copy;',
        '\xaa': '&ordf;',
        '\xab': '&laquo;',
        '\xac': '&not;',
        '\xae': '&reg;',
        '\xb0': '&deg;',
        '\xb1': '&plusmn;',
        '\xb2': '&sup2;',
        '\xb3': '&sup3;',
        '\xb5': '&micro;',
        '\xb6': '&para;',
        '\xb9': '&sup1;',
        '\xbb': '&raquo;',
        '\xbf': '&iquest;',
        '\xc7': '&Ccedil;',
        '\xd7': '&times;',
        '\xd8': '&Oslash;',
        '\xde': '&THORN;',
        '\xdf': '&szlig;',
        '\xe6': '&aelig;',
        '\xf0': '&eth;',
        '\xf7': '&divide;',
        '\xf8': '&oslash;',
        '\u0152': '&OElig;',
        '\u0153': '&oelig;',
        '\u0192': '&fnof;',
        '\u02c6': '&circ;',
        '\u0393': '&Gamma;',
        '\u0394': '&Delta;',
        '\u03b1': '&alpha;',
        '\u03b2': '&beta;',
        '\u03b3': '&gamma;',
        '\u03b4': '&delta;',
        '\u03b9': '&iota;',
        '\u03ba': '&kappa;',
        '\u03c6': '&phi;',
        '\u03c8': '&psi;',
        '\u03c9': '&omega;',
        '\u2030': '&permil;',
        '\u2039': '&lsaquo;',
        '\u203a': '&rsaquo;',
        '\u20ac': '&euro;',
        '\u2122': '&trade;',
        '\u2211': '&sum;',
        '\u2212': '&minus;',
        '\u221a': '&radic;',
        '\u221d': '&prop;',
        '\u221e': '&infin;',
        '\u2227': '&and;',
        '\u2228': '&or;',
        '\u2229': '&cup;',
        '\u222a': '&cup;',
        '\u222b': '&int;',
        '\u2234': '&there4;',
        '\u2248': '&asymp;',
        '\u2260': '&ne;',
        '\u2261': '&equiv;',
        '\u2264': '&le;',
        '\u2265': '&ge;',
        '\u2295': '&oplus;',
        '\u22a5': '&perp;'
    };
    var result = [];
    var char0 = 0;
    for (var i = 0; i < str.length; i++) {
        char0 = str.charAt(i);
        if (entityMap[char0]) {
            result.push(entityMap[char0]);
        }
        else {
            result.push(char0);
        }
    }
    return result.join('');
}

function replaceCn(str) {
    var cnenCode = {
        183: 64,
        8212: 45,
        8216: 39,
        8217: 39,
        8220: 34,
        8221: 34,
        8230: 94,
        12288: 32,
        12289: 92,
        12290: 46,
        12298: 60,
        12299: 62,
        12304: 91,
        12305: 93,
        65281: 33,
        65288: 40,
        65289: 41,
        65292: 44,
        65306: 58,
        65307: 59,
        65311: 63,
        65372: 124,
        65509: 36
    };
    var result = [];
    var charCode = 0;
    var char1 = '';
    for (var i = 0; i < str.length; i++) {
        charCode = str.charCodeAt(i);
        if (charCode > 65280 && charCode < 65375) {
            charCode = charCode - 65248;
        }
        if (cnenCode[charCode]) {
            char1 = String.fromCharCode(cnenCode[charCode]);
        }
        else {
            char1 = str.charAt(i);
        }
        result.push(char1);
    }
    return result.join('');
}

function cnToEn4T(str) {
    var result = replaceEntities(replaceCn(str));
    return result.replace(/[\u4E00-\u9FA5]|[\uFE30-\uFFA0]/gi, '');
}
function checkTel() { 
   
    var errorTel1 = 'Please specify Telephone No.'; 
    var errorTel2 = 'Your telephone number is too long';
    
     if(window.lan=='es'){
        errorTel1 = 'Por favor, especifica tu n¨²mero de tel¨¦fono.';
     }else if(window.lan=='de'){
        errorTel1 = 'Bitte geben Sie Ihre Telefonnumer ein.';
     }else if(window.lan=='fr'){
        errorTel1 = 'Veuillez indiquer un num&eacute;ro de t&eacute;l&eacute;phone, s'+"'"+'il vous pla&icirc;t. '; 
     } else if(window.lan=='nl'){
        errorTel1 = 'Vul uw Telefoonnummer in.'; 
     } else if(window.lan=='it'){
        errorTel1 = 'Per favore, inserite il vostro numero di telefono.'; 
     } else if(window.lan=='pt'){
        errorTel1 = 'Por favor, digite um numero de telefone'; 
     } else if(window.lan=='ru'){
        errorTel1 = '&#1055;&#1086;&#1078;&#1072;&#1083;&#1091;&#1081;&#1089;&#1090;&#1072;,'+
        '&#1091;&#1082;&#1072;&#1078;&#1080;&#1090;&#1077; &#1085;&#1086;&#1084;&#1077;'+
        '&#1088; &#1042;&#1072;&#1096;&#1077;&#1075;&#1086; &#1090;&#1077;&#1083;&#1077;&#1092;&#1086;&#1085;&#1072;'; 
     } else if(window.lan=='jp'){
        errorTel1 = '&#38651;&#35441;&#30058;&#21495;&#12434;&#20837;&#21147;&#12375;&#12390;&#12367;&#12384;&#12373;&#12356;&#12290;'; 
     } else if(window.lan=='kr'){
        errorTel1 = '&#51204;&#54868;&#48264;&#54840;&#47484; &#51648;&#51221;&#54616;&#49901;&#49884;&#50724;.'; 
     } else if(window.lan=='sa'){
        errorTel1 = '&#1576;&#1585;&#1580;&#1575;&#1569; &#1578;&#1581;&#1583;&#1610;&#1583; &#1585;&#1602;&#1605; &#1575;&#1604;&#1578;&#1604;&#1610;&#1601;&#1608;&#1606;'; 
     } 
  
    if (jQuery.trim(jQuery('#comTelephone1').val()).length == 0 || jQuery.trim(jQuery('#comTelephone3').val()).length == 0 || jQuery('#comTelephone3').hasClass('grayColor')) {
        return errorTel1;
    }
    if (jQuery.trim(jQuery('#comTelephone1').val()).length + jQuery.trim(jQuery('#comTelephone2').val()).length + jQuery.trim(jQuery('#comTelephone3').value).length > 40) {
        return errorTel2;
    }
    return true;
}

function checkFax() {
    var errorFax1 = 'Your fax number is too long.';
    if (jQuery.trim(jQuery('#comFax1').val()).length + jQuery.trim(jQuery('#comFax2').val()).length + jQuery.trim(jQuery('#comFax3').val()).length > 40) {
        return errorFax1;
    }
    return true;
}

function checkEmailInfo(email, el) {
    var lancode = window.lan;
    
    if (jQuery('#check_email_info').length && email != window.lastCheckedEmail) {
        jQuery('#check_email_info').load('/inquiry.do', {
            'xcase': 'checkEmail',
            'lanCodeflag': lancode,
            'senderMail': email
        }, function() {
            window.lastCheckedEmail = email;
            jQuery('#check_email_info a').click(function() {
                window.MICE.view.modaldialog.focus('<iframe src="' + (window.MIC_SRVRNMVO || '') + '/logon.do?xcase=logonPage4Inquiry" frameborder="0" allowTransparency="true" style="margin:0 auto; padding:0; border:0; width:295px; height:295px;"></iframe>', {
                    title: 'Sign In ',
                    width: 310
                });
                return false;
            });
        });
    }
    return true;
}

function getTelCode(country) {
    jQuery.get('/inquiry.do', {
        xcase: 'countryCode',
        senderCountry: country
    }, function(data) {
        jQuery('#comTelephone1').val(data);
        jQuery('#comFax1').val(data);
    });
}
function telphoneNote() {
    var tel2 = jQuery('#comTelephone2');
    var tel3 = jQuery('#comTelephone3'); 
    var tel2Area = 'Area Code';
    var tel3Number = 'Number';
     if(window.lan=='es'){
        tel2Area = 'C&oacute;digo de &aacute;rea';
        tel3Number = 'N&uacute;mero';
     }else if(window.lan=='de'){
        tel2Area = 'Vorwahl';
        tel3Number = 'Anzahl';
     }else if(window.lan=='fr'){
         tel2Area = 'Indicatif r&eacute;gional'; 
         tel3Number = 'Num&eacute;ro';
     } else if(window.lan=='nl'){
        tel2Area = 'Netnummer'; 
         tel3Number = 'Aantal';
     } else if(window.lan=='it'){
        tel2Area = 'Prefisso Telefonico'; 
         tel3Number = 'Numero';
     } else if(window.lan=='pt'){
        tel2Area = 'C&oacute;digo de &Aacute;rea'; 
         tel3Number = 'N&uacute;mero';
     } else if(window.lan=='ru'){
        tel2Area = '§Ü§à§Õ §Ù§à§ß§í'; 
        tel3Number = '§¹§Ú§ã§Ý§à'; 
     } else if(window.lan=='jp'){
        tel2Area = '&#12456;&#12522;&#12450;&#12467;&#12540;&#12489;'; 
         tel3Number = '&#12487;&#12472;&#12479;&#12523;';
     } else if(window.lan=='kr'){
        tel2Area = '&#51648;&#50669; &#53076;&#46300;'; 
         tel3Number = '&#49688;';
     } else if(window.lan=='sa'){
        tel2Area = '&#1603;&#1608;&#1583; &#1575;&#1604;&#1605;&#1606;&#1591;&#1602;&#1577;'; 
        tel3Number = '&#1593;&#1583;&#1583;';
     } 
      
    tel2.blur(function() {
        var value = jQuery.trim(jQuery(this).val());
        transCharTemp(tel2Area);  
        if (value.length == 0 || value == 'Area Code' || value == $("#transTemp").html()) {
            jQuery(this).val($("#transTemp").html()).addClass('grayColor');  
        }
    }).focus(function() {
        var obj = jQuery(this);
        if (obj.attr('class') == 'grayColor') {
            obj.val('').removeClass('grayColor');
        }
    });
    tel2.blur();
    tel3.blur(function() {
        var value = jQuery.trim(jQuery(this).val());
         transCharNumTemp(tel3Number); 
        if (value.length == 0 || value == 'Number' || value == $("#transNumTemp").html()) {
            jQuery(this).val( $("#transNumTemp").html()).addClass('grayColor');
        }
    }).focus(function() {
        var obj = jQuery(this);
        if (obj.attr('class') == 'grayColor') {
            obj.val('').removeClass('grayColor');
        }
    });
    jQuery('#comCountry').change(function() {
        getTelCode(jQuery(this).val());
    }); 
     transCharTemp(tel2Area);
    if (jQuery.trim(tel2.val()).length == 0 || tel2.val() == 'Area Code' || tel2.val() == $("#transTemp").html()) {
        tel2.val($("#transTemp").html()).addClass('grayColor'); 
    }
    else {
        tel2.removeClass('grayColor');
    }
    transCharNumTemp(tel3Number);
    if (jQuery.trim(tel3.val()).length == 0 || tel3.val() == 'Number' || tel3.val() == $("#transNumTemp").html()) {
        tel3.val($("#transNumTemp").html()).addClass('grayColor');
    }
    else {
        tel3.removeClass('grayColor');
    }
}

function transCharTemp(value) {
    if ($("#transTemp").length == 0) {
        $("body").append("<div id='transTemp'></test>");
    }
    $("#transTemp").html(value).hide();
}
function transCharNumTemp(value) {
    if ($("#transNumTemp").length == 0) {
        $("body").append("<div id='transNumTemp'></test>");
    }
    $("#transNumTemp").html(value).hide();
}

function faxNote() {
    var fax2 = jQuery('#comFax2');
    var fax3 = jQuery('#comFax3');
    
    var fax2Area = 'Area Code';
    var fax3Number = 'Number';
     if(window.lan=='es'){
        fax2Area = 'C&oacute;digo de &aacute;rea';
        fax3Number = 'N&uacute;mero';
     }else if(window.lan=='de'){
        fax2Area = 'Vorwahl';
        fax3Number = 'Anzahl';
     }else if(window.lan=='fr'){
         fax2Area = 'Indicatif r&eacute;gional'; 
         fax3Number = 'Num&eacute;ro';
     } else if(window.lan=='nl'){
        fax2Area = 'Netnummer'; 
         fax3Number = 'Aantal';
     } else if(window.lan=='it'){
        fax2Area = 'Prefisso Telefonico'; 
         fax3Number = 'Numero';
     } else if(window.lan=='pt'){
        fax2Area = 'C&oacute;digo de &Aacute;rea'; 
         fax3Number = 'N&uacute;mero';
     } else if(window.lan=='ru'){
        fax2Area = '§Ü§à§Õ §Ù§à§ß§í'; 
        fax3Number = '§¹§Ú§ã§Ý§à';
     } else if(window.lan=='jp'){
        fax2Area = '&#12456;&#12522;&#12450;&#12467;&#12540;&#12489;'; 
         fax3Number = '&#12487;&#12472;&#12479;&#12523;';
     } else if(window.lan=='kr'){
        fax2Area = '&#51648;&#50669; &#53076;&#46300;'; 
         fax3Number = '&#49688;';
     } else if(window.lan=='sa'){
        fax2Area = '&#1603;&#1608;&#1583; &#1575;&#1604;&#1605;&#1606;&#1591;&#1602;&#1577;'; 
        fax3Number = '&#1593;&#1583;&#1583;';
     } 
     
    fax2.blur(function() { 
        var value = jQuery.trim(jQuery(this).val());
         transCharTemp(fax2Area);
        if (value.length == 0 || value == 'Area Code' || value == $("#transTemp").html()) {
            jQuery(this).val($("#transTemp").html()).addClass('grayColor');
        }
    }).focus(function() {
        var obj = jQuery(this);
        if (obj.attr('class') == 'grayColor') {
            obj.val('').removeClass('grayColor');
        }
    });
    fax3.blur(function() {
        var value = jQuery.trim(jQuery(this).val());
        transCharNumTemp(fax3Number); 
        if (value.length == 0 || value == 'Number' || value == $("#transNumTemp").html() ) {
            jQuery(this).val($("#transNumTemp").html()).addClass('grayColor');
        }
    }).focus(function() {
        var obj = jQuery(this);
        if (obj.attr('class') == 'grayColor') {
            obj.val('').removeClass('grayColor');
        }
    });
     transCharTemp(fax2Area);
    if (jQuery.trim(fax2.val()).length == 0 || fax2.val() == 'Area Code' || fax2.val() == $("#transTemp").html()) {
        fax2.val($("#transTemp").html()).addClass('grayColor');
    }
    else {
        fax2.removeClass('grayColor');
    }
      transCharNumTemp(fax3Number); 
    if (jQuery.trim(fax3.val()).length == 0 || fax3.val() == 'Number' || fax3.val() ==$("#transNumTemp").html()) {
        fax3.val($("#transNumTemp").html()).addClass('grayColor');
    }
    else {
        fax3.removeClass('grayColor');
    }
}

function failCallFun() {
    if (!jQuery('#hb:visible').length && jQuery('#hb div.onError').length) {
        jQuery('#ht').click();
    }
}

function setLogonInfo(canSendAttachment) {
    var id = '', lgInfo = window.logonInfo || {};
    for (id in lgInfo) {
        jQuery('#' + id).val(lgInfo[id]);
    }
    jQuery('#formComCountry').val(jQuery('#comCountry').val()).attr('name', jQuery('#comCountry').attr('name'));
    jQuery('#comCountry').attr('disabled', true).removeAttr('name');
    if (canSendAttachment) {
        jQuery('#sendAttachmentRow').show();
        jQuery('#sendAttachment').removeAttr('disabled');
    }
    jQuery('#checkAlert').load(window.location.href + ' #checkAlert > td', function() {
        if (!jQuery('#checkAlertInput').attr('disabled')) {
            jQuery('#checkAlert').show();
        }
    });
    jQuery('#unlogonOptions').hide().find('input').attr('disabled', true).removeAttr('name');
    jQuery('div.onError').hide();
    jQuery('#check_email_info').remove();
    telphoneNote();
    faxNote();
    if ($.browser.mozilla && $.browser.version < "1.9") {
        jQuery('#dialog,#dialog-mask').remove();
    }
    window.MICE.view.modaldialog.hide();
    setTimeout(function() {
        window.logonStatus = '';
        window.requestLogonStatus && requestLogonStatus();
    }, 50);
}
//
(function($) {
    var comCountry = document.getElementById('comCountry'), formComCountry = document.getElementById('formComCountry');
    var locateCountry = function(selectInput) {
        $.getScript((window.MIC_SRVRNMVO || '') + '/account.do?xcase=parseIPToCountryKey', function() {
            var response = window.countryKey || '';
            if (response && response != 'N/A') {
                $(selectInput).find('option[value="' + response + '"]').eq(0).attr('selected', true);
                $(selectInput).change();
            }
        })
    };
    if (comCountry && !comCountry.disabled && !comCountry.value && formComCountry && !formComCountry.value) {
        locateCountry(comCountry);
    }
    
    $('#ht').css('cursor', 'pointer').click(function() {
        if ($('#hb:visible').length) {
            $(this).find('img').attr('src', '/images/vo/more.gif');
            $('#hb').hide();
        }
        else {
            $(this).find('img').attr('src', '/images/vo/less.gif');
            $('#hb').slideDown();
        }
    });

    $('#senderMail').change(function() {
        if ($('#check_email_info').html()) {
            window.lastCheckedEmail = $(this).val();
            $('#check_email_info').html('');
        }
    });
    $('#validateNumber').attr('autoComplete', 'off');
    $('#comTelephone1,#comTelephone2,#comTelephone3,#comFax1,#comFax2,#comFax3').keyup(function() { 
        $(this).val(cnToEn4T($(this).val()));
    });
    telphoneNote();
    faxNote();
})(jQuery);

