﻿/// <reference path="JQuery/jquery-1.4.2.min.js" />
/// - added for intellisense

$(document).ready(function () {
    //this is for colorbox email this page
    $(".emailthispagelink").click(function () {
        $("#EmailThisPage #formContainer").show();
        $("#EmailThisPage").show();
        $(this).colorbox({ width: '750px', height: '480px', opacity: '0.7', inline: true, href: "#EmailThisPage", onCleanup: hideContent });
        setTimeout(FocusOnNameField, 1000);

    });

    $("#EmailThisPage #btnSendThisPage").click(function () { processEmailThisPage(); });
    //initialise help
    initialiseHelp();
});

//Functions for colorbox (Send this page)
function FocusOnNameField() {

    if ($('#EmailThisPage  #txtYourName') != null) {
        $('#EmailThisPage  #txtYourName').focus();
    }
}

function hideContent() {
    //hide errors
    $("#EmailThisPage #errorPara").hide();
    //also reset the 'completion' containers
    $("#EmailThisPage #errorContainer").hide();
    $("#EmailThisPage #successContainer").hide();
    $("#EmailThisPage").hide();
}

function emailThisPageValidate(formfields) {
    var errorPara = $("#EmailThisPage #errorPara");
    errorPara.html("");
    var isError = false;
    if (formfields.txtYourName.length == 0) {
        errorPara.html((errorPara.html().length==0 ? "" : (errorPara.html() + "<br />")) + "Please enter your name");
        isError = true;
    }
    if (formfields.txtYourEmail.length == 0) {
        errorPara.html((errorPara.html().length == 0 ? "" : (errorPara.html() + "<br />")) + "Please enter your email address");
        isError = true;
   }
   if (formfields.txtFriendName.length == 0) {
       errorPara.html((errorPara.html().length == 0 ? "" : (errorPara.html() + "<br />")) + "Please enter your friend's name");
       isError = true;
   }
   if (formfields.txtFriendEmail.length == 0) {
       errorPara.html((errorPara.html().length == 0 ? "" : (errorPara.html() + "<br />"))  + "Please enter your friend's email address");
       isError = true;
   }

   if (!isError) {
       //validate the email addresses
       if (!validateEmail(formfields.txtYourEmail)) {
           errorPara.html((errorPara.html().length == 0 ? "" : (errorPara.html() + "<br />")) + "Your email address is invalid");
           isError = true;
       }
       if (!validateEmail(formfields.txtFriendEmail)) {
           errorPara.html((errorPara.html().length == 0 ? "" : (errorPara.html() + "<br />")) + "Your friend's email address is invalid");
           isError = true;
       }
   }
    return !isError;
}

function validateEmail(email) {
    var re = "\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
    var regex = new RegExp(re);
    return regex.test(email);
}

function processEmailThisPage() {
    var formdata = {
        hdnfURLToSend: location.href,
        txtYourName: $.trim($("#EmailThisPage  #txtYourName").val()),
        txtYourEmail: $.trim($("#EmailThisPage  #txtYourEmail").val()),
        txtFriendName: $.trim($("#EmailThisPage  #txtFriendName").val()),
        txtFriendEmail: $.trim($("#EmailThisPage  #txtFriendEmail").val()),
        chkbxCopyToSelf: ($("#EmailThisPage  #chkbxCopyToSelf:checked").length == 1).toString()
    };


    //if there's an error, hide the blurb, show the email and do a bit of a CSS fudge to show the error messages
    //in the colorbox without scrolling in the colorbox
    if (!emailThisPageValidate(formdata)) {
        $("#EmailThisPage #errorPara").show();
        $("#EmailThisPage #emailBlurb").hide();
        $("#EmailThisPage h1").css("margin-bottom","0.5em");
        return;
    }

    //now send the ajax
    var url = "/tellafriend.aspx?";
    data = formdata;
    $.ajax({
        type: 'POST',
        url: url,
        data: data,
        success: function (response, textStatus, XMLHttpRequest) { ajaxSuccessHandler(textStatus); },
        error: function(XMLHttpRequest, textStatus, errorThrown) { ajaxErrorHandler(textStatus, errorThrown); }
    });
}

function ajaxSuccessHandler(status) {
    $("#EmailThisPage #successContainer").show();
    $("#EmailThisPage #formContainer").hide();
    emailThisPageFormClear();
}

function ajaxErrorHandler(status, error) {
    $("#EmailThisPage #errorContainer").show();
    $("#EmailThisPage #formContainer").hide();
    emailThisPageFormClear();
}

function emailThisPageFormClear() {
    $("#EmailThisPage #txtYourName").val("");
    $("#EmailThisPage #txtYourEmail").val("");
    $("#EmailThisPage #txtFriendName").val("");
    $("#EmailThisPage #txtFriendEmail").val("");
    $("#EmailThisPage #chkbxCopyToSelf:checked").click();
    $("#EmailThisPage #errorPara").html("");
}

//*************************************************Help*****************************
function initialiseHelp() {
    var needHelpAnchors = document.getElementsByTagName("a");
    for (var i = 0; i < needHelpAnchors.length; i++) {
        if (needHelpAnchors[i].getAttribute("href") != null) {
            if ((needHelpAnchors[i].getAttribute("href").indexOf('&hlppg=') >= 0) || (needHelpAnchors[i].getAttribute("href").indexOf('?hlppg=') >= 0)) {
                var needHelpAnchor = needHelpAnchors[i];
                needHelpAnchor.setAttribute("href", "javascript: getHTMLView('" + getUrlParam("hlppg", needHelpAnchor.getAttribute("href")) + "','" + needHelpAnchor.getAttribute("id") + "');");
            }
        }
    }
}

function getUrlParam(pramname, url) {
    pramname = pramname.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + pramname + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(url);
    if (results == null) return "";
    else return results[1];
}

//***************************************************ajax call for help page*************************************
var xhGHV
var helpdocVal
function getHTMLView(helpdoc, linkId) {
    helpdocVal = helpdoc;
    xhGHV = GetXmlHttpObject();
    containerprefix = linkId.replace('hypHelp', '');
    if (xhGHV == null) {
        alert("Your browser does not support Javascript, or Javascript is turned off!");
        return;
    }
    else {
        //document.getElementById("datacontainer").style.backgroundImage = "url('images/processing.gif')";
        var url = "/basket/help/" + helpdoc + ".htm";
        xhGHV.onreadystatechange = function () { showHTMLView(containerprefix); };  //showHTMLView is the next function called
        xhGHV.open("Get", url, true);
        xhGHV.send(null);
    }
}

function showHTMLView(containerprefix) {
    if (xhGHV.readyState == 4) {
        helpdocVal = containerprefix + 'pnlHelpContainer';
        helpContainer = document.getElementById(helpdocVal);
        helpContainer.style.display = "block";
        helpContainer.style.position = "absolute";
        helpContainer.innerHTML = "<p onclick=\"javascript: document.getElementById('" + helpContainer.getAttribute("id") + "').style.display = 'none';\">Close</p>";
        helpContainer.innerHTML += xhGHV.responseText;
        helpContainer.innerHTML += "<p onclick=\"javascript: document.getElementById('" + helpContainer.getAttribute("id") + "').style.display = 'none';\">Close</p>";
    }
}
//***************************************************ajax call for help page*************************************
//************************************************** XML HTTP Object ******************************************
var xmlHttp
function GetXmlHttpObject() {
    var xmlHttp = null;
    try {
        // Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
    }
    catch (e) {
        // Internet Explorer
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;
}
//************************************************** XML HTTP Object ******************************************
