// General settings
var noOfLhsBoxes = 6;
var currentTab = 0;
var currentNavItem = '';

// Set cookie
function setCookie(name, value, expires, path, domain, secure) {
    var curCookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
    document.cookie = curCookie;
    }

// Get cookie
function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
            } else
            begin += 2;
        var end = document.cookie.indexOf(";", begin);
        if (end == -1)
            end = dc.length;
    return unescape(dc.substring(begin + prefix.length, end));
    }

// Delete cookie
function deleteCookie(name, path, domain) {
    if (getCookie(name)) {
        document.cookie = name + "=" +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        "; expires=Thu, 01-Jan-70 00:00:01 GMT";
        }
    }

// Fix date
function fixDate(date) {
    var base = new Date(0);
    var skew = base.getTime();
    if (skew > 0)
        date.setTime(date.getTime() - skew);
    }

// Handles check out from the sales sorter
function checkOutSorter() {
    // use default plan initially
    var partnerProdID = getCookie('PartnerProdId');
    checkOut(partnerProdID);
}

// Handles choice check out from the sales sorter
function checkOutSorterChoice() {
    // use default plan initially
    var partnerProdChoiceID = getCookie('PartnerProdChoiceId');
    checkOut(partnerProdChoiceID);
}

// Handles check out when a planId is known
function checkOut(planId) {
    handleRegistrationForPlan(planId);
}

// Handles code check from rowlands page
function checkOutRowlands() {

    var codeBox = findElementById('codeBox');
    var code = codeBox.value;

    var service = new CoreSystems.SalesUI.LoginService();
    service.set_path('LoginService.asmx');

    var planID = service.CheckCode(code, checkSucceededCallback);
}

// Callback from rowlands code check
function checkSucceededCallback(result) {
    if (result > 0) {

        var codeBox = findElementById('codeBox');
        var code = codeBox.value;
        
        // ensure we have the correct link to store code in profile
        var partnerCode = getCookie('PartnerCode');
        partnerCode = partnerCode + code;

        var partnerOrg = getCookie('PartnerOrgProd');
        
        // then register
        var service = new CoreSystems.SalesUI.LoginService();
        service.set_path('LoginService.asmx');
        service.GetRegistrationByPlan(partnerCode, result, partnerOrg, changeUrlSucceededCallback);
    }
    else {alert('Sorry, you entered an invalid code.');}
}


// Handles redirection where there is a price plan and organisation
function handleRegistrationForPlan(planId) {

    var service = new CoreSystems.SalesUI.LoginService();
    service.set_path('LoginService.asmx');
    var partnerCode = getCookie('PartnerCode');
    var partnerOrg = getCookie('PartnerOrgProd');
    var partnerPlanDefault = getCookie('PartnerProdId');

    if (partnerCode == null) { partnerCode = ""; }
    if (partnerOrg == null) { partnerOrg = ""; }
    if (partnerPlanDefault == null) { partnerPlanDefault = "559"; }
    if (planId == null) { planId = ""; }

    var localPlanId = (planId != "") ? planId : partnerPlanDefault;

    service.GetRegistrationByPlan(partnerCode, localPlanId, partnerOrg, changeUrlSucceededCallback);
}

function handleRegistrationCode(codes) {
    // codes have already been validated
    var service = new CoreSystems.SalesUI.LoginService();
    service.set_path('LoginService.asmx');
    service.GetRegistrationByRegCode(codes, changeUrlSucceededCallback, errorCallback);
}

function changeUrlSucceededCallback(result) {
    window.location = result;
}

function errorCallback(error) {

    // Display an error message
    var message = error.get_message();
    window.location = "Get_Started.aspx?Error=" + "An error occurred validating your registration code.";
}

/* Reg Code from 5 boxes*/
function handleRegCode() {

    var boxes = new Array(5);
    for (i = 1; i <= 5; i++) {
        var box = findElementById('regCode' + i);
        boxes[i - 1] = box.value;
    }

    handleRegistrationCode(boxes);
}

/* Reg Code from 1 box*/
function handleOneRegCode() {

    var box = findElementById('regCode');
    var regStr = new String(box.value);
    regStr = regStr.replace(/ /g, "");
    regStr = regStr.replace(/-/g, "");
    var boxes = new Array(5);
    for (i = 0; i <= 4; i++) {
        boxes[i] = regStr.substring((i * 4), (i * 4) + 4);
    }

    handleRegistrationCode(boxes);
}


/* contact us */
// Used on sales pages that allow consumer to contact the sales/support team
// e.g. Contact Us, Employer page, Healthcare Provider Page
// Assumes page has 3 text boxes with IDs as below.
// detailBox can be any extra detail, e.g. name or tel number
function sendMailToEnquiries() {
    var detailBox = findElementById('detailBox');
    var phoneNo = detailBox.value;

    var emailBox = findElementById('emailBox');
    var email = emailBox.value;

    var msgBox = findElementById('msgBox');
    var msg = msgBox.value;

    var service = new CoreSystems.SalesUI.LoginService();
    service.set_path('LoginService.asmx');
    service.SendMessagetoEnquiries(phoneNo, email, msg);
}


/* send vtrenz survey data */
function sendSurveyData() {

    var webSyncID = getCookie('com.vtrenz.iMAWebCookie');
    
    var nameBox = findElementById('nameBox');
    var name = nameBox.value;

    var email = "";
    var emailBox = findElementById('emailBox');
    email = emailBox.value;

    var bpRad = findElementById('bpRadio');
    var bp = bpRad.checked;

    var diabCheck = findElementById('diabCheck');
    var heartCheck = findElementById('heartCheck');
    var conditions = "";
    if (diabCheck.checked) {
        conditions += "diabetes";
    }
    
    if (heartCheck.checked) {
        if (conditions.length > 0) conditions += "&";
        conditions += "enlargedHeart";
    }

    if (email.length > 0 && email.indexOf('@') > 0) {
        var service = new CoreSystems.SalesUI.LoginService();
        service.set_path('LoginService.asmx');
        service.SendDataToWebSurvey(webSyncID, name, email, bp, conditions);

        alert("Thank you, your request has been submitted");
    }
}

// -----------------------------------------------------------------------------
// Description: 
//		Portable replacement for findElementById
//   
// IN:	id	- object id	
// OUT: obj	- object with specified id.
// -----------------------------------------------------------------------------
function findElementById(objId) {
    return document.all ? document.all[objId] : document.getElementById(objId);
}

// -----------------------------------------------------------------------------
// Description: 
//      initialises error and adds it to the load event
//   
//IN - control to postback
// -----------------------------------------------------------------------------

function ErrorInitialise(error) {
    //prime error variable
    errorMessage = error;
    //add error to onload events
    addLoadEvent(ShowError);

}

// -----------------------------------------------------------------------------
// Description: 
//      does postback for the control
//   
//IN - control to postback
// -----------------------------------------------------------------------------
function ShowError() {
    //if there's an error, show it
    if (errorMessage != null && errorMessage != "") {
        help.Hide();
        help.autoHide = false;
        help.autoAway = false;
        help.showAnchor = false;
        help.helpPopUpPanelClassHeight = "helpPopup_AutoHeight";
        help.Show({ message: errorMessage, title: "Error", top: 300, left: 400 });
    }
    return true;
}

// -----------------------------------------------------------------------------
// Description: 
//      adds to the onload with out disturbing previous onload events.
//   
//IN - function to add
// -----------------------------------------------------------------------------
function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}
