// resets the drop down to 'all handsets' and enables the checkboxes
function resetHandsetType() {
   var hsetfield = document.mainsearch.idhandset;
   hsetfield.selectedIndex = 0;
   ableChange('man_',false);
   ableChange('feat_',false);
}

// disables or enables checkboxes depending on drop down value
function checkHandsetType() {
   var hsetfield = document.mainsearch.idhandset;
   if (hsetfield.options[hsetfield.selectedIndex].value == 'allhandsets') {
      ableChange('man_',false);
      ableChange('feat_',false);  
   } else {
      ableChange('man_',true);
      ableChange('feat_',true);
   }
}

// this actually does the disabling and enabling of the checkboxes
function ableChange(tag, flag) {
  document.mainsearch.allmanu.disabled = flag;
   var valid = true;
   for (var i = 1; valid == true; i++) {
     if (thisfield = document.getElementById(tag+i)) {
	    thisfield.disabled = flag;
	 } else {
	    valid = false;
	 }
  }
}

// uncheck the tickboxs on the networks if all networks is ticked
function uncheck () {
	var done = false;
    if (document.getElementById('anet').checked == true) {
        var prefix = 'net';
        for(var x = 1; done == false; x++) {
            if(theitem = document.getElementById(prefix+'_'+x)) {
                if (theitem.checked) {
                    theitem.checked = false;
                }
            } else {
                done = true;
            }
        }
    }
}
// uncheck any network selected if all networks is ticked
function uncheckallnets () {
	if (document.getElementById('anet').checked == true) {
		document.getElementById('anet').checked = false;
	} else if (document.getElementById('anet').checked == false){
		document.getElementById('anet').checked = false;
  }
}

// This checks all the checkboxes when 'all'
function checkBoxMans (tag, typeId) {
	var done = false;
    if (document.getElementById(typeId).checked == true) {
        var prefix = tag;
        for(var x = 1; done == false; x++) {
            if(theitem = document.getElementById(prefix+'_'+x)) {
                //if (theitem.checked) {
                    theitem.checked = true; // to check all boxes
                //}
            } else {
                done = true;
            }
        }
    }else if (document.getElementById(typeId).checked == false) {
        var prefix = tag;
        for(var x = 1; done == false; x++) {
            if(theitem = document.getElementById(prefix+'_'+x)) {
                //if (theitem.checked) {
                    theitem.checked = false; // to uncheck all boxes
                //}
            } else {
                done = true;
            }
        }
    }
}


// uncheck any manu selected if all networks is ticked
function uncheckallmans() {
	if (document.getElementById('allmanu').checked == true) {
		document.getElementById('allmanu').checked = false;
	} else if (document.getElementById('allmanu').checked == false){
		document.getElementById('allmanu').checked = false;
  }
}

// To check the all button if all the checboxes are selected

function selectAll() {
  document.getElementById('allmanu').checked = true;	
}

//  this is now the main ajax function you now add the url and parimeters as one link and it will call it
function GlobalCall(url) {
    var friendurl = url;
    http.open('GET', friendurl , true);
    //http.onreadystatechange = handleHttpResponse;
    http.setRequestHeader('If-Modified-Since', 'Sat, 1 Jan 2000 00:00:00 GMT');
    http.send('null');
}
// this controls what models are dispalyed in the dropdown box
function buildajax () {
	// check manufacturers

	var prefix = 'man';
	var manstring = '';
	var url = '';
	var urlstring = '';
        for(var x = 1; x<13; x++) {
            if(theitem = document.getElementById(prefix+'_'+x)) {
                if (theitem.checked == true) {
                    manstring = manstring + '&manu[]=' + theitem.value;
                }
            } 
		}
	// check networks
	var prefix = 'net';
	var netstring = '';
	var url = '';
        for(var x = 1; x<8; x++) {
		    
			//  check also if the checkbox all is selected
            if(theitem = document.getElementById(prefix+'_'+x)) {
                if (theitem.checked == true) {
                    netstring = netstring + '&nets[]=' + theitem.value;
                }
            } 
		}
	// check features
	var prefix = 'feat';
	var fetstring = '';
	var url = '';
        for(var x = 1; x<11; x++) {
		
            if(theitem = document.getElementById(prefix+'_'+x)) {
                if (theitem.checked == true) {
                    fetstring = fetstring + '&feature[]=' + theitem.value;
                }
            } 
		}
// check min mins
if (document.getElementById('m-value').value != 0 || document.getElementById('m-value').value != 'Any' ) {
minstring = '&mins='+ document.getElementById('m-value').value;	
}
// check text
if (document.getElementById('t-value').value != 0 || document.getElementById('t-value').value != 'Any' ) {
txtstring = '&text='+ document.getElementById('t-value').value;	
}
// check budget
if (document.getElementById('b-value').value != 0 || document.getElementById('b-value').value != 'Any' ) {
budstring = '&budget='+ document.getElementById('b-value').value;	
}

// check contract length
if (document.getElementById('c-value').value != 0 || document.getElementById('c-value').value != 'Any' ) {
constring = '&contract='+ document.getElementById('c-value').value;	
}
// check freegifts
var len = document.getElementById('freegifts').length;
i = 0;
chosen = "none";

for (i = 0; i < len; i++) {
if (document.getElementById('freegifts')[i].selected) {
chosen = document.getElementById('freegifts')[i].value;
}
if (chosen != '') {
freegift = '&freegifts='+chosen;
}
}

// check retailer
var lenret = document.getElementById('retailer').length;
r = 0;
chosenret = "none";

for (r = 0; r < lenret; r++) {
if (document.getElementById('retailer')[r].selected) {
chosenret = document.getElementById('retailer')[r].value;
}
if (chosenret != '') {
ret = '&retailer='+chosenret;
}
}
// check if handset has been set
var lenh = document.getElementById('idhandset').length;
ha = 0;
chosenhandset = "none";

for (ha = 0; ha < lenh; ha++) {
if (document.getElementById('idhandset')[ha].selected) {
chosenhandset = document.getElementById('idhandset')[ha].value;
}
if (chosenhandset != '') {
handset = '&idhandset='+chosenhandset;
}
}

		if (manstring != '') {
		  urlstring = urlstring + manstring; 	
		}
		
		if (netstring != '') {
			urlstring = urlstring + netstring; 
		}
		
		if (fetstring != '') {
			urlstring = urlstring + fetstring; 
		}
		
		if (minstring != '') {
		urlstring = urlstring + minstring;	
		}
		
		if (constring != '') {
		urlstring = urlstring + constring;	
		}
		if  (txtstring != '') {
		urlstring = urlstring + txtstring;	
		}
		
		if  (budstring != '') {
		urlstring = urlstring + budstring;	
		}
		
		 if (freegift != '') {
		 urlstring = urlstring + freegift;	
		}
		
		if (ret != '') {
		 urlstring = urlstring + ret;	
		}
		
		if (handset != '') {
		 urlstring = urlstring + handset;	
		}
	url = '/mobile-phones/site-find-model.php?'+ urlstring;
	//alert (url);
	GlobalCall(url);
}

// this is the main ajax function using http.responseText
function getHTTPObject() {
    var xmlhttp;
    /*@cc_on
    @if (@_jscript_version >= 5)
    try {
    xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');
    } catch (e) {
    try {
    xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
    } catch (E) {
    xmlhttp = false;
    }
    }
    @else
    xmlhttp = false;
    @end @*/
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
        try {
            xmlhttp = new XMLHttpRequest();
        } catch (e) {
            xmlhttp = false;
        }
    }
    return xmlhttp;
}
var http = getHTTPObject(); // We create the HTTP Object
