var Event = {};

Event.RegisterEventHandler = function(obj, Name, EventCode, Bubling){
	if(typeof(Bubling) == "undefined"){
		Bubling = false;
	}
	if(document.all){
		obj.attachEvent(Name, EventCode);//element.detachEvent('onclick',spyOnUser)
	}else{
		Name = Name.substr(2, (Name.length - 2));
		obj.addEventListener(Name, EventCode, Bubling);
	}
}

Event.RemoveEventHandler = function(obj, Name, EventCode, Bubling){
	if(typeof(Bubling) == "undefined"){
		Bubling = false;
	}
	if(document.all){
		obj.detachEvent(Name, EventCode);
	}else{
		Name = Name.substr(2, (Name.length - 2));
		obj.removeEventListener(Name, EventCode, Bubling);
	}
}

switch_skin = function(skin_id) {
	skin_id = parseInt(skin_id);
	
	if (skin_id > 0 && skin_id < 5) {
		var middle = document.getElementById("middle");
		
		if (middle != null) {
			middle.style.background = 'url("images/middle_skin_0' + skin_id + '.jpg") repeat-x scroll center top transparent';
			
			var ajax = new ajax_request();
			ajax.call_back = "switch_skin_response";
			ajax.file_path = "includes/base.php";
			ajax.class_name = "base";
			ajax.method_name = "x_switch_skin";
			ajax.register_argument("skin_id", skin_id);
			var xmlhttp = ajax.send();
		}
	}
}

switch_skin_response = function(XmlHttp, attributes) {
	if (XmlHttp.responseText != '') {
		//
	}
}

add_to_basket = function(product_id, className) {
	if (parseInt(product_id) > 0) {
		var ajax = new ajax_request();
		ajax.call_back = "add_to_basket_response";
		ajax.file_path = "includes/base.php";
		ajax.class_name = "base";
		ajax.method_name = "x_add_to_basket";
		ajax.attributes = new Array();
		ajax.attributes[0] = product_id;
		ajax.attributes[1] = className;
		ajax.register_argument("product_id", product_id);
		
		if (className == 'order_btn') {
			var color_picker = document.getElementById("color_picker");
			var size_picker = document.getElementById("size_picker");
			var pro_width = document.getElementById("pro_width");
			var pro_length = document.getElementById("pro_length");
			
			if (color_picker != null) {
				ajax.register_argument("colorID", parseInt(color_picker.value));
			}
			
			if (size_picker != null) {
				ajax.register_argument("sizeID", parseInt(size_picker.value));
			}
			
			if (pro_width != null) {
				ajax.register_argument("width", pro_width.value);
			}
			
			if (pro_length != null) {
				ajax.register_argument("length",pro_length.value);
			}
		}
		
		var xmlhttp = ajax.send();
	}
}

add_to_basket_response = function(XmlHttp, attributes) {
	var product_id = parseInt(attributes[0]);
	
	if (XmlHttp.responseText != '' && XmlHttp.responseText.indexOf('___###___') != -1 && product_id > 0) {
		var tmp = XmlHttp.responseText.split('___###___');
		
		var order = document.getElementById("order");
		
		if (order != null) {
			order.innerHTML = tmp[0];
		}
		/*
		var add_icon = document.getElementById("add_icon");
		
		if (add_icon != null) {
			add_icon.className = attributes[1] + " disabled";
		}
		*/
		var message_box = document.getElementById("msg_" + product_id);
		
		if (message_box != null) {
			message_box.innerHTML = tmp[1];
		}
	}
}

change_image = function(image_path, image_link, loading_text) {
	if (image_path != '') {
		var image_box = document.getElementById("image_box");
		
		if (image_box != null) {
			image_box.src = image_path;
		}
		
		var image_link = document.getElementById("image_link");
		
		if (image_link != null) {
			image_link.href = image_link;
		}
	}
}

pick_color = function(productID, colorID) {
	if (parseInt(colorID) > -1 && parseInt(productID) > 0) {
		var productID = parseInt(productID);
		var colorID = parseInt(colorID);
		
		var ajax = new ajax_request();
		ajax.call_back = "pick_color_response";
		ajax.file_path = "includes/base.php";
		ajax.class_name = "base";
		ajax.method_name = "x_pick_color";
		ajax.register_argument("productID", productID);
		ajax.register_argument("colorID", colorID);
		var xmlhttp = ajax.send();
	}
}

pick_color_response = function(XmlHttp, attributes) {
	
}

pick_size = function(productID, sizeID) {
	if (parseInt(sizeID) > -1 && parseInt(productID) > 0) {
		var productID = parseInt(productID);
		var sizeID = parseInt(sizeID);
		
		var ajax = new ajax_request();
		ajax.call_back = "pick_size_response";
		ajax.file_path = "includes/base.php";
		ajax.class_name = "base";
		ajax.method_name = "x_pick_size";
		ajax.register_argument("productID", productID);
		ajax.register_argument("sizeID", sizeID);
		var xmlhttp = ajax.send();
	}
}

pick_size_response = function(XmlHttp, attributes) {
	if (XmlHttp.responseText != '' && XmlHttp.responseText.indexOf('___###___') != -1) {
		var tmp = XmlHttp.responseText.split('___###___');
		//alert(XmlHttp.responseText);
		var order = document.getElementById("order");
		
		if (order != null) {
			order.innerHTML = tmp[0];
		}
		
		var price = document.getElementById("price");
		
		if (price != null) {
			price.innerHTML = tmp[1];
		}
	}
}

order_now = function() {
	var action_key = document.getElementById("action_key");
	
	if (action_key != null) {
		action_key.value = 'submit';
		
		document.cart_form.submit();
	}
}

change_quantity = function(obj, product_id, colorID, sizeID, width, length) {
	if (parseInt(product_id) > 0) {
		if (obj != null && parseInt(obj.value) > 0) {
			
		}
		else {
			obj.value = 1;
		}
		
		var product_id = parseInt(product_id);
		var quantity = parseInt(obj.value);
		
		var ajax = new ajax_request();
		ajax.call_back = "change_quantity_response";
		ajax.file_path = "includes/base.php";
		ajax.class_name = "base";
		ajax.method_name = "x_change_quantity";
		ajax.attributes = new Array();
		ajax.attributes[0] = product_id;
		ajax.attributes[1] = product_id + "_" + parseInt(colorID) + "-" + parseInt(sizeID) + "-" + width + "-" + length;
		ajax.register_argument("product_id", product_id);
		ajax.register_argument("quantity", quantity);
		ajax.register_argument("colorID", parseInt(colorID));
		ajax.register_argument("sizeID", parseInt(sizeID));
		ajax.register_argument("width", width);
		ajax.register_argument("length", length);
		var xmlhttp = ajax.send();
	}
}

change_quantity_response = function(XmlHttp, attributes) {
	var product_id = parseInt(attributes[0]);
	
	if (XmlHttp.responseText != '' && XmlHttp.responseText.indexOf('___###___') != -1 && product_id > 0) {
		var tmp = XmlHttp.responseText.split('___###___');
		
		var order = document.getElementById("order");
		
		if (order != null) {
			order.innerHTML = tmp[0];
		}
		
		//var total_product = document.getElementById("total_" + product_id);
		if (attributes[1]) {
			var total_product = document.getElementById("total_" + attributes[1]);
			
			if (total_product != null) {
				total_product.innerHTML = tmp[1];
			}
		}
		
		var total_sum = document.getElementById("total_sum");
		
		if (total_sum != null) {
			total_sum.innerHTML = tmp[2];
		}
	}
}

reload_small_basket = function() {
	var ajax = new ajax_request();
	ajax.call_back = "reload_small_basket_response";
	ajax.file_path = "includes/base.php";
	ajax.class_name = "base";
	ajax.method_name = "x_reload_small_basket";
	var xmlhttp = ajax.send();
}

reload_small_basket_response = function(XmlHttp, attributes) {
	if (XmlHttp.responseText != '') {
		var order = document.getElementById("order");
		
		if (order != null) {
			order.innerHTML = XmlHttp.responseText;
		}
	}
}

calculate_quadratic = function(product_id, obj) {
	var pro_quadratic = document.getElementById("pro_quadratic");
	var pro_width = document.getElementById("pro_width");
	var pro_length = document.getElementById("pro_length");
	
	if (parseFloat(obj.value) > 0) {
		if (obj.value.indexOf(",") != -1) {
			obj.value = obj.value.replace(",", ".");
		}
		
		obj.value = parseFloat(obj.value);
	}
	else {
		obj.value = 1;
	}
	
	if (pro_width != null && pro_length != null && pro_quadratic != null) {
		pro_quadratic.value = (parseFloat(pro_width.value) * parseFloat(pro_length.value)).toFixed(2);
		
		var ajax = new ajax_request();
		ajax.call_back = "calculate_quadratic_response";
		ajax.file_path = "includes/base.php";
		ajax.class_name = "base";
		ajax.method_name = "x_calculate_quadratic";
		ajax.register_argument("productID", product_id);
		ajax.register_argument("pro_width", pro_width.value);
		ajax.register_argument("pro_length", pro_length.value);
		var xmlhttp = ajax.send();
	}
}

calculate_quadratic_response = function(XmlHttp, attributes) {
	
}

calculate_linear = function(product_id, obj) {
	var pro_length = document.getElementById("pro_length");
	
	if (parseFloat(obj.value) > 0) {
		if (obj.value.indexOf(",") != -1) {
			obj.value = obj.value.replace(",", ".");
		}
		
		obj.value = parseFloat(obj.value);
	}
	else {
		obj.value = 1;
	}
	
	if (pro_length != null) {
		var ajax = new ajax_request();
		ajax.call_back = "calculate_linear_response";
		ajax.file_path = "includes/base.php";
		ajax.class_name = "base";
		ajax.method_name = "x_calculate_linear";
		ajax.register_argument("productID", product_id);
		ajax.register_argument("pro_length", pro_length.value);
		var xmlhttp = ajax.send();
	}
}

calculate_linear_response = function(XmlHttp, attributes) {
	
}
