function seed () {
	return Math.floor(Math.random()*10000000000);
}
function $ (element)
{
	return document.getElementById(element);
}
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)
				{
					//alert(r.responseText);
					//$("test").innerHTML = "<select>"+r.responseText+"</select>";
					//$(targetInputName).innerHTML = r.responseText;
					/*var elm = document.getElementById(targetInputName);
					var parent = elm.parentNode;
					//elm.parentNode.removeChild(elm);
					parent.innerHTML="<select name=\""+targetInputName+"\" id=\""+targetInputName+"\" class=\"inputText\">" + r.responseText + "</select>";*/
					//$("massiveUpdateSub_category_id").innerHTML = r.responseText;
					if (navigator.userAgent.indexOf('MSIE') !=-1)
					{
						$("massiveUpdateSubCategoryDiv").innerHTML = "<select name=\""+targetInputName+"\" id=\""+targetInputName+"\" class=\"inputText\">" + r.responseText + "</select>";
					}
					else
					{
						$(targetInputName).innerHTML = r.responseText;
					}
					
				}
			}
		);	
	}
}
function updateSubCategoryEditor(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)
				{
					//alert(r.responseText);
					//$("test").innerHTML = "<select>"+r.responseText+"</select>";
					//$(targetInputName).innerHTML = r.responseText;
					/*var elm = document.getElementById(targetInputName);
					var parent = elm.parentNode;
					//elm.parentNode.removeChild(elm);
					parent.innerHTML="<select name=\""+targetInputName+"\" id=\""+targetInputName+"\" class=\"inputText\">" + r.responseText + "</select>";*/
					//$("massiveUpdateSub_category_id").innerHTML = r.responseText;
					$("updateSubCategoryDiv").innerHTML = "<select name=\""+targetInputName+"\" id=\""+targetInputName+"\" class=\"inputText\">" + r.responseText + "</select>";;
				}
			}
		);	
	}
}
function toggleProductImage(sender, productId, width, height)
{
	var display = $("image_" + productId).style.display;
	if(display == "block")
	{
		$("image_" + productId).style.display = "none";
	}
	else
	{
		if($("image_" + productId).innerHTML != "")
		{
			$("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);
						$("image_" + productId).style.display="block";
						$("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);
				$(divId).innerHTML = r.responseText;
			}
		}
	);	
	sender.parentNode.appendChild(dv);*/
	
}

function hideProductImage(productId)
{
	$("image_" + productId).style.display="none";
	//var elm = document.getElementById(childDivId);
	//elm.parentNode.removeChild(elm);
}
/*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)
				{
					//alert(r.responseText);
					$(targetInputName).innerHTML = r.responseText;
				}
			}
		);	
	}
}
function showProductImage(sender, divId, width, height,productId)
{
	dv = document.createElement('div'); // create dynamically div tag
	dv.setAttribute('id',divId);       //give id to it
    // set the style classname  
	//set the inner styling of the div tag 
	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)
			{
				$(divId).innerHTML = r.responseText;
			}
		}
	);	
	//dv.style.backgroundColor="red";
	//set the html content inside the div tag
	//dv.innerHTML='<br> hi <br>';
	sender.parentNode.appendChild(dv);
}

function hideProductImage(childDivId)
{
	var elm = document.getElementById(childDivId);
	elm.parentNode.removeChild(elm);
}*/
/*Price Related Function*/
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)
			{
				$('productPriceDisplay').style.display = "" ;
				$('productPriceDisplay').innerHTML = r.responseText;
				setTimeout("jClearMessage('productPriceMsg');", 2000);
			}
		}
	);	
	
}

function jSubmitProductPrice(sender)
{
	/*
	var err = '';
	if($('username').value == '') err += 'You must have a username';
	
	if(err != '')
	{
		alert(err);
		return false;
	}
	*/
	//alert(type);
	
	var type = '';
	var chk = '';
	var str = '';
	var elem = $("submit").elements;
	str += "<element>";
	for(var i = 0; i < elem.length; i++)
	{
		//str +=  "<"+ elem[i].name +'>'+ elem[i].value +"</"+ elem[i].name +">";
		if(elem[i].name != 'submit' && elem[i].name != 'programmer')
		{
			if(elem[i].type == 'radio' && elem[i].checked == true)
			{
				str +=  "<"+ elem[i].name +">"+ elem[i].value +"</"+ elem[i].name +">\n";
			}
			else if(elem[i].type == 'checkbox' && elem[i].checked == true)
			{
				str +=  "<"+ elem[i].name +'>'+ elem[i].value +"</"+ elem[i].name +">\n";
			}
			else if(elem[i].type == 'textarea')
			{
				str +=  "<"+ elem[i].name +'><![CDATA['+ elem[i].value +"]]></"+ elem[i].name +">\n";
			}
			else if(elem[i].type == 'text')
			{
				str +=  "<"+ elem[i].name +'><![CDATA['+ elem[i].value +"]]></"+ elem[i].name +">\n";
			}
			else if(elem[i].type != 'radio' && elem[i].type != 'checkbox')
			{
				str +=  "<"+ elem[i].name +'>'+ elem[i].value +"</"+ elem[i].name +">\n";
			}
		}
		//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>";
	//alert(chk+"|"+type);
	//return false ;
		new Ajax.Request('/inc/ajax/productAjax.php',
		{
			method:'post',
			parameters:
			{
				action: "submitProductPrice",
				data: str,
				seed: seed ()
			},
			onSuccess: function(r)
			{
				
				$('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)
				{
					
					$('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	   	   = $("price_"+productPriceId).value;
	var price_qty	   = $("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)
			{
				$('productPriceDisplay').innerHTML = r.responseText;
				setTimeout("jClearMessage('productPriceMsg');", 2000);
			}
		}
	);

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

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

function jUpdateAllPriceForProduct(sender)
{
	var productId = sender.id;
	var str = '';
	var elem = $("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);
				$('productPriceDisplay').innerHTML = r.responseText;
				setTimeout("jClearMessage('productPriceMsg');", 2000);
			}
		}
	);
}
function jClearMessage(msgDiv)
{
	$(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(!jValidatePostedRadioButton())
	{
		err += "Post Can NOT Be Blank!\n";
	}
	/*if(!jValidatePPSRemoteRadioButton())
	{
		err += "Remote Site Can NOT Be Blank!\n";
	}*/
	if(!jValidatePPSSiteRadioButton())
	{
		err += "PPS Site Can NOT Be Blank!\n";
	}
	if($("title").value == "")
	{
		err += "Title Can NOT Be Blank!\n";
	}
	if(err == "")
	{
		return true;
	}
	else
	{
		alert(err);
		return false;
	}
	
}

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

function jValidatePPSSiteRadioButton()
{
	var radioInput = $("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($("category").value == "")
	{
		err += "Category Can NOT Be Blank!\n";
	}
	if(err == "")
	{
		return true;
	}
	else
	{
		alert(err);
		return false;
	}
}

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

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

function jValidatePriceStructure()
{
	if($("qty").value == "" || $("percentage").value == "")
	{
		alert("Quantity AND Percentage MUST NOT be Blank");
		return false;
	}
	return true;
}

function jSortProductByCatalogue()
{
	
	$("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; 
}
/*END Product Admin Side Related Function */


