var xmlhttp;

//used on broadband page
//here you place the ids of every element you want.
var ids=new Array('q1','q2','q3','q4','q5','q6','q7');

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

//used for checking review form on home page
function reviewcheck()
{
	var w = document.reviewForm.Make.selectedIndex;
   	var selected_text = document.reviewForm.Make.options[w].text;
	if (selected_text == 'Select a Make') {
		alert('Oops you forgot to select a Make');
		return false;
	}
	else {
		return true;
	}
}

// Show hide script
function showhide(id) {

	if (document.getElementById(id).style.display == 'none') {
		showdiv(id);
		document.getElementById('link'+id).innerHTML="Hide";
	}
	else {
		hidediv(id);
		document.getElementById('link'+id).innerHTML="Show";
	}
}

//hideallids in the above array
function hideallids(){
	for (var i=0;i<ids.length;i++){
		hidediv(ids[i]);
		document.getElementById('link'+ids[i]).innerHTML="More";
	}		  
}

// question broadband hide show
function qashowhide(id) {

	if (document.getElementById(id).style.display == 'none') {
		hideallids();
		showdiv(id);
		document.getElementById('link'+id).innerHTML="Less";
	}
	else {
		hidediv(id);
		document.getElementById('link'+id).innerHTML="More";
	}
}

// payg models hide show
function paygshowhide(id) {

	if (document.getElementById('link'+id).innerHTML == "Show") {
		document.getElementById('usedid').innerHTML+=id+'|';
		var brokenstring=document.getElementById('usedid').innerHTML.split("|");
		for (var i=0;i<brokenstring.length;i++){
			if (id == brokenstring[i]) {
			
			}
			else if (brokenstring[i]=="") {
			
			}
			else {
				document.getElementById(brokenstring[i]).innerHTML="";
				document.getElementById(brokenstring[i]).style.height="0px";
				document.getElementById('link'+brokenstring[i]).innerHTML="Show";

			}
		}
		document.getElementById(id).innerHTML="<img src=\"http://www.yourphonereviews.com/images/loading.gif\">";
		document.getElementById(id).style.height="";
		getPAYGList(id);
		document.getElementById('link'+id).innerHTML="Hide";
	}
	else {
		document.getElementById(id).innerHTML="";
		document.getElementById(id).style.height="0px";
		document.getElementById('link'+id).innerHTML="Show";
	}
}

//switchid same as showdiv
function switchid(id){	
	//hideallids();
	showdiv(id);
}

// hidediv
function hidediv(id) {
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'none';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'none';
		}
		else { // IE 4
			document.all.id.style.display = 'none';
		}
	}
}

// show div
function showdiv(id) {
	
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'block';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'block';
		}
		else { // IE 4
			document.all.id.style.display = 'block';
		}
	}
	
}

// get payg models
function getPAYGList(str)
{
	xmlhttp = GetXmlHttpObject();
	if (xmlhttp == null) {
		alert("Browser does not support HTTP Request");
		return;
	}
	var url = "http://www.yourphonereviews.com/ajax/payg.php?op=returnpayglist";
	url = url + "&q=" + str;
	url = url + "&sid=" + Math.random();
	xmlhttp.onreadystatechange = function(){
		if (xmlhttp.readyState == 4) {
			document.getElementById(str).innerHTML = xmlhttp.responseText;
		}
	}
	xmlhttp.open("GET", url, true);
	xmlhttp.send(null);
}

// function used in Ajax calls
function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  return new XMLHttpRequest();
  }
if (window.ActiveXObject)
  {
  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
}

//retrieve deals models for use in drop downs
function getModels(str)
{
	xmlhttp = GetXmlHttpObject();
	if (xmlhttp == null) {
		alert("Browser does not support HTTP Request");
		return;
	}
	var url = "http://www.yourphonereviews.com/ajax/models.php?op=returnmodels";
	url = url + "&q=" + str;
	url = url + "&sid=" + Math.random();
	hidediv('model_image');
	xmlhttp.onreadystatechange = function(){
		modelSelect = document.getElementById('Model');
		modelSelect.options.length = 0;	
		if (xmlhttp.readyState == 4) {
			if (str != 0) {
				modelSelect.disabled = false;
				modelSelect.options[0] = new Option("[Please Select Model]", "");
				models = xmlhttp.responseText.split("|")
				for (i = 1; (i - 1) < models.length; i++) {
					modelSelect.options[i] = new Option(models[(i - 1)].split(",")[1], models[(i - 1)].split(",")[0]);
				}
			}
			else{
				modelSelect.disabled = true;
				modelSelect.options[0]= new Option("[Any Mobile Phone]", "");
			}	
		}
	}
	xmlhttp.open("GET", url, true);
	xmlhttp.send(null);
}

function getDealsModels1(str)
{
	xmlhttp = GetXmlHttpObject();
	if (xmlhttp == null) {
		alert("Browser does not support HTTP Request");
		return;
	}
	var url = "http://www.yourphonereviews.com/ajax/models.php?op=returndealsmodels1";
	url = url + "&q=" + str;
	url = url + "&sid=" + Math.random();
	hidediv('model_image_show');
	xmlhttp.onreadystatechange = function(){
		modelSelect = document.getElementById('ModelID');
		modelSelect.options.length = 0;	
		if (xmlhttp.readyState == 4) {
			if (str != 0) {
				
				jsn = xmlhttp.responseText;
				var res = eval('('+jsn+')');
				var i = 1;	
				modelSelect.disabled = false;
					for (var x in res.info)
					{
						modelSelect.options[i] = new Option(res.info[x].name, res.info[x].modelid);
						i++;
					}
				modelSelect.options[0] = new Option("[Please Select Model]", "");
				modelSelect.options[0].selected = "1"
			}
			else{
				modelSelect.disabled = true;
				modelSelect.options[0]= new Option("[Any Mobile Phone]", "");
			}	
		}
	}
	xmlhttp.open("GET", url, true);
	xmlhttp.send(null);
}

function getDealsModels(str)
{
	xmlhttp = GetXmlHttpObject();
	if (xmlhttp == null) {
		alert("Browser does not support HTTP Request");
		return;
	}
	var url = "http://www.yourphonereviews.com/ajax/models.php?op=returndealsmodels";
	url = url + "&q=" + str;
	url = url + "&sid=" + Math.random();
	hidediv('model_image_show');
	xmlhttp.onreadystatechange = function(){
		modelSelect = document.getElementById('ModelID');
		modelSelect.options.length = 0;	
		if (xmlhttp.readyState == 4) {
			if (str != 0) {
				modelSelect.disabled = false;
				modelSelect.options[0] = new Option("[Please Select Model]", "");
				models = xmlhttp.responseText.split("|")
				for (i = 1; (i - 1) < models.length; i++) {
					modelSelect.options[i] = new Option(models[(i - 1)].split(",")[1], models[(i - 1)].split(",")[0]);
				}
			}
			else{
				modelSelect.disabled = true;
				modelSelect.options[0]= new Option("[Any Mobile Phone]", "");
			}	
		}
	}
	xmlhttp.open("GET", url, true);
	xmlhttp.send(null);
}

function getDealsModelsMain(str)
{
	xmlhttp = GetXmlHttpObject();
	if (xmlhttp == null) {
		alert("Browser does not support HTTP Request");
		return;
	}
	var url = "http://www.yourphonereviews.com/ajax/models.php?op=returndealsmodels";
	url = url + "&q=" + str;
	url = url + "&sid=" + Math.random();
	xmlhttp.onreadystatechange = function(){
		modelSelect = document.getElementById('ModelID');
		modelSelect.options.length = 0;	
		if (xmlhttp.readyState == 4) {
			if (str != 0) {
				modelSelect.disabled = false;
				modelSelect.options[0] = new Option("[Please Select Model]", "");
				models = xmlhttp.responseText.split("|")
				for (i = 1; (i - 1) < models.length; i++) {
					modelSelect.options[i] = new Option(models[(i - 1)].split(",")[1], models[(i - 1)].split(",")[0]);
				}
			}
			else{
				modelSelect.disabled = true;
				modelSelect.options[0]= new Option("[Any Mobile Phone]", "");
			}	
		}
	}
	xmlhttp.open("GET", url, true);
	xmlhttp.send(null);
}

function getDealsModelsMain1(str)
{
	xmlhttp = GetXmlHttpObject();
	if (xmlhttp == null) {
		alert("Browser does not support HTTP Request");
		return;
	}
	var url = "http://www.yourphonereviews.com/ajax/models.php?op=returndealsmodels1";
	url = url + "&q=" + str;
	url = url + "&sid=" + Math.random();
	xmlhttp.onreadystatechange = function(){
		modelSelect = document.getElementById('ModelID');
		modelSelect.options.length = 0;	
		if (xmlhttp.readyState == 4) {
			if (str != 0) {
				
				jsn = xmlhttp.responseText;
				var res = eval('('+jsn+')');
				var i = 1;	
				modelSelect.disabled = false;
					for (var x in res.info)
					{
						modelSelect.options[i] = new Option(res.info[x].name, res.info[x].modelid);
						i++;
					}
				modelSelect.options[0] = new Option("[Please Select Model]", "");
				modelSelect.options[0].selected = "1"
			}
			else{
				modelSelect.disabled = true;
				modelSelect.options[0]= new Option("[Any Mobile Phone]", "");
			}	
		}
	}
	xmlhttp.open("GET", url, true);
	xmlhttp.send(null);
}

function getPAYGModels(str)
{
	xmlhttp = GetXmlHttpObject();
	if (xmlhttp == null) {
		alert("Browser does not support HTTP Request");
		return;
	}
	hidediv('model_image_show');
	var url = "http://www.yourphonereviews.com/ajax/payg.php?op=returnpaygmodels";
	url = url + "&q=" + str;
	url = url + "&sid=" + Math.random();
	xmlhttp.onreadystatechange = function(){
		modelSelect = document.getElementById('PAYGModel');
		modelSelect.options.length = 0;	
		if (xmlhttp.readyState == 4) {
			if (str != 0) {
				modelSelect.disabled = false;
				modelSelect.options[0] = new Option("[Please Select Model]", "");
				models = xmlhttp.responseText.split("|")
				for (i = 1; (i - 1) < models.length; i++) {
					modelSelect.options[i] = new Option(models[(i - 1)].split(",")[1], models[(i - 1)].split(",")[0]);
				}
			}
			else{
				modelSelect.disabled = true;
				modelSelect.options[0]= new Option("[Any Mobile Phone]", "");
			}	
		}
	}
	xmlhttp.open("GET", url, true);
	xmlhttp.send(null);
}

function getPAYGModels1(str)
{
	xmlhttp = GetXmlHttpObject();
	if (xmlhttp == null) {
		alert("Browser does not support HTTP Request");
		return;
	}
	var url = "http://www.yourphonereviews.com/ajax/payg.php?op=returnpaygmodels1";
	url = url + "&q=" + str;
	url = url + "&sid=" + Math.random();
	hidediv('payg_model_image_show');
	xmlhttp.onreadystatechange = function(){
		modelSelect = document.getElementById('PAYGModel');
		modelSelect.options.length = 0;	
		if (xmlhttp.readyState == 4) {
			if (str != 0) {
				
				jsn = xmlhttp.responseText;
				var res = eval('('+jsn+')');
				var i = 1;	
				modelSelect.disabled = false;
					for (var x in res.info)
					{
						modelSelect.options[i] = new Option(res.info[x].name, res.info[x].modelid);
						i++;
					}
				modelSelect.options[0] = new Option("[Please Select Model]", "");
				modelSelect.options[0].selected = "1"
			}
			else{
				modelSelect.disabled = true;
				modelSelect.options[0]= new Option("[Any Mobile Phone]", "");
			}	
		}
	}
	xmlhttp.open("GET", url, true);
	xmlhttp.send(null);
}

function getModelImage(str)
{
	xmlhttp = GetXmlHttpObject();
	if (xmlhttp == null) {
		alert("Browser does not support HTTP Request");
		return;
	}
	var url = "http://www.yourphonereviews.com/ajax/models.php?op=modelimage";
	url = url + "&q=" + str;
	url = url + "&sid=" + Math.random();
	xmlhttp.onreadystatechange = function(){
		if (xmlhttp.readyState == 4) {
			if (xmlhttp.responseText == "") {
				hidediv('model_image_show');
			}
			else {
				showdiv('model_image_show');
				document.getElementById('model_image_show').innerHTML = xmlhttp.responseText;
			}
		}
	}
	xmlhttp.open("GET", url, true);
	xmlhttp.send(null);
}

function getPAYGModelImage(str)
{
	xmlhttp = GetXmlHttpObject();
	if (xmlhttp == null) {
		alert("Browser does not support HTTP Request");
		return;
	}
	var url = "http://www.yourphonereviews.com/ajax/models.php?op=modelimage";
	url = url + "&q=" + str;
	url = url + "&sid=" + Math.random();
	xmlhttp.onreadystatechange = function(){
		if (xmlhttp.readyState == 4) {
			if (xmlhttp.responseText == "") {
				hidediv('payg_model_image_show');
			}
			else {
				showdiv('payg_model_image_show');
				document.getElementById('payg_model_image_show').innerHTML = xmlhttp.responseText;
			}
		}
	}
	xmlhttp.open("GET", url, true);
	xmlhttp.send(null);
}

function getReviewImage(str)
{
	xmlhttp = GetXmlHttpObject();
	if (xmlhttp == null) {
		alert("Browser does not support HTTP Request");
		return;
	}
	var url = "http://www.yourphonereviews.com/ajax/models.php?op=reviewmodelimage";
	url = url + "&q=" + str;
	url = url + "&sid=" + Math.random();
	xmlhttp.onreadystatechange = function(){
		if (xmlhttp.readyState == 4) {
			if (xmlhttp.responseText == "") {
				hidediv('model_image');
			}
			else {
				showdiv('model_image');
				document.getElementById('model_image').innerHTML = xmlhttp.responseText;
			}
		}
	}
	xmlhttp.open("GET", url, true);
	xmlhttp.send(null);
}

//hide show paym search
function paymshowhide(id) {

	if (document.getElementById(id).style.display == 'none') {
		showdiv(id);
		document.getElementById('link').innerHTML="Hide options";
	}
	else {
		hidediv(id);
		document.getElementById('link').innerHTML="Show more search options";
	}
}

// show/hide review form
function showReviewForm(){

	var brokenstring = document.getElementById('usedid').innerHTML.split("|");
	for (var i = 0; i < brokenstring.length; i++) {
		if (brokenstring[i] == "") {
			
			}
			else {
				hidediv(brokenstring[i]);
			}
	}
	if (document.getElementById('show').style.display == 'none') {
		showdiv('show');
		showdiv('review1');
		hidediv('hide');
		showdiv('links');
		document.getElementById('rlink').innerHTML = "Add a Review";
	}
	else {
		showdiv('hide');
		hidediv('show');
		hidediv('links');
		document.getElementById('rlink').innerHTML = "Show Reviews";
	}
}

// Change image on review page
function changeImage(file) {

	path = "http://www.yourphonereviews.com/";
	document.getElementById('image').src = path + file;
}

function readmore(id)
{
	showdiv(id);
	document.getElementById('rrlink').innerHTML="";
	
}

function showReviews(id,num)
{

		var nextsum = parseInt(num)+parseInt('1');
		var prevsum = parseInt(num)-parseInt('1');
		if (document.getElementById(id).style.display="none") {
		document.getElementById('usedid').innerHTML+=id+'|';
		var brokenstring=document.getElementById('usedid').innerHTML.split("|");
		for (var i=0;i<brokenstring.length;i++){
			if (id == brokenstring[i]) {
			
			}
			else if (brokenstring[i]=="") {
			
			}
			else {
				hidediv(brokenstring[i]);

			}
		}
		showdiv(id);
		//document.getElementById('prev').onclick = function() {showReviews('review'+prevsum,prevsum)};
		//document.getElementById('next').onclick = function() {showReviews('review'+nextsum,nextsum)};
	}
	else {
		hidediv(id);
	}
}

function validateForm(webEntry) {	
	if (webEntry.name.value == "") {
		alert("Please enter your name!")
		webEntry.name.focus()
		return false
	}	
	if (!validEmail(webEntry.email.value)) {
		alert("Invalid email address")
		webEntry.email.focus()
		webEntry.email.select()
		return false
	}
        	if (webEntry.sitename.value == "") {
		alert("Please enter your site name!")
		webEntry.sitename.focus()
		return false
	}
        	if (webEntry.siteurl.value == "") {
		alert("Please enter your site url!")
		webEntry.siteurl.focus()
		return false
	}
        	if (webEntry.reurl.value == "") {
		alert("Please enter the url where your site links back to ours!")
		webEntry.reurl.focus()
		return false
	}
	        if (webEntry.emailtext.value == "") {
		alert("Please enter a short description!")
		webEntry.emailtext.focus()
		return false
	}			
	return true
}

function validEmail(email) {
	invalidChars = " /:,;"
	if (email == "") {
		return false
	}
	for (i=0; i<invalidChars.length; i++) {
		badChar = invalidChars.charAt(i)
		if (email.indexOf(badChar,0) > -1) {
			return false
		}
	}
	atPos = email.indexOf("@",1)
	if (atPos == -1) {
		return false
	}
	if (email.indexOf("@",atPos+1) > -1) {
		return false
	}
	periodPos = email.indexOf(".",atPos)
	if (periodPos == -1) {
		return false
	}
	if (periodPos+3 > email.length)	{
		return false
	}
	return true
}

function addreviewcheck()
{
	if (document.getElementById('name').value=="") {
		alert("Oops you forgot your name!")
		return false
	}
	if (document.getElementById('reviewbox').value=="") {
		alert("Oops you forgot to enter a review!")
		return false
	}
	else {
		return true;
	}
}

function alertme(value)
{
	alert(value);
}

				