var isInternetExplorer = (navigator.appName.indexOf("Microsoft") != -1);

/*
future references:
var keycode = isInternetExplorer ? tender.keyCode : tender.which;
*/

function countSpaces(obj){
	var iLength = obj.value.length;
	var strSpaces = obj.value.match(new RegExp("( )", "g"));
	var countSpaces = strSpaces ? strSpaces.length : 0;
	return countSpaces;
}

function countLineBreaks(obj){
	var iLength = obj.value.length;
	var strLineBreaks = obj.value.match(new RegExp("(\\n)", "g"));
	var countLineBreaks = strLineBreaks ? strLineBreaks.length : 0;
	return countLineBreaks;
}

function textCounter(field, counter_field, maxlimit) {
	var lineBreaks = countLineBreaks(field);
	var adjust = isInternetExplorer ? 1 : 0;
	if (field.value.length - lineBreaks * adjust > maxlimit){
		field.value = field.value.substring(0, maxlimit + lineBreaks * adjust);
		field.focus();
	} else {
		counter_field.value = maxlimit - field.value.length + lineBreaks * adjust;
	}
}

function backToSection(backToURL, forceBackToURL){
	if(forceBackToURL == null) forceBackToURL = false;
	if(history.length > 1 && !forceBackToURL) history.back(); else window.location.href = backToURL;
}

function hideStatus() {
	window.defaultStatus='';
	window.status='';
	return true;
}

function searchReset() {
	tot = document.search_form.elements.length;
	for(i=0;i<tot;i++) {
		if (document.search_form.elements[i].type == 'text') {
			document.search_form.elements[i].value = "";
		} else if (document.search_form.elements[i].type == 'checkbox' || document.search_form.elements[i].type == 'radio') {
			document.search_form.elements[i].checked = false;
		} else if (document.search_form.elements[i].type == 'select-one') {
			document.search_form.elements[i].selectedIndex = 0;
		}
	}
	if ((document.search_form.country_id) || (document.search_form.state_id) || (document.search_form.region_id) || (document.search_form.city_id) || (document.search_form.area_id)) {
		searchLocationReset();
	}
}


function easyFriendlyUrl(name2friendlyurl, target, validchars, separator) {

        A = new Array();
        A['¨']='YO';A['É']='I';A['Ö']='TS';A['Ó']='U';A['Ê']='K';A['Å']='E';A['Í']='N';A['Ã']='G';A['Ø']='SH';A['Ù']='SCH';A['Ç']='Z';A['Õ']='H';A['Ú']='';
        A['¸']='yo';A['é']='i';A['ö']='ts';A['ó']='u';A['ê']='k';A['å']='e';A['í']='n';A['ã']='g';A['ø']='sh';A['ù']='sch';A['ç']='z';A['õ']='h';A['ú']='';
        A['Ô']='F';A['Û']='I';A['Â']='V';A['À']='A';A['Ï']='P';A['Ð']='R';A['Î']='O';A['Ë']='L';A['Ä']='D';A['Æ']='ZH';A['Ý']='E';
        A['ô']='f';A['û']='i';A['â']='v';A['à']='a';A['ï']='p';A['ð']='r';A['î']='o';A['ë']='l';A['ä']='d';A['æ']='zh';A['ý']='e';
        A['ß']='YA';A['×']='CH';A['Ñ']='S';A['Ì']='M';A['È']='I';A['Ò']='T';A['Ü']='';A['Á']='B';A['Þ']='YU';
        A['ÿ']='ya';A['÷']='ch';A['ñ']='s';A['ì']='m';A['è']='i';A['ò']='t';A['ü']='';A['á']='b';A['þ']='yu';A['\"']='-';
            name2friendlyurl = name2friendlyurl.replace(/([\u0410-\u0451])/g,
            function (str,p1,offset,s) {
                if (A[str] != 'undefined'){return A[str];}
            }
	       ).replace(/ /g,'-'); 

   	var str = "";
	var i;
	var exp_reg = new RegExp("[" + validchars + separator + "]");
	var exp_reg_space = new RegExp("[ ]");
	var exp_reg_dotted = new RegExp("[.]");
	name2friendlyurl.toString();
	for (i=0 ; i<name2friendlyurl.length; i++) {
		if (exp_reg.test(name2friendlyurl.charAt(i))) {
			str = str+name2friendlyurl.charAt(i);
		} else {
			if ((exp_reg_space.test(name2friendlyurl.charAt(i))) || (exp_reg_dotted.test(name2friendlyurl.charAt(i)))){
				if (str.charAt(str.length-1) != separator) {
					str = str + separator;
				}
			}
		}
	}
	if (str.charAt(str.length-1) == separator) str = str.substr(0, str.length-1);
	document.getElementById(target).value = str.toLowerCase();
}


function searchLocationReset() {
	if (document.search_form.country_id) {
		if (document.search_form.state_id) {
			while (document.search_form.state_id.options.length>1) {
				deleteIndex=document.search_form.state_id.options.length-1;
				document.search_form.state_id.options[deleteIndex]=null;
			}
		}
		if (document.search_form.region_id) {
			while (document.search_form.region_id.options.length>1) {
				deleteIndex=document.search_form.region_id.options.length-1;
				document.search_form.region_id.options[deleteIndex]=null;
			}
		}
		if (document.search_form.city_id) {
			while (document.search_form.city_id.options.length>1) {
				deleteIndex=document.search_form.city_id.options.length-1;
				document.search_form.city_id.options[deleteIndex]=null;
			}
		}
		if (document.search_form.area_id) {
			while (document.search_form.area_id.options.length>1) {
				deleteIndex=document.search_form.area_id.options.length-1;
				document.search_form.area_id.options[deleteIndex]=null;
			}
		}
	} else if (document.search_form.state_id) {
		if (document.search_form.region_id) {
			while (document.search_form.region_id.options.length>1) {
				deleteIndex=document.search_form.region_id.options.length-1;
				document.search_form.region_id.options[deleteIndex]=null;
			}
		}
		if (document.search_form.city_id) {
			while (document.search_form.city_id.options.length>1) {
				deleteIndex=document.search_form.city_id.options.length-1;
				document.search_form.city_id.options[deleteIndex]=null;
			}
		}
		if (document.search_form.area_id) {
			while (document.search_form.area_id.options.length>1) {
				deleteIndex=document.search_form.area_id.options.length-1;
				document.search_form.area_id.options[deleteIndex]=null;
			}
		}
	} else if (document.search_form.region_id) {
		if (document.search_form.city_id) {
			while (document.search_form.city_id.options.length>1) {
				deleteIndex=document.search_form.city_id.options.length-1;
				document.search_form.city_id.options[deleteIndex]=null;
			}
		}
		if (document.search_form.area_id) {
			while (document.search_form.area_id.options.length>1) {
				deleteIndex=document.search_form.area_id.options.length-1;
				document.search_form.area_id.options[deleteIndex]=null;
			}
		}
	} else if (document.search_form.city_id) {
		if (document.search_form.area_id) {
			while (document.search_form.area_id.options.length>1) {
				deleteIndex=document.search_form.area_id.options.length-1;
				document.search_form.area_id.options[deleteIndex]=null;
			}
		}
	}
}

//function $(id) {
//	return document.getElementById(id);
//}

function showText(text) {
	return unescape(text);
}

function ftype()
{
var ctype = document.getElementById("select_type");
var ctable = document.getElementById("cost-table");

if (ctype.selectedIndex == 1) {ctable.style.display = 'table';}
else ctable.style.display = 'none';
}



function f1()
{
var s = document.getElementById("select_price");
var t = document.getElementById("price_fird");
var k = document.getElementById("price_second");
var sf = document.getElementById("price_from");
var st = document.getElementById("price_to");


if(s.selectedIndex == 1){
t.style.display = 'inline';
k.style.display = 'inline';
sf.style.display = 'inline';
st.style.display = 'inline';

}
else {
k.style.display = 'none';
sf.style.display = 'none';
st.style.display = 'none';
}
if(s.selectedIndex == 2){
t.style.display = 'none';
k.style.display = 'none';
sf.style.display = 'none';
st.style.display = 'none';
}
if(s.selectedIndex == 0){
t.style.display = 'inline';
k.style.display = 'none';
sf.style.display = 'none';
st.style.display = 'none';
}
}
