var IE4 = false;
var NS6 = false;
if (document.all){IE4 = true;}else if (document.getElementById){NS6 = true;}

function gotFocus(elem, text){
	if(elem.value == text) elem.value = ''
}
function lostFocus(elem, text){
	if(elem.value == '') elem.value = text
}
/** -- PESQUISA -- **/
	
function modelos(frm) {
	frm.field4.disabled = true;
	while (frm.field4.length > 1) {
		frm.field4.remove(frm.field4.options.length-1);
	}
	
	if (frm.field3.value != "") {
		frm.field4.disabled = false;
		frm.target='busca';
		frm.action="feed.php?id=modelos&idMarca=" + frm.field3.value;
		frm.submit();
	}
}
	
/** submit CONTACTO: Enviar Pedido de Contacto **/
function submitContacto(obj){
	var msg = "Por favor, preencha os seguintes campos:\n\n";
	var erro = false;
	var first;
	if(trim(obj.nomefrom.value).length == 0){
		msg += "--> Nome\n";
		erro = true;
		obj.nomefrom.className += " boxErro";
		if(!first) first = obj.nomefrom;
	}
	if(trim(obj.emailfrom.value).length == 0){
		msg += "--> Email\n";
		erro = true;
		obj.emailfrom.className += " boxErro";
		if(!first) first = obj.emailfrom;
	}
	if(trim(obj.telef.value).length == 0){
		msg += "--> Contacto telefónico\n";
		erro = true;
		obj.telef.className += " boxErro";
		if(!first) first = obj.telef;
	}
	if(trim(obj.assunto.value).length == 0){
		msg += "--> Assunto\n";
		erro = true;
		obj.assunto.className += " boxErro";
		if(!first) first = obj.assunto;
	}
	if(trim(obj.mensagem.value).length == 0){
		msg += "--> Mensagem\n";
		erro = true;
		obj.mensagem.className += " boxErro";
		if(!first) first = obj.mensagem;
	}
	if(erro){
		alert(msg);
		first.focus();
		return false;
	}
	obj.target='';
	obj.action = "mods.php?name=Contactos_CC&op=2";
	obj.submit();
}


/** submit SUGESTOES: Enviar sugestões **/
function submitSugestao(obj){
	var msg = "Por favor, preencha os seguintes campos:\n\n";
	var erro = false;
	var first;
	if(trim(obj.nomefrom.value).length == 0){
		msg += "--> Nome\n";
		erro = true;
		obj.nomefrom.className += " boxErro";
		if(!first) first = obj.nomefrom;
	}
	if(trim(obj.emailfrom.value).length == 0){
		msg += "--> Email\n";
		erro = true;
		obj.emailfrom.className += " boxErro";
		if(!first) first = obj.emailfrom;
	}
	if(trim(obj.mensagem.value).length == 0){
		msg += "--> Mensagem\n";
		erro = true;
		obj.mensagem.className += " boxErro";
		if(!first) first = obj.mensagem;
	}
	if(erro){
		alert(msg);
		first.focus();
		return false;
	}
	obj.target='';
	obj.action = "mods.php?name=Sugestoes_CC&op=2";
	obj.submit();
}
	
	
	
	
	function clearClass(f, classe){
		for(i=0; i<f.elements.length;i++){
			f.elements[i].className = classe;
		}
	}



/** -- MISCELLANEOUS -- **/

	/** restringe a insercao de valores apenas do tipo numero [0..9] **/
	function tstSoNum(e) {
		if(e.ctrlKey){
			return false;
		}
		if(e.shiftKey){
			return false;
		}
		if(NS6){
			var tc = e.charCode;
			if(tc==0 || (tc >= 48 && tc <= 57)){
				return true;
			}
		}
		else{
			var tc = e.keyCode;
			if(tc==0 || tc==13 || (tc >= 48 && tc <= 57)){
				return true;
			}
		}
		return false
	}
	
	function cleanForm(f){
		for(i=0; i<f.elements.length;i++){
			if (f.elements[i].type == "checkbox") f.elements[i].checked = false;
			if (f.elements[i].type == "text") f.elements[i].value = "";
			if (f.elements[i].type == "select-one") f.elements[i].selectedIndex = 0;
			if (f.elements[i].type == "radio") f.elements[i].checked = false;
			if (f.elements[i].type == "textarea"){ f.elements[i].value = ""};
			if (f.elements[i].type == "hidden"){}
		}
	}

	/** Remove espaços no inicio e no fim da string. Remove espaços consecutivos e substitui por um espaço. **/
	function trim(inputString) {
	   if (typeof inputString != "string") { return inputString; }
	   var retValue = inputString;
	   var ch = retValue.substring(0, 1);
	   while (ch == " ") { // Check for spaces at the beginning of the string
	      retValue = retValue.substring(1, retValue.length);
	      ch = retValue.substring(0, 1);
	   }
	   ch = retValue.substring(retValue.length-1, retValue.length);
	   while (ch == " ") { // Check for spaces at the end of the string
	      retValue = retValue.substring(0, retValue.length-1);
	      ch = retValue.substring(retValue.length-1, retValue.length);
	   }
	   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
	      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
	   }
	   return retValue;
	}
	
	/** Valida um endereço de email **/
	function email_validate(email){
		em_val = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/
		if(em_val.test(email)) return true
		else return false
	}
