﻿
//var wsUrl = "http://localhost/ShopCornUK/";
//var wsUrl = "http://localhost:2857/shopcorn.co.uk/";
//var wsUrl = "http://172.16.13.61/ShopCornUK/";

function GetHttpObject(){
var xmlHttpObj;
    try {   
        xmlHttpObj = new XMLHttpRequest();              
        return xmlHttpObj;
    }
    catch (e){        
        try{
            xmlHttpObj = new ActiveXObject("Msxml2.XMLHTTP");                   
            return xmlHttpObj; 
        }
        catch (e){           
            try{
                xmlHttpObj = new ActiveXObject("Microsoft.XMLHTTP");                      
                return xmlHttpObj;      
            }
            catch (e){        
                alert("Sorry, Your browser does not support AJAX!");        
                return null;        
            }     
        }   
    }     
} 

function ChangeOtherHandset(handset, dealType){
    var handsets = handset.replace(/ /g,"-");
    if (dealType == "c"){
        document.location.href = "mobile-deals.aspx?phonename=" + handsets;
    }
    else if (dealType == "p"){
        document.location.href = "mobile-deals.aspx?phonename=" + handsets + "&dealtype=payasyougo";
    }
    else if (dealType == "s"){
        document.location.href = "mobile-deals.aspx?phonename=" + handsets + "&dealtype=simfree";
    }
    else if (dealType == "cl"){
        document.location.href = "mobile-clearance.aspx?phonename=" + handsets;
    }
    else if (dealType == "flr12" ){
        document.location.href = "mobile-deals.aspx?phonename=" + handsets + "&contractmonths=12";
    } 
    else if (dealType == "u"){
        document.forms[0].submit();
    }
} 

function ChangeFreeGift(freegift){
    var freegifts = freegift.replace(/ /g,"-");    
    document.location.href = "free-gift-deals.aspx?gift=" + freegifts;   
} 

/* Masked Handset By Type In Deal Page */
function MaskedHandsetByType(dealType, handset){   
    try {     
        xmlHttpHandset = GetHttpObject();
        var url =  wsUrl + "mobile/getadvancesearch.aspx?dealtype=" + dealType + "&handset=" + handset + "&k=" + Math.random();;
        xmlHttpHandset.open("GET", url, true);
        xmlHttpHandset.onreadystatechange = function() {
            if(xmlHttpHandset.readyState == 4 ){             
                document.getElementById("spnOtherPhone").innerHTML = "Other Phones " + xmlHttpHandset.responseText;
            }else{
                document.getElementById("spnOtherPhone").innerHTML ="Other Phones <select name=\"ddlHandset\" id=\"ddlHandset\"><option>Loading...</option></select>"
            }            
        }    
        xmlHttpHandset.send(null);
    }catch(ex) {} 
}

/* Masked Handset By Type In Deal Page */
function MaskedHandsetByNetwork(network, handset){   
    try {     
        xmlHttpHandset = GetHttpObject();
        var url =  wsUrl + "mobile/getadvancesearch.aspx?nt=" + network + "&handset=" + handset + "&k=" + Math.random();
        xmlHttpHandset.open("GET", url, true);
        xmlHttpHandset.onreadystatechange = function() {
            if(xmlHttpHandset.readyState == 4 ){             
                document.getElementById("spnOtherPhone").innerHTML = "Other Phones " + xmlHttpHandset.responseText;
            }else{
                document.getElementById("spnOtherPhone").innerHTML ="Other Phones <select name=\"ddlHandset\" id=\"ddlHandset\"><option>Loading...</option></select>"
            }            
        }    
        xmlHttpHandset.send(null);
    }catch(ex) {} 
}

/* Masked Other Gifts in Gift Deal Page */
function GetOtherGift(freegift){   
    try {     
        xmlHttpHandset = GetHttpObject();
        var url =  wsUrl + "mobile/getadvancesearch.aspx?gifttype=1&freegift=" + freegift + "&k=" + Math.random();;
        xmlHttpHandset.open("GET", url, true);
        xmlHttpHandset.onreadystatechange = function() {           
            if(xmlHttpHandset.readyState == 4 && xmlHttpHandset.status == 200){             
                document.getElementById("spnOtherGift").innerHTML = "Change free gifts " + xmlHttpHandset.responseText;
            }else{
                document.getElementById("spnOtherGift").innerHTML = "Change Free gifts <select><option>Loading Free Gifts</option></select>";
            }            
        }    
        xmlHttpHandset.send(null);
    }catch(ex) {} 
}
    
function wait(msecs){
    var start = new Date().getTime();
    var cur = start
    while(cur - start < msecs){        
        cur = new Date().getTime();
    }
} 

function GetInnerText (node){
    return (node.textContent || node.innerText || node.text) ;
}

// window.alert = function(){return null;}

/* Gift Deal Page */
function GiftAdvanceSearch(freegift, handset, cost, net, term, min, txt, lr, merch, dc, selection){        
    var xmlHttpHandset = GetHttpObject(); // Function Written In rupiz-compare.js  
    var url =  wsUrl + "mobile/getadvancesearch.aspx?deal=fg&freegift="+freegift+"&handset="+handset+"&cost="+cost+"&net="+net+"&term="+term+"&min="+min+"&txt="+txt+"&lr="+lr+"&merch="+merch+"&dc="+dc+"&sel="+selection+"&k="+Math.random(); 

    xmlHttpHandset.open("GET", url, true);              
    xmlHttpHandset.onreadystatechange = function() {
        if(xmlHttpHandset.readyState == 4 && xmlHttpHandset.status== 200){             
             GetOtherGift(freegift);
             document.getElementById("divAdvSearch").innerHTML = xmlHttpHandset.responseText;      
        }else{
             document.getElementById("divAdvSearch").innerHTML = "<div class=\"left_m\"><select name=\"ddlASHandset\" id=\"ddlASHandset\" class='ddl_230'><option value=\"\">Change Handset</option></select></div> <div class=\"gift_search_bar\"><select id=\"ddlASHandsetCost\" name=\"ddlASHandsetCost\" class='ddl_60'><option value=\"\">Cost</option></select> <select name=\"ddlASNetwork\" id=\"ddlASNetwork\" class='ddl_75'><option value=\"\">Network</option></select> <select name=\"ddlASContractMonths\" id=\"ddlASContractMonths\" class='ddl_60'><option value=\"\">Term</option></select> <select name=\"ddlASMinutes\" id=\"ddlASMinutes\" class='ddl_75'><option value=\"\">Minutes</option></select> <select name=\"ddlASTexts\" id=\"ddlASTexts\" class='ddl_75'><option value=\"\">Texts</option></select> <select name=\"ddlASLinerental\" id=\"ddlASLinerental\" class='ddl_85'><option value=\"\">Line Rental</option></select> <select name=\"ddlASMerchant\" id=\"ddlASMerchant\" class='ddl_125'><option value=\"\">Select Merchant</option></select></div>";           
        }            
    }      
    xmlHttpHandset.send(null);    
}

function SubmitForm(){
    document.forms[0].hidHandset.value = 'true';
    document.forms[0].submit();  
}

function stripHTML(oldString) {
    var newString = "";
    var inTag = false;
    for(var i = 0; i < oldString.length; i++) {
        if(oldString.charAt(i) == '<') inTag = true;
        if(oldString.charAt(i) == '>') {
            if(oldString.charAt(i+1)=="<"){
                //dont do anything
            }else{
                inTag = false;
                i++;
            }
        }
        if(!inTag) newString += oldString.charAt(i);
    }
    return newString;
}

/* Handset Deal Page */
function BindAdvanceSearch(deal, net, min, txt, freegift, handset, lr, merch, cd, pd, sd, hname, term, pr, selection, clr, plan){      
    var xmlHttpHandset = GetHttpObject();    
    var url =  wsUrl + "mobile/getadvancesearch.aspx?deal="+deal+"&net="+net+"&min="+min+"&txt="+txt+"&freegift="+freegift+"&handset="+handset+"&lr="+lr+"&cd="+cd+"&pd="+pd+"&sd="+sd+"&merch="+merch+"&term="+term+"&pr="+pr+"&sel="+selection+"&clr="+clr+"&plan="+plan+"&k="+Math.random();
    //alert(wsUrl);
    //alert(url);
    xmlHttpHandset.open("GET", url, true);   
    xmlHttpHandset.onreadystatechange = function() {
        if(xmlHttpHandset.readyState == 4 && xmlHttpHandset.status== 200){
           // alert (deal);
           // alert (handset);
            MaskedHandsetByType(deal, handset);
            document.getElementById("divAdvSearch").innerHTML = xmlHttpHandset.responseText;
        }else{
            if (deal == "c" || deal == "flr12")
                document.getElementById("divAdvSearch").innerHTML = "<select name='ddlASDealType' id='ddlASDealType' class='ddl_100'><option value=''>Contract</option></select> <select name='ddlASNetwork' id='ddlASNetwork' class='ddl_110'><option value=''>Network</option></select> <select name='ddlASContractMonths' id='ddlASContractMonths' class='ddl_70'><option value=''>Term</option></select> <select name='ddlASMinutes' id='ddlASMinutes' class='ddl_80'><option value=''>Minutes</option></select> <select name='ddlASTexts' id='ddlASTexts' class='ddl_80'><option value=''>Texts</option></select> <select name='ddlASLinerental' id='ddlASLinerental' class='ddl_90'><option value=''>Line Rental</option></select> <select name='ddlASMerchant' id='ddlASMerchant' class='ddl_120'><option value=''>Select Merchant</option></select>";
        }
        
    }      
    xmlHttpHandset.send(null);    
}

function GetInnerText (node) {
    return (node.textContent || node.innerText || node.text) ;
}

function ValidateNoDeal(email){   
    try{
        var _email = document.getElementById(email);                      
        if (_email.value == ""){
            alert("Kindly enter your email."); 
            _email.focus();       
            return false;
        }
        else if (_email.value != "" && !checkemail(_email.value)){
            alert("Kindly enter a valid email."); 
            _email.focus();       
            return false;
        }     
        else
            return true;
    }
    catch (err){}
}

function menu2(){
    var z = document.getElementById("show1");
    z.style.display = "block"; 
}

function menu3(){
    var z = document.getElementById("show1");
    z.style.display = "none"; 
}

var args, stopTime;    
function getposOffset(overlay, offsettype){
    var totaloffset=(offsettype=="left")? overlay.offsetLeft : overlay.offsetTop;    
    var parentEl=overlay.offsetParent;
    while (parentEl!=null){
        totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
        parentEl=parentEl.offsetParent;
    }
    return totaloffset;
}

function overlay(curobj, subobjstr, iframe, opt_position){
        
    try{
        var cntl = document.getElementById(iframe);
        if(cntl != null){
            cntl.style.display = 'block';    
        }
        if (document.getElementById){        
            var subobj = document.getElementById(subobjstr);            
            subobj.style.display = (subobj.style.display!="block")? "block" : "none";
            curobj = document.getElementById(curobj)  ;
            var xpos=getposOffset(curobj, "left") +((typeof opt_position!="" && opt_position.indexOf("right")!=-1)? -(subobj.offsetWidth-curobj.offsetWidth) : 0) 
            var ypos=getposOffset(curobj, "top") +((typeof opt_position!="" && opt_position.indexOf("bottom")!=-1)? curobj.offsetHeight : 0)
            subobj.style.left = xpos + "px";
            subobj.style.top = ypos + 100 + "px";            
            //alert(subobj.style.right);
            document.getElementById(iframe).style.left = subobj.style.left;
            document.getElementById(iframe).style.top = subobj.style.top;   
            document.getElementById(iframe).style.display = "";
            return false
        }
        else
        return true
    }
    catch(err){/*alert(err.description);*/}
}  

function overlayclose(subobj){
    document.getElementById(subobj).style.display="none";
    var cntl = document.getElementById('ifrm');    
    if(cntl != null){    
        cntl.style.display = 'none';    
    }
}

function mouseOver(arg){
    clearTimeout(stopTime);
    arg.style.display='block';
    var cntl = document.getElementById('ifrm');
    if(cntl != null)    {
        cntl.style.display = '';    
    }    
}

function mouseOut(arg){
    var cntl = document.getElementById('ifrm');
    if(cntl != null)    {
        cntl.style.display = 'none';    
    }
    args = arg;
    if (args.style.display != 'none'){
        stopTime = setTimeout('mouseOutTimed()',0);
    }  
}

function mouseOutTimed(){ 
    clearTimeout(stopTime);
    args.style.display='none'; 
    var cntl = document.getElementById('ifrm');
    if(cntl != null)    {
        cntl.style.display = 'none';    
    }      
}


function GetSHandset(type, manu){       
     xmlHttpSHandset = GetHttpObject(); // Function Written In rupiz-compare.js  
     var url =  wsUrl + "mobile/get-search-result.aspx?c=0&t=" + type + "&m=" + manu;
    
    if (navigator.userAgent.indexOf("Chrome")== -1){            
        xmlHttpSHandset.open("GET", url, true);            
    }
    else{
        xmlHttpSHandset.open("GET", url, false);              
    }  
    xmlHttpSHandset.onreadystatechange = function (){ 
        if(type == 'Contract'){
            if(xmlHttpSHandset.readyState == 4 ){ 
                document.getElementById("spnCHandset").innerHTML = xmlHttpSHandset.responseText;                
                GetSNetwork(type, manu,'');
            }else{
                document.getElementById("spnCHandset").innerHTML = "<select name ='ddlHContract' id = 'ddlHContract' style='width: 200px;'><option>Loading...</option>";
            }
        }
        else if(type == 'p'){
            if(xmlHttpSHandset.readyState == 4 ){ 
                document.getElementById("spnPHandset").innerHTML = xmlHttpSHandset.responseText;                
                GetSNetwork(type, manu,'');
            }else{
                document.getElementById("spnPHandset").innerHTML = "<select name ='ddlHPayg' id = 'ddlHPayg' style='width: 200px;'><option>Loading...</option>";
            }
        }
        else if(type == 's'){
            if(xmlHttpSHandset.readyState == 4 ){ 
                document.getElementById("spnSHandset").innerHTML = xmlHttpSHandset.responseText;                                    
                GetPriceRange('s',manu,null)
            }else{
                document.getElementById("spnSHandset").innerHTML = "<select name ='ddlHSimFree' id = 'ddlHSimFree' style='width: 200px;'><option>Loading...</option>";
            }
        }
    }
    xmlHttpSHandset.send(null);    
} 