//This function is used to disable a submit button once it has been clicked
var submitted = false;
var myVarWindow;
function submitOnce() {
    if (!submitted && document.getElementById('isSubmitted').value != '1') {
        document.getElementById('isSubmitted').value='1';
        submitted = true;
        return true;
    } else {
        return false;
    }
}


function check_tab_firstName(e) {
if (e.keyCode == 9 ) {
document.getElementById('lastName').focus();
e.returnValue = false; // for IE
if (e.preventDefault) e.preventDefault(); // for Mozilla
}
}

function check_tab_lastName(e) {
if (e.keyCode == 9 ) {
document.getElementById('state').focus();
e.returnValue = false; // for IE
if (e.preventDefault) e.preventDefault(); // for Mozilla
}
}
function check_tab_state(e) {

if (e.keyCode == 9 ) {
document.getElementById('membershipnumber').focus();
e.returnValue = false; // for IE
if (e.preventDefault) e.preventDefault(); // for Mozilla
}
}
function check_tab_findNumber(e) {
if (e.keyCode == 9 ) {
document.getElementById('emailAddress').focus();
e.returnValue = false; // for IE
if (e.preventDefault) e.preventDefault(); // for Mozilla
}
}

function check_tab_emailAddress(e) {
if (e.keyCode == 9 ) {
document.getElementById('verifyEmailAddress').focus();
e.returnValue = false; // for IE
if (e.preventDefault) e.preventDefault(); // for Mozilla
}
}


//This function is used to handle the AMC secure Sign In
function doLogin(u,p){

    var form_ = document.forms["signOn"];
    var loginServletUrl = "/"+location.pathname.split("/")[1]+"/login";
    var loginUrl = "https://"+location.hostname;

    if ( location.port == "8988"  ) {
        //for the local test
        loginUrl = "http://"+location.hostname+":"+location.port;
    }

    loginUrl = loginUrl + loginServletUrl;

    form_.action=loginUrl;

    document.getElementById("hiddenname").value = document.getElementById(u).value;
    document.getElementById("hiddenpassword").value = document.getElementById(p).value;

    var qs = window.location.href;
    var i = qs.indexOf("loc");
    if (i!=-1){
        i = i + 4;
        var j = qs.indexOf("&vid");
        if (j!=-1){
            document.getElementById("loc").value = qs.substring(i,j);
        }
        else{
            document.getElementById("loc").value = qs.substring(i);
        }
    }
    form_.submit();

    return false;
}

//This function is used to handle the login submit button
function onClickSubmitLogin(r,u) {
    if (validate()) {
        rememberUsername(r,u);
        if (!submitted) {
              submitted = true;
              return true;
        } else {
              return false;
        }
        return submitOnce();
    } else {
        return false;
    }
}

function onClickSubmitLoginHome(r,u,p) {
    if (onClickSubmitLogin(r,u)) {
        doLogin(u,p);
    }
    return false;
}

//This function is used to handle the createPasswordPanel submit button
function onClickSubmitCreatePasswordPanel() {
    if (validate()) {
        return submitOnce();
    } else {
        resetIsSubmitted();
        return false;
    }
}

//This function is used to handle the confirmIdentityPanel submit button
function onClickSubmitConfirmIdentityPanel() {
    if (validate()) {
        return submitOnce();
    } else {
        resetIsSubmitted();
        return false;
    }
}

//This function is used to handle the personalInfo submit button
function onClickSubmitPersonalInfo() {
    if (validate()) {
        document.profile.isSubmittedProfile.value='1';
        return submitOnce();
    } else {
        resetIsSubmitted();
        document.profile.isSubmittedProfile.value='0';
        return false;
    }
}

//This function is used to handle the joinProfile submit button
function onClickSubmitJoinProfile() {
    if (validateProfile(document.personal_info)) {
        return submitOnce();
    } else {
        resetIsSubmitted();
        return false;
    }
}

//This function is used to handle the joinPaymentOptions submit button
function onClickSubmitJoinPaymentOptions() {
    if (validate()) {
        return submitOnce();
    } else {
       resetIsSubmitted();
       return false;
    }
}

//This function is used to handle the memberProfile submit button
function onClickSubmitMemberProfile(form) {

    var result = validate();
    var additionalResult = validateProfileAdditionalMemberRequiredFields(form);
    if (result && additionalResult) {
        return submitOnce();
    } else {
        document.personal_info.isSubmitted.value='0';
        return false;
    }
}

//This function is used to handle the memberPaymentOptions submit button
function onClickSubmitMemberPaymentOptions() {
    if (validate()){
        return submitOnce();
    } else{
        resetIsSubmitted();
        return false;
    }
}

//This function is used to handle the contactForm submit button
function onClickSubmitContactForm() {
    if (validate()) {
       return submitOnce();
    } else {
        resetIsSubmitted();
        return false;
    }
}

//This function is used to handle the editPayment submit button
function onClickSubmitEditPayment() {
    if (validate()) {
        return submitOnce();
    } else {
        resetIsSubmitted();
        return false;
    }
}

//This function is used to handle the editPaymentConfirm submit button
function onClickSubmitEditPaymentConfirm() {
    if (validate()) {
        return submitOnce();
    } else {
        resetIsSubmitted();
        return false;
    }
}

//This function is used to handle the orderCard submit button
function onClickSubmitOrderCard() {
    if (validate()) {
        return submitOnce();
    } else {
        resetIsSubmitted();
        return false;
    }
}

//This function is used to handle the carfaxReport submit button
function onClickSubmitCarfaxReport() {
    if (validate()) {
        return submitOnce();
    } else {
        document.carfax_report.isSubmitted.value='0';
        return false;
    }
}

var myWindow;

function openCenteredWindow(url, width, height) {
    var left = parseInt((screen.availWidth/2) - (width/2));
    var top = parseInt((screen.availHeight/2) - (height/2));
    var windowFeatures = "width=" + width + ",height=" + height + ",scrollbars=1,status,resizable=yes,left=" + left + ",top=" + top + "screenX=" + left + ",screenY=" + top;
    myWindow = window.open(url, "Carfax", windowFeatures);
}


function getCarReport(width, height) {
   var appCtxt = document.getElementById("appctxt").value;
   var vin = document.getElementById("VIN");
   if (vin != null){
      var url = appCtxt + '/carfax/carfax.jsp';
       if (vin.value != ""){
          openCenteredWindow(url, width, height);
       }
   }
   return false;
}

//This function is used to handle the carfaxReport submit button II
function onClickSubmitReport(url, width, height) {
    var vin = document.getElementById("VIN");
    if (vin!=null){
        if (vin.value!=""){
            if (onClickSubmitCarfaxReport()) {
                openCenteredWindow(url, width, height);
                return true;
            }
            else{
                return false;
            }
        }
    }
}

//This function is used to handle the RMC Trip Plan secure Sign In using hidden variables
function doSecuredPostExt(){
    var form_ = document.forms["frmAMC"];
    if (form_!=null){
        form_.action = document.getElementById("hSite").value;
        form_.method = "POST";
        document.getElementById("urn").value = document.getElementById("hMemberID").value;
        document.getElementById("loc").value = document.getElementById("hLoc").value;
        document.getElementById("secure").value = document.getElementById("hSecure").value;
        document.getElementById("email").value = document.getElementById("hEmail").value;
        form_.submit();
    }
    return false;
}

//This function is used to show the RMC disclaimer using a modal window
function emaillegalprompt(){
    var left = parseInt((screen.availWidth/2) - (256));
    var top = parseInt((screen.availHeight/2) - (115));
    legalwindow = dhtmlmodal.open('legalbox', 'div', 'modalalertdiv', '', 'width=512px,height=270px,left='+left+'px,top='+top+'px,resize=0,scrolling=0');
}

//This function is used to cancel the modal window processing
function onClickCancelPromptButton(){
    var c = document.getElementById('modalalertdiv');
    if (c!=null){
        c.style.visibility = 'hidden';
        legalwindow.hide();
    }
    return false;
}

//This function is used to continue the modal window processing
function onClickContinuePromptButton(){
    var c = document.getElementById('modalalertdiv');
    var f = document.getElementById('hApproved');
    if (c!=null)
        c.style.visibility = 'hidden';
    if (f!=null)
        document.getElementById('hApproved').value='true';
    legalwindow.hide();
    return doSecuredPostExt();
}

//This function is used to start trip planning using RMC
function onClickStartTripPlanningButton(){
    var f = document.getElementById('hApproved');
    if (f.value=='false') {
        emaillegalprompt();
        return false;
    }
    else {
        return doSecuredPostExt();
    }
}

//This function is used to handle the promo join from the home page
function doJoin(){
    var f = document.getElementById("mainForm");
    if (f!=null){
      var b = document.getElementById("mainForm:welcomepanel:joinButton");
      if (b!=null){
        b.click();
      }
    }
    return false;
}

//This function is used to avoid using the back browser button in some pages
function noBack(){
   window.history.forward(1);
}

//This function is used to handle the enrollment submit button
function onClickSubmitEnrollment() {
    if (validateProfile(document.personal_info)) {
        if (validate()) {
            return submitOnce();
        }
        else {
            document.carfax_report.isSubmitted.value='0';
            return false;
        }
    } else {
        document.carfax_report.isSubmitted.value='0';
        return false;
    }
}

function openCenteredSmallWindow(url, width, height) {
    var left = parseInt((screen.availWidth/2) - (width/2));
    var top = parseInt((screen.availHeight/2) - (height/2));
    var windowFeatures;
    if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) {
       var ieversion = new Number(RegExp.$1) // capture x.x portion and store as a number
       if (ieversion >= 7) {
          windowFeatures = 'width=' + width + ',height=' + height + ',location=yes,scrollbars=1,status,resizable=yes,align=center,left=' + left + ',top=' + top + 'screenX=' + left + ',screenY=' + top;
       }else{
          windowFeatures = 'width=' + width + ',height=' + height + ',location=yes,toolbar=yes,scrollbars=1,status,resizable=yes,align=center,left=' + left + ',top=' + top + 'screenX=' + left + ',screenY=' + top;
       }
    }else {
      windowFeatures = 'width=' + width + ',height=' + height + ',toolbar=yes,scrollbars=1,status,resizable=yes,align=center,left=' + left + ',top=' + top + 'screenX=' + left + ',screenY=' + top;
    }
    myVarWindow = window.open(url, "Travel", windowFeatures);
    myVarWindow.focus();
    return false;
}

function submitpromo(id, uiid, hcid, hoffer, appcontext) {
   if(validate(id)) {
      var url = '/member_services/Promo.faces?PROMO_CODE=';
      var promoObj = document.getElementById(uiid);
      var hbcid = document.getElementById(hcid);
      var hoffercode = document.getElementById(hoffer);
      var appctxt = document.getElementById(appcontext);
      if(hoffercode.value != promoObj.value) {
         hbcid.value = '';
      }
      if(promoObj.value != null && promoObj.value != '') {
        var hurl = appctxt.value + url + promoObj.value;
        if(hbcid.value != null && hbcid.value != '') {
           hurl = hurl + '&cid=' + hbcid.value;
        }
        document.location.href = hurl;
      }
   }
   return false;
}

function fireOnClickEvent(objID) {
     if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) {
        document.getElementById(objID).onclick();
     }else {
        var evt = document.createEvent("MouseEvents");
        evt.initMouseEvent("click", true, true, window,
          0, 0, 0, 0, 0, false, false, false, false, 0, null);
        document.getElementById(objID).dispatchEvent(evt);
     }
}

function resetIsSubmitted() {
   document.getElementById('isSubmitted').value='0';
}

function checkIsSubmitted() {
   if (!submitted && document.getElementById('isSubmitted').value != '1') {
      return true;
   }else {
      return false;
   }
}


