loadimage = new Image();
loadimage.src = APP_DIR + '/image/ajax-loader.gif';

function update_products(type, cat_id){
	$('mainBodyDiv').innerHTML += '<div style="position: absolute; width: 100%; height: 100%; background-color: white; -moz-opacity: 0.7; opacity: 0.7; left: 0; top: 0">'
	+ '</div>'
	+ '<div style="position: absolute; top: 0; left: 0; width: 400px;; text-align: center;">'
	+ '<div style="font-size: 14px; width: 200px; margin: auto; padding: 40px; background-color: white; border: 1px solid #ddd; margin-top: 100px; text-align: center;"><img src="' + APP_DIR + '/image/ajax-loader.gif"/><br/></br/><br/>Παρακαλώ περιμένετε</div>'
	+ '</div>'
	
	new Ajax.Updater('mainBodyDiv', '' + APP_DIR + '/' + type + '/view_by_category/'+cat_id+'/1', {
		onComplete: function() { }	
	});
}


function confirm_delete(obj, text) {	
	try {
		i = 0;
		element = obj;
		tagName = 'tr';
		while (element.parentNode && (!element.tagName ||
			(element.tagName.toUpperCase() != tagName.toUpperCase())))
		{
			element = element.parentNode;
			if (i++ > 2) throw e;
		}
		
		Element.addClassName(element, 'toDelete');

	}
	catch (e) {
		alert('ok');
	}
	conf = confirm("Διαγραφή: \n" + text + "\n\nΕίστε βέβαιος;");
	Element.removeClassName(element, 'toDelete');
	return conf;
}

//window.addEventListener('DOMContentLoaded', setup_page, false);

function testt() {
	b = new MultipleSelector();
	b.title = 'text2';
	b.loadHost();
	b.show();
}

function setup_page() {

	// Date fields
	date_fields = document.getElementsByClassName('date_field');
	for (i in date_fields) {
		
		if (date_fields[i].tagName != 'INPUT') continue;

		cal = document.createElement('img');

		cal.src = '/' + APP_DIR + '/image/calendar.png';
		cal.id = date_fields[i].id + "_trigger";

		date_fields[i].size = '11'
		date_fields[i].style.padding = '2px;';
		date_fields[i].style.paddingRight = '17px;';
		date_fields[i].style.background = '#fff url(/' + APP_DIR + '/image/calendar.png) 99% 50% no-repeat';
		date_fields[i].style.border = "1px solid lightgray"
//		date_fields[i].parentNode.appendChild(cal);

		Calendar.setup({
			inputField     :    date_fields[i].id,
			ifFormat       :    "%d/%m/%Y",
			button         :    date_fields[i].id + "_trigger",
			singleClick    :    true
		});
	}

	// Numeric fields
	date_fields = document.getElementsByClassName('numeric_field');
	for (i in date_fields) {
		
		if (date_fields[i].tagName != 'INPUT') continue;

		date_fields[i].onkeydown = validateNumChar;
		date_fields[i].onblur = validateNumeric;
	}

	// Float fields
	date_fields = document.getElementsByClassName('float_field');
	for (i in date_fields) {
		
		if (date_fields[i].tagName != 'INPUT') continue;

		date_fields[i].onkeydown = validateNumChar;
		date_fields[i].onblur = validateFloat;
	}

}

function validateNumChar(e) {

	res = true;

	switch (e.which)
	{	
		case 9: res = true; break; // TAB
		case 39: res = true; break; // LEFT
		case 37: res = true; break; // RIGHT
		case 38: res = true; break; // UP
		case 40: res = true; break; // DOWN
		case 106: res = true; break; // F5
		
		case 48: res = true; break; // 0
		case 49: res = true; break; // 1
		case 50: res = true; break; // 2
		case 51: res = true; break; // 3
		case 52: res = true; break; // 4
		case 53: res = true; break; // 5
		case 54: res = true; break; // 6
		case 55: res = true; break; // 7
		case 56: res = true; break; // 8
		case 57: res = true; break; // 9

		case 188: this.value += '.'; res = false; break; // ,
		case 190: res = true; break; // .
		case 8: res = true; break; // del

		default: res = false;
	}

	return res;
}

function validateNumeric() {

	this.style.padding = "1px";

	if (this.value == '' || this.value.match('^[0-9]+$')) {
		this.style.border = "1px solid ActiveBorder";
		this.style.backgroundColor = "#fff";
		return;
	}

	this.style.border = "1px solid red";
	this.style.backgroundColor = "#fdd";

}

function validateFloat() {

	this.style.padding = "1px";

	if (this.value == '' || this.value.match('^[0-9]+[\.]{0,1}[0-9]+$') || this.value.match('^[0-9]+$')) {
		this.style.border = "1px solid ActiveBorder";
		this.style.backgroundColor = "#fff";
		return;
	}

	this.style.border = "1px solid red";
	this.style.backgroundColor = "#fdd";

}

function get_region_by_country(type_select, cat_select_id) {
	new Ajax.Request(APP_DIR+"/search/get_region_by_country/" + $F(type_select),
	{onSuccess: update_region_categories(cat_select_id)});

}

function update_region_categories(cat_select_id, selected){
	return function (resp){
		try{
			c=eval(resp.responseText);
			$(cat_select_id).options.length=0;
			if (cat_select_id == "region"){
				$(cat_select_id).options[$(cat_select_id).options.length]=new Option('Περιφέρειες','0')
			} else if (cat_select_id == "prefecture"){
				$(cat_select_id).options[$(cat_select_id).options.length]=new Option('Νομοί','0')
			} else if (cat_select_id == "city"){
				$(cat_select_id).options[$(cat_select_id).options.length]=new Option('Πόλεις','0')
			}
			for(i in c){
				if (!c[i]['_id'])
				{
					continue;
				}
				$(cat_select_id).options[$(cat_select_id).options.length]=new Option(c[i]['name'],c[i]['_id'])
			}
		}
		catch(e){
		}
	}
}


function empty_categories(cat_select_id){
	$(cat_select_id).options.length=0;
	if (cat_select_id == "prefecture"){
		$(cat_select_id).options[$(cat_select_id).options.length]=new Option('Νομοί','0')
	} else if (cat_select_id == "city"){
		$(cat_select_id).options[$(cat_select_id).options.length]=new Option('Πόλεις','0')
	}
}


function get_hour_km(veh_select_id) {
	
	new Ajax.Request("/"+APP_DIR+"/vehicle/get_vehicle_hour_km/" + $F(veh_select_id),
	{onSuccess: choose_hour_km});

}

function choose_hour_km(resp){
		try{
			c=eval(resp.responseText);
			if (c=='hour')
			{
				$('km').style.display='none';
				$('km1').style.display='none';
				$('km2').style.display='none';
				$('hour').style.display='block';
				$('hour1').style.display='block';
				$('hour2').style.display='block';
			}
			else if (c=='km')
			{
				$('hour').style.display='none';
				$('hour1').style.display='none';
				$('hour2').style.display='none';
				$('km').style.display='block';
				$('km1').style.display='block';
				$('km2').style.display='block';
			}
		}
		catch(e){
			//alert('malakia re baggelh');
		}
}

function choose_hour_km2(radio_id){
	if ($F(radio_id)=="km")
	{
		$('hour1').style.display='none';
		$('hour2').style.display='none';
		$('hour3').style.display='none';
		$('km1').style.display='block';
		$('km2').style.display='block';
		$('km3').style.display='block';
	}else
	{
		$('km1').style.display='none';
		$('km2').style.display='none';
		$('km3').style.display='none';
		$('hour1').style.display='block';
		$('hour2').style.display='block';
		$('hour3').style.display='block';
	}
}

