var dom = (document.getElementById) ? true : false;
var ns5 = ((navigator.userAgent.indexOf("Gecko")>-1) && dom) ? true: false;
var ie5 = ((navigator.userAgent.indexOf("MSIE")>-1) && dom) ? true : false;
var ns4 = (document.layers && !dom) ? true : false;
var ie4 = (document.all && !dom) ? true : false;
var ns6 = ns5;
var ns6_1= (ns5 && navigator.userAgent.indexOf("6.1") != -1) ? true : false;
var ns6_2= (ns5 && navigator.userAgent.indexOf("6.2") != -1) ? true : false;
var ie6 = ((navigator.userAgent.indexOf("MSIE")>-1) && dom) ? true : false;
var ie7 = ((navigator.userAgent.indexOf("MSIE 7.0")>-1) && dom) ? true : false;
var ie8 = ((navigator.userAgent.indexOf("MSIE 8.0")>-1) && dom) ? true : false;
var ie9 = ((navigator.userAgent.indexOf("MSIE 9.0")>-1) && dom) ? true : false;
var nodyn = (!ns5 && !ns4 && !ns6 && !ns6_1 && !ns6_2 && !ie4 && !ie5 && !ie6 && !ie7 && !ie8 && !ie9) ? true : false;
if (nodyn) { event = "nope" }
var tooltip;
var toolinfo;
var off_x = 8; var off_y = 8;
var bBlink = false;

function jsTrim(stringa) {
    while (stringa.substring(0,1) == ' ') {
        stringa = stringa.substring(1, stringa.length);
    }
    while (stringa.substring(stringa.length-1, stringa.length) == ' ') {
        stringa = stringa.substring(0,stringa.length-1);
    }
    return stringa;
}

function roundTo(value, decimalpositions) {
	var nDec = Math.pow(10,decimalpositions);
    var i = value * nDec;
    i = Math.round(i);
    return i / Math.pow(10,decimalpositions);
}

function showTip(event,TableText) {
var tip;

	if (ns4) {
		tip = '<table width="100%" cellspacing="0" cellpadding="1" border="0"><tr><td><table width="100%" cellspacing="0" cellpadding="3" border="0"><tr><td class="tdTip">' + TableText + '</td></tr></table></td></tr></table>';
		tooltip = document.divTip;
		tooltip.document.write(tip);
		tooltip.document.close();

		if ((event.pageX + off_x + tooltip.clip.width) > (window.innerWidth + window.pageXOffset)) {
			tooltip.left = event.pageX - tooltip.clip.width;
		} else { 
			tooltip.left = event.pageX + off_x; 
		}

		if ((event.pageY + off_y + tooltip.clip.height) > (window.innerHeight + window.pageYOffset)) {
			tooltip.top = event.pageY - tooltip.clip.height;
		} else { 
			tooltip.top = event.pageY + off_y; 
		}
		tooltip.visibility = "show";
	}

	if (ie4 || ie5 || ie6 || ie7 || ie8 || ie9) {

		event = window.event;
		tip = TableText;
		tooltip = document.all.divTip;
		tooltip.innerHTML = tip;

		if(ie7 || ie8 || ie9) {
			if ((event.x + off_x + tooltip.clientWidth) > (document.body.clientWidth)) {
				tooltip.style.pixelLeft = (event.x + document.body.scrollLeft) - tooltip.clientWidth;
			} else { 
				tooltip.style.pixelLeft = event.x + off_x + document.body.scrollLeft; 
			}
			if ((event.y + off_y + 2*tooltip.clientHeight) > (document.body.clientHeight)) {
				tooltip.style.pixelTop = (event.y + document.body.scrollTop) - tooltip.clientHeight;
			} else { 
				tooltip.style.pixelTop = event.y + off_y + document.body.scrollTop; 
			}
			if(ie7)
				tooltip.style.pixelTop += 3*tooltip.clientHeight;
			if(ie9) {
				if(document.documentElement.scrollTop!=0) {
					tooltip.style.pixelTop += document.documentElement.scrollTop;
				}
				else {
					tooltip.style.pixelTop += document.body.scrollTop;
				}
			}
		}
		else {
			if ((event.clientX + off_x + tooltip.style.pixelWidth) > (document.body.clientWidth)) {
				tooltip.style.pixelLeft = (event.clientX + document.body.scrollLeft) - tooltip.style.pixelWidth;
			} else { 
				tooltip.style.pixelLeft = event.clientX + off_x + document.body.scrollLeft; 
			}
			if ((event.clientY + off_y + tooltip.clientHeight) > (document.body.clientHeight)) {
				tooltip.style.pixelTop = (event.clientY + document.body.scrollTop) - tooltip.clientHeight;
			} else { 
				tooltip.style.pixelTop = event.clientY + off_y + document.body.scrollTop; 
			}
		}
		tooltip.style.visibility = "visible";
	}
	
	if (ns5 || ns6 || ns6_1 || ns6_2) {

		var sLeft, sTop;
		
		tip = TableText;
		tooltip = document.getElementById('divTip');
		tooltip.innerHTML = tip;

		if ((event.pageX + off_x + tooltip.offsetWidth) > (window.innerWidth + window.pageXOffset)) {
			sLeft = event.pageX - tooltip.offsetWidth;
		} else { 
			sLeft = event.pageX + off_x;
		}
		sLeft = sLeft+'px';
		tooltip.style.left = sLeft;

		if ((event.pageY + off_y + tooltip.offsetHeight) > (window.innerHeight + window.pageYOffset)) {
			sTop = event.pageY - tooltip.offsetHeight;
		} else { 
			sTop = event.pageY + off_y;
		}
		sTop = sTop+'px';
		tooltip.style.top = sTop;

		tooltip.style.visibility = "visible"; 
	}
}

function hideTip() {
	if(tooltip==null)
		return;
	if (ns4) { 
		tooltip.visibility = "hide" 
	}
	if (ie4 || ie5 || ie6 || ns5 || ns6 || ns6_1 || ns6_2 || ie7 || ie8 || ie9) {
		tooltip.style.visibility = "hidden"
	}
}

function showInfo(event,TableText) {
var info;

	if (ns4) {
		info = '<table width="100%" cellspacing="0" cellpadding="1" border="0"><tr><td><table width="100%" cellspacing="0" cellpadding="3" border="0"><tr><td class="tdTip">' + TableText + '</td></tr></table></td></tr></table>';
		toolinfo = document.divInfo;
		toolinfo.document.write(info);
		toolinfo.document.close();
		toolinfo.visibility = "show";
	}

	if (ie4 || ie5 || ie6) {
		event = window.event;
		info = '<div>' + TableText + '</div>';
		toolinfo = document.all.divInfo;
		toolinfo.innerHTML = info;
		toolinfo.style.visibility = "visible";
	}
	
	if (ns5 || ns6 || ns6_1 || ns6_2) {
		info = '<div>' + TableText + '</div>';
		toolinfo = document.getElementById('divInfo');
		toolinfo.innerHTML = info;
		toolinfo.style.visibility = "visible"; 
	}
}

function hideInfo() {
	if (ns4) { 
		toolinfo.visibility = "hide" 
	}
	if (ie4 || ie5 || ie6 || ns5 || ns6 || ns6_1 || ns6_2) {
		toolinfo.style.visibility = "hidden"
	}
}

function SetLang(LangID) {
	document.forms["frmSetLangID"].LangID.value = LangID;
	document.forms["frmSetLangID"].submit();
}

function LoginUser() {
	if(document.forms["frmLogin"].UserName.value=="") {
		document.forms["frmLogin"].UserName.focus()
		return;
	}
	if(document.forms["frmLogin"].Password.value=="") {
		document.forms["frmLogin"].Password.focus()
		return;
	}
	document.forms["frmLogin"].submit();
	return;
}

function CheckEnterToLogin(event) {
var key;
	if (document.all) {
		key=event.keyCode;
	} else {
        key=event.which;	
	}
	if(key==13) {
		return LoginUser();
	}
	return true;
}

function LogoutUser() {
	document.forms["frmLogout"].submit();
}

function SubmitSimpleSearch() {
var nComboValue;
	nComboValue = document.forms["frmSimpleSearch"].selBrand.value;
	if(nComboValue>0) {
		document.forms["frmSimpleSearch"].submit();
	}
}

function SubmitMainSearch(idType) {
	document.forms["frmMainSearch"].idType.value = idType;
	document.forms["frmMainSearch"].submit();
}

function GotoResultPage(sPageName, nPage) {
	document.forms["frmRepost"].action = sPageName+'?nPag='+nPage;
	document.forms["frmRepost"].submit();
}

function SetStyle(obj,bSet) {
	if(bSet) {
		obj.style.background='#DFEFEF';
	}
	else {
		obj.style.background='#FFFFFF';	
	}
}

//
// Scan all textboxes in forms and set the style
function SetTextBoxesStyle() {
	var objForms = document.forms;
	for (var i=0; i<objForms.length; i++) {
		var objFields = objForms[i].elements;
		for (var j=0; j<objFields.length; j++) {
			if(objFields[j].type=='text' || objFields[j].type=='password' || objFields[j].type=='textarea') {
				objFields[j].onfocus=function() {
					this.style.background='#EFEFEF';//'#E4E7F0';//'#CFE7E7';
				}
				objFields[j].onblur=function() {
					this.style.background='#FFFFFF';
				}
			}
/*
			if(objFields[j].tagName=='SELECT') {
				objFields[j].onfocus=function() {
					this.style.background='#FCFAE6';
					this.focus();
				}
				objFields[j].onblur=function() {
					this.style.background='#FFFFFF';
				}
			}
			
			if(objFields[j].type=='checkbox' || objFields[j].type=='radio') {
				objFields[j].onfocus=function() {
					return this.style.background='#FCFAE6';
				}
				objFields[j].onblur=function() {
					return this.style.background='#FFFFFF';
				}
			}
*/			
		}
	}
}

function isEmpty(strTxt) {
var re = /\s/g; //Match any white space including space, tab, form-feed, etc.
var str = strTxt;

	RegExp.multiline = true; // IE support
	str = str.replace(re, "");
	if(str.length == 0) {
		return true;
	}
	else {
		return false;
	}
}

function ValidField(FormField) {
	if(FormField==undefined || FormField==null || FormField=='' || FormField.length<=0)
		return false;
	return true;
}

function ValidateForm(nType) {
var sUserName = document.getElementById('UserName').value;
var sUserMail = document.getElementById('UserMail').value;
var sUserPhone = document.getElementById('UserPhone').value;
var bPrivacy = document.getElementById('chkPrivacy');
if(nType==2) {
	var sUserCity = document.getElementById('UserCity').value;
	var sUserProvince = document.getElementById('UserProvince').value;
	var sCarBrandModel = document.getElementById('CarBrandModel').value;
	var sCarVersion = document.getElementById('CarVersion').value;
	var sCarMileage = document.getElementById('CarMileage').value;
	var sCarEngine = document.getElementById('CarEngine').value;
}

	// Privacy
	if(!bPrivacy.checked)
		return -1;

	// Nome utente
	if(!ValidField(sUserName))
		return 1;
	if(sUserName.length<6)
		return 1;

	// Email
	if(!ValidField(sUserMail))
		return 2;
	if(sUserMail.length<6)
		return 2;
	nPos = sUserMail.indexOf('@',1);
	if(nPos<=0)
		return 2;
	nPos = sUserMail.indexOf('.',nPos+1);
	if(nPos<=0)
		return 2;

	if(nType==2) {
		// Cittą - provincia
		if(!ValidField(sUserCity))
			return 4;
		if(sUserCity.length<6)
			return 4;

		if(!ValidField(sUserProvince))
			return 5;
		if(sUserProvince.length<2)
			return 5;
	}

	// Telefono
	if(!ValidField(sUserPhone))
		return 3;
	if(sUserPhone.length<5)
		return 3;
	
	if(nType==2) {
		// Marca-modello
		if(!ValidField(sCarBrandModel))
			return 6;
		if(sCarBrandModel.length<6)
			return 6;
		
		// Versione-allestimento
		if(!ValidField(sCarVersion))
			return 7;
		if(sCarVersion.length<2)
			return 7;

		// Km
		if(!ValidField(sCarMileage))
			return 8;

		// Cilindrata
		if(!ValidField(sCarEngine))
			return 9;
		if(sCarEngine.length<2)
			return 9;
	}

	return 0;
}

function SetBlink() {
var BlinkText = document.getElementById('BlinkText');
	
	bBlink = !bBlink;
	if(BlinkText) {
		if(bBlink) {
			BlinkText.style.color = '#FF0000';
			/* 
			BlinkText.style.color = '#F8E890';
			BlinkText.style.textShadow = '1px 1px 0px #836D09, 2px 2px 0px #AC900C';
			if (ie4 || ie5 || ie6 || ie7 || ie8 || ie9) {			
				BlinkText.style.filter = 'Shadow(Color=#836D09, Direction=135, Strength=2)';
			}
			*/
			setTimeout("SetBlink()", 500);
		}
		else {
			BlinkText.style.color = '#FFFFFF';
			BlinkText.style.textShadow = 'none';
			if (ie4 || ie5 || ie6 || ie7 || ie8 || ie9) {			
				BlinkText.style.filter = '';
			}
			setTimeout("SetBlink()", 800);
		}
	}
}

