// JavaScript Document

function saveOrder() {
	document.forms[ 'client_comment' ].submit();
//	window.location = '/Finalizacja-zamowienia';
}

function backToIndex() {
	window.location = '/index';
}

function cancelOrder () {
	window.location = '/index/action/clearCart';
}

function printOrder () {
	window.location =  '/Zamowienie/krok/4/action/print';
}

function printOrderSaved() {
	window.location =  '/Finalizacja-zamowienia/action/print';
}


function gotoSummary() {
	document.forms['pay_type'].submit();
}

function gotoPriceType() {
	var t 	= get( 'accept_delivery_terms' );
	var deliv = getRadioValue( document.forms[ 'delivery_type' ].elements[ 'delivery_type_radio' ] );
	//var deliv = getSelect( 'delivery_type', 'delivery_type_radio' );
	if( ( ( deliv == 'poland' )  || ( deliv == 'czestochowa' ) ) && ( !t.checked ) ) {
		alert( 'Musisz zaakceptować warunki płatności przy dostawie wysyłkowej' );
	} else {
		document.forms[ 'delivery_type' ].submit();
	}

}


function gotoPersonalData () {
	window.location = '/Zamowienie/krok/1';
}


function gotoDeliveryData () {
	window.location = '/Zamowienie/krok/2';
}


function gotoPaymentData () {
	window.location = '/Zamowienie/krok/3';
}


function showDeliveryTerms() {
	alert( 'Popup z warunkami dostawy' );
}

function addToCart( object, title, id, quantity ) {
	if( quantity == undefined ) {
		var fn = 'product_'+id.toString();
		var selname = 'quantity_'+id.toString();
		var sel = document[fn][selname];
		for( j = 0; j < sel.length; j++ ) {
			if( sel.options[ j ].selected ) {
				quantity = sel.options[ j ].value;
			}
		}
	} 

	var ajax = new myAjax();
	ajax.action = 'add_to_cart';
	ajax.post( 'object='+object+'&id='+id+'&quantity='+quantity );
	ajax.onLoad = function () {
		showShoppingCartInfo('Dodano do koszyka: <strong>' + title + '</strong>.');
		refreshShoppingCartBox ();
	}
}


function refreshShoppingCartBox ()
{
	var cart = document.getElementById('koszyk')
	var ajax = new myAjax();
	ajax.action = 'refreshShoppingCartBox';
	ajax.post ('', cart);
	ajax.onLoad = function () {
		cart.className = ajax.response.indexOf('Suma') > -1 ? 'full' : '';
	}
}


function updateShoppingCart (i)
{
	var F     = document.forms['shoppingCart'].elements;
	var cn    = F['item_c_'+i].value;
	var id    = F['item_n_'+i].value;
	var qty   = F['item_q_'+i].value;
	var price = F['item_p_'+i].value;

	var ajax = new myAjax();
	ajax.action = 'updateShoppingCart';
	ajax.post('to_edit=' + cn + '_' + id + '_' + qty + '_' + price);
	ajax.onLoad = function () {
		var elem = ajax.response.split('|');
		document.getElementById('item_v_'+i).innerHTML = elem[0];
		document.getElementById('total').innerHTML     = elem[1];
	}

	refreshShoppingCartBox();
}


function removeFromShoppingCart (i)
{
	var F     = document.forms['shoppingCart'].elements;
	var count = F['allCount'].value;
	var str_remove, str_info;

	if (i == undefined) {
		for (i = 0; i < count; i++) {
			var cn = F['item_c_'+i].value;
			var id = F['item_n_'+i].value;
			str_remove += cn+'_'+id+'|';
		}
		str_info   = 'Usunięto wszystko z koszyka.';
	}
	else {
		var cn = F['item_c_'+i].value;
		var id = F['item_n_'+i].value;
		var ti = F['item_t_'+i].value;
		str_remove = cn+'_'+id+'|';
		str_info   = 'Usunięto z koszyka: <strong>' + ti + '</strong>.';
	}

	var ajax = new myAjax();
	ajax.action = 'removeFromShoppingCart';
	ajax.post('to_remove=' + str_remove, 'mainCol');
	ajax.onLoad = function () {
		showShoppingCartInfo(str_info);
		refreshShoppingCartBox();
	}
}


function shoppingCartActualization() {
	var count = get( 'allCount' ).value;
	var str_edit    = '';
	var str_remove 	= '';
	for( i = 0; i < count; i++ ) {
		var remove	= get( 'item_r_'+i.toString() );
		var cn	 	= get( 'item_c_'+i.toString() ).value;
		var id 		= get( 'item_n_'+i.toString() ).value;
		if( !remove.checked ) {
			// edit
			var quant 	= get( 'item_q_'+i.toString() ).value;
			str_edit += cn+'_'+id+'_'+quant+'|';
		} else {
			// remove
			str_remove += cn+'_'+id+'|';
		}
	}
	var ajax = new myAjax();
	ajax.action = 'updateShoppingCart';
	ajax.post( 'to_edit='+str_edit+'&to_remove='+str_remove );
	ajax.onLoad = refreshShoppingCartFull;

}


function shoppingCartRealization() {
	document.location.href = '/Zamowienie';
}



var shoppingCartInfo = null;
var timeout = null;
var isIE6 = navigator.appName == 'Microsoft Internet Explorer' && navigator.userAgent.indexOf('MSIE 7.') == -1;

function scrollShoppingCartInfo ()
{
	var top = document.documentElement.scrollTop;
	shoppingCartInfo.style.top = top + 'px';
}

function hideShoppingCartInfo ()
{
	shoppingCartInfo.innerHTML     = '';
	shoppingCartInfo.style.display = 'none';
}

function showShoppingCartInfo (title)
{
	window.clearTimeout(timeout); 
		
	if( shoppingCartInfo == null ) {
		initShoppingCartInfo();
	}
	shoppingCartInfo.innerHTML     = title;
	shoppingCartInfo.style.display = 'block';
	
	timeout = window.setTimeout (hideShoppingCartInfo, 1000)
}

function initShoppingCartInfo ()
{
	shoppingCartInfo = document.createElement('div');
	if (!shoppingCartInfo) {
		return;
	}
	document.body.appendChild(shoppingCartInfo);
	
	shoppingCartInfo.id = 'shoppingCartInfo';
	shoppingCartInfo.onmouseout  = function () { this.style.display = 'none'; }
	shoppingCartInfo.onmousemove = function () { this.style.display = 'block'; }

	if (isIE6) {
		addEvent (window, 'scroll', scrollShoppingCartInfo);
		addEvent (window, 'resize', scrollShoppingCartInfo);
	}
}

addEvent (window, 'load', initShoppingCartInfo);
