//var wsUrl = "http://172.16.0.5/mobile/saf/rupizmobilesites/free3gmobilephones_new/";
var wsUrl = "http://www.free3gmobilephoneoffers.co.uk/"
function GetHttpObject(){
	//alert('hello');
    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 PhoneDealSearch(handset, net, incentive, lr, merch, hname, term, selection, clr, phoneType){    
    var xmlHttpHandset = GetHttpObject(); // Function Written In rupiz-compare.js  
	var incen = incentive.replace('+','@');
	incen = incen.replace(' ', '+');
	incen = incen.replace('$','');	
	
	var url =  wsUrl + "include/dealajax.asp?handset="+handset+"&net="+net+"&incentive="+incen+"&lr="+lr+"&merch="+merch+"&term="+term+"&sel="+selection+"&clr="+clr+"&phoneType="+phoneType+"&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;
			showDropdownshead(phoneType);
			CreateNetworkIcons(xmlHttpHandset.responseText);
        }else{           
			//document.getElementById("refinesearch").innerHTML = "<div class='showphone_searchn1'><select name='merchant' id='merchant' class='textfield' style='width:114px;'><option value=''>Select Merchant</option></select></div><div class='mag_left_new'>Refine Search</div><select name='network' id='network' class='textfield'><option value=''>Network</option></select> <select name='term' id='term' class='textfield'><option value=''>Term</option></select> <select name='linerental' id='linerental' class='extfield'><option value=''>Line Rental</option></select> <select name='incentive' id='incentive' class='textfield' style='width:340px;'><option value=''>Select Incentives</option></select> <select name='recordcount' id='recordcount' class='textfield'><option value='50'>50</option></select></div>";
			
			//document.getElementById("refinesearch").innerHTML ="<div id='refinesearch'>	<div class='showphone_searchn1'>	<select name='merchant' id='merchant' class='textfield' onchange='' style='width:114px;'><option value=''>Select Merchant</option></select>	</div>	<div class='mag_left_new'>Refine Search</div>	<div style='float:left; padding-top:3px;'>	<select name='network' id='network' class='textfield'><option value=''>Network</option></select>	<select name='term' id='term' class='textfield'><option value=''>Term</option></select>	<select name='linerental' id='linerental' class='textfield'><option value=''>Line Rental</option></select>	<select name='incentive' id='incentive' class='textfield' style='width:340px;'><option value=''>Select Incentives</option></select>	<select name='recordcount' id='recordcount' class='textfield'><option value='50'>50</option></select>	<input type='hidden' name='networkIdSelected' id='networkIdSelected' /><input name='btnDealSearch' type='image' id='btnDealSearch' value='dealsearch' src='images/go_btn.gif' alt='Search Deal'  align='absmiddle'/>	</div></div>";
        }        
    }      
    xmlHttpHandset.send(null);    
}

function CreateNetworkIcons(res){ 
	// alert(res);
    pattern =  '<\s*select name="network" [^>]*>(.*?)<\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);
	//alert(myArray);
    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 + "<input name='btnDealSearch' type='image' value='dealsearch' alt='" + myArray[i] + "' src = 'images/networkicons/" + myArray[i] + selectedImage + ".gif' onclick = \" SelectValue('"+ myArray[i]+"');\"/> ";
				//  = '" + myArray[i] + "';document.frmsearch.submit()
            }            
        }   
		//alert(networkImages);     
        networkImages = networkImages + "<div class='cl'></div>"; 
        
    }        
    //alert (networkImages);
    document.getElementById("divNetwork").innerHTML = networkImages;
}


function SelectValue(val){
	//alert(val);
	ln  = document.getElementById('network').options.length;
	//alert (ln);
	for (var i=0; i < ln; i++) {
		//alert(document.getElementById('network').options[i].text +"*"+ val)
		//eval("inForm.selectTest.options[i]=option" + i)
		if (document.getElementById('network').options[i].text == val) {
			document.getElementById('network').value = document.getElementById('network').options[i].value;
			//document.getElementById('btnDealSearch').value = "dealsearch";
			//document.getElementById('network').options[i].selected = true;
		}
	}
	//alert(document.getElementById('btnDealSearch').value);
}

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;
}

function submitPhoneType(phonename){
	if(document.formphtype.phoneType.value=="Contract")	{
		document.formphtype.action = "Phone_Deal.asp?Ph=" + phonename;
		document.formphtype.submit();}
	else if(document.formphtype.phoneType.value=="PayG"){
		document.formphtype.action = "phone_dealpayg.asp?Ph=" + phonename;
		document.formphtype.submit();}
	else if(document.formphtype.phoneType.value=="SimFree"){
		document.formphtype.action = "phone_dealsimfree.asp?Ph=" + phonename;
		document.formphtype.submit();}
}

function submitPhoneType1(phonename,net){
	if(document.frmrrefinesearch.phoneType.value=="Contract"){
		document.frmrrefinesearch.action = "Phone_Deal.asp?Ph="+phonename+"&net"+net;
		document.frmrrefinesearch.submit();}
	else if(document.frmrrefinesearch.phoneType.value=="PayG"){
		document.frmrrefinesearch.action = "phone_dealpayg.asp?Ph=" + phonename;
		document.frmrrefinesearch.submit();}
	else if(document.frmrrefinesearch.phoneType.value=="SimFree"){
		document.frmrrefinesearch.action = "phone_dealsimfree.asp?Ph=" + phonename;
		document.frmrrefinesearch.submit();}
}