var error_color = '#ff3232';
var aRegions = new Array();

function return_value() {
	
}

function open_popup(iActive,sURL,sName)
{
	
}

function trIn(tr)
{
	var i;
	for( i = 0; i < tr.cells.length; i++ ) {
		tr.cells[i].style.backgroundColor = '#b8b8b8';
	}
}

function trOut(tr)
{
	var i;
	for( i = 0; i < tr.cells.length; i++ ) {
		tr.cells[i].style.backgroundColor = '';
	}
}

function setSelect(sId, iValue)
{
	var oSelect = document.getElementById(sId);
	oSelect.selectedIndex = iValue;
}

function generate_brands(iVehicleId,aVehicles) {
	if (iVehicleId < 0) {
		return false;
	}
	var oSelect = document.getElementById('brand_id');
	var oFragment = document.createDocumentFragment();

	for (var i=oSelect.options.length-1; i >= 0; i--)
	{
		oSelect.remove(i);
	}

	var oOption = document.createElement("option");
	oOption.appendChild(document.createTextNode(messages[0]));
	oOption.setAttribute("value", "-1");
	oFragment.appendChild(oOption);

	var aTemp = aVehicles[iVehicleId];
	var len = aTemp.length;

	for (var i = 0; i < len; i++)
	{
		var oOption = document.createElement("option");
		oOption.appendChild(document.createTextNode(aTemp[i]));
		oOption.setAttribute("value", i);
		oFragment.appendChild(oOption);
	}
	oSelect.appendChild(oFragment);
	
	if (oSelect.options.length == 2) {
		oSelect.selectedIndex = 1;
	}
}

function generate_models(iBrandId,aVehicle) {
	if (iBrandId < 0) {
		return false;
	}
	var oSelect = document.getElementById('model_id');
	var oFragment = document.createDocumentFragment();
	var aTemp = aVehicle[iBrandId];
	var len = aTemp.length;

	for (var i=oSelect.options.length-1; i >= 0; i--)
	{
		oSelect.remove(i);
	}

	var oOption = document.createElement("option");
	oOption.appendChild(document.createTextNode(messages[0]));
	oOption.setAttribute("value", "-1");
	oFragment.appendChild(oOption);
	for (var i = 0; i < len; i++)
	{
		var oOption = document.createElement("option");
		oOption.appendChild(document.createTextNode(aTemp[i]));
		oOption.setAttribute("value", i);
		oFragment.appendChild(oOption);
	}
	oSelect.appendChild(oFragment);
	
	if (oSelect.options.length == 2) {
		oSelect.selectedIndex = 1;
	}
}

function isValidEmail(sText) {
	var reEmail = new RegExp("^[a-zA-Z0-9_\\.\\-]+@[a-zA-Z0-9_\\-]+\\.[a-zA-Z0-9]{2,6}$");
	return reEmail.test(sText);
}

function isValidPhone(sText) {
	var rePhone = /^(\+)?[0-9\-]{6,}$/
	return rePhone.test(sText);
}

function category_change(sCategory) {
	if (sCategory != '') {
		window.location = 'index.php?action=search_' + sCategory;
	}
}

function category_change2(sCategory) {
	if (sCategory != '') {
		window.location = 'index.php?action=publish_' + sCategory;
	}
}

function customer_profile() {

	var oOldPass = document.getElementById("password_old");
	var oNewPass1 = document.getElementById("password_new1");
	var oNewPass2 = document.getElementById("password_new2");
	
	var oName = document.getElementById("name");
	var oContacts = document.getElementById("contacts");
	var oPhone = document.getElementById("phone");
	var oPhone2 = document.getElementById("phone2");
	var oCountry = document.getElementById("country");
	var oRegion = document.getElementById("region");

	if (oName.value == "") {
		alert("Εισάγετε έγκυρα δεδομένα σε όλα τα πεδία!");
		oName.focus();
		oName.style.background = error_color;
		return false;
	} else {
		oName.style.background = '#FFFFFF';
	}
	
	if (oContacts.value == "") {
		alert("Εισάγετε έγκυρα δεδομένα σε όλα τα πεδία!");
		oContacts.focus();
		oContacts.style.background = error_color;
		return false;
	} else {
		oContacts.style.background = '#FFFFFF';
	}
	
	if (oCountry.value == 0) {
		alert("Εισάγετε έγκυρα δεδομένα σε όλα τα πεδία!");
		oCountry.focus();
		oCountry.style.background = error_color;
		return false;
	} else {
		oCountry.style.background = '#FFFFFF';
	}
	
	if (oCountry.value == 'gr' && oRegion.value == 0) {
		alert("Εισάγετε έγκυρα δεδομένα σε όλα τα πεδία!");
		oRegion.focus();
		oRegion.style.background = error_color;
		return false;
	} else {
		oRegion.style.background = '#FFFFFF';
	}
	
	if ((oPhone.value == "" || !isValidPhone(oPhone.value))) {
		alert("Εισάγετε έγκυρα δεδομένα σε όλα τα πεδία!");
		oPhone.focus();
		oPhone.style.background = error_color;
		return false;
	} else {
		oPhone.style.background = '#FFFFFF';
	}

	if ((oPhone2.value != "" && !isValidPhone(oPhone2.value))) {
		alert("Εισάγετε έγκυρα δεδομένα σε όλα τα πεδία!");
		oPhone2.focus();
		oPhone2.style.background = error_color;
		return false;
	} else {
		oPhone2.style.background = '#FFFFFF';
	}
	
	if (oNewPass1.value != "" || oNewPass2.value != "") {
		if (oOldPass.value == "" || oNewPass1.value == "" || oNewPass2.value == "" || oNewPass1.value != oNewPass2.value) {
			alert("Εισάγετε έγκυρα δεδομένα σε όλα τα πεδία!");
			oOldPass.style.background = error_color;
			oNewPass1.style.background = error_color;
			oNewPass2.style.background = error_color;
			oOldPass.focus();
			return false;
		} else {
			oOldPass.style.background = '#FFFFFF';
			oNewPass1.style.background = '#FFFFFF';
			oNewPass2.style.background = '#FFFFFF';
			return true;
		}
	}
	
	return true;	
}

function show_error_div(sErrorMessage, sDivId) {
	var oDiv = document.getElementById(sDivId);
	oDiv.innerHTML = sErrorMessage;
	oDiv.style.display = "block";
}

function hide_error_div(sDivId) {
	var oDiv = document.getElementById(sDivId);
	oDiv.innerHTML = '';
	oDiv.style.display = "none"
}

function disable_button(oButton) {
	oButton.disabled=true;
}

function sp_ad_button(oButton) {
	oButton.disabled=true;
	
	var oForm = document.getElementById('sp_form');
	
	oForm.submit();
}

function restore_data() {
	
		var oSelect = document.getElementById('brand_id');
		if (oSelect.value > 0) {
		oSelect.onchange();
		}

}

function preview_back()
{
    var url="index.php?action=images_clean"
    xmlHttp = GetXmlHttpObject(return_value);
    xmlHttp.open("GET", url , false);
    xmlHttp.send(null);
    
    history.back(1);
}

function country_change(sName) {

	var oSelect = document.getElementById('region');

	if (sName != 'gr') {
		for (var i=0; i = oSelect.options.length-1; i++)
		{
			aRegions[i] = oSelect.options[i];
			oSelect.remove(i);
		}
		var oOption = document.createElement("option");
		oOption.appendChild(document.createTextNode('Other'));
		oOption.setAttribute("value", "0");
		oOption.setAttribute("selected", "selected");
		oSelect.appendChild(oOption);
	} else if (aRegions.length > 0) {
		for (var i=0; i = oSelect.options.length-1; i++)
		{
			oSelect.remove(i);
		}
		for (var i = 0; i < aRegions.length-1; i++)
		{
			oSelect.appendChild(aRegions[i+1]);
		}
	}
}

function set_country(sCountry) {
	
	var oSelect = document.getElementById('country');

	for (var i=oSelect.options.length-1; i >= 0; i--)
	{
		if (oSelect.options[i].value == sCountry) {
			oSelect.selectedIndex = i;
			break;
		}
	}
	country_change(sCountry);
}

