// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function reloadDivAjax(controller, act, divId, val, promotionId)
{
    new Ajax.Updater(divId, '/' + controller + '/' + act + '/' + val + '/?promotionId=' + promotionId + '&ajax=ajax', {onComplete:function(){}, asynchronous:true, position:'top'});
}

function reloadPhoto(id, path)
{
    var image = document.getElementById(id);
    image.src = ''
    var image1= new Image();
    image1.src = path;
    image.src = image1.src;
}

function calendar_show_hide(divId, id)
{
    var d = document.getElementById('calendar_' + divId + '_' + id);
    if (d.style.visibility == 'hidden')
    {
        if (d.innerHTML == '')
            new Ajax.Updater(d.id, '/promotion/show_in_calendar/?ajax=ajax&id=' + id, {onComplete:function(){hideLoader(id)}, onLoaded:function(){showLoader(id)}, asynchronous:true,evalScripts:true});
        d.style.visibility = 'visible';
        d.style.display = 'block';
    }
    else
    {
        d.style.visibility = 'hidden';
        d.style.display = 'none';
    }
}

function calendar_set_status(divId, id, divStatus)
{
    var d = document.getElementById('calendar_' + divId + '_' + id);
    if (d.innerHTML == '')
        new Ajax.Updater(d.id, '/promotion/show_in_calendar/?ajax=ajax&id=' + id, {onComplete:function(){hideLoader(id)}, onLoaded:function(){showLoader(id)}, asynchronous:true,evalScripts:true});

    if (divStatus == '1')
    {
        d.style.visibility = 'visible';
        d.style.display = 'block';
    }
    else
    {
        d.style.visibility = 'hidden';
        d.style.display = 'none';
    }

}

function calendar_show_hide_all(divId, div)
{
    var divs = document.getElementsByTagName('div');
    var divStatus = document.getElementById('status_' + divId);
	var divImg = document.getElementById('img_' + divId);
    if (divStatus.value == '1')
	{
        divStatus.value = '0';
		divImg.src = '/images/plus-small.gif';
	}
    else
	{
        divStatus.value = '1';
		divImg.src = '/images/minus-small.gif';
	}

    for (i = 0 ; i < divs.length; i++)
    {
        if (divs[i].getAttribute('name') == 'calendar_promotion_title_' + divId)
        {
            calendar_set_status(divId, divs[i].id, divStatus.value);
        }
    }
}

function getUrl(){
	document.getElementById('redirect_url').value = parent.location;
}

function DeleteAttachment(id) {
	var p = document.getElementById('a_' + id);
	var atd = document.getElementById('attachmnets_to_delete');

	atd.value = atd.value + '_' + id;
	p.style.display = 'none';
	p.style.visibility = 'hidden';
}

function runRegistration(alertText)
{
	var cb = document.getElementById('accept_checkbox');
        var cb_newsletter = document.getElementById('newsletter_accept_box');
	var formToSend = document.getElementById('form_rejstracja');
	if (cb.checked && cb_newsletter.checked)
		formToSend.submit();
	else
		alert(alertText);
}

function changeSection(section_id)
{
	document.location.href = '/products/new?section=' + section_id
}

function changeManufacturer(manufacturer_id)
{
	var section = document.getElementById('product_section_id');
	document.location.href = '/products/new?section=' + section.value + '&manufacturer=' +  manufacturer_id
}

function changeFilter(url_prefix, section_id)
{
	url = url_prefix.replace(/sectionID/i, section_id)
	document.location.href = url
}

function copyTextInput(from, to)
{
	var f = document.getElementById(from);
	var to = document.getElementById(to);

	to.value = f.value;
}

function copyTextFCK(from, to)
{
	var fEditor = FCKeditorAPI.GetInstance(from) ;
	var tEditor = FCKeditorAPI.GetInstance(to) ;

	tEditor.SetHTML(fEditor.GetHTML());
}


function showHideCB(cb, id)
{
	var div = document.getElementById(id);
	if (!cb.checked) {
		div.style.display = 'none';
		div.style.visibility = 'hidden';
	} else {
		div.style.display = 'block';
		div.style.visibility = 'visible';
	}
}

function completeDescription(where, configuration)
{
	var description = '';
	for(var i=0;i < configuration.length; i++) {
		if (configuration[i][1][0] && (configuration[i][1][0] == 'main')) {
			description = description + ' <b>' + configuration[i][0] + '</b>: ';
		} else {
			var form_input = document.getElementById(configuration[i][0]);

			if (form_input.type == 'checkbox') {
				var cbs = document.getElementsByName(form_input.name);
				for (var j=0; j < cbs.length; j++) {
					if (cbs[j].checked) {
						var cb_hidden = document.getElementById(configuration[i][0] + '_' + cbs[j].value);

						description = description + ' ' + cb_hidden.value + ', ';
					}
				}
			} else if (form_input instanceof HTMLSelectElement) {
				description = description + ' ' + form_input.options[form_input.selectedIndex].text + ', ';
			} else {
				if (form_input.value != '') {
					description = description + ' ' + form_input.value;
					if (configuration[i][1][0] && (configuration[i][1][0] == 'unit')) {
						description = description + ' ' + configuration[i][1][1];
					}
					description = description + ', ';
				}
			}
		}
	}

	var oEditor = FCKeditorAPI.GetInstance(where);
	oEditor.SetHTML(description);
}

function submitAccessoryForm(act, reset_from)
{
	var f = document.getElementById('accessories_form');
	var rf = document.getElementById('reset_from');
	rf.value = reset_from;

	f.action = f.action.replace(/accessories_create/i, act);
	f.submit();
}

function openWindowWithURL(url)
{
    h = window.open(url, 'new_window', 'menubar=no,toolbar=no,scrollbars=yes,resizable=yes,location=no,status=no,width=615,height=622,left=200,top=60');
}

function openWindowWithURLAndSize(url, w, h)
{
    h = window.open(url, 'new_window', 'menubar=no,toolbar=no,scrollbars=yes,resizable=yes,location=no,status=no,width=' + w + ',height=' + h + ',left=200,top=60');
}

function submit_form(form_id, onsubmit)
{
	var f = document.getElementById(form_id);
	if (onsubmit)
		f.onsubmit();
	else
		f.submit();
}

function submit_form_set_hidden(form_id, hidden_id, onsubmit)
{
  var e = document.getElementById(hidden_id);
  e.value = '1';

  submit_form(form_id, onsubmit);
}

function submit_form_with_loader(form_id, onsubmit, loader)
{
    var l = document.getElementById(loader);
    l.style.display = 'block';

    submit_form(form_id, onsubmit);
}

function submit_form_with_loader_check(form_id, onsubmit, loader, to_check)
{
    var e = document.getElementById(to_check);
    if (e.value != '')
        submit_form_with_loader(form_id, onsubmit, loader);
}

function submit_form_with_loader_check_build(form_id, onsubmit, loader, to_check, message)
{
    var e = document.getElementById(to_check);
    var fo = document.getElementById('form_opcje_dodatkowe');
    var fc = document.getElementById('form_accessories');
    var f = document.getElementById(form_id);
    if (e.value != '') {

        var a = f.elements;
        for (var j=0; j<a.length; j++) {
            if ((a[j].type == 'hidden') && (a[j].name.match('components') || a[j].name.match('accessories'))) {
                f.removeChild(a[j]);
            }

        }

        var cs = fo.elements;
        for (var i=0; i<cs.length; i++) {
            if(cs[i].name.match('components')) {
                if (cs[i].value == -1) {
                    alert(message);
                    return false;
                }

                var h = document.createElement('input');
                h.type = 'hidden';
                h.name = cs[i].name;
                h.value = cs[i].value;
                f.appendChild(h);
            }
        }

        if (fc) {
            var cbs = fc.elements;
            for (var i=0; i<cbs.length; i++) {
                if(cbs[i].name.match('accessories') && (cbs[i].checked)) {
                    var h = document.createElement('input');
                    h.type = 'hidden';
                    h.name = cbs[i].name;
                    h.value = cbs[i].value;
                    f.appendChild(h);
                }
            }
        }

        submit_form_with_loader(form_id, onsubmit, loader);
        return false;
    } else {
        return false;
    }
}

function search_change_param(param_value, param_name)
{
	var s = document.getElementById(param_name);
	s.value = param_value;
	submit_form('search_categories_form', false);
}

function select_all_checkboxes(group_name, status)
{
  var statusEl = document.getElementById(status);

  if (statusEl.value == '0')
    statusEl.value = '1';
  else
    statusEl.value = '0';

  var cbs = document.getElementsByName(group_name);
  for (var i=0 ; i<cbs.length; i++)
  {
    if (statusEl.value == '1')
      cbs[i].checked = true;
    else
      cbs[i].checked = false;

  }
}

function price_format(price, lang)
{
  if (lang == 'pl') {
          result = price + '';
          result = result.replace('.', ',');
  }

  if ((parseInt(price*10) == (price*10)) && ((parseInt(price) - price) != 0) && (price>0))
    result = result + '0';

	return result;
}

function change_product_price_with_part(key, id, lang)
{
	if ((key != -1) && (key != -2)) {
		actual_price = Math.round(Math.round(actual_price*100) + Math.round(parts_prices[key]*100) -
                    Math.round(previous_components_prices[id]*100))/100;

		previous_components_prices[id] = parts_prices[key];

		update_price_display(actual_price, lang);

		var cp = document.getElementById('component_price_' + id);
		sign = '+';
		if (parts_prices[key] < 0) sign = '-';
		cp.innerHTML = sign + ' ' + price_format(Math.abs(parts_prices[key]), lang) + ' zł';
	} else {
		actual_price = Math.round(Math.round(actual_price*100) - Math.round(previous_components_prices[id]*100))/100;

		previous_components_prices[id] = 0;
		update_price_display(actual_price, lang);

		var cp = document.getElementById('component_price_' + id);
		cp.innerHTML = '+ 0 zł';
	}
}

function change_product_price_with_accessory(cb, key, lang)
{
	if (cb.checked) {
		actual_price = (Math.round(Math.round(actual_price*100) + Math.round(accessories_prices[key]*100))/100);

		update_price_display(actual_price, lang);

		var ap = document.getElementById('accessory_price_' + key);
		sign = '+';
		if (accessories_prices[key] < 0) sign = '-';
		ap.innerHTML = sign + ' ' + price_format(Math.abs(accessories_prices[key]), lang) + ' zł';
	} else {
		actual_price = (Math.round(Math.round(actual_price*100) - Math.round(accessories_prices[key]*100))/100);

		update_price_display(actual_price, lang);

		var ap = document.getElementById('accessory_price_' + key);
		ap.innerHTML = '+ 0 zł';
	}
}

function update_price_display(price, lang)
{
	var p = document.getElementById('price_display');
	p.innerHTML = price_format(price, lang);
}

function reset_select(id)
{
	var s = document.getElementById(id);
	s.selectedIndex = 0;
}

function reset_checkbox(id)
{
	var cb = document.getElementById(id);
	cb.checked = false;
}

function show_image_in_popup(img, title)
{
    var newWindow;

    newWindow = window.open('', 'nw', 'menubar=no, toolbar=no, location=no, scrollbars=no, resizable=yes, status=no, width=600, height=500, left=400, top=400');

    newWindow.document.write("<html><head><title>" + title + "</title></head><body><a href='javascript:window.close();'><img id='imagePreview'  border='0' alt='' src='" + img + "' / onLoad='window.resizeTo(this.width+30, this.height+70); e = ((screen.width - this.width + 30) / 2); f = ((screen.height - this.height + 70) / 2); window.moveTo(f, e);'></a></body></html>");

    newWindow.document.close();
}

function show_hide(id)
{
	var div = document.getElementById(id);
	if (div.style.display == 'block') {
		div.style.display = 'none';
	} else {
		div.style.display = 'block';
	}
}

function show_element(id)
{
    var div = document.getElementById(id);
    div.style.display = 'block';
}

function show_hide_delay(id, period)
{
	setTimeout("show_hide('" + id + "')", period);
}

function hide_delay(id, period)
{
	setTimeout("$('" + id + "').hide()", period);
}

function select_baner_type(nr)
{
    var baner_type_1 = document.getElementById('baner_1');
    var baner_type_2 = document.getElementById('baner_2');

    if (nr == 0)
    {
        baner_type_1.style.display = 'block';
        baner_type_2.style.display = 'none';
    }
    else
    {
        baner_type_1.style.display = 'none';
        baner_type_2.style.display = 'block';
    }
}

function submit_section(pin_id, type_id)
{
    pin = document.getElementById('pin_text_' + pin_id);

    document.location.href = '/sections/destroy/' + pin_id + '/?pin=' + pin.value + '&type=' + type_id;
}

function clear_div(id)
{
    var div = document.getElementById(id);

    div.innerHTML = '';
}

function generate_product_autocomplete(url)
{
  var myServer = url;
  var mySchema = ["\n", "\t"];
  var myDataSource1 = new YAHOO.widget.DS_XHR(myServer, mySchema);
  myDataSource1.responseType = YAHOO.widget.DS_XHR.TYPE_FLAT;
  var myAutoComp = new YAHOO.widget.AutoComplete("myInput","myContainer", myDataSource1);
  myAutoComp.autoHighlight = false;
  myAutoComp.useShadow = true;
  myAutoComp.forceSelection = false;
  myAutoComp.typeAhead = false;
}

function copyDataToInvoice() {
  var da = document.getElementById('delivery_address');
  var oa = document.getElementById('order_address');
  oa.value = da.value;

  var dc = document.getElementById('delivery_city');
  var oc = document.getElementById('order_city');
  oc.value = dc.value;

  var dp = document.getElementById('delivery_post_code');
  var op = document.getElementById('order_post_code');
  op.value = dp.value;

  var dcc = document.getElementById('delivery_country');
  var occ = document.getElementById('order_country');
  occ.value = dcc.value;
}

function copyDataFromInvoice() {
  var da = document.getElementById('delivery_address');
  var oa = document.getElementById('order_address');
  da.value = oa.value;

  var dc = document.getElementById('delivery_city');
  var oc = document.getElementById('order_city');
  dc.value = oc.value;

  var dp = document.getElementById('delivery_post_code');
  var op = document.getElementById('order_post_code');
  dp.value = op.value;

  var dcc = document.getElementById('delivery_country');
  var occ = document.getElementById('order_country');
  dcc.value = occ.value;
  
  var dco = document.getElementById('delivery_company');
  var oco = document.getElementById('order_company');
  dco.value = oco.value;
}