function seed () {
	return Math.floor(Math.random()*10000000000);
}
function changeImage(filename)
{
	document.mainimage.src = filename;
}

function eid(element)
{
	return document.getElementById(element);
}
function jAddToCart(sender)
{
	var err = '';
	if(eid(sender.id).value == 0 || eid(sender.id).value == '') err = "You must have a Qty greater then Zero";
	
	if(err != '')
	{
		alert(err);
		return false;
	}
	else
	{
		var data = $("#product_id"+sender.id).serialize();
		$.ajax({
		type: "POST",
		url: "/inc/ajax/addToCartAjax.php",
		dataType: "html",
		data: data,
		
		success: function(msg)
		{
			//alert(msg);
			if(msg != '')
			{
				eid("cartMessage").style.display = "block";
				eid("cartMessage").innerHTML = msg;
			}
			jClearObject("cartMessage");
		}
		});
	}
}
function jClearCartMessage(sender)
{
	eid("cartMessage").style.display = "none";
}
function jClearObject(msg)
{
	switch(msg)
	{
		case 'cartMessage':
			setTimeout("jClearCartMessage();", 2000);
		break;
	}
}
function jCurrencyFrm(sender)
{
	eid("currencyForm").submit();
}


/*     OLD STUFF    */
function jShowRows(sender)
{
	//alert($("displayRow"+sender).style.display);
	if(eid("displayRow"+sender).style.display == "none")
	{
		eid("displayRow"+sender).style.display = ""
	}
	else
	{
		eid("displayRow"+sender).style.display = "none"
	}
}
function updateSubCategory(sender, targetInputName)
{
	var categoryId = sender.options[sender.selectedIndex].value;
	//alert(categoryId);
	if(categoryId != "")
	{
			new Ajax.Request('/inc/ajax/productAjax.php',
			{
				method:'post',
				parameters:
				{
					action: 'updateSubcategory', categoryId: categoryId, targetInputName:targetInputName
				},
				onSuccess: function(r)
				{
					if (navigator.userAgent.indexOf('MSIE') !=-1)
					{
						eid("massiveUpdateSubCategoryDiv").innerHTML = "<select name=\""+targetInputName+"\" id=\""+targetInputName+"\" class=\"inputText\">" + r.responseText + "</select>";
					}
					else
					{
						eid(targetInputName).innerHTML = r.responseText;
					}
					
				}
			}
		);	
	}
}
function updateSubCategoryEditor(sender, targetInputName)
{
	var categoryId = sender.options[sender.selectedIndex].value;
	//alert(categoryId);
	if(categoryId != "")
	{
		var data = 'action=updateSubcategory';
			data += '&categoryId='+categoryId;
			data += '&targetInputName='+targetInputName;
		
		$.ajax({
		type: "POST",
		url: "/inc/ajax/productAjax.php",
		dataType: "html",
		data: data,
		
		success: function(msg)
		{
			//alert(msg+targetInputName);
			eid("updateSubCategoryDiv").innerHTML = "<select name=\""+targetInputName+"\" id=\""+targetInputName+"\" class=\"inputText\">" + msg+ "</select>";
		}
		});
	}
	else
	{
		eid("updateSubCategoryDiv").innerHTML = "";
	}
}
function toggleProductImage(sender, productId, width, height)
{
	var display = eid("image_" + productId).style.display;
	if(display == "block")
	{
		eid("image_" + productId).style.display = "none";
	}
	else
	{
		if(eid("image_" + productId).innerHTML != "")
		{
			eid("image_" + productId).style.display="block";
		}
		else
		{
				new Ajax.Request('/inc/ajax/productAjax.php',
				{
					method:'post',
					parameters:
					{
						action: 'getProductImage', productId: productId, width: width, height: height
					},
					onSuccess: function(r)
					{
						//alert(r.responseText);
						eid("image_" + productId).style.display="block";
						eid("image_" + productId).innerHTML = r.responseText;
					}
				}
			);	
		}
			
	}
	
	//alert(divId);
	
	/*dv.setAttribute("id","sdf");      
	dv.style.position="absolute";       
	dv.style.pixelLeft=20;
	dv.style.pixelTop=50;
	dv.style.width=width;
	dv.style.height=height;
	dv.style.border="1px blue solid;";
	new Ajax.Request('/inc/ajax/productAjax.php',
		{
			method:'post',
			parameters:
			{
				action: 'getProductImage', productId: productId, width: width, height: height
			},
			onSuccess: function(r)
			{
				//alert(r.responseText);
				eid(divId).innerHTML = r.responseText;
			}
		}
	);	
	sender.parentNode.appendChild(dv);*/
	
}

function hideProductImage(productId)
{
	eid("image_" + productId).style.display="none";
	//var elm = document.getElementById(childDivId);
	//elm.parentNode.removeChild(elm);
}

function productPriceEditor(sender)
{
	var bits = sender.id.split('-');
	// bits[0] = add or edit
	// bits[1] = productPrice id or 0
	// bits[2] = product_id
	new Ajax.Request('/inc/ajax/productAjax.php',
		{
			method:'post',
			parameters:
			{
				action: 'productPriceEditor', 
				product_id: bits[2], 
				price: bits[1],
				type: bits[0],
				seed : seed()
			},
			onSuccess: function(r)
			{
				eid('productPriceDisplay').style.display = "" ;
				eid('productPriceDisplay').innerHTML = r.responseText;
				setTimeout("jClearMessage('productPriceMsg');", 2000);
			}
		}
	);	
	
}

function jDeleteProductPrice(sender)
{
	var resp = window.confirm("Are your sure to delete this price?");
	if(resp)
	{
		var idArray = sender.id.split("-");
		var productPriceId = idArray[1];
		var productId	   = idArray[2];
		new Ajax.Request('/inc/ajax/productAjax.php',
			{
				method:'post',
				parameters:
				{
					action: "deleteProductPrice",
					productPriceId: productPriceId,
					productId: productId
				},
				onSuccess: function(r)
				{
					
					eid('productPriceDisplay').innerHTML = r.responseText;
					setTimeout("jClearMessage('productPriceMsg');", 2000);
				}
			}
		);
	}
	else
	{
		return false;
	}
	
}
function fUpdateSingleProductPrice(sender)
{
	var idArray = sender.id.split("-");
	var productPriceId = idArray[1];
	var productId	   = idArray[2];
	var price	   	   = eid("price_"+productPriceId).value;
	var price_qty	   = eid("price_qty_"+productPriceId).value; 
	
	new Ajax.Request('/inc/ajax/productAjax.php',
		{
			method:'post',
			parameters:
			{
				action: "updateSingleProductPrice",
				productPriceId: productPriceId,
				productId: productId,
				price : price,
				price_qty : price_qty
			},
			onSuccess: function(r)
			{
				eid('productPriceDisplay').innerHTML = r.responseText;
				setTimeout("jClearMessage('productPriceMsg');", 2000);
			}
		}
	);

}
function jHideDiv(divId)
{
	eid(divId).style.display = "none" ;
}

function jShowDiv(divId)
{
	eid(divId).style.display = "" ;
}
function jShowPricing(productId)
{
	
	new Ajax.Request('/inc/ajax/productAjax.php',
		{
			method:'post',
			parameters:
			{
				action: "getProductPrice",
				productId: productId
			},
			onSuccess: function(r)
			{
				eid('productPriceDisplay').style.display = '';
				eid('productPriceDisplay').innerHTML = r.responseText;
				setTimeout("jClearMessage('productPriceMsg');", 2000);
			}
		}
	);
}

function jUpdateAllPriceForProduct(sender)
{
	var productId = sender.id;
	var str = '';
	var elem = eid("productPrice").elements;
	str += "<element>";
	for(var i = 0; i < elem.length; i++)
	{
		//str +=  "<"+ elem[i].name +'>'+ elem[i].value +"</"+ elem[i].name +">";
		//XML format
		/*
		<productPrice>
			<price>12</price>
			<price_qty>2</price_qty>
			<markup>3</markup>
		</productPrice>
		......
		*/
	
		if(elem[i].type == 'text')
		{
			var elemId = elem[i].id;
			var elemValue = elem[i].value;
			var productPriceId = jGetLastSubString(elemId,"_");
			if(elemId.search(/price_/) != -1 && elemId.search(/price_qty/) == -1)
			{
				//start of an element which is price
				str += "<productPrice><price>" + elem[i].value + "</price>";
			}
			else if(elemId.search(/markup_/) != -1)
			{
				//end of an element which is markup
				str += "<markup>" + elem[i].value + "</markup></productPrice>";
			}
			else if(elemId.search(/price_qty/) != -1)
			{
				str += "<price_qty>" + elem[i].value + "</price_qty>";
			}
		}
	
		//chk += elem[i].type+"-";
		//if(elem[i].type == 'checkbox') str += "["+elem[i].type+"]["+elem[i].checked+"]";
		//if(elem[i].name == 'menu_title') menu_id = elem[i].value;
	}
	str += "</element>";
	new Ajax.Request('/inc/ajax/productAjax.php',
		{
			method:'post',
			parameters:
			{
				action: "updateAllPriceForProduct",
				productId: productId,
				str:str
			},
			onSuccess: function(r)
			{
				//alert(r.responseText);
				eid('productPriceDisplay').innerHTML = r.responseText;
				setTimeout("jClearMessage('productPriceMsg');", 2000);
			}
		}
	);
}
function jClearMessage(msgDiv)
{
	eid(msgDiv).innerHTML = "";
}
function jGetLastSubString(str, splitBy)
{
	var strArr = str.split(splitBy);
	return strArr[strArr.length - 1];
}
/*end Price Related Function*/

function toggledisplay(hiddenid,plusid)
{
	var element = document.getElementById(hiddenid);
	if (element.style.display == 'none') 
	{
		element.style.display = '';
		if (plusid != '') plusid.innerHTML = '-';
	}
	else 
	{
		element.style.display = 'none';
		if (plusid != '') plusid.innerHTML = '+';
	}
}
function checkAll(box)
{
	var i = 0, el, f = box.form, bWhich = box.checked;
	while (el = f.elements[i++])
		if (box != el && el.id == "remove")
			el.checked = bWhich;
		/*if (box != el && /^option/.test(el.name))
			el.checked = bWhich;*/
}

/*Product Search Related Function */
function jSearchProduct(ketwordInputId)
{
	alert(1);
}
/*END Product Search Related Function */

/*Product Admin Side Related Function */
function jValidateProductForm()
{
	var err = "";
	//alert(jValidatePostedRadioButton());
	if(eid("title").value == "")
	{
		err += "Title Can NOT Be Blank!\n";
	}
	if(err == "")
	{
		return true;
	}
	else
	{
		alert(err);
		return false;
	}
	
}

function jValidatePostedRadioButton()
{
	var radioInput = eid("productEditorForm").posted;
	for(i = 0; i < radioInput.length; i++)
	{
		if(radioInput[i].checked) return true;
	}
	return false
}

function jValidatePPSSiteRadioButton()
{
	var radioInput = eid("productEditorForm").pps_site;
	for(i = 0; i < radioInput.length; i++)
	{
		if(radioInput[i].checked) return true;
	}
	return false
}



function jValidateCategoryForm(sender)
{
	var err = "";
	//alert(jValidatePostedRadioButton());
	if(!jValidatePPSSiteCategoryRadioButton())
	{
		err += "PPS Site Can NOT Be Blank!\n";
	}
	if(eid("category").value == "")
	{
		err += "Category Can NOT Be Blank!\n";
	}
	if(err == "")
	{
		return true;
	}
	else
	{
		alert(err);
		return false;
	}
}

function jValidatePPSSiteCategoryRadioButton()
{
	var radioInput = eid("productCategoryEditorForm").pps_site;
	for(i = 0; i < radioInput.length; i++)
	{
		if(radioInput[i].checked) return true;
	}
	return false
}

function jValidateProductSubCategoryForm(sender)
{
	if(eid('sub_category').value == "")
	{
		alert("Sub Category Can NOT be Blank");
		return false;
	}
	else
	{
		return true;
	}
}


function jSortProductByCatalogue()
{
	
	eid("sortByCatalogueForm").submit();

}

function PopupReturnWindow(file_id,w,h,rs,sb,mb)
{

  var winl = (screen.width-w)/2;
  var wint = (screen.height-h)/2;
  
  settings = 'height='+h+',';
  settings += 'width='+w+',';
  settings +='top='+wint+',';
  settings +='left='+winl+',';
  settings +='scrollbars='+sb+',';
  settings +='menubar='+mb+',';
  settings +='resizable='+rs;
    
  myPopup = window.open(file_id,"",settings);
    if (!myPopup.opener)
         myPopup.opener = self; 
}

function jSubmitCatalogueFilterForm(sender)
{
	
	if(sender.value != 0) 
	{
		$("#filterByCatalogue").submit();
	}
}

function jSubmitTypeFilterForm(sender)
{
	if(sender.value != 0) 
	{
		$("#filterByType").append("<input type='hidden' value='"+sender.value+"' name='submitTypeFilter'  /> ");
		$("#filterByType").submit();
	}
}

function jSubmitForm(formId)
{
	$('#'+formId).submit();
}

function jComfirmDelRelateProduct(target)
{

	var answer = confirm("Are you sure to delete " + target + "?")
	if (answer){
		return true;
	}
	else{
		return false;
	}


}
/* to IE6
sfHover = function() {
	var sfEls = document.getElementById("leftNavigationMenu").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" fshover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" fshover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);
*/

/*END Product Admin Side Related Function */