var SK = new Array();
var SD = new Array();
SK[0]="Image";
SD[0]="&nbsp;";
SK[1]="Brand";
SD[1]="Brand";
SK[2]="Model";
SD[2]="Model";
SK[3]="OperatingSystem";
SD[3]="Operating System";
SK[4]="Processor";
SD[4]="Processor";
SK[5]="Chipset";
SD[5]="Chipset";
SK[6]="Memory";
SD[6]="Memory";
SK[7]="Storage";
SD[7]="Storage";
SK[8]="Display";
SD[8]="Display";
SK[9]="Resolution";
SD[9]="Display Resolution";
SK[10]="Graphics";
SD[10]="Graphics<br />Max Allocated RAM Size";
SK[11]="Audio";
SD[11]="Audio";
SK[12]="Camera";
SD[12]="Webcam";
SK[13]="Ethernet";
SD[13]="Ethernet LAN";
SK[14]="Wireless";
SD[14]="Wireless LAN";
SK[15]="Bluetooth";
SD[15]="Bluetooth";
SK[16]="OpticalDrive";
SD[16]="Optical Drive";
SK[17]="CardReader";
SD[17]="Memory Card Reader";
SK[18]="Ports";
SD[18]="Ports";
SK[19]="Input";
SD[19]="Keyboard and Touchpad";
SK[20]="Security";
SD[20]="Security";
SK[21]="Dimensions";
SD[21]="Dimensions";
SK[22]="Weight";
SD[22]="Weight";
SK[23]="Power";
SD[23]="Power/Battery";
SK[24]="Warranty";
SD[24]="Warranty and Support";
SK[25]="Color";
SD[25]="Color";
SK[26]="ReleaseDate";
SD[26]="Release Date";
SK[27]="ListPrice";
SD[27]="Retail Price";
SK[28]="AmazonComMarketplace";
SD[28]="Color Available";
	var SpecificationsDescription = '';
	var ProductList = new Array();
	
	function initHttpRequest() {
		HttpRequest = false; 	
	  if(window.XMLHttpRequest && !(window.ActiveXObject)) {
	  	try {
				HttpRequest = new XMLHttpRequest();
	   	} catch(e) {
				HttpRequest = false;
	  	}  
	 	} else if(window.ActiveXObject) {
	  	try {
	    	HttpRequest = new ActiveXObject('Msxml2.XMLHTTP');
	  	} catch(e) {
	    	try {
	      	HttpRequest = new ActiveXObject('Microsoft.XMLHTTP');
	    	} catch(e) {
	      	HttpRequest = false;
	   		}
			}
	  }
	  if(HttpRequest) return HttpRequest;	  	  
	}	

	function Specifications() {
		comparisonDiv = document.getElementById("comparison");
		if(comparisonDiv && SK && SD) {
			var comparisonTable = document.createElement("table");
			comparisonTable.setAttribute("id","comparisontable")
			comparisonDiv.appendChild(comparisonTable);
			j=0;
			for(i = 0; i < SK.length; i++) {
				var comparisonTr = document.createElement("tr");
				comparisonTr.setAttribute("id",SK[i]);
				if(j==1) {
					comparisonTr.setAttribute("class","grey");
					j=0;
				} else {
					j++;
				}
				comparisonTable.appendChild(comparisonTr);
				var newTD = document.createElement("td");
				newTD.setAttribute("class","description");
				newTD.innerHTML = SD[i];
				comparisonTr.appendChild(newTD);
			}
		}
		SpecificationsDescription = 1;
	}
	
	function disableSelect(disabled) {		
		if(document.getElementById('brands')) document.getElementById('brands').disabled=disabled;
		if(document.getElementById('models')) document.getElementById('models').disabled=disabled;
		if(document.getElementById('add_laptop')) document.getElementById('add_laptop').disabled=disabled;
	}

	function ListBox(id,brand) {
		var url = 'http://www.bestnetbooksreview.com/compare/index.php';
		var param = '';
		if(brand)
			param = param+'&brand='+brand;			
		var obj = initHttpRequest();						
		if(obj) {
			obj.onreadystatechange = function() {
				switch(obj.readyState) {
					case 0:
						showCompareStatus('comparison_status','http://www.bestnetbooksreview.com/compare/loader.gif','Uninitialized...');
					break;
					case 1:
						showCompareStatus('comparison_status','http://www.bestnetbooksreview.com/compare/loader.gif','Loading Data...');
					break;
					case 2:
						showCompareStatus('comparison_status','http://www.bestnetbooksreview.com/compare/loader.gif','Data loaded...');
					break;
					case 3:
						showCompareStatus('comparison_status','http://www.bestnetbooksreview.com/compare/loader.gif','Interactive...');
					break;
					case 4:
						showCompareStatus('comparison_status','http://www.bestnetbooksreview.com/compare/loader.gif','Complete...');
					break;																								
				}											
				if(obj.readyState == 4) {
					showCompareStatus('comparison_status');
					if(obj.status == 200) {						
						document.getElementById(id).innerHTML = obj.responseText;
					}
				}
			};					
			obj.open("POST", url, true);
			obj.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			obj.setRequestHeader("Content-length", param.length);
			obj.setRequestHeader("Connection", "close");      
			obj.send(param);				
		}
	}

	function AddProduct() {
		if(document.getElementById('brands').selectedIndex == 0) {
			return;
		} else if(document.getElementById('models').selectedIndex == 0) {
			return;			
		} else {
			var brand = document.getElementById('brands').value;
			var model = document.getElementById('models').value;
		}
		showCompareStatus('comparison_status');
		disableSelect(true);
		if(!SpecificationsDescription)
			Specifications();
		removeAmazonWebserviceHTML();
		if(ProductList.in_array(brand+'_'+model)) {
			var ErrTxt = '<span style="font-weight:bold;color:red;">'+brand+' '+model+' already exists</span>';
			showCompareStatus('comparison_status','',ErrTxt);
			disableSelect(false);
			return;
		}	
		var url = 'http://www.bestnetbooksreview.com/compare/index.php';
		param = 'brand='+brand+'&model='+model;	
		var obj = initHttpRequest();						
		if(obj) {	
			obj.onreadystatechange = function() {
				switch(obj.readyState) {
					case 0:
						showCompareStatus('comparison_status','http://www.bestnetbooksreview.com/compare/loader.gif','Uninitialized...');
					break;
					case 1:
						showCompareStatus('comparison_status','http://www.bestnetbooksreview.com/compare/loader.gif','Loading Data...');
					break;
					case 2:
						showCompareStatus('comparison_status','http://www.bestnetbooksreview.com/compare/loader.gif','Data loaded...');
					break;
					case 3:
						showCompareStatus('comparison_status','http://www.bestnetbooksreview.com/compare/loader.gif','Interactive...');
					break;
					case 4:
						showCompareStatus('comparison_status','http://www.bestnetbooksreview.com/compare/loader.gif','Complete...');
					break;																								
				}											
				if(obj.readyState == 4) {
					showCompareStatus('comparison_status');
					if(obj.status == 200) {						
						var responseText = obj.responseText;
						var arr = responseText.split("}}");
						if(arr) {
							ProductList[ProductList.length] = brand+'_'+model;			
							for(i = 0; i < arr.length; i++) {
								if(arr[i]) {
									arr1 = arr[i].split("{{");									
									var TR = document.getElementById(arr1[0]);
									if(arr1[0] == 'ASIN')
										var ASIN = arr1[1];
									if(TR) {										
										var newTD = document.createElement("td");
										newTD.setAttribute("id",brand+'_'+model);
										if(arr1[0] == 'Image') {
											newTD.setAttribute("class","productimage");
											newTD.innerHTML = '<a href="javascript:RemoveProduct(\''+brand+'\',\''+model+'\');">Remove this item</a><br>'+arr1[1];
										} else {
											if(arr1[0] == 'AmazonComMarketplace')
												newTD.setAttribute("class","productmarketplace");
											if(arr1[0] == 'AmazonComCustomerReviews')
												newTD.setAttribute("class","productcustomerreviews");												
											if(arr1[0] == 'Brand')
												newTD.setAttribute("class","brand");
											if(arr1[0] == 'Model')
												newTD.setAttribute("class","model");												
											if(arr1[0] == 'ListPrice')
												newTD.setAttribute("class","listprice");
											newTD.innerHTML = arr1[1];
										}
										TR.appendChild(newTD);										
									} 									
								}
							}
						}						
					}	
					document.getElementById('comparisontable').getElementsByTagName('tr')[0].lastChild.scrollIntoView(true);
					if(document.getElementById('models')) document.getElementById('models').selectedIndex = 0;				
					disableSelect(false);
				}
			};
			obj.open("POST", url, true);
			obj.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
			obj.setRequestHeader("Content-length", param.length);
			obj.setRequestHeader("Connection", "close");      
			obj.send(param);						
		}		
	}

	function RemoveProduct(brand,model) {
		showCompareStatus('comparison_status');
		if(ProductList.in_array(brand+'_'+model)==false) {
			var ErrTxt = '<span style="font-weight:bold;color:red;">'+brand+' '+model+' already exists</span>';
			showCompareStatus('comparison_status','',ErrTxt);
			disableSelect(false);
			return;
		} else {
			ProductList = ProductList.remove(brand+'_'+model);
			if(ProductList.in_array(brand+'_'+model)==false) {
				for(i = 0; i < SK.length; i++) {			
					var TR = document.getElementById(SK[i]);
					var TD = document.getElementById(brand+'_'+model);
					TR.removeChild(TD);			
				}
			}
		}
		removeAmazonWebserviceHTML();
		removeComparisonTable();
	}
	
	function removeComparisonTable() {
		if(!ProductList.length) {
			var comparisonDiv = document.getElementById("comparison");	
			var comparisonTable = document.getElementById("comparisontable");			
			comparisonDiv.removeChild(comparisonTable);
			SpecificationsDescription = '';
		}	
	}
	
	function removeAmazonWebserviceHTML() {
		if(document.getElementById('AmazonWebservice')) {
			document.getElementById('AmazonWebservice').innerHTML = '';
		}	
	}	

	function ValidateBrand() {
		if(document.getElementById('brands')) {
			if(document.getElementById('model'))
				document.getElementById('model').innerHTML = '';		
			if(document.getElementById('brands').selectedIndex == 0) {
				if(document.getElementById('model'))
					document.getElementById('model').innerHTML = '';
				return false;
			}
		}
		return true;
	}

	function ValidateModel() {
		if(document.getElementById('models')) {
			if(document.getElementById('model').selectedIndex == 0) {
				return false;
			}
		}
		return true;
	}		

	function showCompareStatus(id,icon,msg) {
		var html = '';
		if(icon) {
			html = '<img src="'+icon+'" border="0" alt="" style="margin-right:3px;vertical-align:middle;">';
		}
		if(msg) {
			html = html+'<small>'+msg+'</small>';
		}
		if(html) {
			document.getElementById(id).innerHTML = html;
		} else {
			document.getElementById(id).innerHTML = '';
		}
	}

	Array.prototype.in_array = function(p_val) {
		for(var i = 0, l = this.length; i < l; i++) {
			if(this[i] == p_val) {
				return true;
			}
		}
		return false;
	}

	Array.prototype.remove = function(p_val) {
		var a = [];
		for (var i=0; i < this.length; i++) {
			if (this[i] != p_val) {
				a.push(this[i]);
			}
		}
		return a;
	}


