// www.free3gmobilephoneoffers.co.uk
// Created By : Raman Sachin Omar
// Created On : 23rd April, 2010
// Mobile Search

// ChangeTab Desc+Keyfeatures
 function ChangeTab(id){
    document.getElementById('divDescription').style.display="none";
    document.getElementById('divKeyfeatures').style.display="none";
    document.getElementById(id).style.display ="";
    
    //alert(document.getElementById("imgDescription").src )
    if (id == "divDescription"){
        document.getElementById("imgDescription").src = "images/deal_des_tab.gif";
        document.getElementById("imgKeyfeatures").src = "images/deal_feat_tab.gif";
    }else{
        document.getElementById("imgDescription").src = "images/deal_des_tab_selected.gif";
        document.getElementById("imgKeyfeatures").src = "images/deal_feat_tab_selected.gif";     
    } 
}

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 + "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);
    //alert(xmlHttpHandset);
    xmlHttpHandset.open("GET", url, true);   
    xmlHttpHandset.onreadystatechange = function() {
        //alert(xmlHttpHandset.readyState+"*"+xmlHttpHandset.status);
        if(xmlHttpHandset.readyState == 4 && xmlHttpHandset.status== 200){
            //alert("Hi")      
            MaskedHandsetByType(deal, handset);
            //alert(xmlHttpHandset.responseText);
            document.getElementById("RefineSearch").innerHTML = xmlHttpHandset.responseText;
            CreateNetworkIcons(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 BindAdvanceSearchGiftHandset(deal, net, min, txt, freegift, handset, lr, merch, cd, pd, sd, hname, term, pr, selection, clr, plan){      
    var xmlHttpHandset = GetHttpObject();    
    var url =  wsUrl + "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("RefineSearch").innerHTML = xmlHttpHandset.responseText;
            CreateNetworkIcons(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); 
    //document.forms[0].submit();   
}

// Masked Handset By Type In Deal Page
function MaskedHandsetByType(dealType, handset){   
    try {     
        xmlHttpHandset = GetHttpObject(); 
        var url =  wsUrl + "getadvancesearch.aspx?dealtype=" + dealType + "&handset=" + handset + "&k=" + Math.random();;
        xmlHttpHandset.open("GET", url, true);
        //alert(url);
        xmlHttpHandset.onreadystatechange = function() {
            if(xmlHttpHandset.readyState == 4 ){             
                //alert( xmlHttpHandset.responseText);
                //alert(document.getElementById("ddlOtherHandsets").innerHTML)
                document.getElementById("spnOtherHandsets").innerHTML = xmlHttpHandset.responseText;
            }else{
                document.getElementById("spnOtherHandsets").innerHTML ="<select name=\"ddlOtherHandsets\" id=\"ddlOtherHandsets\" class=\"textfield\"><option>Loading Handsets...</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 ChangeOtherHandset(handset, dealType){
    //var handsets = handset.replace(/ /g,"-");
    if (dealType == "c"){
        document.getElementById("hidDealType").value = "c";
        document.forms[0].action = wsUrl+handset+"-deals.aspx";
    } else if (dealType == "p"){
        document.getElementById("hidDealType").value = "p";
        document.forms[0].action = wsUrl+handset+"-deals.aspx";
    }else if (dealType == "s"){
        document.getElementById("hidDealType").value = "s";
        document.forms[0].action = wsUrl+handset+"-deals.aspx";
    }else if (dealType == "cl"){
        document.getElementById("hidDealType").value = "cl";
        document.forms[0].action = wsUrl+handset+"-deals.aspx";
    }else if (dealType == "flr12" ){
        document.getElementById("hidDealType").value = "flr12";
        document.forms[0].action = wsUrl+handset+"-deals.aspx";
    }else if (dealType == "g" ){
        document.getElementById("hidDealType").value = "g";
        document.forms[0].action = wsUrl+handset+"-deals.aspx";
    }else if (dealType == "u"){
        document.getElementById("hidDealType").value = "u";  
        document.forms[0].action = wsUrl+handset+"-deals.aspx";      
    }
    //alert(document.getElementById("hidDealType").value);
    document.forms[0].submit();
}

function CreateNetworkIcons(res){ 	 
    pattern =  '<\s*select name="ddlASNetwork" [^>]*>(.*?)<\s*/\s*select>';
    attributes = 'm';  

    var txt=new RegExp(pattern,'g')
    var refineOptions = res.match(txt);  

	//alert(refineOptions);
    var re = new RegExp('<option value="(.*?)"*.>(.*?)</option>', 'g');
    var myArray = refineOptions[0].match(re);
    
    var re2 = new RegExp('"([\\d]+)"','gim');
    var myArray2 = myArray.toString().match(re2);
	// alert(myArray);
    var networkImages = "";
    var selectedImage = "";
    // alert(myArray2);
    
    //for (j=0;j< myArray2.length; j++){
    //   // alert(myArray2[j]);    
    //}
 
    if ( myArray != null) {
        for ( i = 1; i < myArray.length; i++ ) { 
            if (myArray[i].search("selected")>-1)
                selectedImage = "_s";
            else
                selectedImage = "";
            myArray[i] = stripHTML(myArray[i]);
            if (myArray[i] != "" ){           
                if (myArray2[i-2] == undefined && myArray2[i-2] == null){
                    netId = "0";
                }else{
                    netId = myArray2[i-2].replace(/"/g,'');
                }
                //networkImages = networkImages + "<input name='btnDealSearch' id='btnDealSearch' type='image' value='dealsearch' alt='" + myArray[i] + "' src = 'images/networkicons/" + myArray[i] + selectedImage + ".gif' onclick = \" SelectValue('"+ myArray[i]+"');\"/> ";
				networkImages = networkImages + "<img alt='" + myArray[i] + "' src = 'images/networkicons/" + myArray[i] + selectedImage + ".gif' align='absbottom' onclick = \"document.getElementById('ddlASNetwork').value = '" + netId + "';document.getElementById('ctl00_f3g_networkIdSelected').value = '" + netId + "'; document.aspnetForm.submit();\"/> "; 
				//  = '" + myArray[i] + "';document.frmsearch.submit()
			
            }            
        }   
		//alert(networkImages);     
        networkImages = networkImages + "<div class='clear_both'><!-- --></div>"; 
        
    }        
   //alert (networkImages);
    document.getElementById("divNetwork").innerHTML = networkImages;
    //alert("hi");
    //document.getElementById("divNetwork").innerHTML ='';
}

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;
}


var theForm = document.forms['aspnetForm'];
if (!theForm) {
    theForm = document.aspnetForm;
}
function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}

