﻿// Rupiz Compare :: Mobile Javascript
// Created By   : Raman Sachin Omar
// Created On   : 23th Feb, 2008    
 
// Change Tab on Deal Page ..
function ChangeDealTab(dealId, featureId, tab){
    // alert(dealId +"*"+ featureId +"*" + tab);
    // alert (dealId +" * "+ featureId +" * "+ tab);    
    var browserName = BrowserName();

    document.getElementById(dealId).style.display= "none";     
    document.getElementById(featureId).style.display= "none";
    
    if (browserName == "Chrome" || browserName == "Firefox")
        document.getElementById(tab).style.display= "table";

    else if (browserName == "Microsoft Internet Explorer")
        document.getElementById(tab).style.display= "block";
    
    else
        document.getElementById(tab).style.display= "table";

    if (tab == dealId){         
        document.getElementById('aDeals').className = "selected";
        document.getElementById('aFeatures').className = "";
    }else{
        document.getElementById('aDeals').className = "";
        document.getElementById('aFeatures').className = "selected";
    }    
}

// For Mobile Home Page Search
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 == 'c'){
            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);    
} 

// For Mobile Home Page Search
function GetSNetwork(type, manu, hid){ 
   // alert (type);
     xmlHttpHandset = GetHttpObject(); // Function Written In rupiz-compare.js  
     var url =  wsUrl + "mobile/get-search-result.aspx?c=1&t=" + type + "&m=" + manu + "&hid=" + hid;
    
    if (navigator.userAgent.indexOf("Chrome")== -1){            
        xmlHttpHandset.open("GET", url, true);            
    }
    else{
        xmlHttpHandset.open("GET", url, true);              
    }  

    xmlHttpHandset.onreadystatechange = function() {
        if(type == 'c'){
            if(xmlHttpHandset.readyState == 4 ){  
                document.getElementById("spnCNetwork").innerHTML = xmlHttpHandset.responseText;
            }
            else{
                document.getElementById("spnCNetwork").innerHTML = "<select name = 'ddlNContract' id = 'ddlNContract' style='width: 130px;'><option>Loading...</option>";
            }
        }
        else if(type == 'p'){
            if(xmlHttpHandset.readyState == 4){  
                document.getElementById("spnPNetwork").innerHTML = xmlHttpHandset.responseText;
            }else{
                document.getElementById("spnPNetwork").innerHTML = "<select name = 'ddlNPayg' id = 'ddlNPayg' style='width: 130px;'><option>Loading...</option>";
        }
     }
  }    
    xmlHttpHandset.send(null);    
}   
    
function GetPriceRange(type, manu, hid){ 
     if(type=='s' && (manu != null || manu != '') && hid != null){
        document.getElementById('ctl00_cnt_ucSearch_ddlSPriceRange').style.display = "none";   
     }else if(type=='s' && (manu == null || manu == '') && hid == null){
        document.getElementById('ctl00_cnt_ucSearch_ddlSPriceRange').style.display = "block";   
     }
}

function ChangeOtherHandset(handset, dealType){
    var handsets = handset.replace(/ /g,"-");
    if (dealType == "c"){
        document.location.href = wsUrl+"mobile/phone/"+"deals.aspx?phonename=" + handsets;
    }
    else if (dealType == "p"){
        document.location.href = wsUrl+"mobile/phone/"+"deals.aspx?phonename=" + handsets + "&dealtype=payasyougo";
    }
    else if (dealType == "s"){
        document.location.href = wsUrl+"mobile/phone/"+"deals.aspx?phonename=" + handsets + "&dealtype=simfree";
    }
    else if (dealType == "cl"){
        document.location.href = wsUrl+"mobile/phone/"+"clearance.aspx?phonename=" + handsets;
    }
    else if (dealType == "flr12" ){
        document.location.href = wsUrl+"mobile/phone/"+"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.aspx?gift=" + freegifts;   
} 

// Masked Handset By Type In Deal Page
function MaskedHandsetByType(dealType, handset){   
    try {     
        xmlHttpHandset = GetHttpObject(); // Function Written In rupiz-compare.js
        var url =  wsUrl + "mobile/phone/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(errv) {
		//alert("The application cannot contact the server at the moment. Please try again in a few seconds.\nError detail: " + errv.message);
	} 
}

// Masked Other Gifts in Gift Deal Page
function GetOtherGift(freegift){   
    try {     
        //alert(freegift);
        xmlHttpHandset = GetHttpObject(); // Function Written In rupiz-compare.js
        var url =  wsUrl + "mobile/phone/getadvancesearch.aspx?gifttype=1&freegift=" + freegift + "&k=" + Math.random();;
        xmlHttpHandset.open("GET", url, true);
        xmlHttpHandset.onreadystatechange = function() {           
            if(xmlHttpHandset.readyState == 4 && xmlHttpHandset.status == 200){           
                //alert(xmlHttpHandset.responseText);  
                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(errv) {
		//alert("The application cannot contact the server at the moment. Please try again in a few seconds.\nError detail: " + errv.message);
	} 
}
    
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/phone/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.write(xmlHttpHandset.responseText);
             document.getElementById("divAdvSearch").innerHTML = xmlHttpHandset.responseText;
             CreateNetworkIcons(xmlHttpHandset.responseText, freegift);
             
           
        }else{
             document.getElementById("divAdvSearch").innerHTML = "<div class=\"left1\"><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 CreateNetworkIcons(res, freegift){ 
    //alert (res);
    pattern =  '<\s*select name="ddlASNetwork" [^>]*>(.*?)<\s*/\s*select>';
    attributes = 'm';  

    var txt=new RegExp(pattern,'g')
    var refineOptions = res.match(txt);  

    var re = new RegExp('<option value ="(.*?)"*.>(.*?)</option>', 'g');
    var myArray = refineOptions[0].match(re);

    var networkImages = "";
    var selectedImage = "";

    if ( myArray != null) {
        for ( i = 0; i < myArray.length; i++ ) { 
            if (myArray[i].search("selected")>-1)
                selectedImage = "_s";
            else
                selectedImage = "";
            myArray[i] = stripHTML(myArray[i]);
            if (myArray[i] != "Network"){           
                networkImages = networkImages + "<img alt='" + myArray[i] + "' src = '../../images/telecom/networks/icons/" + myArray[i] + selectedImage + ".gif' onclick = \"document.getElementById('ddlASNetwork').value = '" + myArray[i] + "';document.getElementById('networkIdSelected').value = '" + myArray[i] + "'; document.aspnetForm.submit();\"/>";         
            }            
        }        
        networkImages = networkImages + "<div class='cl'></div>";        
    }        
    //alert (networkImages);
    document.getElementById("divNetwork").innerHTML = networkImages;
}

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){     
    //alert("hi");   
    var xmlHttpHandset = GetHttpObject(); // Function Written In rupiz-compare.js  
    //alert("hi");
    var url =  wsUrl + "mobile/phone/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(url);
    xmlHttpHandset.open("GET", url, true);              
    xmlHttpHandset.onreadystatechange = function() {
        if(xmlHttpHandset.readyState == 4 && xmlHttpHandset.status== 200){
            MaskedHandsetByType(deal, handset);
            //alert(xmlHttpHandset.responseText);
            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);    
}