var sImgSrcPLoading = "/app/img/web/icons/icon_process_loading.gif";
var sImgSrcPDone = "/app/img/web/icons/icon_process_done.gif";
var sImgSrcPError = "/app/img/web/icons/icon_process_error.gif";
var sImgSrcPNone = "/app/img/web/icons/icon_process_none.gif";

function ATmaxlen(text,maxlong){
	// onKeyDown = "return ATmaxlen(this,maxlong);"
	var tecla, int_value, out_value;
	if (text.value.length > maxlong) {
		in_value = text.value;
		out_value = in_value.substring(0,maxlong);
		text.value = out_value;
		return false;
	}
	return true;
}

function searchbox_submit(field) {
	
	if ($('#' + field).val().length < 3)  { 
		return false;
	} else { 
		return true; 
	}
	
}

function changeDisplay (sName, sForce) {
 	
 	var oName = document.getElementById (sName);
	if (oName== undefined) return false;
	
	if (sForce == undefined) {
		if ((oName.style.display == "block" && oName.style.visibility == "visible") || (oName.style.display == "" && oName.style.visibility == "")) {
			sForce = "hide";
		}
		else {
			sForce = "show";
		}
	}
	
	if (sForce == "hide") {
		oName.style.display = "none";
		oName.style.visibility = "hidden";
	}
	else if (sForce == "show") {
		oName.style.display = "block";
		oName.style.visibility = "visible";
	}
}


function modifyMinDate (sDateText, sObjName) {
			
	var aDate = sDateText.split('-');
	var dDate = new Date (aDate[2], parseInt (aDate[1], 10) - 1, parseInt(aDate[0],10));
	dDate.setHours (23, 59, 59, 999);
	
	var sDateOld = document.getElementById (sObjName).value;
	var aDateOld = sDateOld.split('-');
	var dDateOld = new Date (aDateOld[2], parseInt (aDateOld[1], 10) - 1, aDateOld[0])
	
	if (dDate > dDateOld) {
		aDate[0] = dDate.getDate();
		aDate[1] = dDate.getMonth() + 1;
		aDate[2] = dDate.getFullYear();
		if (aDate[0] < 10) 
			aDate[0] = "0" + aDate[0];
		if (aDate[1] < 10) 
			aDate[1] = "0" + aDate[1];
		$('#' + sObjName).datepicker('setDate', dDate);
		document.getElementById(sObjName).value = aDate[0] + "-" + aDate[1] + "-" + aDate[2];
	}
	
	$('#' + sObjName).datepicker('change', { minDate: dDate});
}

// Calendar disbaled functionality
function changeCalendarEnabled (iSelectedCalendar) {
	if (iSelectedCalendar == 1) {
		$('#selCalendar').attr ('disabled', false);
		$('#startdate').attr ('disabled', 'disabled');
		$('#enddate').attr ('disabled', 'disabled');
	}
	else if (iSelectedCalendar == 2) {
		$('#selCalendar').attr ('disabled', 'disabled');
		$('#startdate').attr ('disabled', false);
		$('#enddate').attr ('disabled', false);
	}
}

/**
 * Checks service of the subservice
 * @param {Object} sev_id
 */
function checkServiceCheck (sev_id) {
	if ($('#service' + sev_id).is(':checked') == false) $('#service' + sev_id).attr('checked', true);
	if ($("input[type='checkbox'][class='sev_" + sev_id +"']:checked").length == 0) { $('#service' + sev_id).attr('checked', false); } ;
	
}
/**
 * Unchecks subservices of the service
 * @param {Object} sev_id
 */
function checkSubservice (sev_id) {
	$('.sev_' + sev_id).attr('checked', $('#service' + sev_id).is(':checked'));
}


function getLocs (data, divId) {
	$.ajax({
		type: "GET",
		url: "/geo/load_populations_by_province/",
		data: data,
		success: function(msg) {
			if (msg == 0) {
				alert ("ERROR");
			}
			else {
				$('#' + divId).html (msg);
			}
		}
	});
}

function changeImage (prd, mv) {
	
	src = $('#img_' + prd).attr('src');
	cont = 0;
	for (i in aImages[prd]) {
		if (aImages[prd][i] == src) {
			cont = i;
			break;
		}
	}
	cont = parseInt(cont) + parseInt(mv);
	if (cont < 0) cont = (aImages[prd].length) -1 ; 
	if (cont > (aImages[prd].length - 1)) cont = 0;
	$('#img_' + prd).attr('src', aImages[prd][cont]);
}

/**
 * Checks first click on a form field
 * @param {Object} oFormField
 * @param {Object} bOut
 */
function formFirstClick (oFormField, bOut) {
	switch (oFormField.type) {
		case 'text':
			if (!bOut && !aFormRequest[oFormField.name]['clicked']) {
				oFormField.value = "";
				$('#'+oFormField.name).removeClass('off');
			}
			else {
				if (oFormField.value.length == 0) {
					oFormField.value = aFormRequest[oFormField.name]['value'];
					aFormRequest[oFormField.name]['clicked'] = false;
					$('#'+oFormField.name).addClass('off');
				}
				else {
					aFormRequest[oFormField.name]['clicked'] = true;
					$('#'+oFormField.name).removeClass('off');
				}
			}
		break;
		case 'textarea':
			if (!bOut && !aFormRequest[oFormField.name]['clicked']) {
				oFormField.value = "";
				$('#'+oFormField.name).removeClass('off');
			}
			else {
				if (oFormField.value.length == 0) {
					oFormField.value = aFormRequest[oFormField.name]['value'];
					aFormRequest[oFormField.name]['clicked'] = false;
					$('#'+oFormField.name).addClass('off');
				}
				else {
					aFormRequest[oFormField.name]['clicked'] = true;
					$('#'+oFormField.name).removeClass('off');
				}
			}
		break;
	}
}

function valueFirstClick (sFormFieldId, value){
	$('#' + sFormFieldId).val(value);
	$('#' + sFormFieldId).removeClass('off');
}


// GEO SEARCH FUNCTIONS
function geoSearch () {
	
	// Checks form
	if (aFormRequest['geoSearch_address']['clicked'] == false && aFormRequest['geoSearch_cp']['clicked'] == false) {
	}
	else {
		changeDisplay ("geoSearch_dl", "hide");
		changeDisplay ("geoSearch_didyoumean", "hide");
		changeDisplay ("geoSearch_dl_nofound", "hide");
		
		if (aFormRequest['geoSearch_address']['clicked'] == false) $('#geoSearch_address').val("");
		if (aFormRequest['geoSearch_cp']['clicked'] == false) $('#geoSearch_cp').val("");
		
		$.ajax({
			type: "POST",
			url: "/index/get_geo_search_data",
			data: $("#formGeoSearch").serialize (),
			success: function (msg) {
				parseGeo(msg);
			}
		});
	}
}

function exeForm(fName,e)
{
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;

	if (keycode == 13)
	{
		$('#geoSearch_button').focus();
		$('#geoSearch_button').click();
		return false;
	}
	else
		return true;
}

function parseGeo (value) {
	
	aTmp = value.split ("\n");
	switch (aTmp[0]) {
		case 'MULTIPLE':
			output =  "";
			for (i in aTmp) {
				if (i == 0) continue;
				aTmpValue = aTmp[i].split('|');
				aTmpCoords = aTmpValue[1].split(',');
				output += "<li><a href=\"/listing/geo_search?search=1&lon=" + aTmpCoords[0] + "&lat=" + aTmpCoords[1] + "&geosearch=" + aTmpValue[0] + "&address=" + aTmpValue[4] + "&cp=" + aTmpValue[3] + "\" title=\"" + aTmpValue[0] + "\">" + aTmpValue[0] + "</a></li>";
			}
			changeDisplay ("geoSearch_didyoumean", "show");
			$('#geoSearch_ul').html (output);
		break;
		case 'DIRECT':
			aTmpValue = aTmp[1].split('|');
			aTmpCoords = aTmpValue[1].split(',');
			document.location.href = "/listing/geo_search?search=1&lon=" + aTmpCoords[0] + "&lat=" + aTmpCoords[1] + "&geosearch=" + aTmpValue[0] + "&address=" + escape(aTmpValue[2]) + "&cp=" + aTmpValue[3];
		break;
		case 'ERROR':
			changeDisplay ("geoSearch_dl", "show");
		break;
	}
}

function ban_click(ban_child) {
	var sUrl = $('#banner_link').attr('href');
	
	$.ajax({
		type: "POST",
		url: "/banner/click?ban_child=" + ban_child,
		success: function(msg) {
			if (msg == 'ERROR') {
			}
			else {
				window.open(sUrl ,'_blank');
			}
		}
	});
	return false;
}


function order (value) {
	
	regexp = new RegExp (/[&|\?]ord=(.*)/);
	match = regexp.exec (url);
	
	if (match != null) {
		newvalue = url.replace (match[1], escape (value));
	
	}
	else {
		if (url.indexOf('?') != -1){	
			newvalue = url + "&ord=" + escape (value);
		} 
		else {
			newvalue = url + "?ord=" + escape (value);
		}
	}
	
	window.document.location = newvalue;
}