// JavaScript Document
function loadSubGroups(gname, gid,selobj)
 {
	var parent =document.createElement("div");
	parent.setAttribute("width","80%");
	parent.className="groups";
	parent.setAttribute("id",gid);
	
	var a_elm= document.createElement('a');
	var cellText = document.createTextNode(unescape(gname));
	a_elm.className="grouplink";
	a_elm.setAttribute("name",gid);
	a_elm.appendChild(cellText);
	parent.appendChild(a_elm);
	
			var products_div= document.createElement('div');
			var cellText = document.createTextNode('Products');
			products_div.className="hdr1";
			products_div.setAttribute("id","'"+gid+"_child'");
			products_div.appendChild(cellText);
			parent.appendChild(products_div);
			
			var licensefee_div= document.createElement('div');
			var cellText = document.createTextNode('License Fee');
			licensefee_div.className="hdr2";
			licensefee_div.appendChild(cellText);
			parent.appendChild(licensefee_div);	
		
			var ams_div= document.createElement('div');
			var cellText = document.createTextNode('AMS*');
			ams_div.className="hdr3";
			ams_div.setAttribute("title","Annual Maintenance & Support Fee");
			ams_div.appendChild(cellText);
			parent.appendChild(ams_div);
			
	
	var child_div =document.createElement("div");
	child_div.setAttribute("width","95%");
	child_div.setAttribute("id",gid+"_child");
	child_div.className="pricelistbox";
	parent.appendChild(child_div);
	
	selobj.appendChild(parent);

	 }
function loadProducts(prdname,prdid,uri)
 {
	var selobj = document.getElementById(uri+"_child");
    var row = document.createElement("div");
	row.setAttribute("id",prdid);
	row.setAttribute("class","hlight_dis");
	selobj.appendChild(row);
	ZohoStore.get("getproductdetails", prdid);
 }

 function loadProductDetails(SKU,Price,productname,uri,productid,ams_price, opt_sku)
 {
	var selobj = document.getElementById(uri);
	var row = document.createElement("div");
	row.className="trhover";

	var cell = document.createElement("div");
	cell.className="col1";
	
	var chkbox = document.createElement('input');
	chkbox.type="checkbox";
	var newsku=uri;
	var newsku1=newsku.replace(/\D/g,'')
	chkbox.id=newsku1;
	chkbox.value=SKU;
	chkbox['onchange'] = function(){check(this,productid,SKU,uri,opt_sku)};
	cell.appendChild(chkbox);
	
	var cellText = document.createTextNode(unescape(productname));
	cell.appendChild(cellText);
	row.appendChild(cell);

	var cell = document.createElement("div");
	var cost = "$"+Price;
	var Pricenew = cost.split(".");
	var cellText = document.createTextNode(Pricenew[0]);
	cell.appendChild(cellText);
	cell.className="col2";
	row.appendChild(cell);

	var cell = document.createElement("div");
	var amscost = "$"+ams_price;
	var ams_price_new = amscost.split(".");
	if (ams_price_new=="$Included")
	{
		var cellText = document.createTextNode("Included");
	}
		else
		
		var cellText = document.createTextNode(ams_price_new[0]);
		cell.appendChild(cellText);
		cell.className="col3";
		row.appendChild(cell);
		cell.appendChild(cellText);
			
		var cell = document.createElement("div");
		row.appendChild(cell);
		
		var newdiv = document.createElement('div');
		newdiv.style.cssText = "clear:both";
		row.appendChild(newdiv);
		
		selobj.appendChild(row);
		
	var loadurl=window.location.href; // to stop the page near anchors
	if (loadurl.match("#"))
	{
	window.location=loadurl;
	}
 	}
	
function handleData(opt,rs)
 {
	var res = eval ('(' + rs + ')' );
		if(opt=="getcategories")
		{		
				handleCategories(res);	
		}
		else if(opt == "getproducts")
		{
				handleProducts(res);
		}
		else if(opt == "getsubgroups")
		{
				handleSubGroups(res);
		}
		else if(opt == "getproductdetails")
		{
				handleProductDetails(res);
		}
 }
function handleSubGroups(respobj)
{
		
		var result = respobj.response.result.subgroups.subgroup;
		var gid = "";
		var gname = "";
		var selobj = document.getElementById('tablepr_list');
		selobj.innerHTML = '';
		if(result.name) //The result is a single category
		{
						gid = result.id;
						gname = result.name;
						loadSubGroups(gname, gid,selobj);

						ZohoStore.get('getproducts',gid);
		}

		else // The result contains multiple categories
		{
						for(var i in result)
						{
										var ob = result[i];
										gid = ob.id;
										gname = ob.name;
										loadSubGroups(gname, gid,selobj);
										}
						for(var i in result)
						{
										var ob = result[i];
										gid = ob.id;
										gname = ob.name;
										ZohoStore.get('getproducts',gid);
										
										}

		}
													
}
function handleProducts(respobj)
{
				var product = respobj.response.result.products.product;
				var prdname = '';
				var prdid = '';
				
				var req_uri = respobj.response.uri;
				//req_uri=req_uri.replace("/","\,");
				var req_com=req_uri.split("/");
				var uri=req_com[5];
				if(product.name)
				{
								//Single Product
								prdname = product.name;
								prdid = product.id;
								loadProducts(prdname, prdid,uri);
				}
				else
				{
								//Multiple Products
								for(var i in product)
								{
									var prd = product[i];
									prdname = prd.name;
									prdid = prd.id;
								loadProducts(prdname, prdid,uri);
								}
				}
		
}
function handleProductDetails(respobj)
{	
				var property = respobj.response.result.product.property;
				if (!respobj.response.result.product.options)
				{
				var ams_price='Included';
				var opt_sku='';
				}
				else
				{
				var optionElement = respobj.response.result.product.options.option.optionelements;		
				var ams_price=optionElement.price;
				var opt_sku=optionElement.sku;
				}
				var SKU="";
				var Price="";
				var productid="";
				var req_uri=respobj.response.uri;
				var req_com=req_uri.split("/");
				var uri=req_com[5];
				var selobj = document.getElementById('tablepr_list');
				
								for(var i in property)
								{
									var content = property[i].content;
									var name = property[i].name;
									var reg = /\s+/g;
									
								name=name.replace(reg,"_");
								content=escape(content);
								eval ("var "+name +"=\""+content+"\";");
								}
								loadProductDetails(SKU, Price, productname, uri, productid, ams_price, opt_sku);					
								
}

function check(element,productid, SKU, uri, opt_sku) 
{
//		alert(element.id)
		var newuri=element.id;
		var divid = productid+"_hiddiv";
		var hidvalue = document.getElementById(divid);
		if (element.checked==true)
		{		
		document.getElementById(productid).className="hlight";
		var childDiv=document.getElementById('pricingid');		
		if(hidvalue)
		{
			hidvalue.innerHTML = '';
		}
		else
		{
			hidvalue =document.createElement("div");
			hidvalue.id= divid;
		}
		var input = document.createElement('input');
		input.setAttribute("name","prdid");
		input.setAttribute("value",productid);
		input.setAttribute("type","hidden");
		hidvalue.appendChild(input);
		
		var sku_input = document.createElement('input');
		sku_input.setAttribute("name","sku");
		sku_input.setAttribute("value",SKU+","+opt_sku);
		sku_input.setAttribute("type","hidden");
		hidvalue.appendChild(sku_input);
		
		var input_txt = document.createElement('input');
		input_txt.type = "text";
		input_txt.size="1";
		input_txt.value = "1";
		input_txt.name="qty";
		input_txt.className="qtybox";
		input_txt.setAttribute("type","hidden");
		hidvalue.appendChild(input_txt);
		childDiv.appendChild(hidvalue);	
		}
		else 
		{	
		document.getElementById(productid).className="hlight_dis";
			if(hidvalue)
			{
				hidvalue.innerHTML="";
			}
			return false;
		}

}


function validate()
{

	var numOfElements = document.pricingform.elements.length;
	if (numOfElements<2)
	{
	alert('Please Select a Product..!');
	return false;
	}
	else
	{
		return true;	
	}
	
}


function checkIP()
{
	if( typeof(ZohoStore) =='undefined')
	{
		location.href = "https://www.manageengine.com/me_partners.html";
	}

}
