  
function initialize() {
	if(document.getElementById('brand').selectedIndex != 0){
		ajaxFunction();
	}
}

function initSearch(phone){
	if(document.getElementById('brand').selectedIndex != 0){
		ajaxSearchBrand(phone);
	}
}

var ajaxRequest;

function ajaxReq(){
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser is not compatible!");
				return false;
			}
		}
	}		
}

function ajaxModel(){
	
	if(document.getElementById("phones").selectedIndex == 0){
		document.getElementById("model_image_show").style.display = "none";
		document.getElementById("model_image_show").innerHTML = '&nbsp;';
	}else{
		ajaxReq();
				
		ajaxRequest.onreadystatechange = function(){
			if(ajaxRequest.readyState == 4){
				var modelResponse = ajaxRequest.responseText;
							
				document.getElementById("model_image_show").innerHTML = '<img src="http://www.rightmobilephone.co.uk/img/mini-load.gif" name="model_image" id="model_image" height="75" />';
				document.getElementById("model_image").src = modelResponse;
				document.getElementById("model_image_show").style.display = "block";
			}
		}
			
		var brandId = document.getElementById('brand').value;
		var phoneId = document.getElementById('phones').value;
		var queryString = "?id=" + brandId + "&phone=" + phoneId;
		
		ajaxRequest.open("GET", "/script/phone-image.php" + queryString, true);
		ajaxRequest.send(null);
	}
}

function ajaxFunction(){
	modelSelect = document.getElementById('phones');
	
	modelSelect.options.length = 0;				
	modelSelect.options[0]=new Option("[Loading... Please Wait]", "");			

	ajaxReq();
	
	document.getElementById("model_image_show").style.display = "none";	
	
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			
			var modelResponse = ajaxRequest.responseText;
			
			modelSelect.options.length = 0;				
			
			if(document.getElementById('brand').selectedIndex != 0){
				modelSelect.disabled = false;
				modelSelect.options[0]=new Option("[Please Select Model]", "");			
	
				modelArray = modelResponse.split("|");
	
				for (i = 1; (i-1) < modelArray.length; i++){
					modelSelect.options[i]=new Option(modelArray[(i-1)].split(",")[1], modelArray[(i-1)].split(",")[0]);
				}
				
			}else{
				modelSelect.disabled = true;
				modelSelect.options[0]=new Option("[Any Mobile Phone]", "");
			}			
		}
	}
		
	var brandId = document.getElementById('brand').value;
	var queryString = "?id=" + brandId;
	
	ajaxRequest.open("GET", "/script/phone-models.php" + queryString, true);
	ajaxRequest.send(null);
}

function ajaxSearchModel(){
	
	if(document.getElementById("phones").selectedIndex == 0){
		
	}else{
		ajaxReq();
				
		ajaxRequest.onreadystatechange = function(){
			if(ajaxRequest.readyState == 4){
				var modelResponse = ajaxRequest.responseText;
			}
		}
			
		var brandId = document.getElementById('brand').value;
		var phoneId = document.getElementById('phones').value;
		var queryString = "?id=" + brandId + "&phone=" + phoneId;
		
		ajaxRequest.open("GET", "/script/phone-image.php" + queryString, true);
		ajaxRequest.send(null);
	}
}

function ajaxSearchBrand(phone){
	ajaxReq();
		
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			
			var modelResponse = ajaxRequest.responseText;
			
			modelSelect = document.getElementById('phones');
			modelSelect.options.length = 0;				
			
			if(document.getElementById('brand').selectedIndex != 0){
				modelSelect.disabled = false;
				modelSelect.options[0]=new Option("[Please Select Model]", "");			
	
				modelArray = modelResponse.split("|");
	
				index = 0;
	
				for (i = 1; (i-1) < modelArray.length; i++){
					if(modelArray[(i-1)].split(",")[0] == phone){
						index = i;	
					}
					
					modelSelect.options[i]=new Option(modelArray[(i-1)].split(",")[1], modelArray[(i-1)].split(",")[0]);
				}
				
				if(index != 0){
					modelSelect.selectedIndex = index;
				}
				
			}else{
				modelSelect.disabled = true;
				modelSelect.options[0]=new Option("[Any Mobile Phone]", "");
			}			
		}
	}
		
	var brandId = document.getElementById('brand').value;
	var queryString = "?id=" + brandId;
	
	ajaxRequest.open("GET", "/script/phone-models.php" + queryString, true);
	ajaxRequest.send(null);
}

function getElm(elm)
{
	return document.getElementById(elm);
}

function getTag(xml,param)
{
	return xml.getElementsByTagName(param);
}

function reviewCounter() {
	getElm('counter').value = getElm('review').value.length;
}

function confirmSubmit()
{	
	if((getElm('modelid').selectedIndex==0) && (getElm('other').value=='')){
		alert('Please select model or enter your phone model name in the "Other" field provided and try again!');
		return false;
	}else{
		if(getElm('review').value.length < 250){
			alert('Your mobile phone review must have a minimum of 250 characters to pass editorial guidelines! You only have '+(250-getElm('review').value.length)+' characters to go. Please click OK and continue!');
			return false;
		}else{
			return true;
		}
	}
}

function callServ(address, callback)
{
	function callFunction()
	{
		if(req1.readyState==4)
		{
				if(req1.status==200)
				{
						if(processXML!=null)
						{
							//alert(req1.responseText);
							processXML(req1.responseXML);
						}
				}
		}

	}

	var req1 = null;
	var processXML = callback;

	if(typeof XMLHttpRequest != "undefined")
	   req1 =  new XMLHttpRequest();
	else if(window.ActiveXObject)
	{
	  var ajs = ["MSXML2.XMLHttp.5.0","MSXML2.XMLHttp.4.0","MSXML2.XMLHttp.3.0","MSXML2.XMLHttp","Microsoft.XMLHttp"];

	  for(var i=0; i<ajs.length;i++)
	  {
		try
		{
			req1 = new ActiveXObject(ajs[i]);
			break;
		}
		catch(oError) {
			//
		}
	  }
	}

	req1.onreadystatechange = callFunction;
	req1.open('GET', address, true);
	req1.send(null);
}

function urlencode(str) {
str = escape(str);
str = str.replace('+', '%2B');
str = str.replace('%20', '+');
str = str.replace('*', '%2A');
str = str.replace('/', '%2F');
str = str.replace('@', '%40');
return str;
}

function copy_clip(meintext)
{
 if (window.clipboardData) 
   {
   
   window.clipboardData.setData("Text", meintext);
   
   }
   else if (window.netscape) 
   { 
   
   netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
   
   var clip = Components.classes['@mozilla.org/widget/clipboard;1']
                 .createInstance(Components.interfaces.nsIClipboard);
   if (!clip) return;
   
   var trans = Components.classes['@mozilla.org/widget/transferable;1']
                  .createInstance(Components.interfaces.nsITransferable);
   if (!trans) return;
   
   trans.addDataFlavor('text/unicode');
   
   var str = new Object();
   var len = new Object();
   
   var str = Components.classes["@mozilla.org/supports-string;1"]
                .createInstance(Components.interfaces.nsISupportsString);
   
   var copytext=meintext;
   
   str.data=copytext;
   
   trans.setTransferData("text/unicode",str,copytext.length*2);
   
   var clipid=Components.interfaces.nsIClipboard;
   
   if (!clip) return false;
   
   clip.setData(trans,null,clipid.kGlobalClipboard);
   
   }
   return false;
}


styletype='table-row';
version=0;
if (navigator.appVersion.indexOf("MSIE")!=-1){
	temp=navigator.appVersion.split("MSIE");
	version=parseFloat(temp[1]);
}

if(version>=5.5){
	styletype='block';
}

siteurl = 'http://www.rightmobilephone.co.uk';

function loadmodels(manufacturerid){
	if(manufacturerid != ""){
		callServ(siteurl+'/xml.php?xml=loadmodels&manufacturerid='+manufacturerid,displaymodels);
	}else{
		getElm('modelid').disabled = true;
		getElm('modelid').options.length = 0;				
		getElm('modelid').options[0]=new Option("[Please Select Manufacturer]", "");			
	}
	getElm('displayphone').style.display = 'none';		
}

function displaymodels(xml){
	getElm('modelid').options.length = 0;				
	getElm('modelid').disabled = false;
	getElm('modelid').options[0]=new Option("[Please Select Model]", "");			

	i = 0;
	
	while(i < getTag(xml,'totalmodels')[0].firstChild.data){		
		getElm('modelid').options[i+1]=new Option(getTag(xml,'model')[i].firstChild.data,getTag(xml,'id')[i].firstChild.data);
		
		i++;
	}
}

function loadmodelpicture(modelid){
	if(modelid != ""){
		callServ(siteurl+'/xml.php?xml=loadmodelpictures&modelid='+modelid,displaymodelpicture);
	}else{
		getElm('displayphone').style.display = 'none';
	}
}

function displaymodelpicture(xml){
	getElm('displayphoneimage').src = siteurl+'/img/mobile-phone/'+getTag(xml,'imagesmall')[0].firstChild.data;
	getElm('displayphone').style.display = 'block';
}

function loadreviewpage(){
	callServ(siteurl+'/xml.php?xml=loadmodelname&modelid='+getElm('modelid').value,gotoreviewpage);
}

function gotoreviewpage(xml){	
	document.location.href=getTag(xml,'reviewurl')[0].firstChild.data;
}

function initcontractsearch(manufacturerid,modelid){
	if(manufacturerid!=''&&modelid!=''){		
		callServ(siteurl+'/xml/?xml=loadmodels&manufacturerid='+manufacturerid+'&modelid='+modelid,updatedisplaymodels);
	}
}

function updatedisplaymodels(xml){	
	totalmodels = getTag(xml,'totalmodels')[0].firstChild.data;
	modelselectedid = getTag(xml,'modelselectedid')[0].firstChild.data;
	
	i=0;
	
	index = '';

	getElm('phones').options.length = 0;				
	getElm('phones').disabled = false;
	getElm('phones').options[0]=new Option("[Please Select Model]", "");			

	while(i<totalmodels){
		getElm('phones').options[i+1]=new Option(getTag(xml,'name')[i].firstChild.data,getTag(xml,'id')[i].firstChild.data);
		if(modelselectedid == getTag(xml,'id')[i].firstChild.data){
			index = i+1;
		}
		
		i++;
	}
	
	if(index!=''){
		getElm('phones').selectedIndex = index;
	}
}

function ct_loadmodels(manufacturerid,modelid){
	if(manufacturerid != ""){
		callServ(siteurl+'/xml.php?xml=loadcmodels&manufacturerid='+manufacturerid+'&modelid='+modelid,ct_updatemodels);
	}else{
		getElm('phones').disabled = true;
		getElm('phones').options.length = 0;				
		getElm('phones').options[0]=new Option("[Any Model]", "");			
	}
}
function ct_updatemodels(xml){
	getElm('phones').options.length = 0;				
	getElm('phones').disabled = false;
	getElm('phones').options[0]=new Option("[Select Model]", "");	
	
	var preselected = getTag(xml,'preselected')[0].firstChild.data;
	var preselectid = '';
	var index = '';
	if(preselected=='true'){
		preselectid = getTag(xml,'preselectid')[0].firstChild.data;
	}

	i = 0;
	
	while(i < getTag(xml,'totalmodels')[0].firstChild.data){		
		getElm('phones').options[i+1]=new Option(getTag(xml,'model')[i].firstChild.data,getTag(xml,'id')[i].firstChild.data);
		
		if(preselected=='true'&&preselectid==getTag(xml,'id')[i].firstChild.data){			
			index = i+1;
		}
		
		i++;
	}
	
	if(index!=''){
		getElm('phones').selectedIndex = index;
	}	
}
function ct_init(manufacturerid, modelid){
	if(manufacturerid!=''){		
		ct_loadmodels(manufacturerid,modelid);
	}
}
