// JavaScript Document

// To enable/disable the background:
function backgroundFilter(divId) {
	var divObj = false;
	if(document.getElementById) {
		// Standard way to get element
		divObj = document.getElementById(divId);
	}
	else if(document.all) {
		// Get the element in old IE's
		divObj = document.all[divId];
	}
	if(!divObj) {
		divObj = document.createElement('div');
		divObj.id = divId;
		document.body.appendChild(divObj);
	}
	// if the style.display value is blank we try to check it out here
	if(divObj.style.display == '' && divObj.offsetWidth != undefined && divObj.offsetHeight != undefined) {
		divObj.style.display = (divObj.offsetWidth != 0 && divObj.offsetHeight != 0) ? 'block' : 'none';
	}
	// If the background is hidden ('none') then it will display it ('block').
	// If the background is displayed ('block') then it will hide it ('none').
	divObj.style.display = (divObj.style.display == '' || divObj.style.display == 'block') ? 'none' : 'block';
	
	return false;
}

// To display/hide the popup:
function popUpDiv(divId) {
	var divObj = false;
	if(document.getElementById) {
		// Standard way to get element
		divObj = document.getElementById(divId);
	}
	else if(document.all) {
		// Get the element in old IE's
		divObj = document.all[divId];
	}
	
	if(!divObj) {
		divObj = document.createElement('div');
		divObj.id = divId;
		document.body.appendChild(divObj);
	}
	
	// if the style.display value is blank we try to check it out here
	if(divObj.style.display == '' && divObj.offsetWidth != undefined && divObj.offsetHeight != undefined) {
		divObj.style.display = (divObj.offsetWidth != 0 && divObj.offsetHeight != 0) ? 'block' : 'none';
	}
	// If the PopUp is hidden ('none') then it will display it ('block').
	// If the PopUp is displayed ('block') then it will hide it ('none').
	divObj.style.display = (divObj.style.display == '' || divObj.style.display == 'block') ? 'none' : 'block';
	
	return false;
}

// This function will ask for delete confirmation of item id
// If they confirm it will send them to url, where if they have the correct permissions, the item will be deleted
function deletefromdb(id,url) {
	input_box=confirm("Are you sure you want to delete this?");

	if(input_box) {
		str1 = url.concat("task=delete&id=");
		str2 = str1.concat(id);
		window.location = str2;
	}
}


var addpaymentoptionstotal = 1;

// This function will show an element based on the id you pass in
function showobj(objid) {
	var obj = document.getElementById(objid);
	obj.style.display = "block";
}

// This function will hide an element based on the id you pass in
function hideobj(objid) {
	var obj = document.getElementById(objid);
	obj.style.display = "none";
}

// This function will check to see if the value of a form is equal to the condition you supplied
// If it is, it will show the objid you passed in, otherwise it will hide the element
function getSelectedValue(value,condition,objid) {
	if(value == condition) {
		showobj(objid);
	}
	else {
		hideobj(objid);
	}
}

function addPaymentOption(divObjId) {
	divObj = document.getElementById(divObjId);
	
	/*	Dynamically generated. Unfortunately DateInput wasn't working with this method.
	divObj.innerHTML = divObj.innerHTML + '<p>Option ' + (addpaymentoptionstotal + 1) + ':</p>\n<table cellpadding="5" cellspacing="0" border="0" id="paymentOption' + addpaymentoptionstotal + '">\n<tr>\n<td align="left" class="formLabelsSmall" style="font-weight:bold">Type:</td>\n<td align="left" class="productPriceText"><select name="selPayOptionType[' + addpaymentoptionstotal + ']" onchange="changePaymentOptionType(this.options[this.selectedIndex].value, \'paymentOption' + addpaymentoptionstotal + '\', addpaymentoptionstotal)"><option value="1">Pay In Full</option><option value="2">Recurring</option></select></td>\n</tr>\n<tr>\n<td align="left" class="formLabelsSmall" style="font-weight:bold">Price:</td><td align="left" class="productPriceText">$<input type="text" name="txtPayOptionPrice[' + addpaymentoptionstotal + ']" /></td>\n</tr>\n<tr>\n<td align="left" class="formLabelsSmall" style="font-weight: bold">Pay By Date:</td>\n<td align="left" class="productPriceText"><script>' + eval('DateInput(\'hidPayOptionDate\' + addpaymentoptionstotal , true, \'YYYY-MM-DD\')') +'</script></td>\n</tr>\n</table><br />';
	*/
	//	Less Dynamic way --- hidden divs
	hidDivId = 'paymentOptionDiv' + addpaymentoptionstotal;
	if(document.getElementById(hidDivId)) {
		showobj(hidDivId);
	}
	else {
		alert('To add even more Payment Options please save your changes, then Update this product again.');
	}
	
	addpaymentoptionstotal++;
}

function changePaymentOptionType(optionValue, tableId, optionCount) {
	tableObj = document.getElementById(tableId);
	// How many rows in table?
	tableSize = tableObj.rows.length;
	//alert(tableSize);
	// Want to keep  the last row and the first two
	startrow = tableSize - 2;
	endrow = 2;
	for(j = startrow; j >= endrow; j--) {
		tableObj.deleteRow(j);
	}
	
	if(optionValue == 1) {
		tableObj.insertRow(2);
		row1 = tableObj.rows[2];
		row1.insertCell(0);
		row1.insertCell(1);
			cell1 = row1.cells[0];
			cell2 = row1.cells[1];
			cell1.className = 'formLabelsSmall';
			cell1.style.fontWeight = 'bold';
			cell1.align = 'left';
			cell1.innerHTML = 'Price:';
			cell2.className = 'productPriceText';
			cell2.align = 'left';
			cell2.innerHTML = '$<input type="text" name="txtPayOptionPrice[' + optionCount + ']" />';
	}
	else if(optionValue == 2) {
		tableObj.insertRow(2);
		tableObj.insertRow(3);
		tableObj.insertRow(4);
		row1 = tableObj.rows[2];
		row2 = tableObj.rows[3];
		row3 = tableObj.rows[4];
		row1.insertCell(0);
		row1.insertCell(1);
			cell1 = row1.cells[0];
			cell2 = row1.cells[1];
			cell1.className = 'formLabelsSmall';
			cell1.style.fontWeight = 'bold';
			cell1.align = 'left';
			cell1.innerHTML = 'Up Front Price:';
			cell2.className = 'productPriceText';
			cell2.align = 'left';
			cell2.innerHTML = '$<input type="text" name="txtPayOptionUpFrontPrice[' + optionCount + ']" />';
		row2.insertCell(0);
		row2.insertCell(1);
			cell1 = row2.cells[0];
			cell2 = row2.cells[1];
			cell1.className = 'formLabelsSmall';
			cell1.style.fontWeight = 'bold';
			cell1.align = 'left';
			cell1.innerHTML = 'Recurring Price:';
			cell2.className = 'productPriceText';
			cell2.align = 'left';
			cell2.innerHTML = '$<input type="text" name="txtPayOptionRecurringPrice[' + optionCount + ']" />';
		row3.insertCell(0);
		row3.insertCell(1);
			cell1 = row3.cells[0];
			cell2 = row3.cells[1];
			cell1.className = 'formLabelsSmall';
			cell1.style.fontWeight = 'bold';
			cell1.align = 'left';
			cell1.innerHTML = 'Num. Months<br />Recurring:';
			cell2.className = 'productPriceText';
			cell2.align = 'left';
			cell2.innerHTML = '<input type="text" size="4" name="txtPayOptionRecurringMonths[' + optionCount + ']" />';
	}
	else if(optionValue == 3) {
		tableObj.insertRow(2);
		tableObj.insertRow(3);
		tableObj.insertRow(4);
		row1 = tableObj.rows[2];
		row2 = tableObj.rows[3];
		row3 = tableObj.rows[4];
		row1.insertCell(0);
		row1.insertCell(1);
			cell1 = row1.cells[0];
			cell2 = row1.cells[1];
			cell1.className = 'formLabelsSmall';
			cell1.style.fontWeight = 'bold';
			cell1.align = 'left';
			cell1.innerHTML = 'Down Payment:';
			cell2.className = 'productPriceText';
			cell2.align = 'left';
			cell2.innerHTML = '$<input type="text" name="txtPayOptionUpFrontPrice[' + optionCount + ']" />';
		row2.insertCell(0);
		row2.insertCell(1);
			cell1 = row2.cells[0];
			cell2 = row2.cells[1];
			cell1.className = 'formLabelsSmall';
			cell1.style.fontWeight = 'bold';
			cell1.align = 'left';
			cell1.innerHTML = 'Balance:';
			cell2.className = 'productPriceText';
			cell2.align = 'left';
			cell2.innerHTML = '$<input type="text" name="txtPayOptionRecurringPrice[' + optionCount + ']" />';
		row3.insertCell(0);
		//row3.insertCell(1);
			cell1 = row3.cells[0];
			//cell2 = row3.cells[1];
			cell1.colSpan = 2;
			cell1.className = 'formLabelsSmall';
			cell1.style.fontWeight = 'bold';
			cell1.align = 'left';
			cell1.innerHTML = 'The Pay By Date is when the Balance will be charged.';
			/*cell2.className = 'productPriceText';
			cell2.align = 'left';
			cell2.innerHTML = '<input type="text" size="4" name="txtPayOptionRecurringMonths[' + optionCount + ']" />';*/
		//alert('Down Payment Option in testmode.');
	}
	else {
		alert('Unknown Payment Option Type');
	}
}


function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

var lleft=788;
var ltop=355;

function hidel(layer){
if (document.body.clientWidth>788){
	lleft=(document.body.clientWidth-788)/2
	}else{lleft=0;}

document.getElementById('Layer1').style.left=lleft+179;
document.getElementById('Layer2').style.left=lleft+256;
document.getElementById('Layer3').style.left=lleft+348;
document.getElementById('Layer4').style.left=lleft+416;
// dormant 
//document.getElementById('Layer5').style.left=lleft+587;
//document.getElementById('Layer6').style.left=lleft+699;
// 
document.getElementById(layer).style.top=ltop;
document.getElementById(layer).style.height=1;
document.getElementById(layer).style.visibility="hidden";

loaded='Layer0';
a=0;
s=1;
clearTimeout(utimer);
clearTimeout(ptimer);
clearTimeout(ltimer);
}

var ltimer=0;
var utimer=0;
var ptimer=0;
var a=0;
var s=1;
var loaded='Layer0';


function loadl(layer){
document.getElementById(layer).style.visibility="visible";
clearTimeout(utimer);
x=4;s=1;
loaded=layer;
if (a<110){
	a=a+x;
	document.getElementById(layer).style.height=a;
	txt="loadl('" + layer + "')";
	ltimer=setTimeout(txt,3);
	}
}

function unloadl(layer){
clearTimeout(ltimer);
x=5;s=1;
loaded=layer;
if (a>5){
	a=a-x;
	document.getElementById(layer).style.height=a;
	txt="unloadl('" + layer + "')";
	utimer=setTimeout(txt,3);
	}else{hidel(layer);a=0;}

}


function kstopl(){
clearTimeout(ptimer);
s=1;
}


function stopl(layer){
s=s+1;
if (s<10){
	txt="stopl('" + layer + "')";
	ptimer=setTimeout(txt,10);}
else{
	unloadl(layer);}
}

function starthide(){
	hidel('Layer1');
	hidel('Layer2');
	hidel('Layer3');
	hidel('Layer4');
	hidel('Layer5');
	hidel('Layer6');
}

function confirm_action(url, action, id, alertText) {
	if(alertText == undefined) {
		alertText = "Are you sure you want to remove this?";
	}
	
	input_box=confirm(alertText);
	
	if(input_box) {
		str1 = url.concat("task=");
		str2 = str1.concat(action);
		str3 = str2.concat("&id=");
		str4 = str3.concat(id);
		window.location = str4;
	}
}

// This function will send a value from the child window to it's parent
function sendValue(fieldObjId, sendValue, closeWin) {
	fieldObj = window.opener.document.getElementById(fieldObjId);
	fieldObj.value = sendValue;
	if(closeWin == undefined || closeWin == true) {
		window.close();
	}
}

// This function will open the specified url in a new window
function popup(winLink, winName, winWidth, winHeight, winResize, winScroll, winMenu) {
	if(winWidth == undefined || winWidth == NaN) {
		winWidth = 400;
	}
	if(winHeight == undefined || winHeight == NaN) {
		winHeight = 400;
	}
	if(winResize == undefined || (winResize != "yes" && winResize != "no" && winResize.constructor != Boolean)) {
		winResize = "yes";
	}
	if(winScroll == undefined || (winScroll != "yes" && winScroll != "no" && winScroll.constructor != Boolean)) {
		winScroll = "yes";
	}
	if(winMenu == undefined || (winMenu != "yes" && winMenu != "no" && winMenu.constructor != Boolean)) {
		winMenu = "no";
	}
	if(!window.focus) {
		return true;
	}
	var winURL;
	if(typeof(winLink) == 'string') {
		winURL=winLink;
	}
	else {
		winURL=winLink.href;
	}
	var winStyle;
	winStyle = 'width=' + winWidth + ',height=' + winHeight + ',scrollbars=' + winScroll + ',menubar=' + winMenu + ',location=' + winMenu + ',toolbar=' + winMenu + ',resizeable=' + winResize;
	window.open(winURL, winName, winStyle);
	
	return false;
}