// JavaScript Document

function orderby(o){
	document.forms["hidform"].orderby.value = o;
	document.forms["hidform"].submit();
}
function nomore(f){
	df = eval('document.forms["hidform"].' + f); 
	df.value = "";
	document.forms["hidform"].submit();
}
function editadvanced(){
	document.forms["hidform"].find.value = "";
	document.forms["hidform"].submit();
}

function compa(){
	var count = 0;
	var phones = new Array();
	var deleted = 0;
	this.addmodel = function(id, model){
		// Return false if phone already exist
		for(i=0;i<count;i++){
			if(phones[i][0] == id){
				return false;
			}
		}
		
		// Delete first if max is reached
		deleted = 0;
		if(count == 3){
			this.delfirst();
			count--;
		}
		
		// Do the math
		phones[count] = new Array(id, model);
		count++;
	}
	this.delmodel = function(id){
		for(i=0;i<count;i++){
			if(phones[i][0] == id){
				d = phones.slice(i,i+1);
				phones.splice(i,1);
				count--;
				deleted = d[0];
			}
		}
	}
	this.delfirst = function(){
		d = phones.shift();
		deleted = d[0];
	}
	this.getid = function(){
		return phones[count].id;
	}
	this.getname = function(){
		return phones[count][0];
	}
	this.count = function(){
		return count;
	}
	this.lastdeleted = function(){
		if(deleted != 0){
			return deleted;
		}
		else{
			return false;
		}
	}
	this.makehtml = function(){
		html = "";
		for(i=0;i<count;i++){
			html += "<div class=\"comparebox\"><img src=\"http://www.esato.com/phones/phoneimg.php?id=" + phones[i][0] + "&ns=100\" alt=\"\" style=\"height:100px\"><br>" + phones[i][1] + "<br><a href=\"#\" onclick=\"removep(\'" + phones[i][0] + "\')\"><img src=\"http://static.esato.com/gfx/closex.png\" alt=\"\" class=\"xremove\">Remove</a></div>";
		}
		if(html != ""){
			if(count == 2){
				html += "<div class=\"comparebox\"><a href=\"/phones/compare.php?phone=" + phones[0][0] + "&cp=" + phones[1][0] + "\" class=\"comparelink\">Compare<br>these two<br>models</a></div>";
													  //http://www.esato.com/phones/compare.php?phone=419&cp=431
			}
			else if(count == 3){
				html += "<div class=\"comparebox\"><a href=\"/phones/comparemore.php?phone=" + phones[0][0] + "&cp=" + phones[1][0] + "&cp2=" + phones[2][0] + "\" class=\"comparelink\">Compare<br>these three<br>models</a></div>";
			}
			html += "<div style=\"clear:both\"></div>";
		}
		return html;
	}
}

var compObj = new compa();

function removep(id){
	compObj.delmodel(id);
	$("#trid"+id).css("backgroundColor", "#FFF");
	html = compObj.makehtml();
	if(html != ""){
		$("#compdiv").html(html);
	}
	else{
		$("#compdiv").html();
		$("#compdiv").hide();
	}
}

function compareit(id, model){
	compObj.addmodel(id,model);
	if(last = compObj.lastdeleted()){
		$("#trid"+last).css("backgroundColor", "#FFF");
	}
	html = compObj.makehtml();
	if(html != ""){
		$("#compdiv").html(html);
		$("#compdiv").slideDown("fast");
		$("#trid"+id).css("backgroundColor", "#D8D8D8");
	}
}

function setall(onoff){
	if(onoff == "on"){
		$("input:checkbox").attr("checked", true);
	}
	else{
		$("input:checkbox").attr("checked", false);
	}
}