﻿// JScript File
var CHECK = false;
var Exiting = true;
var EnqSave = false;

//window.onbeforeunload = ExitLeadForm;

function ExitLeadForm(){
    EnqSave=false;
    Conf="yes";
    if(Exiting){
        Exiting = false;
        setTimeout ( "OpenExitModal()", 1000 );
        return "***************************************************************************\n\r"+
               "this is the text you shuld put here\n\r"+
               "this is the text you shuld put here\n\r"+
               "this is the text you shuld put here\n\r"+
               "***************************************************************************\n\r";
    }
    Exiting = false;
}
// open the modal box of exit lead
function OpenExitModal(){
    var pName = "ExitLead";
    Conf='no';    
    Modalbox.show('<div style=\'padding: 10px; height:100%; background:#FFF;z-index:100; border: 1px solid #CCC\'><table width=\'606\' border=\'0\' cellspacing=\'0\' cellpadding=\'0\'><tr><td><table width=\'606\' border=\'0\' cellspacing=\'0\' cellpadding=\'0\'><tr><td><img src=\'images/title_ExitLead.gif\' title=\'Exit Lead\' width=\'606\' height=\'150\'></td></tr></table></td></tr><tr><td bgcolor=\'e6e6e6\'><table width=\'606\' border=\'0\' cellspacing=\'0\' cellpadding=\'0\'><tr><td align=\'left\' valign=\'top\'><table border=\'0\' width=\'606\' cellpadding=\'0\' cellspacing=\'0\' class=\'mainTable\'><tr><td class=\'mainRight\'>Name:</td><td height=\'50\' width=\'550\' class=\'mainRight\'><label><input name=\'textfield\' id=\'wuName\' type=\'text\' size=\'40\'></label><span id=\'ValidName\' style=\'display:none;\' class=\'formCopyRed\'><nobr>Name required</nobr></span></td></tr><tr><td colspan=\'3\' class=\'mainDivide\'><img src=\'images/shim.gif\'></td></tr><tr><td class=\'mainRight\'>Postcode:</td><td height=\'50\' width=\'550\' class=\'mainRight\'><label><input name=\'textfield\' id=\'wuPostCode\' type=\'text\' size=\'20\'></label><span id=\'ValidPostCode\' style=\'display:none;\' class=\'formCopyRed\'><nobr>PostCode required</nobr></span></td></tr><tr><td colspan=\'3\' class=\'mainDivide\'><img src=\'images/shim.gif\'></td></tr><tr><td class=\'mainRight\'>Contact Number:</td><td height=\'50\' width=\'550\' class=\'mainRight\'><label><input name=\'textfield\' id=\'wuContactNumber\' type=\'text\' size=\'20\'></label><span id=\'ValidContactNumber\' style=\'display:none;\' class=\'formCopyRed\'><nobr>Contact Number required</nobr></span></td></tr><tr><td colspan=\'3\' class=\'mainDivide\'><img src=\'images/shim.gif\'></td></tr><tr><td class=\'mainRight\'>Preferred Time of Call:</td><td height=\'50\' width=\'550\' class=\'mainRight\'><select name=\'ddlTimeofCall\' id=\'wuTimeofCall\'><option value=\'8:00 to 12:00\'>8:00 to 12:00</option><option value=\'12:00 to 16:00\'>12:00 to 16:00</option><option value=\'16:00 to 20:00\'>16:00 to 20:00</option></select><span id=\'ValidTimeofCall\' style=\'display:none;\' class=\'formCopyRed\'><nobr>Time of Call required</nobr></span></td></tr><tr><td colspan=\'3\' class=\'mainDivide\'><img src=\'images/shim.gif\'></td></tr><tr><td class=\'mainRight\'>Email:</td><td height=\'50\' width=\'550\' class=\'mainRight\'><label><input name=\'textfield\' id=\'wuEmail\' type=\'text\' size=\'20\'></label><span id=\'ValidEmail\' style=\'display:none;\' class=\'formCopyRed\'><nobr>Valid email required</nobr></span></td></tr><tr><td colspan=\'3\' class=\'mainDivide\'><img src=\'images/shim.gif\'></td></tr><tr><td class=\'mainRight\' style=\'vertical-align:top;\'>Comments:</td><td height=\'50\' width=\'550\' class=\'mainRight\'><label><textarea name=\'textfield\' id=\'wuComments\' style=\'width:260px;height:60px;\'  ></textarea></label></td></tr><tr><td colspan=\'3\' class=\'mainDivide\'><img src=\'images/shim.gif\'></td></tr><tr><td colspan=\'3\' class=\'mainDivide\'><img src=\'images/shim.gif\'></td></tr></table></td></tr></table></td></tr><tr><td><table width=\'100%\' border=\'0\' cellspacing=\'0\' cellpadding=\'0\'><tr><td width=\'440\'><img style=\'cursor:pointer;\' src=\'images/but_Cancel.gif\' onclick=\'Modalbox.hide();\' title=\'Close\' width=\'40\' height=\'34\'><img src=\'images/but_goLeft.gif\' title=\'Go Switch\' width=\'400\' height=\'34\'></td><td align=\'right\'><img style=\'cursor:pointer;\' src=\'images/but_save.gif\' title=\'save\' name=\'save\' width=\'166\' height=\'34\' border=\'0\' id=\'save\' onMouseOver=\'MM_swapImage(\"save\",\"\",\"images/but_save_over.gif\",1)\' onMouseOut=\'MM_swapImgRestore()\' onclick=\'wuSaveData("ExitLead");\'></a></td></tr></table></td></tr></table></div>', {title: '' })
}
// fired after the modal box form sent
function onSuccessMail(result)
{
    var tName = "";
    switch (CurrentModalWindow)
    {
        case "helpRes":
            tName = "NeedHelpMailSent";
            break;
        case "helpBus":
            tName = "NeedHelpMailSent";
            break;
        case "commercial":
            tName = "CommercialUserMailSent";
            break;
        case "newConn":
            tName = "MovingHomeMailSent";
            break;
        case "3rd Request":
            tName = "NeedHelpMailSent";
            break;
    }
    window.location = "FreeText.aspx?tName=" + tName + "&Tab="+jsCurrentTab;
}
// fired if the sending fails
function onErrorMail(error){
}
// saving functions fires from the modalbox.js
var Conf = "no";
var CurrentModalWindow;
function wuSaveData(source){
    Conf = "CHECK...";
    var AllValid = true
    var Name = document.getElementById('wuName').value;
    var ContactNumber = document.getElementById('wuContactNumber').value;
    var TimeofCall = document.getElementById('wuTimeofCall').value;
    var Email = "";
    var PostCode = "";
    if(source != "3rd Request"){
        Email = document.getElementById('wuEmail').value;
        PostCode = document.getElementById('wuPostCode').value;
    }
    CurrentModalWindow = source;
    if(Name == ""){
        AllValid = false;
        Conf = "no";
        document.getElementById('ValidName').style.display = "";
    }
    if(source != "3rd Request"){
        if(PostCode == ""){
            AllValid = false;
            Conf = "no";
            document.getElementById('ValidPostCode').style.display = "";
        }
    }
    if(ContactNumber == ""){
        AllValid = false;
        Conf = "no";
        document.getElementById('ValidContactNumber').style.display = "";
    }
    if(TimeofCall == ""){
        AllValid = false;
        Conf = "no";
        document.getElementById('ValidTimeofCall').style.display = "";
    }
    if(source != "3rd Request"){
        document.getElementById('ValidEmail').style.display = "none";
        if(Email == "" || !checkEmail(Email)){
            AllValid = false;
            document.getElementById('ValidEmail').style.display = "";
        }
    }
    if(AllValid){
        Conf = "yes";
        Modalbox.hide();
    }
}
function SaveData(IsConf){
    try{
        
        if(IsConf == "yes"){ 
            if(EnqSave){
                ret = GoSwitchUtils.SaveWUEMail(document.getElementById('wuEmail').value,jsStep1DataID,jsSAreaIDs,document.getElementById(jshfPlanID).value);
            }
            else{      
                var tbComments = document.getElementById('wuComments');
                var comments = (tbComments == null ? '' : tbComments.value);
                var varPostCode = "";
                var varEmail = "";
                if(CurrentModalWindow != "3rd Request"){
                    varPostCode = $get("wuPostCode").value;
                    varEmail = $get("wuEmail").value;
                }
                
                ret = GoSwitchUtils.SendModalWindowMail(CurrentModalWindow,$get("wuName").value,'','',varPostCode,$get("wuContactNumber").value,$get("wuTimeofCall").value,varEmail,comments,jsReffererID,onSuccessMail,onErrorMail);   
            }
        }
    }
    catch(err)
    {
    //Handle errors here
    }
}
