function initAJAX () {
	var xmlHttp;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	return xmlHttp;
}

function getContent() {
	var xmlHttp = initAJAX();

	var phoneArray = new Array();
	var request = 'ajax_products.php';
	if (window.location.pathname.substring(1,10) == 'index.php' ||
	    window.location.pathname == '/')
	    request = "ajax_index.php";
	var first = true;
	for (i = 0; i < 5; i++) {
		if (document.getElementById('phone' + i)) {
			if (first) {
				request += '?';
				first = false;
			} else {
				request += '&';
			}
			request += 'phone' + i + '=' + document.getElementById('phone' + i).value;
		}
	}

	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4) {
			var contenttd;
			contenttd = document.getElementById('center_column');
			if ((window.location.pathname.substring(1,10) == 'index.php' || window.location.pathname == '/')
			   && request == "ajax_products.php") return;
			if (window.location.pathname.substring(1,12) == 'gpsserv.php') return;
			if (contenttd == null && window.location.pathname.substring(1,13) == 'products.php') {
				contenttd = document.getElementById('center_column_only');
			} else {
				if (contenttd == null) return;
			}
			contenttd.innerHTML = xmlHttp.responseText;
		}
	}
	xmlHttp.open("GET", request, true);
	xmlHttp.send(null);
	
	var xmlHttp2 = initAJAX();
	var request2 = 'ajax_products_left.php';
	var first2 = true;
	for (i2 = 0; i2 < 5; i2++) {
		if (document.getElementById('phone' + i2)) {
			if (first2) {
				request2 += '?';
				first2 = false;
			} else {
				request2 += '&';
			}
			request2 += 'phone' + i2 + '=' + document.getElementById('phone' + i2).value;
		}
	}

	xmlHttp2.onreadystatechange=function() {
		if(xmlHttp2.readyState==4) {
			var contentp;
			contentp = document.getElementById('prod_list');
			/*if (contenttd == null && window.location.pathname.substring(1,13) == 'products.php') {
				contenttd = document.getElementById('center_column_only');
			} else {
				if (contenttd == null) return;
			}*/
			contentp.innerHTML = xmlHttp2.responseText;
		}
	}
	xmlHttp2.open("GET", request2, true);
	xmlHttp2.send(null);
}

function setCookie(c_name, value, expiredays) {
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + expiredays);
	document.cookie = c_name + "=" + escape(value)+ ((expiredays==null) ? "" : ";expires=" + exdate.toGMTString());
}

function getCookie(c_name) {
	if (document.cookie.length > 0) {
		c_start = document.cookie.indexOf(c_name + "=");
		if (c_start != -1) {
			c_start = c_start + c_name.length + 1;
			c_end = document.cookie.indexOf(";", c_start);
			if (c_end == -1) c_end = document.cookie.length;
			return unescape(document.cookie.substring(c_start, c_end));
		}
	}
	return "";
}

function deleteCookie(c_name) {
	if (getCookie(c_name)) document.cookie = c_name + "=" + "none" + ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function addpic(image, phonename, displayname) {
	var header = document.getElementById('header');
	var noofphones = parseInt(getCookie('noofphones'));//parseInt(document.getElementById('noofphones').value);
	for (i = 0; i < 5; i++) {
		var currentPicId = parseInt(getCookie('phone_no' + i));
		if (currentPicId && currentPicId == phonename) {
			return;
		}
	}
	if (noofphones < 5) {
		setCookie('phone_no' + noofphones, phonename, 1);
		setCookie('image_no' + noofphones, image, 1);
		setCookie('display_no' + noofphones, displayname, 1);
		//alert(noofphones);
		var newpic = header.appendChild(document.createElement('div'));
		newpic.id = 'phone_no' + noofphones;
		newpic.className = "phone_img";
		newpic.innerHTML = '<div><img width="71" height="70" src="' + image + '" alt="" /><img width="71" height="70" usemap="#close_button'+ noofphones + '" onmouseover="Tip(\'' + displayname + '\')" alt="" src="images/box.png" /><input id="phone' + noofphones + '" name="phone' + noofphones + '" type="hidden" value="' + phonename + '"/></div>';
		noofphones++;

		var newnoofphones = noofphones.toString();
		//document.getElementById('noofphones').value = newnoofphones;
		setCookie('noofphones', newnoofphones, 1);
	}
	// AJAX handler
	getContent();
}

function removepic(id) {
	var header = document.getElementById('header');
	var noofphones = parseInt(getCookie('noofphones'));//parseInt(document.getElementById('noofphones').value);
	header.removeChild(document.getElementById('phone_no' + id));
	deleteCookie('phone_no' + id);
	deleteCookie('image_no' + id);
	deleteCookie('display_no' + id);
	noofphones--;
	var newnoofphones = noofphones.toString();
	setCookie('noofphones', newnoofphones, 1); //document.getElementById('noofphones').value = newnoofphones;
	getContent();
}

function genpics () {
	var header = document.getElementById('header');

	for (i = 0; i < 5; i++) {
		var currentPicId = parseInt(getCookie('phone_no' + i));
		if (currentPicId) {
			var newpic = header.appendChild(document.createElement('div'));
			newpic.id = 'phone_no' + i;
			newpic.className = "phone_img";
			newpic.innerHTML = '<div><img src="' + getCookie('image_no' + i) + '" alt="" /><img width="71" height="70" usemap="#close_button'+ i + '" onmouseover="Tip(\'' + getCookie('display_no' + i) + '\')" alt="" src="images/box.png" /><input id="phone' + i + '" name="phone' + i + '" type="hidden" value="' + currentPicId + '"/></div>';
		}
	}
	getContent();
}