function submitRegister()
{
    if ($('rules').checked && $('agree1').checked && $('agree2').checked )
        document.forms['shipment'].submit();
    else
        //alert(lang_rules_confirm);
		alert("Aby skorzystać ze sklepu musisz wyrazić zgodę na umieszczenie swoich danych teleadresowych w bazie oraz zaakceptować warunki regulaminu.");
}




function searchConnected()
{
    var val = $('search_conn').value;
    if (val.length > 1)
    {
        var request_url = host_url+'/www/connected.php?pro='+val;
        var json = new cmsAjaxResponse(request_url, {loader: false }).doRequest();
    }
}



function getBox(box, page)
{
    markPageButton(box, page);

    $('pro'+box).className = 'loader';
    $('pro'+box).setHTML('');
	var request_url = host_url+'/www/box.php?page='+page+'&box='+box;
	var json = new cmsAjaxResponse(request_url, {loader: false }).doRequest();
}

function hideBoxLoader(box)
{
    $('pro'+box).className = '';
}

function markPageButton(box, page)
{
    $$('span.page_button_'+box).each(function(el){
        el.className = 'page_green page_button_'+box;
    });

    $('pro'+box+'_'+page).className = 'page_yellow page_button_'+box;
}

/**
 * dodaje do koszyka produkt za pomoca Ajaxa
 * @param {int} id_product
 * @param {int} id_color
 * @param {int} id_period
 * @param {int} quantity
 */
function cartAdd(id_product, id_color, id_period, quantity)
{
	var q = "";
	q += "?cart=1";
	q += "&id_product=" + id_product;
	q += "&id_color=" + id_color;
	q += "&id_period=" + id_period;
	q += "&quantity=" + quantity;
	
	var request_url = host_url + "/www/ajax.php" + q;
	var json = new cmsAjaxResponse(request_url, {loader: false }).doRequest();
}

/**
 * Dokonuje walidacji formularza przy zamiawianiu produktu oraz dodaje produkt do koszyka
 * @param {int} id_product
 */
function cartQuantityAdd(id_product) {	
	var err_message = '';
	
	var pro_colors_arr = document.getElementsByName('pro_colors');
	var pro_periods_arr = document.getElementsByName('pro_period');
	var id_color, id_period, quantity;
	var i, error;
	
	var isColors = $('isColors').get('value');
	
	if (isColors == 1) {
		error = true;
		for (i = 0; i < pro_colors_arr.length; i++) {
			if (pro_colors_arr[i].checked) {
				id_color = pro_colors_arr[i].value;
				error = false;
				break;
			}
		}
		if (error) {
			err_message += "Musisz wybrać jeden z dostępnych kolorów! \n";
		}
	}else {
		id_color = 'x';
	}
	
	error = true;
	for(i = 0; i < pro_periods_arr.length; i++) {
		if(pro_periods_arr[i].checked) {
			id_period = pro_periods_arr[i].value;
			error = false;
			break;
		}
	}
	if(error) {
		err_message += "Musisz wybrać jeden z dostępnych okresów wynajmu! \n";
	}
	
	if(!error) {
		quantity = parseInt($("pro_quantity" + id_period).value);
		if (quantity == Number.NaN || quantity === 0) {
			err_message += "Wpisz poprawną liczbę sztuk produktu! \n";
		}
	}	
	
	if(err_message == "") {	
		cartAdd(id_product, id_color, id_period, quantity);
	}else {
		alert(err_message);
	}	
	
}

function showInfo( text ){

	width = 200;
	height = 200;
	
	windowHeight = getVisibleHeight();
	if (document.documentElement && document.documentElement.scrollTop) theTop = document.documentElement.scrollTop;
	else theTop = document.body.scrollTop;
	
	$('cartInfo').style.top = (theTop+(windowHeight/2)-(height/2)-45)+'px';
	$('cartInfo').style.left = (document.body.scrollLeft+((document.body.clientWidth)/2)-(width/2)-5)+'px';
	
	$('cartInfo').innerHTML = text;
	$('cartInfo').style.display = "block";
	
	setTimeout("closeInfo()", 2000);
}

function closeInfo(){
	$('cartInfo').style.display = "none";
}


function getVisibleHeight(Width, Height) {
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
	//Non-IE
	myWidth = window.innerWidth;
	myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	//IE 6+ in 'standards compliant mode'
	myWidth = document.documentElement.clientWidth;
	myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	 //IE 4 compatible
	myWidth = document.body.clientWidth;
	myHeight = document.body.clientHeight;
	}
	return myHeight;
}


function showHide(e){

	obj = document.getElementById(e);
	if (obj.style.display == "none"){
		obj.style.display = '';
	}
	else{
		obj.style.display = 'none';
	}
}

function clearForm( form ){
	
	var form = document.forms[form];
	if(form==null) return false;
	
	var elements = form.elements;
	
	for(var index=0; index<elements.length; index++) 
	{
		if(elements[index].nodeName.toUpperCase() == 'INPUT' && elements[index].type.toUpperCase() == 'TEXT')
			elements[index].value = '';
		if(elements[index].nodeName.toUpperCase() == 'SELECT')
			elements[index].selectedIndex = 0;
	}
	return true;
}

function userOtherPlaceChange(){
	var form = document.forms['form_user'];
	
	if (form.place_other.checked){
		form.place_address.readOnly = false;
		form.place_postcode.readOnly = false;
		form.place_city.readOnly = false;
	}
	else{
		form.place_address.readOnly = true;
		form.place_postcode.readOnly = true;
		form.place_city.readOnly = true;
	}
}


function userSearch(){
	var form = document.forms['formSearch'];
	
	if(checkSearch()){
		form.submit();
	}
}


function userPerPage(){
	var form = document.forms['formPerPage'];
	
	if(true){
		form.submit();
	}
}

function checkSearch(){
	var accept = true
	var form = document.forms['formSearch'];
	
	if(form==null)	return true;
	obj = form.elements;

	var error=0;
	var i=0;
	for(i=0;i<obj.length;i++){
		if( obj[i].name.indexOf("search")!=-1 ){
			if (obj[i].id == 'text' && obj[i].value==""){
				obj[i].className = 'input_error';
				error=1;
				continue;
			}
			if (obj[i].id == 'int' && (!parseInt(obj[i].value) && obj[i].value!=0) && obj[i].value!=''){
				obj[i].className = 'input_error';
				error=1;
				continue;
			}
			if( obj[i].className == 'input_error'){
				obj[i].className = 'input_ok';
			}
		}
	}
	if(error){
		alert('Wypelnij poprawnie wszystkie czerwone pola');
		return false;
	}
	else{
		return true;
	}	
}

function editCurrency(){

	var accept = true
	var form = document.forms['form_currency'];
	
	if(form.cur_title.value==""){
		accept = false;
	}
	if (accept) {
		form.submit();
	}
	else{
		alert('Podaj nazwe waluty!');
	}
}

function checkAll()
{
    var all = $$('input.pro_cart');
    all.each( function(element){
        element.checked = true;
    });
}

function uncheckAll()
{
    var all = $$('input.pro_cart');
    all.each( function(element){
        element.checked = false;
    });
}

function cartAddAll()
{
    var all = $$('input.pro_cart');
    all.each( function(element){
        if (element.checked == true)
        {
            javascript:cartQuantityAdd(element.value, 'pro_quantity_'+element.value)
        }
    });
}

function cartAddForm(pro){
	
    var attr = $$('input.check_attr');
    var quant = $$('input.input_quantity');
    var flag = false;

    attr.each(function(el, id){ 
        if (el.checked == true)
        {
            flag = true;

            if (parseInt(quant[id].alt) > parseInt(quant[id].value))
            {
                alert(lang_shop_minimal_quantity+quant[id].alt);
            }
            else
            {
                cartAdd(pro+'_'+el.value, parseInt(quant[id].value));
            }
        }
    });

    if (flag == false)
        alert(choose_any_product+'...');
}

/* select */

var sb_show = [];
var sb_currentFX = 0;
var is_exp = [];
function menuTop(){
    var fx = [];
    var std = [];
    submenuItems = $$('.options_select').each(function(item,i){
        std[i] = item.clientHeight;
        sb_show[i]=false;
        item.setStyles({
            'overflow':'hidden',
            'height':0,
            'display':'none'
        });
        fx[i] = new Fx.Style(item,'height',{
            onStart: function(){
                sb_currentFX = i;
                item.setStyles({
                    'z-index':'10000'
                });
                if(sb_show[i]==true) {
                    item.setStyles({
                        'display':''
                    }); is_exp[sb_currentFX]=1
                }
            },
            onComplete: function(){
                item.setStyles({
                    'z-index':'10000'
                });
                if(sb_show[i]==false) {
                    item.setStyles({
                        'display':'none'
                    }); is_exp[sb_currentFX]=0
                }
            }
        });
    });
    menuItems = $$('.selectbox').each(function(item,i){

        item.addEvents({
            'click': function(e){
                var ev = new Event(e);
                ev.stopPropagation();
                if(sb_show[i]==true){
                    sb_show[i]=false;
                    fx[i].stop();
                    fx[i].start(std[i],0);
                    return
                }
                if(sb_show[i]==false){
                    sb_show[i]=true;
                    fx[i].stop();;
                    fx[i].start(0,std[i]);
                    return;
                }
            }
        })
    });
    document.body.onclick = function(){
        for(var i=0; i<is_exp.length; i++){
            if(is_exp[i]==1){
                sb_show[i] = false;
                fx[i].stop();;
                fx[i].start(std[i],0);
            }
        }
    }
}

function menuFix(){
    var menu = $$('.active');
    if(menu[0]){
        wh = parseInt(menu[0].getStyle('width'));
        if(wh%2!=0){
            menu[0].setStyles({
                'width':wh+1
            });
        }
    }
}

function updateUserIsTax()
{
	var tax = document.forms.shipment.user_is_tax;
	var postzone = document.forms.shipment.postzone;
	
	if (postzone.value == 1)
		tax.checked = false;
	else
		tax.checked = true;
}
