
// Communication

var type_to_select = -1;

function prepareUrl(){
	var b = document.getElementById('marka');
	var m = document.getElementById('tip');
	var bid = b.options[b.selectedIndex].value;
	var mid = m.options[m.selectedIndex].value;
	document.location.href = '#'+bid+'-'+mid;
}

function AjaxRequestText(url, callback)
{
    req = null;

    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        try {
            req = new ActiveXObject("Msxml2.XMLHTTP")
        } catch (e) {
            try {
                req = new ActiveXObject("Microsoft.XMLHTTP")
            } catch (e) {}
        }
    }
    if (req == null) return false;

    req.onreadystatechange = function() {
        if ((req.readyState == 4) && (req.status == 200)) {
            callback(req.responseText);
        }
    }

    req.open("GET", url);
    // disable cache - optional
    req.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
    req.send(null);

    return true;
}

// Generic infobox routines

function show_data(data, target)
{
   var box = document.getElementById(target);
   if (navigator.appVersion.indexOf("MSIE")!=-1){
     box.outerHTML = '<select name="' + target +'" id="' + target + 
	 	'" class="DropD-2" onchange="onchange_set_value(\'tip\'); prepareUrl();">' + data + '</select>';
   } else {
     box.innerHTML = data;
   }
    box = document.getElementById('tip');	
	if (box != null){		
		if (type_to_select > -1){			
			box.value = type_to_select;
			type_to_select = -1;
		}
   }
   prepareUrl();
}

function show_car_types(data) { show_data(data, 'tip'); }
function show_car_models(data) { show_data(data, 'model'); }

function onchange_set_value(id){
	var el = document.getElementById(id);
	var name = el.options[el.selectedIndex].text;	
	var nameEl = document.getElementById(id + '_name');
	if (nameEl){
		nameEl.value = name;
	}
}

function delete_image_el(data){
	if (data == 'FAILED')
		return;
	var imgEl = document.getElementById(data);
	if (imgEl)
		imgEl.src = 'images/no_image2.gif';
	var linkEl = document.getElementById(data + "del");
	if (linkEl)
		linkEl.parentNode.removeChild(linkEl);
}

function delete_image(img_id, car_id, suffix){
	AjaxRequestText('ajax_service.php?req=deleteimg&id=' + car_id + '&sub=' + suffix + '&imgid=' + img_id, delete_image_el);
}

function load_types(in_use)
{
    var bid = document.getElementById('marka').value;
	onchange_set_value('marka');
    var use_flag = '';
    if (in_use) use_flag = '&in_use=1';
    AjaxRequestText('ajax_service.php?req=ctypes&brand=' + bid + use_flag, show_car_types);
}

function show_up_img(up, img)
{
    var fname = document.getElementById(up).value;
	if (!isIE())
		fname = 'file:///' + fname.replace(/\\/g, "/");
    document.getElementById(img).src = fname; 
}

function isIE(){
	var xOp7Up,xOp6Dn,xIE4Up,xIE4,xIE5,xNN4,xUA=navigator.userAgent.toLowerCase();
	if(window.opera){
	  var i=xUA.indexOf('opera');
	  if(i!=-1){
		var v=parseInt(xUA.charAt(i+6));
		xOp7Up=v>=7;
		xOp6Dn=v<7;
	  }
	}
	else if(navigator.vendor!='KDE' && document.all && xUA.indexOf('msie')!=-1){
	  xIE4Up=parseFloat(navigator.appVersion)>=4;
	  xIE4=xUA.indexOf('msie 4')!=-1;
	  xIE5=xUA.indexOf('msie 5')!=-1;
	}
	else if(document.layers){xNN4=true;}
	xMac=xUA.indexOf('mac')!=-1;
	return xIE4Up;
}

function anketa_glasaj()
{
    var id = document.getElementById('anketa_id').value;
    var odgovor = document.getElementById('odabran').value;
    
	if (odgovor == ""){
		alert ("Niste odabrali odgovor niti jedan odgovor!");
		return false;
	}
    wopen('ankete.php?ajax=1&id=' + id + '&odgovor=' + odgovor, 'anketa', 320, 240);
    return false;
}

function wopen(url, name, w, h)
{
	// Fudge factors for window decoration space.
	// In my tests these work well on all platforms & browsers.
	w += 32;
	h += 96;
	wleft = (screen.width - w) / 2;
	wtop = (screen.height - h) / 2;
	var win = window.open(url,
	name,
	'width=' + w + ', height=' + h + ', ' +
	'left=' + wleft + ', top=' + wtop + ', ' +
	'location=no, menubar=no, ' +
	'status=no, toolbar=no, scrollbars=no, resizable=no');
	// Just in case width and height are ignored
	win.resizeTo(w, h);
	// Just in case left and top are ignored
	win.moveTo(wleft, wtop);
	win.focus();
	return win;
}

function wopen_sr(url, name, w, h)
{
	// Fudge factors for window decoration space.
	// In my tests these work well on all platforms & browsers.
	w += 32;
	h += 96;
	wleft = (screen.width - w) / 2;
	wtop = (screen.height - h) / 2;
	var win = window.open(url,
	name,
	'width=' + w + ', height=' + h + ', ' +
	'left=' + wleft + ', top=' + wtop + ', ' +
	'location=no, menubar=no, ' +
	'status=no, toolbar=no, scrollbars=yes, resizable=yes');
	// Just in case width and height are ignored
	win.resizeTo(w, h);
	// Just in case left and top are ignored
	win.moveTo(wleft, wtop);
	win.focus();
	return win;
}


