var isInternetExplorer = (navigator.appName.indexOf("Microsoft") != -1);

/*
future references:
var keycode = isInternetExplorer ? event.keyCode : event.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) {
	var str = "";
	var i;
	var exp_reg = new RegExp("[" + validchars + separator + "]");
	var exp_reg_space = 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))) {
				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 showPhone(listingid, default_url) {
	try {
		xmlhttp = new XMLHttpRequest();
	} catch (exc) {
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (ex) {
			try {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				xmlhttp = false;
			}
		}
	}
	if (xmlhttp) {
		xmlhttp.open("GET", default_url+'/countphoneclick.php?listing_id='+listingid, true);
		xmlhttp.send(null);
	}
	document.getElementById("phoneLink"+listingid).className = "controlPhoneHide";
	document.getElementById("phoneNumber"+listingid).className = "controlPhoneShow";
}

function showFax(listingid, default_url) {
	try {
		xmlhttp = new XMLHttpRequest();
	} catch (exc) {
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (ex) {
			try {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				xmlhttp = false;
			}
		}
	}
	if (xmlhttp) {
		xmlhttp.open("GET", default_url+'/countfaxclick.php?listing_id='+listingid, true);
		xmlhttp.send(null);
	}
	document.getElementById("faxLink"+listingid).className = "controlFaxHide";
	document.getElementById("faxNumber"+listingid).className = "controlFaxShow";
}

var obj;
var field_0;
var field_1;
var field_2;
var field_loading;

function fillSelect(host, obj, fk_value, form) {

	this.obj = obj;
	this.field_0 = form.country_id;
	this.field_1 = form.state_id;
	this.field_2 = form.region_id;

	resetSelect(obj);

	// check if object exisits.
	if (typeof obj != 'object') return false;

	if (fk_value > 0) {
		form.country_id.disabled=true;
		form.state_id.disabled=true;
		form.region_id.disabled=true;
	}

	if (obj.name == "state_id") {
		resetSelect(form.region_id);
		if (fk_value > 0) {
			form.state_id.options[1] = new Option(showText(LANG_JS_LOADING), -1);
			form.state_id.options[1].selected=true;
			this.field_loading = form.state_id;
		}
		url = host + '/location.php?country_id=' + fk_value;
		if (fk_value > 0) loadResult(url, '');
	}

	if (obj.name == "region_id") {
		if (fk_value > 0) {
			form.region_id.options[1] = new Option(showText(LANG_JS_LOADING), -1);
			form.region_id.options[1].selected=true;
			this.field_loading = form.region_id;
		}
		url = host + '/location.php?state_id=' + fk_value;
		if (fk_value > 0) loadResult(url, '');
	}

}

function fillSelectSearchPage(host, obj, fk_value, form) {

	this.obj = obj;
	this.field_0 = form.search_country;
	this.field_1 = form.search_state;
	this.field_2 = form.search_region;

	resetSelect(obj);

	// check if object exisits.
	if (typeof obj != 'object') return false;

	if (fk_value > 0) {
		form.search_country.disabled=true;
		form.search_state.disabled=true;
		form.search_region.disabled=true;
	}

	if (obj.name == "search_state") {
		resetSelect(form.search_region);
		if (fk_value > 0) {
			form.search_state.options[1] = new Option(showText(LANG_JS_LOADING), -1);
			form.search_state.options[1].selected=true;
			this.field_loading = form.search_state;
		}
		url = host + '/location.php?country_id=' + fk_value + '&searchpage=1';
		if (fk_value > 0) loadResult(url, '');
	}

	if (obj.name == "search_region") {
		if (fk_value > 0) {
			form.search_region.options[1] = new Option(showText(LANG_JS_LOADING), -1);
			form.search_region.options[1].selected=true;
			this.field_loading = form.search_region;
		}
		url = host + '/location.php?state_id=' + fk_value + '&searchpage=1';
		if (fk_value > 0) loadResult(url, '');
	}

}

function resetSelect(obj) {
	while (obj.options.length>1) {
		deleteIndex=obj.options.length-1;
		obj.options[deleteIndex]=null;
	}
}

function loadXMLDoc(url) {
	// branch for native XMLHttpRequest object
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
		req.onreadystatechange = processReqChange;
		req.open("GET", url, true);
		req.send(null);
	// branch for IE/Windows ActiveX version
	} else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
		if (req) {
			req.onreadystatechange = processReqChange;
			req.open("GET", url, true);
			req.send();
		}
	}
}

function processReqChange() {
	// only if req shows "complete"
	if (req.readyState == 4) {
		// only if "OK"
		if (req.status == 200) {
			// ...processing statements go here
			response  = req.responseXML.documentElement;
			if (response) {
				var result = new Array();
				for (i=0; i < response.getElementsByTagName('id').length; i++) {
					result[i] = {'id': response.getElementsByTagName('id')[i].firstChild.data, 'name': response.getElementsByTagName('name')[i].firstChild.data, 'obj_name': response.getElementsByTagName('obj_name')[i].firstChild.data};
				}
				loadResult('',result);
				unlockLocation();
			}
		} else {
			alert("There was a problem retrieving the XML data:\n" + req.statusText);
		}
	}
}

function loadResult(url, result) {
	if (result != '') {
		// Response mode
		for (i=0; i < result.length; i++) {
			document.getElementById(result[i].obj_name).options[document.getElementById(result[i].obj_name).options.length] = new Option(result[i].name,result[i].id);
		}
	} else if (url != '') {
		// Input mode
		return (loadXMLDoc(url));
	}
}

function unlockLocation() {
	if (this.field_loading) {
		this.field_loading.options[1]=null;
		this.field_loading.options[0].selected=true;
	}
	this.field_0.disabled=false;
	this.field_1.disabled=false;
	this.field_2.disabled=false;
}

function review_formPost(postUrl) {
 	
 	postData = new Object();
 	postData.postType			= 'ajax';
 	postData.submit				= 'submit';
 	postData.item_type			= $('#item_type').attr('value');
 	postData.item_id			= $('#item_id').attr('value');
 	postData.reviewer_name 		= $('#reviewer_name').attr('value');
 	postData.review_title 		= $('#review_title').attr('value');
 	postData.reviewer_email 	= $('#reviewer_email').attr('value');
 	postData.review 			= $('#review').attr('value');
 	postData.reviewer_location 	= $('#reviewer_location').attr('value');
 	postData.captchatext 		= $('#captchatext').attr('value');
 	postData.rating      		= $('#rating').attr('value');
 	
 	$("#TB_ajaxContent").html("<div id='TB_load'><img src='"+imgLoader.src+"' /></div>");
 	$("#TB_load").show();
 	
 	$.post(postUrl, postData, function(html) { $("#TB_ajaxContent").html(html); }, 'html');
 		
}
