// JavaScript Document

/* BEGIN openPopup */
function open_radio(url, name, width, height) {
 window.open(url,name,'toolbar=0,status=0,resizeable=0,width='+width+',height='+height);
}
/* END openPopup */

/* BEGIN externaLinks : to use with <a rel="external">...</a> */
function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "external")
     anchor.target = "_blank";
 }
}
/* END externalLinks */

/* BEGIN bookmark */
function bookmark(url,title){
  if ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4)) {
  	window.external.AddFavorite(url,title);
  } else { 
  	var message = 'Windows:\nPrima CRTL+D para adicionar aos favoritos.\n\n';
	message += 'Mac:\nCommand+D para adicionar aos favoritos.';
  	alert(message);
  }
}
/* END bookmark */

/* BEGIN go to top */
function stopScroll() {
    	clearTimeout(scrolldelay);
}

function pageScroll() {
    	window.scrollBy(0,-50); // horizontal and vertical scroll increments
		if (window.pageYOffset==0) {
				scrolldelay = setTimeout('pageScroll()',1); // scrolls every 100 milliseconds
				document.getElementById('go-to-top').blur();
				stopScroll();
		} else {
			if (document.documentElement.scrollTop==0 && navigator.appName=='Microsoft Internet Explorer') {
				scrolldelay = setTimeout('pageScroll()',1); // scrolls every 100 milliseconds
				document.getElementById('go-to-top').blur();
				stopScroll();
			}else {
		    	scrolldelay = setTimeout('pageScroll()',1); // scrolls every 100 milliseconds
			}
		}
}
/* END go to top */

/* BEGIN search functions */
function searchBlur(id) {
	var element = document.getElementById(id);
	if (element.value.strip() == '')
		element.value = 'Pesquisar...';
	else
		element.value = element.value.strip();
	element.blur();
}

function searchFocus(id) {
	var element = document.getElementById(id);
	if (element.value=='Pesquisar...')
		element.value = '';
}

function searchSubmit(type) {
	if ($('sType').value == '0') {
		loadAlertMessageBox('oops','Para pesquisar escolha o tipo de pesquisa.');
		return false;
	}
	else
	if ($('searchInputField').value.strip() == '' || $('searchInputField').value.strip()=='Pesquisar...') {
		loadAlertMessageBox('oops','Para pesquisar introduza um critério de pesquisa.');
		return false;
	}
	
	if (type==0)
		return true;
	else
		$('searchForm').submit();
}
/* END search functions */

/* BEGIN form functions */
function linkBlur(id) {
	var element = document.getElementById(id);
	if (element.value.indexOf('http://')==-1 && element.value!='')
		element.value = 'http://'+element.value;
}

function linkFocus(id) {
	var element = document.getElementById(id);
	if (element.value.indexOf('http://')!=-1)
		element.value = element.value.replace('http://','');
}

function timeBlur(id) {
	var element = document.getElementById(id);
	if (element.value=='')
		element.value = '00';
	if (element.value.length==1)
		element.value = '0'+element.value;
}

function timeFocus(id) {
	var element = document.getElementById(id);
	if (parseInt(element.value)==0)
		element.value = '';
}

function priceBlur(id) {
	var element = document.getElementById(id);
	if (element.value=='')
		element.value = '0';
}

function priceFocus(id) {
	var element = document.getElementById(id);
	if (parseInt(element.value)==0)
		element.value = '';
}

function priceDecimalBlur(id) {
	var element = document.getElementById(id);
	if (element.value=='')
		element.value = '00';
	if (element.value.length==1)
		element.value = '0'+element.value;
}

function priceDecimalFocus(id) {
	var element = document.getElementById(id);
	if (parseInt(element.value)==0)
		element.value = '';
}

function onlyNumbers(e) {
	var key;
	var keychar;
	
	if (window.event)
	   key = window.event.keyCode;
	else if (e)
	   key = e.which;
	else
	   return true;
	keychar = String.fromCharCode(key);
	
	// control keys
	if ((key==null) || (key==0) || (key==8) ||
		(key==9) || (key==13) || (key==27) )
	   return true;
	
	// numbers
	else if (("0123456789").indexOf(keychar) > -1)
	   return true;
	
	else
	   return false;
}

function notSpaces(e) {
	var key;
	var keychar;
	
	if (window.event)
	   key = window.event.keyCode;
	else if (e)
	   key = e.which;
	else
	   return true;
	keychar = String.fromCharCode(key);
	
	// control keys
	if ((key==null) || (key==0) || (key==8) ||
		(key==9) || (key==13) || (key==27) )
	   return true;
	
	// numbers
	else if (!((" ").indexOf(keychar) > -1))
	   return true;
	
	else
	   return false;
}

function textCharsLimit(textId,charsLimit,spanId) {
	var span = document.getElementById(spanId);
	var textarea = document.getElementById(textId);
	if (textarea.value.length > charsLimit) 
		textarea.value = textarea.value.substring(0, charsLimit);
	else
		span.innerHTML = charsLimit - textarea.value.length;
}

function inputBlur(elem) {
	elem.style.backgroundColor = "#FFF";
	elem.style.color = "#7B7979";
	elem.style.borderColor = "#CCC";
}

function inputFocus(elem) {
	elem.style.backgroundColor = "#E7FAB2";
	elem.style.color = "#709A00";
	elem.style.borderColor = "#AEEE00";
}

function inputTextBlur(id,text) {
	var element = document.getElementById(id);
	if (element.value.strip() == '')
		element.value = text;
	else
		element.value = element.value.strip();
	element.blur();
}

function inputTextFocus(id,text) {
	var element = document.getElementById(id);
	if (element.value==text)
		element.value = '';
}
/* END form functions */

/* BEGIN other functions */
function htmlspecialcharsToString(str) {
	while (str.indexOf('&amp;')!=-1)
		str = str.replace('&amp;','&');
	while (str.indexOf('&quot;')!=-1)
		str = str.replace('&quot;','"');
	while (str.indexOf('&#039;')!=-1)
		str = str.replace('&#039;',"'");
	while (str.indexOf('&lt;')!=-1)
		str = str.replace('&lt;','<');
	while (str.indexOf('&gt;')!=-1)
		str = str.replace('&gt;','>');
	return str;
}
/* END other functions */

