var admin = {};

admin.active_basket = null;
admin.caller_elm = null;
var my_int = 0;

admin.mark_as_shipped = function(obj, order) {
	var ajax = new ajax_request();
	
	ajax.call_back = "admin.mark_as_shipped_response";
	ajax.file_path = "templates/eshop.php";
	ajax.class_name = "eshop_template";
	ajax.method_name = "x_mark_as_shipped";
	ajax.register_argument("order", order);
	
	var xmlhttp = ajax.send();
}

admin.mark_as_shipped_response = function(XmlHttp, attributes) {
	//alert(XmlHttp.responseText);
	window.location = window.location;
}

admin.show_edited_basket = function(obj, order) {
	var basket_inline = document.getElementById("basket_inline_" + order);
	
	if (basket_inline == null) {
		if (admin.active_basket != null){
			admin.active_basket.parentNode.removeChild(admin.active_basket);
		}
		
		var ajax = new ajax_request();
		ajax.call_back = "";
		ajax.async = false;
		ajax.file_path = "templates/basket.php";
		ajax.class_name = "basket_template";
		ajax.method_name = "x_render_edited";
		ajax.register_argument("order", order);
		var xmlhttp = ajax.send();
		
		var coord = DocumentDOM.GetCoordinates(obj.parentNode.parentNode.parentNode);
		
		var layer = document.createElement("div");
		layer.setAttribute("id", "basket_inline_" + order);
		layer.style.position = "absolute";
		layer.style.backgroundColor = "#F8F8F8";
		layer.style.width = obj.parentNode.parentNode.parentNode.offsetWidth + "px";
		layer.style.left = coord[0] + "px";
		
		if (xmlhttp.responseText.indexOf("__FAIL__") == -1) {
			layer.innerHTML = xmlhttp.responseText;
		}
		else {
			alert(xmlhttp.responseText.split("__FAIL__")[0]);
		}
		
		admin.active_basket = layer;
		admin.caller_elm = obj.getElementsByTagName("span")[0];
		
		obj.parentNode.appendChild(layer);
	}
	else {
		var parent = document.getElementById("temp_basket_" + order);
		
		if (parent != null) {
			admin.active_basket = null;
			parent.removeChild(basket_inline);
		}
	}
}

admin.show_basket = function(obj, order){
	var basket_inline = document.getElementById("basket_inline_" + order);
	
	if (basket_inline == null) {
		if(admin.active_basket != null){
			admin.active_basket.parentNode.removeChild(admin.active_basket);
		}
		
		var ajax = new ajax_request();
		ajax.call_back = "";
		ajax.async = false;
		ajax.file_path = "templates/basket.php";
		ajax.class_name = "basket_template";
		ajax.method_name = "x_render";
		ajax.register_argument("order", order);
		var xmlhttp = ajax.send();
		
		var coord = DocumentDOM.GetCoordinates(obj.parentNode.parentNode.parentNode);
		
		var layer = document.createElement("div");
		layer.setAttribute("id", "basket_inline_" + order);
		layer.style.position = "absolute";
		layer.style.backgroundColor = "#F8F8F8";
		layer.style.width = obj.parentNode.parentNode.parentNode.offsetWidth + "px";
		layer.style.left = coord[0] + "px";
		
		if (xmlhttp.responseText.indexOf("__FAIL__") == -1) {
			layer.innerHTML = xmlhttp.responseText;
		}
		else {
			alert(xmlhttp.responseText.split("__FAIL__")[0]);
		}
		
		admin.active_basket = layer;
		admin.caller_elm = obj.getElementsByTagName("span")[0];
		
		obj.parentNode.appendChild(layer);
	}
	else {
		var parent = document.getElementById("temp_basket_" + order);
		
		if (parent != null) {
			admin.active_basket = null;
			parent.removeChild(basket_inline);
		}
	}
}
admin.save_basket = function(){
	if(admin.active_basket != null){
		admin.active_basket.parentNode.removeChild(admin.active_basket);
	}
	
	admin.active_basket = null;
	
	var ajax = new ajax_request();
	ajax.call_back = "";
	ajax.async = false;
	ajax.file_path = "templates/basket.php";
	ajax.class_name = "basket_template";
	ajax.method_name = "x_update";
	var xmlhttp = ajax.send();
	
	admin.caller_elm.innerHTML = xmlhttp.responseText;
}
admin.pay = function(obj, order, obj_id){
	
	var ajax = new ajax_request();
	ajax.call_back = "";
	ajax.async = false;
	ajax.file_path = "templates/basket.php";
	ajax.class_name = "basket_template";
	ajax.method_name = "x_pay";
	ajax.register_argument("order", order);
	if(obj.src.match("unchecked")){
		var xmlhttp = ajax.send();
		if(obj.src.match("unchecked")){
			obj.src = obj.src.replace("unchecked", "checked");
		}else{
			obj.src = obj.src.replace("checked", "unchecked");
		}
	}
	
	window.location = window.location;
}
admin.publicate_invoice = function(obj, order, obj_id){
	
	var ajax = new ajax_request();
	ajax.call_back = "";
	ajax.async = false;
	ajax.file_path = "templates/basket.php";
	ajax.class_name = "basket_template";
	ajax.method_name = "x_publicate_invoice";
	ajax.register_argument("order", order);
	ajax.register_argument("invoice", document.getElementById(obj_id + "invoice_num").value);
	ajax.register_argument("proforma_invoice", document.getElementById(obj_id + "proforma_num").value);
	ajax.register_argument("date_of_payment", document.getElementById(obj_id + "date_of_payment").value);
	if(obj.src.match("unchecked")){
		var xmlhttp = ajax.send();
		if(obj.src.match("unchecked")){
			obj.src = obj.src.replace("unchecked", "checked");
		}else{
			obj.src = obj.src.replace("checked", "unchecked");
		}
	}
	
	window.location = window.location;
	
//	var inp = document.getElementsByTagName("input");
//	for(var z = 0; z < inp.length; z++){
//		if(inp.id.match("invoice_num") && inp.id != obj.id + "invoice_num"){
//			inp.innerHTML = 
//		}
//	}
}

admin.fill_proforma_info = function(order) {
	var pi_obj = document.getElementById("proforma_info_holder");
	
	if (pi_obj != null) {
		var ajax = new ajax_request();
		ajax.call_back = "admin.fill_proforma_info_response";
		ajax.file_path = "templates/eshop.php";
		ajax.class_name = "eshop_template";
		ajax.method_name = "x_fill_proforma";
		ajax.register_argument("order", order);
		ajax.attributes = new Array();
		ajax.attributes[0] = pi_obj;
		ajax.attributes[1] = order;
		ajax.send();
		
		admin.request_in_progress++;
	}
}

admin.fill_proforma_info_response = function(XmlHttp, attributes) {
	obj = attributes[0];
	order = attributes[1];
	
	if (obj.style.display == "none") {
		var hidden = document.getElementById("order_id_inp");
		hidden.value = order;
		
		response_arr = new Array();
		response_arr = XmlHttp.responseText.split("###");
		
		var bar = document.getElementById("proforma_info_bar");
		
		if (bar != null) {
			bar.innerHTML = response_arr[0];
		}
		
		var courier = document.getElementById("proforma_info_courier_inp");
		
		if (courier != null) {
			//courier.value = response_arr[1];
			courier.innerHTML = response_arr[1];
		}
		
		var row = document.getElementById("row_" + order);
		
		offset_w = ((row != null) ? row.offsetLeft : 0);
		offset_h = ((row != null) ? row.offsetTop : 0);
		
		obj.style.left = 0 + offset_w + "px";
		obj.style.top = 230 + offset_h + "px";
		obj.style.display = "block";
	}
	
	admin.request_in_progress--;
}

admin.close_proforma_info_holder = function() {
	var obj = document.getElementById("proforma_info_holder");
	
	if (obj != null) {
		obj.style.display = "none";
		
		var calendar = document.getElementById("CalendarPopUp");
		
		if (calendar != null) {
			calendar.style.display = "none";
		}
	}
}

admin.change_status = function(obj, order){
	
	var ajax = new ajax_request();
	ajax.call_back = "";
	ajax.async = false;
	ajax.file_path = "templates/basket.php";
	ajax.class_name = "basket_template";
	ajax.method_name = "x_change_order_status";
	ajax.register_argument("order", order);
	ajax.register_argument("invoice", document.getElementById(obj.id + "invoice_num").value);
	ajax.register_argument("proforma_invoice", document.getElementById(obj.id + "proforma_num").value);
	if(obj.src.match("unchecked")){
		var xmlhttp = ajax.send();
		/*
		var usr = document.getElementById(obj.id + "slave");
		if(obj.src.match("unchecked")){
			obj.src = obj.src.replace("unchecked", "checked");
			usr.src = usr.src.replace("unchecked", "checked");
		}else{
			obj.src = obj.src.replace("checked", "unchecked");
			usr.src = usr.src.replace("checked", "unchecked");
		}
		*/
	}
	
	window.location = window.location;
	
//	var inp = document.getElementsByTagName("input");
//	for(var z = 0; z < inp.length; z++){
//		if(inp.id.match("invoice_num") && inp.id != obj.id + "invoice_num"){
//			inp.innerHTML = 
//		}
//	}
}

admin.user_approve = function(obj, order) {
	var ajax = new ajax_request();
	ajax.call_back = "";
	ajax.async = false;
	ajax.file_path = "templates/basket.php";
	ajax.class_name = "basket_template";
	ajax.method_name = "x_user_approve";
	ajax.register_argument("order", order);
	
	if (obj.src.match("unchecked")) {
		var xmlhttp = ajax.send();
	}
	
	window.location = window.location;
}

admin.valid_delivery_date = function(delivery_date, invoice_id) {
	valid = "yes";
	
	inv_arr = invoice_id.split("inv_");
	inv_id = inv_arr[1].split("date_delivery");
	
	if (delivery_date[4] == "-" && delivery_date[7] == "-" && delivery_date.length == 10) {
		var date_arr = new Array();
		date_arr = delivery_date.split("-");
		
		if (!isNaN(parseInt(date_arr[0]))) {
			if (!isNaN(parseInt(date_arr[1])) && parseInt(date_arr[1]) > 0 && parseInt(date_arr[1]) < 13) {
				if (!isNaN(parseInt(date_arr[2])) && parseInt(date_arr[2]) > 0 && parseInt(date_arr[2]) < 32) {
					var ajax = new ajax_request();
					ajax.call_back = "";
					ajax.async = false;
					ajax.file_path = "templates/basket.php";
					ajax.class_name = "basket_template";
					ajax.method_name = "x_change_delivery_date";
					ajax.register_argument("delivery_date", delivery_date);
					ajax.register_argument("invoice_id", inv_id[0]);
					
					var xmlhttp = ajax.send();
					
					return valid;
				}
				else {
					valid = "no";
				}
			}
			else {
				valid = "no";
			}
		}
		else {
			valid = "no";
		}
	}
	else {
		valid = "no";
	}
	
	if (valid == "no") {
		return valid;
	}
}

admin.mass_receiver = function(type_id, obj_id) {
	if (type_id != "-1") {
		var ajax = new ajax_request();
		ajax.call_back = "admin.mass_receiver_list";
		ajax.file_path = "templates/basket.php";
		ajax.class_name = "basket_template";
		ajax.method_name = "mass_list";
		ajax.register_argument("type_id", type_id);
		ajax.attributes = new Array();
		ajax.attributes[0] = obj_id;
		ajax.send();
		
		admin.request_in_progress++;
	}
	else {
		var obj = document.getElementById(obj_id);
		obj.innerHTML = '';
		//var val = document.getElementById('mass_receiver_id').value;
		//alert(val);
	}
}

admin.mass_receiver_list = function(XmlHttp, attributes) {
	var obj = document.getElementById(attributes[0]);
	obj.innerHTML = XmlHttp.responseText;
	
	admin.request_in_progress--;
}

admin.change_colors = function(obj_id, old_obj_id) {
	var old_obj = document.getElementById(old_obj_id);
	id = old_obj.value;
	
	var obj = document.getElementById(id);
	
	if (obj != null) {
		obj.style.background = '#ffffff';
	}
	
	var obj2 = document.getElementById(obj_id);
	obj2.style.background = '#cccccc';
}

admin.use_message_template = function(template_id, target_id) {
	var ajax = new ajax_request();
	ajax.call_back = "admin.use_message_template_response";
	ajax.file_path = "templates/basket.php";
	ajax.class_name = "basket_template";
	ajax.method_name = "use_message_template";
	ajax.register_argument("template_id", template_id);
	ajax.attributes = new Array();
	ajax.attributes[0] = target_id;
	ajax.send();
	
	admin.request_in_progress++;
}

admin.use_message_template_response = function(XmlHttp, attributes) {
	var obj = document.getElementById(attributes[0]);
	
	if (obj != null) {
		obj.value = XmlHttp.responseText;
	}
}

admin.get_product = function(product_code, target_id) {
	var ajax = new ajax_request();
	ajax.call_back = "admin.set_product";
	ajax.file_path = "templates/basket.php";
	ajax.class_name = "basket_template";
	ajax.method_name = "get_product";
	ajax.register_argument("product_code", product_code);
	ajax.attributes = new Array();
	ajax.attributes[0] = target_id;
	ajax.send();
	
	admin.request_in_progress++;
}

admin.set_product = function(XmlHttp, attributes) {
	var obj = document.getElementById(attributes[0]);
	obj.innerHTML = XmlHttp.responseText;
	
	admin.request_in_progress--;
}

admin.del_stats = function(return_url) {
	final_str = "";
	
	var obj = document.getElementById("stats_form");
	
	for (z = 0; z < obj.length; z++) {
		if (obj[z].type == 'checkbox' && obj[z].name.indexOf("#|") != -1 && obj[z].checked) {
			if (z > 0) {
				final_str += "#@";
			}
			
			final_str += obj[z].name;
		}
	}
	
	if (final_str != "") {
		var ajax = new ajax_request();
		ajax.call_back = "admin.del_done";
		ajax.file_path = "templates/search_stats.php";
		ajax.class_name = "search_stats_template";
		ajax.method_name = "del_stats";
		ajax.register_argument("final_str", final_str);
		ajax.attributes = new Array();
		ajax.attributes[0] = return_url;
		ajax.send();
		
		admin.request_in_progress++;
	}
}

admin.del_done = function(XmlHttp, attributes) {
	admin.request_in_progress--;
	
	document.location.href = '?' + attributes[0];
}

admin.show_service_form = function(service_id, send_str, close_str) {
	var obj = document.getElementById("form_" + service_id);
	var btn = document.getElementById("btn_" + service_id);
	var current = obj.style.display;
	
	if (current == 'none') {
		obj.style.display = 'block';
		btn.innerHTML = close_str;
	}
	else {
		obj.style.display = 'none';
		btn.innerHTML = send_str;
	}
}

admin.service_submit = function(group_id, subgroup_id) {
	var element = document.createElement("input");
	element.setAttributeNode(admin.createHtmlAttribute("type", "hidden"));
	element.setAttributeNode(admin.createHtmlAttribute("name", "action"));
	element.setAttributeNode(admin.createHtmlAttribute("value", "submit"));
	document.forms["service_form_" + subgroup_id].appendChild(element);
	
	var element = document.createElement("input");
	element.setAttributeNode(admin.createHtmlAttribute("type", "hidden"));
	element.setAttributeNode(admin.createHtmlAttribute("name", "group_id"));
	element.setAttributeNode(admin.createHtmlAttribute("value", group_id));
	document.forms["service_form_" + subgroup_id].appendChild(element);
	
	var element = document.createElement("input");
	element.setAttributeNode(admin.createHtmlAttribute("type", "hidden"));
	element.setAttributeNode(admin.createHtmlAttribute("name", "subgroup_id"));
	element.setAttributeNode(admin.createHtmlAttribute("value", subgroup_id));
	document.forms["service_form_" + subgroup_id].appendChild(element);
	
	document.forms["service_form_" + subgroup_id].submit();
}

admin.createHtmlAttribute = function(name, value) {
   var attribute = document.createAttribute(name);
   attribute.nodeValue = value;
   
   return attribute;
}

admin.newsletter_subscribe = function(email_id) {
	var ajax = new ajax_request();
	ajax.call_back = "admin.newsletter_response";
	ajax.file_path = "templates/home.php";
	ajax.class_name = "home_template";
	ajax.method_name = "newsletter_subscribe";
	ajax.register_argument("email_id", email_id);
	
	ajax.send();
	
	admin.request_in_progress++;
}

admin.newsletter_response = function(XmlHttp, attributes) {
	document.getElementById("nl_error").innerHTML = XmlHttp.responseText;
	
	admin.request_in_progress--;
}

admin.delete_request_order = function(obj_id) {
	var ajax = new ajax_request();
	ajax.call_back = "admin.delete_request_order_response";
	ajax.file_path = "templates/order_status.php";
	ajax.class_name = "order_status_template";
	ajax.method_name = "delete_request";
	ajax.register_argument("obj_id", obj_id);
	
	ajax.send();
	
	admin.request_in_progress++;
}

admin.delete_request_order_response = function(XmlHttp, attributes) {
	var obj = document.getElementById("line_" + XmlHttp.responseText);
	obj.style.display = "none";
	
	admin.request_in_progress--;
}

admin.change_presentation = function(id, schedule) {
	var ajax = new ajax_request();
	ajax.call_back = "admin.change_presentation_response";
	ajax.file_path = "templates/basetemplate.php";
	ajax.class_name = "basetemplate";
	ajax.method_name = "new_presentation";
	ajax.register_argument("id", id);
	
	ajax.attributes = new Array();
	ajax.attributes[0] = id;
	ajax.attributes[1] = schedule;
	
	ajax.send();
	
	admin.request_in_progress++;
}

admin.change_presentation_response = function(XmlHttp, attributes) {
	var obj = document.getElementById("presentation_holder");
	obj.innerHTML = XmlHttp.responseText;
	
	admin.request_in_progress--;
	
	if (attributes[1] != 0) {
		admin.schedule_presentation(attributes[0]);
	}
}

admin.schedule_presentation = function(id) {
	var ajax = new ajax_request();
	ajax.call_back = "admin.schedule_presentation_response";
	ajax.file_path = "templates/basetemplate.php";
	ajax.class_name = "basetemplate";
	ajax.method_name = "schedule_presentation";
	ajax.register_argument("id", id);
	
	ajax.send();
	
	admin.request_in_progress++;
}

admin.schedule_presentation_response = function(XmlHttp, attributes) {
	if (XmlHttp.responseText != '') {
		tmp_arr = XmlHttp.responseText.split("|");
		
		id = tmp_arr[0];
		time = tmp_arr[1];
		
		my_int = setTimeout('admin.change_presentation(' + id + ', 1)', time);
	}
	
	admin.request_in_progress--;
}

admin.pres_select = function(id) {
	clearTimeout(my_int);
	
	admin.change_presentation(id, 0);
}
