﻿// #############################
// #  submission methods
// #############################

var prescriptionDateFormat = "dd/MM/yyyy";

// User selects whether to supply prescription details online or via Fax/Post/Email
function selectPrescriptionSupplyMethod(){
    if (document.getElementById('prescription_method').checked == true){
        
        document.getElementById('prescriptionName').disabled = true;
        document.getElementById('prescriptionDate').disabled = true;
        document.getElementById('prescriptionExpiry').disabled = true;
        
        document.getElementById('sphereRight').disabled = true;
        document.getElementById('cylinderRight').disabled = true;
        document.getElementById('axisRight').disabled = true;
        document.getElementById('addRight').disabled = true;
        
        document.getElementById('sphereLeft').disabled = true;
        document.getElementById('cylinderLeft').disabled = true;
        document.getElementById('axisLeft').disabled = true;
        document.getElementById('addLeft').disabled = true;
        
        document.getElementById('pupillaryDistance').disabled = true;
        document.getElementById('prescriptionNotes').disabled = true;
        document.getElementById('optometristName').disabled = true;
        document.getElementById('optometristPhone').disabled = true;
        
        var tableTD = document.getElementById('prescription').getElementsByTagName('td');
        /** IE script to cover <select> elements with <iframe>s **/
        for (j=0; j<tableTD.length; j++) {
            tableTD[j].style.color = '#cccccc';
        }
        var tableTH = document.getElementById('prescription').getElementsByTagName('th');
        /** IE script to cover <select> elements with <iframe>s **/
        for (j=0; j<tableTH.length; j++) {
            tableTH[j].style.color = '#cccccc';
        }
    }else{
        document.getElementById('prescriptionName').disabled = false;
        document.getElementById('prescriptionDate').disabled = false;
        document.getElementById('prescriptionExpiry').disabled = false;
        
        document.getElementById('sphereRight').disabled = false;
        document.getElementById('cylinderRight').disabled = false;
        document.getElementById('axisRight').disabled = false;
        document.getElementById('addRight').disabled = false;
        
        document.getElementById('sphereLeft').disabled = false;
        document.getElementById('cylinderLeft').disabled = false;
        document.getElementById('axisLeft').disabled = false;
        document.getElementById('addLeft').disabled = false;
        
        document.getElementById('pupillaryDistance').disabled = false;
        document.getElementById('prescriptionNotes').disabled = false;
        document.getElementById('optometristName').disabled = false;
        document.getElementById('optometristPhone').disabled = false;
        
        var tableTD = document.getElementById('prescription').getElementsByTagName('td');
        /** IE script to cover <select> elements with <iframe>s **/
        for (j=0; j<tableTD.length; j++) {
            tableTD[j].style.color = '#000000';
        }
        var tableTH = document.getElementById('prescription').getElementsByTagName('th');
        /** IE script to cover <select> elements with <iframe>s **/
        for (j=0; j<tableTH.length; j++) {
            tableTH[j].style.color = '#000000';
        }
    }

}

// User to submit the prescription options to the cart
function addPrescriptionToCart()
{
    if (prescriptionRequired()){
        // validate the prescription
        if (!validatePrescription())
        {
            return;    
        }
    }
    
    AjaxQueue.push(jsWebRoot + 'ajax/addPrescriptionToCart.aspx?nextStep=1&' + makePrescriptionQueryString(), {onSucess: ajaxAddPrescriptionToCartResponse, onfailure: ajaxResponse}, "frameOptions"); 

    location.href="checkout.aspx";
    
}

function goToPrescriptions()
{
    AjaxQueue.push(jsWebRoot + 'ajax/addPrescriptionToCart.aspx??nextStep=0&' + makeKvp("frameUid", document.getElementById("frameUid").value), {onSucess: ajaxResponse, onFailure: ajaxResponse}, "frameOptions"); 
    setTimeout('selectPrescriptionSupplyMethod()', 1000); 
    
}

// Used to load the prescription options from the cart
function loadPrescriptionFromCart()
{
   obj = document.getElementById('frameOptions');
   obj.style.display='block';
    
   AjaxQueue.push(jsWebRoot + 'ajax/addPrescriptionToCart.aspx?nextStep=0&frameUid=' + frameUid, {onSucess: ajaxResponse, onfailure: ajaxResponse}, "frameOptions"); 

    setTimeout('selectPrescriptionSupplyMethod()', 1000);
}

// checks if user has opted to supply prescription via alternative method
function prescriptionRequired(){
    
    if (document.getElementById('prescription_method').checked == true){
        return false;
    }else{
        return true;
    }
}

// Validated the required fields
function validatePrescription()
{
    // if the user has checked the to be supplied box then simply return true
    var objPrescriptionMethod = Trim(document.getElementById('prescription_method'));
    if (objPrescriptionMethod.checked)
    {
        return true;
    }

    // if the expiration date is not set set it to 2 years ahead 
    // of the prescription date if it has been set correctly
    setExpirationDate();
    
    var prescriptionName = Trim(document.getElementById('prescriptionName').value);
    var prescriptionDate = Trim(document.getElementById('prescriptionDate').value);
    var prescriptionExpiry = Trim(document.getElementById('prescriptionExpiry').value);
    var optometristName = Trim(document.getElementById('optometristName').value);
    var optometristPhone = Trim(document.getElementById('optometristPhone').value);
    
    var validationText = ""
    var isValid = true;
    
    
    // prescriptionName
    if (prescriptionName.length == 0)
    {
        validationText = validationText + "\n\tName on the prescription.";
        isValid = false;
    }
    
    // prescriptionDate
    if (prescriptionDate.length == 0)
    {
        validationText = validationText + "\n\tPrescription Date.";
        isValid = false;
    }
    else if (!isDate(prescriptionDate, prescriptionDateFormat))
    {
        validationText = validationText + "\n\tPrescription Date should be in the format dd/mm/yyyy.";
        isValid = false;
    }
    
    // prescriptionExpiry
    if (prescriptionExpiry.length == 0)
    {
        validationText = validationText + "\n\tExpiry Date.";
        isValid = false;
    }
    else if (!isDate(prescriptionExpiry, prescriptionDateFormat))
    {
        validationText = validationText + "\n\tExpiry Date should be in the format dd/mm/yyyy.";
        isValid = false;
    }
    else if (compareDates(prescriptionDate, prescriptionDateFormat, prescriptionExpiry, prescriptionDateFormat) == 1)
    {
        validationText = validationText + "\n\tPrescription Date must be before the Expiry Date.";
        isValid = false;
    }
    
    // right sphere
    if (getSelectValue("sphereRight") == "NA")
    {
        validationText = validationText + "\n\tRight Eye Sphere.";
        isValid = false;
    }
    
    // left sphere
    if (getSelectValue("sphereLeft") == "NA")
    {
        validationText = validationText + "\n\tLeft Eye Sphere.";
        isValid = false;
    }
    
    // optometristName
//    if (optometristName.length == 0)
//    {
//        validationText = validationText + "\n\tOptometrist's name.";
//        isValid = false;
//    }
    
    // optometristPhone
//    if (optometristPhone.length == 0)
//    {
//        validationText = validationText + "\n\tOptometrist's phone number.";
//        isValid = false;
//    }
    
    if (!isValid){
        validationText =  "Please provide the following information before continuing." + validationText;
        alert(validationText);
        return false;
    }else{
        return true;
    }
}

// trap ajax success or failuure
function ajaxAddPrescriptionToCartResponse()
{

}

function ajaxResponse()
{
    
}

// determines the selected options and returns a formatted querystring
function makePrescriptionQueryString()
{
    // hold the price as it is calculated
    var querystring;
    querystring = "";
    
    // Get frame UID
    querystring += makeKvp("frameUid", document.getElementById("frameUid").value); 

    // prescriptionName
    // if the user has checked the to be supplied box then simply return true
    if (document.getElementById('prescription_method').checked)
    {
        querystring += makeKvp("prescriptionName", document.getElementById("prescription_method").value); 
    }
    else
    {
        querystring += makeKvp("prescriptionName", document.getElementById("prescriptionName").value); 
    }
    // prescriptionDate
    querystring += makeKvp("prescriptionDate", document.getElementById("prescriptionDate").value); 

    // prescriptionExpiry
    querystring += makeKvp("prescriptionExpiry", document.getElementById("prescriptionExpiry").value); 

    // sphereRight
    querystring += makeKvp("sphereRight", getSelectValue("sphereRight")); 

    // sphereLeft
    querystring += makeKvp("sphereLeft", getSelectValue("sphereLeft")); 

    // cylinderRight
    querystring += makeKvp("cylinderRight", getSelectValue("cylinderRight")); 

    // cylinderLeft
    querystring += makeKvp("cylinderLeft", getSelectValue("cylinderLeft")); 

    // axisRight
    querystring += makeKvp("axisRight", document.getElementById("axisRight").value);
    
    // axisLeft
    querystring += makeKvp("axisLeft", document.getElementById("axisLeft").value);

    // addRight
    querystring += makeKvp("addRight", getSelectValue("addRight")); 
    
    // addLeft
    querystring += makeKvp("addLeft", getSelectValue("addLeft")); 
    
    // pupillaryDistance
    querystring += makeKvp("pupillaryDistance", document.getElementById("pupillaryDistance").value);

    // prescriptionNotes
    querystring += makeKvp("prescriptionNotes", document.getElementById("prescriptionNotes").value.replace(/\n/g, '##NL##'));

    // optometristName
    querystring += makeKvp("optometristName", document.getElementById("optometristName").value);
    
    // optometristPhone
    querystring += makeKvp("optometristPhone", document.getElementById("optometristPhone").value);
    
    return querystring;
}


function setExpirationDate()
{
    var prescriptionDate = Trim(document.getElementById('prescriptionDate').value);
    var prescriptionExpiry = Trim(document.getElementById('prescriptionExpiry').value);

    // if the expiration has not already been set
    //if (prescriptionExpiry.length == 0)
    //{
        //alert("exp not set");
        // and the prescription date is valid
        if (isDate(prescriptionDate, prescriptionDateFormat))
        {
            //alert ("Setting exp");
            // get the prescription as a date object
            var pDate = parseDate(prescriptionDate);
            // add 2 years to the prescription date
            pDate.addYears(2);
            // update the expiry date for the prescription
            //alert("exp date: " + formatDate(pDate, prescriptionDateFormat));
            document.getElementById('prescriptionExpiry').value = formatDate(pDate, prescriptionDateFormat);
        }
    //}
}

// #############################
// #####  Helper functions
// #############################

// retrieves a from a select list 
function getSelectValue(id)
{
    var tag = document.getElementById(id);
    return tag.options[tag.selectedIndex].value.replace("+", "&#43");
}

function presetExpiryDate(){
    //alert('inside presetExpiryDate()');
    var prescriptionDate    = getElementById('prescriptionDate').value;
    var prescriptionExpiry  = getElementById('PrescriptionExpiry').value;
    
    if (prescriptionExpiry == ''){
        arPrescriptionDate = prescriptionDate.split("/");
        arPrescriptionDate[2] = arPrescriptionDate[2] + 2;
        getElementById('PrescriptionExpiry').value = arPrescriptionDate[0] + "/" + arPrescriptionDate[1] + "/" + arPrescriptionDate[2] 
    }
}
