﻿//
function onPageLoad() {
	// Show shopping cart
	showShoppingCart();
	
	
}

//
function selectProductCategory(obj) 
{
	var n = 0;
	var objCategories = document.getElementById("products_categories_container").getElementsByTagName("li");
	if (objCategories && objCategories.length > 0) {
		for (i=0; i<objCategories.length; i++) {
			if (objCategories[i].className != "split") {
				objScroll = document.getElementById("jcarousel_products_container_" + n);
				objProductImage = document.getElementById("imgProductBig");

				if (objCategories[i] == obj || (!obj && i == 0)) {
					// Selected category
					objCategories[i].oldClassName = "selected pngfix";
					objCategories[i].className = "selected pngfix";
					if (objScroll) {
						objScroll.style.display = "block";
						if (objProductImage) {
							objProductImage.src = "";
						}
						
						var api = $("#products-scroll-" + n).scrollable({ api: true });
						api.click(0);
						var index = api.getClickIndex();

						var objImages = objScroll.getElementsByTagName("img");
						if (objImages && objImages.length > 0) {
							selectProduct(objImages[index].id);
						}
					}
				}
				else {
					//
					objCategories[i].oldClassName = "pngfix";
					objCategories[i].className = "pngfix";
					if (objScroll) {
						objScroll.style.display = "none";
					}
				}
				n++;
			}
		}
	}
}

//
function selectProduct(product_id) {
	destination.push('products_selected_product');
	script_name = templateRoot + 'SelectedProduct.aspx';
	ajax_req = 'product_id=' + product_id + '&lang_id=' + lang_id + '&lang_url=' + lang_url;
	cache.push(ajax_req);
	getAjaxRequest();
}

// Open menu Item
function menuClick(id, cId, cName) {
	var obj = document.getElementById(cId + id);

	if (obj) {
		if (obj.className == cName + 'close') {
			obj.className = cName + 'open';
		}
		else {
			obj.className = cName + 'close';
		}
	}
}

// 
function loginFocus(obj) {
	if (!obj.init) {
		obj.initValue = obj.value;
		obj.value = '';
		obj.init = true;
	}
	else if (obj.value == obj.initValue) {
		obj.value = '';
	}
}

//
function loginBlur(obj) {
	if (obj.value == '') {
		obj.value = obj.initValue;
	}
}

//
function loginPasswordFocus(obj) {
	obj.style.display = 'none';

	if (obj.type && obj.type == 'text') {
		obj_1 = document.getElementById('login_password');
	}
	else {
		obj_1 = document.getElementById('login_password_text');
	}
	obj_1.style.display = 'block';
	obj_1.focus();
}

//
function loginPasswordBlur(obj) {
	if (obj.type && obj.type == 'text') {
		obj.style.display = 'none';
		obj_1 = document.getElementById('login_password');
		obj_1.style.display = 'block';
	}
	else {
		if (obj.value == '') {
			obj.style.display = 'none';
			obj_1 = document.getElementById('login_password_text');
			obj_1.style.display = 'block';
		}
	}
}

//
function loginSubmit() {
	objUsername = document.getElementById('login_username');
	objPassword = document.getElementById('login_password');

	if (objUsername && objPassword) {
		if (objUsername.value != objUsername.initValue && objUsername.value != '' && objPassword.value != '') {
			return true;
		}
	}
	
	return false;
}


// 
function clubFocus(obj) {
	if (!obj.init) {
		obj.initValue = obj.value;
		obj.value = '';
		obj.init = true;
	}
	else if (obj.value == obj.initValue) {
		obj.value = '';
	}
}

//
function clubBlur(obj) {
	if (obj.value == '') {
		obj.value = obj.initValue;
	}
}


// Refresh DIV to show Shopping Cart
function showShoppingCart() {
	var obj = document.getElementById('shopping-cart');
	if (obj) {
		destination.push('shopping-cart');
		script_name = templateRoot + "ShowShoppingCart.aspx";
		ajax_req = 'lang_id=' + lang_id + '&lang_url=' + lang_url;
		cache.push(ajax_req);
		getAjaxRequest();
	}
}

// Add selected product to Shopping Cart
function addToShoppingCart(product_id) {
	var quantity = 1;

	// Get Quantity
	if (document.getElementById('product_qty_' + product_id)) {
		quantity = parseInt(document.getElementById('product_qty_' + product_id).value);
	}

	// Get Product Parameters
	var param_attributes = '';
	var param_options = '';
	var attribute_id = '';
	var option_id = '';
	
	var objAttributes = $('.product-attributes-to-select .product-attribute');
	if (objAttributes.length > 0) 
	{
		for (i = 0; i < objAttributes.length; i++) 
		{
			attribute_id = '';
			option_id = '';
			
			attribute_id = $(objAttributes[i]).find("input[name='attribute_id']").val();
			var options = $(objAttributes[i]).find("input[type='radio']");
			for (n=0; n<options.length; n++)
			{
				if (options[n].checked) 
				{
					option_id = options[n].value;
					break;
				}
			}

			if (param_attributes != '') param_attributes = param_attributes + ';'
			param_attributes = param_attributes + attribute_id;

			if (param_options != '') param_options = param_options + ';'
			param_options = param_options + option_id;
		}
	}

	//alert(param_attributes);
	//alert(param_options);
	
	//
	destination.push('shopping-cart');
	script_name = templateRoot + "AddToShoppingCart.aspx";
	ajax_req = 'product_id=' + product_id + '&quantity=' + quantity + '&lang_id=' + lang_id + '&lang_url=' + lang_url + '&param_attributes=' + param_attributes + '&param_options=' + param_options;
	cache.push(ajax_req);
	getAjaxRequest();

	alert('המוצר נוסף לסל קניות');

	// Refresh Shopping Cart
	showShoppingCart();
}

// Remove product from shopping cart
function removeFromShoppingCart(cart_id) {
	destination.push('shopping-cart');
	script_name = templateRoot + "RemoveFromShoppingCart.aspx";
	ajax_req = 'cart_id=' + cart_id + '&lang_id=' + lang_id + '&lang_url=' + lang_url;
	cache.push(ajax_req);
	getAjaxRequest();

	// Refresh Shopping Cart
	showShoppingCart();
}

//
function setProductsQuantity(id, action) {
	var obj = document.getElementById(id);
	if (obj) {
		if (isNaN(obj.value)) {
			obj.value = 1;
		}

		if (action == '-' && parseInt(obj.value) > 1) {
			obj.value = parseInt(obj.value) - 1;
		}
		else if (action == '+') {
			obj.value = parseInt(obj.value) + 1;
		}

		if (obj.value > 1000) {
			obj.value = 1000;
		}
	}
}

//
function changeProductsQuantity(obj) {
	if (isNaN(obj.value)) {
		obj.value = 1;
	}
	else {
		var val = parseInt(obj.value);
		if (val <= 0) {
			obj.value = 1;
		}
		if (obj.value > 1000) {
			obj.value = 1000;
		}
	}
}

function shortEventDescription(id) {
	var short_text = document.getElementById('short_event_' + id);
	var full_text = document.getElementById('full_event_' + id);
	var btn_open = document.getElementById('btn_open_event_' + id);
	var btn_close = document.getElementById('btn_close_event_' + id);

	if (btn_open && btn_close) {
		btn_open.style.display = 'inline';
		btn_close.style.display = 'none';
	}

	if (short_text && full_text) {
		short_text.style.display = 'block';
		full_text.style.display = 'none';
	}
}

function fullEventDescription(id) {
	var short_text = document.getElementById('short_event_' + id);
	var full_text = document.getElementById('full_event_' + id);
	var btn_open = document.getElementById('btn_open_event_' + id);
	var btn_close = document.getElementById('btn_close_event_' + id);

	if (btn_open && btn_close) {
		btn_open.style.display = 'none';
		btn_close.style.display = 'inline';
	}

	if (short_text && full_text) {
		short_text.style.display = 'none';
		full_text.style.display = 'block';
	}
}

function shortSaleDescription(id) {
	var short_text = document.getElementById('short_sale_' + id);
	var full_text = document.getElementById('full_sale_' + id);
	var btn_open = document.getElementById('btn_open_sale_' + id);
	var btn_close = document.getElementById('btn_close_sale_' + id);

	if (btn_open && btn_close) {
		btn_open.style.display = 'inline';
		btn_close.style.display = 'none';
	}

	if (short_text && full_text) {
		short_text.style.display = 'block';
		full_text.style.display = 'none';
	}
}

function fullSaleDescription(id) {
	var short_text = document.getElementById('short_sale_' + id);
	var full_text = document.getElementById('full_sale_' + id);
	var btn_open = document.getElementById('btn_open_sale_' + id);
	var btn_close = document.getElementById('btn_close_sale_' + id);

	if (btn_open && btn_close) {
		btn_open.style.display = 'none';
		btn_close.style.display = 'inline';
	}

	if (short_text && full_text) {
		short_text.style.display = 'none';
		full_text.style.display = 'block';
	}
}

function openBrand(brand) {
	setTimeout(function() 
	{
		document.location = mainRoot + lang_url + '/brands/' + brand + '/';
	}, 300);
}

function showProductImage(big_image, image) {
	document.getElementById(big_image).src = mainRoot + 'Thumbnail.ashx?image=images/products/' + image + '&w=308&h=400&cp=1&rs=0&rb=1';
}

