function fillComboBox(cbox, checkValue, optionsArray) {
	cbox.disabled = false;
	for (var i=1; i<cbox.options.length;) {
		cbox.options[i] = null;
	}
	
	for (var i=0; i<optionsArray.length; i++) {
		var obj = optionsArray[i];
		if (obj.check == checkValue) {
			cbox.options[cbox.options.length] = new Option(obj.text, obj.value);
		}
	}
}

function estateCategoriesTypes(upBoxId, dnBoxId) {
	var upBox = document.getElementById(upBoxId);
	var dnBox = document.getElementById(dnBoxId);
	
	var upValue = parseInt(upBox.value); 
	
	if (upValue == 0) {
		dnBox.selectedIndex = 0;
		dnBox.disabled = true;
	} else {
		fillComboBox(dnBox, upValue, estatesTypes);
	}	
}

function locationCountriesRegions(upBoxId, mdBoxId, dnBoxId, lastBoxId) {
	var upBox = document.getElementById(upBoxId);
	var mdBox = document.getElementById(mdBoxId);
	var dnBox = document.getElementById(dnBoxId);
	var lastBox = document.getElementById(lastBoxId);
	
	var upValue = parseInt(upBox.value);
	if (upValue == 0) {
		mdBox.selectedIndex = 0;
		mdBox.disabled = true;
		dnBox.selectedIndex = 0;
		dnBox.disabled = true;
		lastBox.selectedIndex = 0;
		lastBox.disabled = true;
	} else {
		fillComboBox(mdBox, upValue, locationsRegions);
	}
}

function locationRegionsPrefectures(mdBoxId, dnBoxId, lastBoxId) {
	var mdBox = document.getElementById(mdBoxId);
	var dnBox = document.getElementById(dnBoxId);
	var lastBox = document.getElementById(lastBoxId);
	
	var mdValue = parseInt(mdBox.value);
	if (mdValue == 0) {
		dnBox.selectedIndex = 0;
		dnBox.disabled = true;
		lastBox.selectedIndex = 0;
		lastBox.disabled = true;
	} else {
		fillComboBox(dnBox, mdValue, locationsPrefectures);
	}
}

function locationPrefecturesCities(dnBoxId, lastBoxId) {
	var dnBox = document.getElementById(dnBoxId);
	var lastBox = document.getElementById(lastBoxId);
	
	var dnValue = parseInt(dnBox.value); 
	
	if (dnValue == 0) {
		lastBox.selectedIndex = 0;
		lastBox.disabled = true;
	} else {
		fillComboBox(lastBox, dnValue, locationsCities);
	}
}

function triggerComboBox(cbox, value) {
	cbox.disabled = false;
	var options = cbox.options;
	for (var i=0, len=options.length; i<len; i++) {
		if (options[i].value == value) {
			options[i].selected = true;
			break;
		}
	}
}

function triggerCategoriesTypes(upBoxId, dnBoxId, val1, val2) {
	var upBox = document.getElementById(upBoxId);
	triggerComboBox(upBox, val1);
	
	if (upBox.value != 0) {
		estateCategoriesTypes(upBoxId, dnBoxId);
		var dnBox = document.getElementById(dnBoxId);
		triggerComboBox(dnBox, val2);
	}
}

function triggerRegionsPrefectureCities(upBoxId, mdBoxId, dnBoxId, val1, val2, val3) {
	var upBox = document.getElementById(upBoxId);
	triggerComboBox(upBox, val1);
	
	if (upBox.value != 0) {
		locationRegionsPrefectures(upBoxId, mdBoxId, dnBoxId);
		var mdBox = document.getElementById(mdBoxId);
		triggerComboBox(mdBox, val2);
		
		if (mdBox.value != 0) {
			locationPrefecturesCities(mdBoxId, dnBoxId);
			var dnBox = document.getElementById(dnBoxId);
			triggerComboBox(dnBox, val3);
		}
	}
}

function triggerCountriesRegionsPrefectureCities(upBoxId, mdBoxId, dnBoxId, lastBoxId, val1, val2, val3, val4) {
	var upBox = document.getElementById(upBoxId);
	triggerComboBox(upBox, val1);
	
	if (upBox.value != 0) {
		locationCountriesRegions(upBoxId, mdBoxId, dnBoxId, lastBoxId);
		var mdBox = document.getElementById(mdBoxId);
		triggerComboBox(mdBox, val2);
		
		if (mdBox.value != 0) {
			locationRegionsPrefectures(mdBoxId, dnBoxId);
			var dnBox = document.getElementById(dnBoxId);
			triggerComboBox(dnBox, val3);
			
			if (dnBox.value != 0) {
				locationPrefecturesCities(dnBoxId, lastBoxId);
				var lastBox = document.getElementById(lastBoxId);
				triggerComboBox(lastBox, val4);
			}
		}
	}
}

function printerFriendly(homePath, action) {
	var newwindow = window.open(homePath + '/'+ action, 'printable_version', 'menubar=yes,location=no,directories=no,copyhistory=no,width=582,height=600');
	newwindow.focus();
}

function addBookmark(url) {
	var title = 'ΣΠΕΔΕΗ';
	if (window.sidebar) { // firefox
		window.sidebar.addPanel(title, url, "");
	} else if( document.all ) { //MSIE
		window.external.AddFavorite(url, title);
	} else {
		alert("Sorry, your browser does nοt support this");
	}
}

setCookie = function(name, value, expires, path, domain, secure) {
	var curCookie = name + '=' + escape(value) +
		((expires) ? '; expires=' + new Date(expires).toGMTString() : '') +
		((path) ? '; path=' + path : '') +
		((domain) ? '; domain=' + domain : '') +
		((secure) ? '; secure' : '');
  	document.cookie = curCookie;
}

getCookie = function(name) {
  	var dc = document.cookie;
  	var prefix = name + '=';
  	var begin = dc.indexOf('; ' + prefix);
  	if (begin == -1) {
    	begin = dc.indexOf(prefix);
    	if (begin != 0) return null;
  	} else{
    	begin += 2;
  	}
  	var end = document.cookie.indexOf(';', begin);
  	if (end == -1) end = dc.length;
  	return unescape(dc.substring(begin + prefix.length, end));
}

function setActiveStyleSheet(file) {
	var link = document.getElementById('activeStyle');
	var pos = link.href.lastIndexOf('/');
	link.href = link.href.substr(0, pos + 1) + file + '.css';
	setCookie("activeStyle", file, '01/01/2200', '/spedeh');	
}

if (!Array.indexOf){
Array.prototype.indexOf = function(obj){
    for(var i=0; i<this.length; i++){
        if(this[i] == obj){
            return i;
        }
    }
    return -1;
}
}


function iconizeLinks() {
	var links = document.getElementById('innerMainColumn').getElementsByTagName('a');
	var fileTypes = ['doc', 'docx', 'pdf', 'txt'];
	
	for(var i=0, len=links.length; i<len; i++) {
		var href = links[i].href;
		var type = href.substr(href.lastIndexOf('.') + 1);
		if (fileTypes.indexOf(type) > -1) {
			var clazz = links[i].className || '';
			links[i].className = clazz + (clazz.length > 0 ? ' ' + type : type);
		}
		
		
	}
	
}
