if (typeof bqc != 'undefined') {
    bqc.itinaryPointAnchor = new GPoint(19, 48);
}

String.prototype.trim = function() {

 // skip leading and trailing whitespace
 // and return everything in between
  var x=this;
  x=x.replace(/^\s*(.*)/, "$1");
  x=x.replace(/(.*?)\s*$/, "$1");
  return x;
}

function getRegionValue() {
        var regionValue;
        var selectedRegion = $("regions").value;
        var multiRegion    = $("multiRegionId").value;
        // *** One regtion is selected
        if (parseInt(selectedRegion) != 0) {
            regionValue  = $("regions").value;
        // *** We check if there is a multiregion
        } else if (multiRegion) {
            regionValue    = multiRegion;
        }

        return regionValue;
}
function isUSZipCode(zip){
    rule = new RegExp(/(^\d{5}$)|(^\d{7}$)|(^\d{9}$)|(^\d{5}-\d{2}$)|(^\d{5} \d{2}$)|(^\d{5}-\d{4}$)|(^\d{5} \d{4}$)/);
    if (rule.test(zip)){
        return true;
    }else{
        alert(errorMsg_fieldsZIP);
        return false;
    }

}

function isCanadianPostalCode(postalCode){
    postalCode = postalCode.trim();
    
    if(postalCode.length==3){
        rule = new RegExp(/^\D{1}\d{1}\D{1}/);
        if(rule.test(postalCode)){
            return true;
        }else{
            alert(errorMsg_fieldsPostal);
            return false;
        }
    }else{
        rule = new RegExp(/^\D{1}\d{1}\D{1}\ ?-?\d{1}\D{1}\d{1}$/);
        if(rule.test(postalCode)){
            return true;
        }else{
            alert(errorMsg_fieldsPostal);
            return false
        }
    }
}

function createXmlHttpRequest() {
    try {
        if (window.XMLHttpRequest) {
            var req = new XMLHttpRequest();
            // some older versions of Moz did not support the readyState property
            // and the onreadystate event so we patch it!
            if (req.readyState == null) {
            req.readyState = 1;
            req.addEventListener("load", function () {
                req.readyState = 4;
                if (typeof req.onreadystatechange == "function")
                   req.onreadystatechange();
                }, false);
            }
            return req;
        }
            if (window.ActiveXObject) {
                return new ActiveXObject("Microsoft.XmlHttp");
            }
        }
    catch (ex) {}
    // fell through
    throw new Error("Your browser does not support XmlHttp objects");
}

function emailCheck (emailStr) {
    var checkTLD=1;
    var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
    var emailPat=/^(.+)@(.+)$/;
    var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
    var validChars="\[^\\s" + specialChars + "\]";
    var quotedUser="(\"[^\"]*\")";
    var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
    var atom=validChars + '+';
    var word="(" + atom + "|" + quotedUser + ")";
    var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
    var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
    var matchArray=emailStr.match(emailPat);

    if (matchArray==null) {
        alert(error_invalidEmailFormat);
        return false;
    }

    var user=matchArray[1];
    var domain=matchArray[2];

    for (i=0; i<user.length; i++) {
        if (user.charCodeAt(i)>127) {
            alert(error_invalidEmailFormat);
            return false;
       }
    }
    for (i=0; i<domain.length; i++) {
        if (domain.charCodeAt(i)>127) {
            alert(error_invalidEmailFormat);
        return false;
       }
    }

    if (user.match(userPat)==null) {
        alert(error_invalidEmailFormat);
        return false;
    }

    var IPArray=domain.match(ipDomainPat);
    if (IPArray!=null) {

    for (var i=1;i<=4;i++) {
        if (IPArray[i]>255) {
            alert(error_invalidEmailFormat);
        return false;
       }
    }
    return true;
    }

    var atomPat=new RegExp("^" + atom + "$");
    var domArr=domain.split(".");
    var len=domArr.length;

    for (i=0;i<len;i++) {
        if (domArr[i].search(atomPat)==-1) {
            alert(error_invalidEmailFormat);
            return false;
        }
    }

    if (checkTLD && domArr[domArr.length-1].length!=2 && 
        domArr[domArr.length-1].search(knownDomsPat)==-1) {
        alert(error_invalidEmailFormat);
        return false;
    }

    if (len<2) {
    alert(error_invalidEmailFormat);
    return false;
    }

    return true;
}            

function validateMail(form){
    if(form.senderMail.value==''){
        alert(error_emptySender);
        return false;
    }else{
        if(emailCheck(form.senderMail.value)){
            if(form.recipientMail.value==''){
                alert(error_emptyRecipient);
                return false;
            }else{
                if(emailCheck(form.recipientMail.value)){
                    return true;
                }else{
                    return false;
                }
            }
        }else{
            return false;
        }
    }
    return true;
}



function getViewportSize() {
    var size = [0, 0];
    var viewPortDimensions =  document.viewport.getDimensions();
    size[0] = viewPortDimensions.width;
    size[1] = viewPortDimensions.height;

    return size;
}

function createPopupWindow(url,windowId) {
    var sName="_blank";
    if(windowId){
        sName=windowId;
    }
    var popupWindow = window.open(url, sName, "location=no,toolbar=no,status=yes,titlebar=no,resizable=yes,scrollbars=yes,width=1014,height=730,top=0,left=0");
    popupWindow.focus();
}

function openTargetWindow() {
        var popupWindow = window.open(baseHREF + "ui/Loading.jsp", "_blank", "location=no,toolbar=no,titlebar=no,status=yes,resizable=yes,scrollbars=yes,width=950,height=700");
        var millis = new Date().getTime();
        popupWindow.name = "popupwindow-" + millis; 
        //alert("name: " + popupWindow.name);
        popupWindow.focus();
        return popupWindow.name;
}

function createMailPopupWindow(url) {
    var popupWindow = window.open(url, "_blank", "location=no,toolbar=no,status=yes,titlebar=no,resizable=yes,scrollbars=yes,width=700,height=560");
    popupWindow.focus();
}

function openMailTargetWindow() {
        var popupWindow = window.open(baseHREF + "ui/Loading.jsp", "_blank", "location=no,toolbar=no,titlebar=no,status=yes,resizable=yes,scrollbars=yes,width=640,height=560");
        var millis = new Date().getTime();
        popupWindow.name = "popupwindow-" + millis; 
        //alert("name: " + popupWindow.name);
        popupWindow.focus();
        return popupWindow.name;
}

function createPrintPopupWindow(url) {
    var popupWindow = window.open(url, "_blank", "location=no,toolbar=no,status=yes,titlebar=no,resizable=yes,scrollbars=yes,width=700,height=700");
    popupWindow.focus();
}

function openPrintTargetWindow() {
        var popupWindow = window.open(baseHREF + "ui/Loading.jsp", "_blank", "location=no,toolbar=no,titlebar=no,status=yes,resizable=yes,scrollbars=yes,width=700,height=700");
        var millis = new Date().getTime();
        popupWindow.name = "popupwindow-" + millis; 
        //alert("name: " + popupWindow.name);
        popupWindow.focus();
        return popupWindow.name;
}

/*
* Methode qui sert a rafraichir le contenu des listbox
*/
function reloadSelect(mySelect, dataType, parentId, secondaryId, baseHREF, locale){
    var str = ""+Math.random();
    var rnd = str.substring(2, str.length);
    mySelect.options.length=0;
    if(locale!=null)
        var documentUrl = "geocoding/GetData.do?dataType="+dataType+"&parentId="+parentId+"&secondaryId="+secondaryId+"&locale="+locale+"&rnd="+rnd;
    else
    
        var documentUrl = "geocoding/GetData.do?dataType="+dataType+"&parentId="+parentId+"&secondaryId="+secondaryId+"&rnd="+rnd;
    if(baseHREF != null)
        documentUrl = baseHREF+documentUrl;        
    var request = new XMLRemoteRequest();
    var doc = request.getRemoteDocument(documentUrl);
    
    var dataOptions = doc.getElementsByTagName('option');
    for(i=0;i<dataOptions.length;i++){
      var option = new Option();
      var node = dataOptions.item(i);
      option.value = node.getElementsByTagName('value').item(0).firstChild.nodeValue;
      option.text = node.getElementsByTagName('text').item(0).firstChild.nodeValue;
      option.abbr = node.getElementsByTagName('abbr').item(0).firstChild.nodeValue;
//      if (node.getElementsByTagName('abbr').item(0).firstChild.nodeValue.equalsIgnoreCase('QC')) {
//        option.selected = 'true';
        // quebec is the default selected
        //option.label = node.getElementsByTagName('abbr').item(0).firstChild.nodeValue;
//      }
      mySelect.options[mySelect.length]=option;
    }
}

        var meterToMilesRatio = 0.621371192237334;

        
        function metersToCurrentUnit(meters) {
            if (bqc.params.p_unit == 'mi' || bqc.params.p_unit == 'MI') {
                return (Math.round(meters * meterToMilesRatio / 100) / 10) + ' ' + bqc.locale.itinary.details.miles;
            } else {
                return (Math.round(meters / 100) / 10) + ' ' + bqc.locale.itinary.details.km;
            }
        }



function isIE() {
    if (navigator.appName.indexOf('Microsoft') != -1) {
        return true;
    } else {
        return false;
    }
}

function validateMaxLength(e)
{
        var key;
        var keychar;

        if (window.event) {
           key = window.event.keyCode;
        } else if (e) {
           key = e.which;
        } else {
           return true;
        }
        keychar = String.fromCharCode(key);
        keychar = keychar.toLowerCase();


        if (window.event) {
                e = window.event;
                target = e.srcElement;
        } else {
                target = e.target;
        }

        var maxLength = parseInt(target.attributes["maxlength"].value)
        if (target.value.length == maxLength) {
                var isTextKey = ((key >= 186 && key <= 222) || (key == 9) || (key == 13) || (key >= 109 && key <= 111) || (key >= 48 && key <= 90) || key == 32);
                if (isTextKey) {
                   return false;
                } else {
                   return true;
                }
        } else if (target.value.length > maxLength) {
                target.value = target.value.substring(0, maxLength);
                return false;
        } else {
                return true;
        }
}

function switchProvince(){
    if(country== "can"){
        country = "us";
    }else{
        country = "can";
    }
    document.geocodingForm.p_province.length=0;
    reloadSelect(document.geocodingForm.p_province, 'province', country, null, href, lang);
}
