alimg = new Image();
alimg.src = "/images/pages_31.gif";
alimg = new Image();
alimg.src = "/images/pages_32.gif";
alimg = new Image();
alimg.src = "/images/pages_33.gif";

function showItemsActual() {
	document.getElementById("menuActual").style.display = "block";
	document.getElementById("menuSoon").style.display = "none";
	document.getElementById("menuArchive").style.display = "none";

	document.getElementById("tabActual").style.display = "block";
	document.getElementById("tabSoon").style.display = "none";
	document.getElementById("tabArchive").style.display = "none";

	document.getElementById("tabImage").style.backgroundImage = "url(/images/pages_31.gif)";
}

function showItemsSoon() {
	document.getElementById("menuActual").style.display = "none";
	document.getElementById("menuSoon").style.display = "block";
	document.getElementById("menuArchive").style.display = "none";

	document.getElementById("tabActual").style.display = "none";
	document.getElementById("tabSoon").style.display = "block";
	document.getElementById("tabArchive").style.display = "none";

	document.getElementById("tabImage").style.backgroundImage = "url(/images/pages_32.gif)";
}

function showItemsArchive() {
	document.getElementById("menuActual").style.display = "none";
	document.getElementById("menuSoon").style.display = "none";
	document.getElementById("menuArchive").style.display = "block";

	document.getElementById("tabActual").style.display = "none";
	document.getElementById("tabSoon").style.display = "none";
	document.getElementById("tabArchive").style.display = "block";
	document.getElementById("tabImage").style.backgroundImage = "url(/images/pages_33.gif)";
}


function compareGroups() {
	var groupId = document.forms["compareItems"].partoofcatalog.value;
	location.href = "/compare.php?groupId=" + groupId;
}

function compareAdd() {
	var allcamersList = document.forms["compareItems"].allcameras;
	var tocompare = document.getElementById("tocompare");
	var itemId = 0;
	var itemItxt = "";
	for (i=0; i<allcamersList.options.length; i++) {
		if (allcamersList[i].selected) {
			itemId = allcamersList[i].value;
			itemTxt = allcamersList[i].text;
			var added = false;
			for (j=0; j<tocompare.options.length; j++) {
				if (tocompare[j].value == itemId) {
					added = true;
					break;
				}
			}
			if (!added) {
				tocompare.options[tocompare.length] = new Option(itemTxt, itemId);
			}
		}
	}
}

function compareDel() {
	var tocompare = document.getElementById("tocompare");
	for (i=tocompare.options.length-1; i>=0; i--) {
		if (tocompare[i].selected) {
			tocompare[i] = null;
		}
	}
}

function compareClearAll() {
	var tocompare = document.getElementById("tocompare");
	for (i=tocompare.options.length-1; i>=0; i--) {
		tocompare[i] = null;
	}
}

function compareSelected() {
	var compareForm = document.forms["compareItems"];
	if (compareForm.tocompare.options.length > 1) {
		for (i=0; i<compareForm.tocompare.options.length; i++) {
			compareForm.tocompare.options[i].selected = true;
		}
		compareForm.submit();
	}
}

var creditTypes = new Array();
creditTypes["DN01"] = [ 2.5, 12 ];
creditTypes["DN02"] = [ 2.2, 10 ];
creditTypes["DN03"] = [ 2.2, 10 ];
creditTypes["DN04"] = [ 1.99, 8 ];

function getMinFirst(total) {
	var p = 10;
	if (total > 3000) {
		p = 20;
	}
	if (total > 5000) {
		p = 30;
	}
	if (total > 12000) {
		p = 40;
	}
	return total * p / 100;
}

function recalculate() {
	var cType = document.forms["creditCalc"].creditType.value;
	var total = parseInt(document.forms["creditCalc"].total.value) + parseInt(document.forms["creditCalc"].total10.value) / 100;
	if (!/^\d+$/.test(total)) {
		total = 0;
	}
	var cPeriod = document.forms["creditCalc"].creditPeriod.value;
	if (cType == "DN01") {
		document.forms["creditCalc"].creditPeriod.getElementsByTagName("OPTION")[2].setAttribute("disabled", "true");
		document.forms["creditCalc"].creditPeriod.getElementsByTagName("OPTION")[3].setAttribute("disabled", "true");
		if (cPeriod > 12) {
			cPeriod = 12;
			document.forms["creditCalc"].creditPeriod.value = cPeriod;
		}
	} else {
		document.forms["creditCalc"].creditPeriod.getElementsByTagName("OPTION")[2].removeAttribute("disabled");
		document.forms["creditCalc"].creditPeriod.getElementsByTagName("OPTION")[3].removeAttribute("disabled");
	}
	minFirst = (cType == "DN03" || cType == "DN04") ? getMinFirst(total).toFixed(2) : 0;
	document.getElementById("minFirstP").innerHTML = minFirst;
	document.getElementById("comm").innerHTML = creditTypes[cType][0];
	document.getElementById("perc").innerHTML = creditTypes[cType][1];
	
	firstP = parseInt(document.forms["creditCalc"].first.value) + parseInt(document.forms["creditCalc"].first10.value) / 100;
	if (!/^\d+$/.test(firstP)) {
		firstP = 0;
	}
	totalPerc = 0;
	rest = total - firstP;
	for (i=0; i<cPeriod; i++) {
		totalPerc += rest * creditTypes[cType][1] / 1200;
		rest -= total / cPeriod;
	}
	pPeriod = parseInt(cPeriod) + 1;
	totalComm = pPeriod * total * creditTypes[cType][0] / 100;
	var overP = totalComm + totalPerc;
	var monthP = (total + overP) / pPeriod;
	if (firstP > total) {
		overP = 0;
		monthP = 0;
	}
	document.getElementById("overP").innerHTML = overP.toFixed(2);
	document.getElementById("monthP").innerHTML = monthP.toFixed(2);
}

function showCatGroup(group) {
	for (i=1; i<=2; i++) {
		v = group == i ? "block" : "none";
		document.getElementById("catGroups"+i).style.display = v;
	}
}

function showPriceDesc(e) {
	document.getElementById("priceDesc").style.display = "block";
	var left = e.offsetLeft;
	var top = e.offsetTop;
	for (var parent = e.offsetParent; parent; parent = parent.offsetParent) {
	    left += parent.offsetLeft - parent.scrollLeft;
	    top += parent.offsetTop - parent.scrollTop;
	}
	document.getElementById("priceDesc").style.left = left + document.body.scrollLeft-100;
	document.getElementById("priceDesc").style.top = top + document.body.scrollTop+40;
}

function hidePriceDesc() {
	document.getElementById("priceDesc").style.display = "none";
}

function showWatchItem() {
	var e = document.getElementById("watchItem");
	e.style.display = e.style.display == "none" ? "block" : "none";
}

function doWatchItem(f) {
	document.getElementById("watchItem").style.display = "none";
	var email = f.email.value;
	var id = parseInt(f.id.value);
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
	}
	req.open("GET", "/watch-item.php?id="+id+"&email="+email, true);
	req.onreadystatechange = processReqChange;
	req.send(null);
}

function processReqChange() {
	if (req.readyState == 4 && req.status == 200) {
	}
}
