// JavaScript Document
var load_new_products = function(ajax_url){
	url = base64_decode(ajax_url);
	$('cartPan').update("<div align='center'><img src='images/ajax-loader.gif' alt='Please wait' width='32' height='32' /></div>")
	new Ajax.Updater('cartPan', url, {
		method : 'get',
		onFailure : function(transport){
			alert(transport.responseText);
		}
	});
}

var load_home_products = function(ajax_url){
	url = base64_decode(ajax_url);
	////alert(url);
	$('home_content_products').update("<div align='center'><img src='images/ajax-loader.gif' alt='Please wait' width='32' height='32' /></div>")
	new Ajax.Updater('home_content_products', url, {
		method : 'get',
		onFailure : function(transport){
			alert(transport.responseText);
		}
	});
}

var semi_accordian = function(toOpen, toCloseA, toCloseB){
	
	if($(toOpen).style.display == "none"){
		$(toOpen).show();
	}
	if($(toCloseA).style.display != "none"){
		$(toCloseA).hide();
	}
	if($(toCloseB).style.display != "none"){
		$(toCloseB).hide();
	}
}

var check_product = function(name,cpid){
	var url = "ajaxserver/user/check_product.php";
	var P = "name="+name+"&cpid="+cpid;
	if(!name.blank()){
		if($('check_item').value == 1){
			new Ajax.Request(url,{
							method : 'post',
							parameters : P,
							onSuccess : function(transport){
								var res = transport.responseText;
								if(res == 1){
									var check_url = 'ajaxserver/user/window_product_message.php';
									create_modal(check_url,'350','200',true);
								}
							}
						 });
		}
	}
	
}

var load_myproducts = function(product_id){
	url = "ajaxserver/user/load_myproducts.php";
	////alert(url);
	$('list_myproducts').update("<div align='center'><img src='images/ajax-loader.gif' alt='Please wait' width='32' height='32' /></div>")
	new Ajax.Updater('list_myproducts', url, {
		method : 'get',
		parameters : { selected_id : product_id },
		onFailure : function(transport){
			alert(transport.responseText);
		}
	});
}

var get_point = function(val,mul){
	if(!val.blank()){
		var v = parseFloat(val);
		var p = Math.round(v);
		$('product_points').value = (p*parseInt(mul));
	}
	
}

var change_img = function(encoded_src,encoded_bsrc){
	var src = base64_decode(encoded_src);
	var bsrc = base64_decode(encoded_bsrc);
	var P = "src="+src+"&bsrc="+bsrc;
	new Ajax.Updater('big_image', 'ajaxserver/user/image.php', {
		method : 'get',
		parameters : P
	});
}


var do_ajax_search = function(){
	var params = "do_srch=1";
	if(!$('srch_main_txt').value.blank() && ($('srch_main_txt').value != 'search for items')){
		params += "&srch_word="+$('srch_main_txt').value;
	}
	/*if(!$('search_by').value.blank()){
		params += "&search_by="+$('search_by').value;
	}*/
	if(!$('cat_id').value.blank()){
		params += "&cat_id="+$('cat_id').value;
	}
	if(!$('price').value.blank()){
		params += "&price="+$('price').value;
	}
	var u = $('search_url').value;
	var url = base64_encode(u+'?params='+params+'');
	load_products(""+url+"",'PageIndex=1&RecordPerPage=10','load_products','ShowPaging');
}



var load_products = function(encoded_url,ajax_params,container,pagidiv){
	$(container).update("<div align='center'><img src='images/ajax-loader.gif' alt='Please wait' width='32' height='32' /></div>");
	var debug = false;
	////alert(encoded_url);
	var url = base64_decode(encoded_url);
	////alert(url);
	var splitted_url = url.split("?");
	var requester = splitted_url[0];
	var extras = splitted_url[1];
	var params = ajax_params+"&"+extras;
	////alert(params);
	new Ajax.Request(url, {
					 	method : 'get',
						parameters : params,
						onSuccess : function(transport){
							$(container).update(transport.responseText);
							
						},
						onFailure : function(transport){
							alert(transport.responseText);
						}
					 });
}

var load_suggesstions = function(product_key){
	var url = "ajaxserver/user/suggessted_products.php";
	new Ajax.Updater('suggested_products',url,
						 {
							method : 'get',
							parameters : { product_key : product_key }
						 }
					 );
}




var check_product_frm = function(){
	
	//$('usps').disabled = false;
	////alert(frm);
	//make all updaters to blank
	$("cat_id_error").update("");
	$("product_name_error").update("");
	$("product_desc_error").update("");
	$("meta_keyword_error").update("");
	$("stock_error").update("");
	$("price_error").update("");
	$("handling_charge_error").update("");
	$("dimension_error").update("");
	$("weight_error").update("");
	$("shipping_error").update("");
	$("dimension_unit_error").update("");
	$("weight_unit_error").update("");
	
	var bool = true;
	
	if($("cat_id").value.blank()){
		$("cat_id_error").update("Required");
		bool = false;
	}
	if($("product_name").value.blank()){
		$("product_name_error").update("Required");
		bool = false;
	}else{
		if(tagvalidate($("product_name").value)){
			$("product_name_error").update("HTML not allowed");
			bool = false;
		}
	}
	if($("product_desc").value.blank()){
		$("product_desc_error").update("Required");
		bool = false;
	}else{
		if(tagvalidate($("product_desc").value)){
			$("product_desc_error").update("HTML not allowed");
			bool = false;
		}
	}
	
	if($("meta_keyword").value.blank()){
		$("meta_keyword_error").update("Required");
		bool = false;
	}else{
		if(tagvalidate($("meta_keyword").value)){
			$("meta_keyword_error").update("HTML not allowed");
			bool = false;
		}
	}
	if($("stock").value.blank() || (parseInt($("stock").value) <= 0)){
		$("stock_error").update("Required");
		bool = false;
	}
	if($("price").value.blank() || (parseFloat($("price").value) <= 0)){
		$("price_error").update("Required");
		bool = false;
	}else{
		if((parseFloat($("price").value) < parseFloat($("mim_upload_amount").value))){
			$("price_error").update("Price cannot be less than $"+$("mim_upload_amount").value);
			bool = false;
		}
	}
	if($("dimension_unit").value.blank()){
		$("dimension_unit_error").update("Required");
		bool = false;
	}
	if($("weight_unit").value.blank()){
		$("weight_unit_error").update("Required");
		bool = false;
	}
	
	if($("handling_charge").value.blank()){
		$("handling_charge_error").update("Required");
		bool = false;
	}
	if($("width").value.blank() || $("length").value.blank() || $("height").value.blank() ||(parseFloat($("width").value) <= 0) || (parseFloat($("length").value) <= 0) || (parseFloat($("weight").value) <= 0)){
		$("dimension_error").update("All the above three are required and cannot be less than or equal to zero");
		bool = false;
	}
	if($("weight").value.blank() || (parseFloat($("weight").value) <= 0)){
		$("weight_error").update("Weight required");
		bool = false;
	}	
	//if(($("royal_mail").checked == false) && ($("power_force").checked == false)){
//		$("shipping_error").update("Atleast one shipping method required");
//		bool = false;
//	}
	
	return bool;
}

var save_product = function(){
	var product_id = $('sproduct_id').value;
	var url = base64_decode($('saver').value);
	var frm = $('product_master_frm').serialize();
	//$('usps').disabled = false;
	////alert(frm);
	//make all updaters to blank
	$("cat_id_error").update("");
	$("product_name_error").update("");
	$("product_desc_error").update("");
	$("meta_keyword_error").update("");
	$("stock_error").update("");
	$("price_error").update("");
	$("handling_charge_error").update("");
	$("dimension_error").update("");
	$("weight_error").update("");
	$("shipping_error").update("");
	$("dimension_unit_error").update("");
	$("weight_unit_error").update("");
	
	var bool = true;
	
	if($("cat_id").value.blank()){
		$("cat_id_error").update("Required");
		bool = false;
	}
	if($("product_name").value.blank()){
		$("product_name_error").update("Required");
		bool = false;
	}
	if($("product_desc").value.blank()){
		$("product_desc_error").update("Required");
		bool = false;
	}else{
		if(tagvalidate($("product_desc").value)){
			$("product_desc_error").update("HTML not allowed");
			bool = false;
		}
	}
	
	if($("meta_keyword").value.blank()){
		$("meta_keyword_error").update("Required");
		bool = false;
	}else{
		if(tagvalidate($("meta_keyword").value)){
			$("meta_keyword_error").update("HTML not allowed");
			bool = false;
		}
	}
	if($("stock").value.blank() || (parseInt($("stock").value) <= 0)){
		$("stock_error").update("Required");
		bool = false;
	}
	if($("price").value.blank() || (parseFloat($("price").value) <= 0)){
		$("price_error").update("Required");
		bool = false;
	}else{
		if((parseFloat($("price").value) < parseFloat($("mim_upload_amount").value))){
			$("price_error").update("Price cannot be less than $"+$("mim_upload_amount").value);
			bool = false;
		}
	}
	if($("handling_charge").value.blank()){
		$("handling_charge_error").update("Required");
		bool = false;
	}
	if($("dimension_unit").value.blank()){
		$("dimension_unit_error").update("Required");
		bool = false;
	}
	if($("weight_unit").value.blank()){
		$("weight_unit_error").update("Required");
		bool = false;
	}
	
	if($("handling_charge").value.blank() || (parseFloat($("handling_charge").value) <= -1))
	{
		$("handling_charge_error").update("Required");
		bool = false;
	}
	
	
	if($("width").value.blank() || $("length").value.blank() || $("height").value.blank() ||(parseFloat($("width").value) <= 0) || (parseFloat($("length").value) <= 0) || (parseFloat($("weight").value) <= 0)){
		$("dimension_error").update("All the above three are required and cannot be less than or equal to zero");
		bool = false;
	}
	if($("weight").value.blank() || (parseFloat($("weight").value) <= 0)){
		$("weight_error").update("Weight required");
		bool = false;
	}
	//if(($("royal_mail").checked == false) && ($("power_force").checked == false)){
//		$("shipping_error").update("Atleast one shipping method required");
//		bool = false;
//	}
//alert(frm);
	if(bool){
		$('p_save_but').hide();
		$('p_save_load').show();
		new Ajax.Request(url, {
						 	method : 'post',
							parameters : frm,
							onSuccess : function(transport){
								var res = transport.responseText;
								//alert(res);
								var resArr = res.split("|");
								if(resArr[0] == 0){
									if(resArr[2] == 'edit'){
										scroll(0,0);
										$('product_succ_msg').update("Product updated");
										//var product_id = $("sproduct_id").value;
										load_myproducts(resArr[1]);
										$('p_save_but').show();
										$('p_save_load').hide();
									}
									
								}
							},
							onFailure : function(transport){
								var res = transport.responseText;
								//alert(res);
							}
						 });
	}
}

var save_product_suggest = function(product_id){
	////alert("Hi");
	var combo_val = $('sugg').value;
	var P = Form.serialize('product_suggest_frm');
	var add_url = 'ajaxserver/user/save_product_suggest.php';
	////alert(P);
	new Ajax.Request(add_url, {
						method : 'get',
						parameters : P,
						onSuccess : function(transport){
							/*var res = transport.responseText;
							//alert(res);*/
							$('suggest_succ_msg').update("Suggestions saved");
							setTimeout("$('suggest_succ_msg').update('');", 5000);
						}
					 });
	//}
}

var show_att_block = function(attribute_id,product_id){
	$('att_frm').show();
	if(attribute_id == 0){
		$('header_txt_att').update('Add Attribute');
		$('mode').value = 'add';
	}else{
		$('header_txt_att').update('Edit Attribute');
		$('mode').value = 'edit';		
	}
	$('attribute_id').value = attribute_id;
	$('att_product_id').value = product_id;
	load_att_form_fields(attribute_id);
}

var load_att_form_fields = function(attribute_id){
	new Ajax.Request('ajaxserver/user/load_att_form_fields.php', {
					 	method : 'get',
						parameters : { attribute_id : attribute_id },
						onSuccess : function(transport){
							Dialog.closeInfo();
							var res = transport.responseText;
							////alert(res);
							//first split
							var res_arr = res.split("&");
							////alert(res_arr[1]);
							var field_arr = res_arr[0].split("|");
							var value_arr = res_arr[1].split("|");
							var limit = field_arr.length;
							var i;
							for(i=0; i<=limit; i++){
								$(field_arr[i]).value = value_arr[i];
							}
							
						}
					 });
}

var save_attributes = function(){	
	var ajax_url = "ajaxserver/user/save_attributes.php";
	
	var mode = $('mode').value;
	var product_id = $('att_product_id').value;
	
	var attribute_id = $('attribute_id').value;	
	
	var attribute_name = $('attribute_name');
	
	var attribute_name_error = $('attribute_name_error');	
	var attribute_value = $('attribute_value');
	var attribute_value_error = $('attribute_value_error');		
	var formID = 'save_attributes_frm';	
	
	var success_div = $('spec_succ_msg');	
	//alert(formID);
	var P = Form.serialize('save_attributes_frm');
	
	////alert($('cat_drop_box').innerHTML);	
	var bool = true;	
	if(attribute_name.value.blank() || attribute_value.value.blank()){
		bool = false;
		attribute_value_error.update("Both are required fields");
	}
	if(tagvalidate(attribute_name.value) || tagvalidate(attribute_value.value)){
		bool = false;
		attribute_value_error.update("HTMLs not allowed");
	}
	if(bool){
		$("att_btns").hide();
		$("att_ldr").show();
		new Ajax.Request(ajax_url, {
						 	method : 'get',
							parameters : P,
							onSuccess : function(transport){
								$("att_btns").show();
								$("att_ldr").hide();
								////alert(transport.responseText);
								if(transport.responseText == 1){
									attribute_name_error.update("Attribute Name exists");
									attribute_name.select();
								}else{
									success_div.update("Attribute saved");
									var TheURL = base64_encode("ajaxserver/user/load_product_attributes.php");
									var Params = base64_encode("att_product_id="+product_id);
									load_dhtml('product_attributes', TheURL, Params);
									setTimeout("$('spec_succ_msg').update('');", 5000);									
									
									
									
								}
							}
						 });
	}
}

var close_att_box = function(){
	$('att_frm').hide();
}

var delete_att = function(attribute_id,product_id){
	var ajax_url = "ajaxserver/user/delete_attributes.php";
	$("action_handlers").hide();
	$("action_ldr").show();
	new Ajax.Request(ajax_url, {
					 	method : 'get',
						parameters : { attribute_id : attribute_id },
						onSuccess : function(transport){
							$('spec_succ_msg').update("Attribute deleted");
							var TheURL = base64_encode("ajaxserver/user/load_product_attributes.php");
							var Params = base64_encode("att_product_id="+product_id);
							load_dhtml('listitem_product_spec', TheURL, Params);
							setTimeout("$('spec_succ_msg').update('');", 5000);													
						},
						onFailure : function(){
							alert("Server Error!!Please check your Network Connection");
						}
					 });
}

var product_image_upload = function (load_url, action_url, instance)
{
	var product_id = $("img_product_id_hid").value;
	////alert(val);
	Dialog.info("<div style='float:left;'><img src='images/loader_big_bar.gif' alt='Loader' title='Loader' /></div><div style-'float:left;'>Uploading ...</div>", {width:500, height:100, showProgress: true});
	//clear all error updaters
	$('uploader_error1').update("");
	/*$('uploader_error2').update("");
	$('uploader_error3').update("");
	$('uploader_error4').update("");*/
	var form = $('product_images_frm');
	//show the loader and hide the input box
	$('inputer'+instance).hide();
	$('loader'+instance).show();
	// Create the iframe...
	var iframe = document.createElement("iframe");
	iframe.setAttribute("id","upload_iframe"+instance);
	iframe.setAttribute("name","upload_iframe"+instance);
	iframe.setAttribute("width","0");
	iframe.setAttribute("height","0");
	iframe.setAttribute("border","0");
	iframe.setAttribute("style","width: 0; height: 0; border: none;");	
	// Add to document...
	form.parentNode.appendChild(iframe);
	window.frames['upload_iframe'+instance].name="upload_iframe"+instance;	
	var iframeId = document.getElementById("upload_iframe"+instance);	
	// Add event...
	var eventHandler = function()  {		
		if (iframeId.detachEvent)
			iframeId.detachEvent("onload", eventHandler);
		else
			iframeId.removeEventListener("load", eventHandler, false);		
		
		/************** RESPONSE HANDLING PART *************************/
		// Getting Server response...
		if (iframeId.contentDocument) {
			content = iframeId.contentDocument.body.innerHTML;
		} else if (iframeId.contentWindow) {
			content = iframeId.contentWindow.document.body.innerHTML;
		} else if (iframeId.document) {
			content = iframeId.document.body.innerHTML;
		}
		
		//alert(content);
		//show the input box and hide the loader
		Dialog.closeInfo();
		$('inputer'+instance).show();
		$('loader'+instance).hide();
		//handling the response html
		
		var response_arr = content.split("&amp;");
		
		var updater_arr = response_arr[1].split("|");
		//alert(updater_arr);
		var msg_arr = response_arr[2].split("|");
		//var html_content = response_arr[3];		
		for(i=0;i<=(msg_arr.length - 1);i++){
			if(updater_arr[i]!=""){
				$(updater_arr[i]).update(msg_arr[i]);														
			}
			
		}
		//alert(response_arr[0]);
		
		if(response_arr[0] == 0){
			////alert(html_content);
			
			var theURL = base64_encode(load_url);
			var param = base64_encode("img_product_id="+product_id);
			load_dhtml('product_images', theURL, param);
			load_myproducts(product_id);
			setTimeout("$('image_succ_msg').update('');", 5000);
		}
		
		
		/************** RESPONSE HANDLING PART ENDS *************************/
		
		// Delete the iframe...
		setTimeout('iframeId.parentNode.removeChild(iframeId)', 250);
	}
	
	if (iframeId.addEventListener)
		iframeId.addEventListener("load", eventHandler, true);
	if (iframeId.attachEvent)
		iframeId.attachEvent("onload", eventHandler);
	
	// Set properties of form...
	form.setAttribute("target","upload_iframe"+instance);
	form.setAttribute("action", action_url);
	form.setAttribute("method","post");
	form.setAttribute("enctype","multipart/form-data");
	form.setAttribute("encoding","multipart/form-data");
	var input = document.createElement("input");
	input.setAttribute("type", "hidden");
	input.setAttribute("name", "instance");
	input.setAttribute("value", instance);
	form.appendChild(input);
	// Submit the form...
	form.submit();	
	//document.getElementById(div_id).innerHTML = "Uploading...";
	
}


var confirm_delete = function(pid){
	$("d"+pid).hide();
	$("l"+pid).show();
	create_modal('ajaxserver/user/window_product_delete.php?p='+pid,'350','200',true);
}

var delete_product = function(p,points){
	$("xd").hide();
	$("xl").show();
	new Ajax.Request('ajaxserver/user/delete_product.php', {
					 	method : 'get',
						parameters : { p : p, points : points },
						onSuccess : function(transport){
							var res = transport.responseText;
							window.location.href='list_item.php';
							
						}
					 });
}
