//--- Form checkers ---------------------------------
function checkfrm_search(frmname) {

	var messg = "";

	

	if ( (document.forms[frmname].q.value=='') || (document.forms[frmname].q.value=='keyword(s)') )

		messg += "Please enter your keyword(s) to perform a search\n";



	if (messg=='') {

		return true;

	}

	else {

		alert(messg);

		return false;

	}

}


function valida_formulario_contato(){
	var styleIncompleto = document.getElementById('incompleto');
	if (
		( (document.forms['contato'].nome.value=='') || (document.forms['contato'].nome.value=='seu nome') ) ||
		( (document.forms['contato'].email.value=='') || (document.forms['contato'].email.value=='seu e-mail') ) ||
		( (document.forms['contato'].mensagem.value=='') ||	(document.forms['contato'].mensagem.value=='sua mensagem') )
		)
	{
		styleIncompleto.style.display = "block";
		return false;
		
	}else {

		document.forms['contato'].action = "index.php";
		document.forms['contato'].submit();
		return true;
	}
}

function valida_formulario_recebido(){
	var styleIncompleto = document.getElementById('incompleto');
	var styleRecebido = document.getElementById('recebido');
	if (
		( (document.forms['contato'].nome.value=='') || (document.forms['contato'].nome.value=='seu nome') ) ||
		( (document.forms['contato'].email.value=='') || (document.forms['contato'].email.value=='seu e-mail') ) ||
		( (document.forms['contato'].mensagem.value=='') ||	(document.forms['contato'].mensagem.value=='sua mensagem') )
		)
	{
		styleIncompleto.style.visibility = "visible";
		styleIncompleto.style.height = "16px";
		styleRecebido.style.visibility = "hidden";
		styleRecebido.style.height = "0px";
		return false;
		
	}else {

		document.forms['contato'].action = "contato.php";
		document.forms['contato'].submit();
		return true;
	}
}

function checkfrm_contact() {

	var messg = "";

	var messg2 = "";



	if ((document.forms['contact'].name.value=='') || (document.forms['contact'].name.value=='seu nome'))

		messg += "seu nome\n";

	//if ((document.forms['contact'].contactnum.value=='') || (document.forms['contact'].contactnum.value=='your contact number'))

		//messg += "your contact number\n";

	if ((document.forms['contact'].message.value=='') || (document.forms['contact'].message.value=='your message'))

		messg += "your message\n";



	if (!validEmail(document.forms['contact'].email.value))

		messg2 += "\nPlease provide a valid E-mail address (eg. name@example.com)";



	if ((messg=='') && (messg2=='')) {

		return true;

	}

	else {

		if (messg == '')

			alert(messg2);

		else {

			messg = "You have left the following required fields blank or\n" + "incomplete, please correct them before continuing:\n\n" + messg + messg2;

			alert(messg);

		}

		return false;

	}

}



function checkfrm_addcomment() {

	var messg = "";

	var messg2 = "";



	if ((document.forms['addcomment'].name.value=='') || (document.forms['addcomment'].name.value=='your name/alias'))

		messg += "your name/alias \n";

	if (document.forms['addcomment'].commentbody.value=='')

		messg += "your comment \n";



	//if (!validEmail(document.forms['addcomment'].email.value))

	//	messg2 += "\nPlease provide a valid E-mail address (eg. name@example.com)";



	if ((messg=='') && (messg2=='')) {

		return true;

	}

	else {

		if (messg == '')

			alert(messg2);

		else {

			messg = "You have left the following required fields blank or\n" + "incomplete, please correct them before continuing:\n\n" + messg + messg2;

			alert(messg);

		}

		return false;

	}

}

//--- show/hide ----------------------------------



function showhide(div, aviso) {
	
	var styleObj = document.getElementById(div);
	

	if (styleObj.style.visibility == "visible"){

		elem = styleObj.childNodes[styleObj.childNodes.length-1];
       //remove o elemento
        elem.removeNode(true);
		styleObj.style.visibility = "hidden";
		styleObj.style.height = "0px";

	}

	else{
			frase = document.createTextNode(aviso+styleObj.childNodes.length);
			styleObj.appendChild(frase);
			styleObj.style.visibility = "visible";
			styleObj.style.height = "16px";
		}

}



function hide(div) {

	var styleObj = document.getElementById(div);

	styleObj.style.visibility = "hidden";

}

// copyright 1999 Idocs, Inc. http://www.idocs.com

// Distribute this script freely but keep this notice in place

function numbersonly(myfield, e, dec) {

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 and punctuation

if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27) || (key==45) || (key==46) || (key==120) || (key==45) || (key==40) || (key==41) || (key==32) || (key==43))

   return true;



// numbers

else if ((("0123456789").indexOf(keychar) > -1))

   return true;



// decimal point jump

else if (dec && (keychar == ".")) {

   myfield.form.elements[dec].focus();

   return false;

}

else return false;

}



function validEmail(email) {

	invalidChars = " /:,;";

	if (email == "") {

		return false;

	}

	for (i=0; i<invalidChars.length; i++) {

		badChar = invalidChars.charAt(i);

		if (email.indexOf(badChar,0) != -1) {

			return false;

		}

	}

	atPos = email.indexOf("@",1);

	if (atPos == -1) {

		return false;

	}

	if (email.indexOf("@",atPos+1) != -1) {

		return false;

	}

	periodPos = email.indexOf(".",atPos);

	if (periodPos == -1) {

		return false;

	}

	if (periodPos+3 > email.length)	{

		return false;

	}

	return true;

}



function replace(string,text,by) {

// Replaces text with by in string

    var strLength = string.length, txtLength = text.length;

    if ((strLength == 0) || (txtLength == 0)) return string;



    var i = string.indexOf(text);

    if ((!i) && (text != string.substring(0,txtLength))) return string;

    if (i == -1) return string;



    var newstr = string.substring(0,i) + by;



    if (i+txtLength < strLength)

        newstr += replace(string.substring(i+txtLength,strLength),text,by);



    return newstr;

}



function confirmClear(f) {

	var agree =	confirm("Are you sure you want to clear the form?");

	if (agree) {

		eval ("document.forms['"+f+"'].reset()");

		return true;

	}

	else return false;

}
