function handle_action(action, arg)
{
    if((action == "delete" || action == "remove") && !confirm("")) return;
	if((action == "save" || action == "send") && !validate_form()) return;
    document.forms.main.form_arg.value = arg;
    document.forms.main.form_action.value = action;
    window.setTimeout("document.forms.main.submit()", 100);
}

function handle_search()
{
	handle_action("search2", 'marka_id:'+document.getElementById("marka_id").value+'|'+
		'model_id:'+document.getElementById("model_id").value+'|'+
		'diesel:'+(document.getElementById("diesel").checked?1:0)+'|'+
		'benzyna:'+(document.getElementById("benzyna").checked?1:0)+'|'+
		'cena_od:'+document.getElementById("cena_od").value+'|'+
		'cena_do:'+document.getElementById("cena_do").value+'|'+
		'pojemnosc_od:'+document.getElementById("pojemnosc_od").value+'|'+
		'pojemnosc_do:'+document.getElementById("pojemnosc_do").value+'|'+
		'rok_od:'+document.getElementById("rok_od").value+'|'+
		'rok_do:'+document.getElementById("rok_do").value+'|'+
		'rodzaj_oferty:'+document.getElementById("rodzaj_oferty").value);
}

function open_window(file, width, height)
{
	var win = window.open(file, '_blank', 'width='+width+', height='+height+', resizable=no, scrollbars=yes, toolbar=no, location=no, directories=no, status=yes, menubar=no');
	win.focus();
}

function getLastSearch(path)
{
	document.location.href=path+(document.getElementById("lastSearchId").checked ? "?lastsearch=1" : "");
}

function previewGallery(gid)
{
	var win = window.open("preview.php?gid=" + gid, "gallery_preview", "width=860, height=620, resizable=no, toolbar=no, location=no, directories=no, menubar=no");
	win.focus();
}

function validate_ne(name, field)
{
    if(document.forms.main[field].value.length == 0)
	{
	    alert("Pole \"" + name + "\" nie może być puste!");
		return false;
	}
	else return true;
}

function validate_se(name, id)
{	
	if(document.getElementById(id).selectedIndex <= 0)
	{
		alert("Pole \"" + name + "\" nie może być puste!");
		return false;
	}
	return true;
}

function validate_ch(name, field)
{	
	for(i=0; i<document.forms.main.elements.length; i++)
	{
		if(document.forms.main.elements[i].name.replace('[]', '') == field)
			if(document.forms.main.elements[i].checked) 
			return true;
	}
	alert("Zaznacz przynajmniej jedno pole w \"" + name + "\"");
	return false;
}

function checkEmail(field) {
	
	var dt=document.forms.main[field];
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(dt.value))	return true;
	alert("Niepoprawny adres e-mail.");
	dt.focus();
	return false;
}

function validate_newsletter()
{
	if(!checkEmail("newsletter")) return false;
	return true;
}

function validate_length(name, id, min_length)
{
	if(document.getElementById(id).value.length < min_length)
	{
		alert("Minimalna ilo¶ć znaków dla pola "+name+" to "+min_length+".");
		return false;
	}
	return true;
}

function is_identical(id1, id2)
{
	if(document.getElementById(id1).value != document.getElementById(id2).value) return false;
	return true;
}

function displayCriterion(cid1, cid2)
{
    var el;
    var dis=false;
    if((el=document.getElementById("listbox"+cid1))!=null)
    {
        dis=el.selectedIndex>0 ? false : true;
    }
    else if((el=document.getElementById("date_from"+cid1))!=null && document.getElementById("date_to"+cid1)!=null)
    {
        dis=(el.value>0 && document.getElementById("date_to"+cid1)>0) ? false : true;
    }
    
    if((el=document.getElementById("listbox"+cid2))!=null)
    {
        el.disabled=dis;
    }
    else if((el=document.getElementById("date_from"+cid2))!=null)
    {
        el.disabled=dis;
        if((el=document.getElementById("date_to"+cid2))!=null)
        {
            el.disabled=dis;
        }
    }
}

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

function getWindowSize() {
  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 [ myWidth, myHeight ];
}

function displaySuggest(id, show, event)
{
	var scrollXY = getScrollXY();
	var winSize = getWindowSize();

	if((el=document.getElementById("suggestdiv"+id))!=null)
	{
		var x=event.clientX;
		var y=event.clientY;
		el.style.left= scrollXY[0] + ((x+250) >= winSize[0] ? x-230 : x+30) + "px";
		el.style.top= scrollXY[1] + ((y+120) >= winSize[1] ? y-60 : y-10) + "px";
		el.style.visibility = show ? "visible" : "hidden";
	}
}

function opendetails(arg)
{
	 var win = window.open("details.php?arg=" + arg, "criterion_details", "width=400, height=200, resizable=no, toolbar=no, location=no, directories=no, menubar=no");
	 win.focus();
}

function cutString(field, maxvalue)
{
 if(field != null) {
	if(maxvalue < field.value.length) {
		field.value = field.value.substr(0, maxvalue);
	}
 }
}

/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "-";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31;
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30};
		if (i==2) {this[i] = 29};
   } 
   return this
}

function isDate(dtStr){
	if(dtStr.length < 1) return true;
	var daysInMonth = DaysArray(12);
	var pos1=dtStr.indexOf(dtCh);
	var pos2=dtStr.indexOf(dtCh,pos1+1);
	var strDay=dtStr.substring(pos2+1);
	var strMonth=dtStr.substring(pos1+1,pos2);
	var strYear=dtStr.substring(0,pos1);
	strYr=strYear;
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1);
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1);
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1);
	}
	month=parseInt(strMonth);
	day=parseInt(strDay);
	year=parseInt(strYr);
	if (pos1==-1 || pos2==-1){
		alert("Format daty musi być następujacy: rrrr-mm-dd");
		return false;
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Wprowadź poprawny dzień");
		return false;
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Wprowadź poprawny miesiąc");
		return false;
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Wprowadź poprawny rok - 4 cyfry pomiędzy "+minYear+" a "+maxYear);
		return false;
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Wprowadź poprawną datę");
		return false;
	}
	
	return true;
}

function checkDate(field){
	var dt=document.forms.main[field];
	if (isDate(dt.value)==false){
		dt.focus();
		return false;
	}
    return true;
}

function fillDeliveryAddress(el)
{
	if(el.checked)
	{
		document.getElementById('delivery_address').value = document.getElementById('address').value;
		document.getElementById('delivery_postcode').value = document.getElementById('postcode').value;
		document.getElementById('delivery_city').value = document.getElementById('city').value;
	}
}

function setTypKlienta()
{
	if(document.getElementById("typ_klienta_firma").checked)
	{
		showElementsByTagName("poleKlienta", false);
		showElementsByTagName("poleFirmy", true);
	}
	else if(document.getElementById("typ_klienta_osoba").checked)
	{
		showElementsByTagName("poleFirmy", false);
		showElementsByTagName("poleKlienta", true);
	}
}

function showElementsByTagName(name, show)
{
	if(navigator.appName=="Microsoft Internet Explorer")
	{
		var x=document.getElementsByTagName("tr");
		for(i=0; i<x.length; i++)
		{
			if(x[i].name==name) x[i].style.display = (show ? "" : "none");
		}
	}
	else
	{
		var x=document.getElementsByName(name);
		for(i=0; i<x.length; i++)
		{
			x[i].style.display = (show ? "" : "none");
		}
	}
}