/* Funções de Cookie */
function setCookie(name, value, expires, path, domain, secure) {
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

function deleteCookie(name, path, domain) {
    if (getCookie(name)) {
        document.cookie = name + "=" +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

/* Função que troca o tamanho da fonte */
function setFontSize(act){

	var today = new Date();
	var zero_date = new Date(0,0,0);
	today.setTime(today.getTime() - zero_date.getTime());
	var cookie_expire_date = new Date(today.getTime() + (8 * 7 * 86400000));

	var	n = getCookie("fontsize");

	switch (act) {
    	case '-': n = (n <= 0) ? 0 : (--n); break
    	case '+': n = (n >= 4) ? 4 : (++n); break
    	case 'res': n = 0; break
    	case 'ini':	n = (!n) ? 0 : n; break
	}

	setCookie("fontsize", n, cookie_expire_date);

//	document.getElementById('pg').className='tam'+n;
	    
}

function nothing() {
	if (!document.getElementsByTagName) return false;
	var links = document.getElementsByTagName("a");
	for (var i=0; i < links.length; i++) {
		if (links[i].className.match("on") || links[i].className.match("nt")) {
			links[i].onclick = function() {
				window.status = "";
				return false;
			}
		}
	}
}
function nt() {
	return;
}
window.onload=function(){setFontSize('ini');nothing();}

function go(url) {
    window.location.href= url;
}

function openpopup(url) {
        window.open(url,"nova","width=400,height=300")
}

/* Funções de highlight de mouse */

function overSetup() {
	var img, sh, sn, sd
	for (var i = 0; (img = document.images[i]); i++) {
		if (img.getAttribute) {

			sn = img.getAttribute("src");
			sh = img.getAttribute("hsrc");
			sd = img.getAttribute("dsrc");

			if (sn != "" && sn != null) {
				img.n = new Image();
				img.n.src = img.src;

				if (sh != "" && sh != null) {
					img.h = new Image();
					img.h.src = sh;
					img.onmouseover = SwapOn
					img.onmouseout  = SwapOff
				}

				if (sd != "" && sd != null) {
					img.d = new Image();
					img.d.src = sd;
					img.onmousedown = SwapDown
				}
			}
		}
	}
}

function SwapOn() {
	this.src = this.h.src;
}

function SwapOff() {
	this.src  = this.n.src;
}

function SwapDown() {
	this.src  = this.d.src;
	this.temp = typeof(document.onmouseup) != 'undefined' && typeof(document.onmouseup) != 'unknown' ? document.onmouseup : "";
	SwapUp.img = this;
	document.onmouseup = SwapUp;
}

function SwapUp() {
	var ths = SwapUp.img;
	ths.src = ths.n.src;
	if (ths.temp) document.onmouseup = ths.temp;
}

/*
 *	Converte os "NAMES" dos campos em nomes padrão em português
*/
function transStr(str) {
    var strings = [
	['_a15788mn884', 'Seu nome'],
	['_043a77me293', 'Seu e-mail'],
	['_b935s4u4731', 'Assunto'],
	['_78o3321c71m', 'Sua empresa'],
	['_486s35e22m8', 'Mensagem']
	];
	
	for (var i = 0; i < strings.length; i ++) {
		if (str.indexOf(strings[i][0]) >= 0) {
			return strings[i][1];
		}
	}
}

Array.prototype.push = function(v) {
	this[this.length] = v
	return v
}

/*
 *	Validador de formulários
*/
function checkRequired(frm, fields) {

	var f = frm;
	var errors = [];
	var foco = [];
	var em = 0;

//	transStr("subject");
	
	for (i=0;i<f.length;i++){

		var fieldobj = f.elements[i];

		if(fields.indexOf(fieldobj.name)!=-1) {
			if(((fieldobj.type=="text"||fieldobj.type=="password"||fieldobj.type=="textarea"||fieldobj.type=="file")&&trim(fieldobj.value)=='')){
				errors.push(transStr(fieldobj.name));
				foco.push(fieldobj.name);
			}
			if(transStr(fieldobj.name) == transStr("_043a77me293") && fieldobj.value != ""){
				var re = new RegExp;
				re = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
				var arr = re.exec(fieldobj.value);

				if (arr == null) {
					foco.push(fieldobj.name);
					errors.push("O e-mail digitado não é válido.");

					em = (transStr(foco[0]) == transStr("_043a77me293")) ? 1 : 0;
				}
			}
			if(fieldobj.type=="select-one"){
				var txt_select = fieldobj.options[fieldobj.selectedIndex].text
				if(txt_select.indexOf("---")!=-1){
                    errors.push(transStr(fieldobj.name));
                    foco.push(fieldobj.name);
				}
			}
		}
		eval("f."+fieldobj.name+".style.border = '1px solid #c0c0c0'");
	}
		
	var s = ((errors.length - em) > 1) ? "s" : "";
	
	if((errors.length - em) == 0){
		var STD_ERROR_PREFIX = "";
	} else {
		var STD_ERROR_PREFIX = "Você deixou "+(errors.length - em)+" campo"+s+" incompleto"+s+".\n";
			STD_ERROR_PREFIX += "Por favor verifique e tente novamente.\n";
	}
	if (errors.length > 0) {
	    eval("f."+foco[0]+".focus(); f."+foco[0]+".select();");

		// Marca os compos errados e incompletos
		for (i=0; i<errors.length; i++){
	    	eval("f."+foco[i]+".style.border = '1px solid #dd0000'");
	    }
		alert(STD_ERROR_PREFIX + errors.join("\n"));
		return false;
	}

 return true;
}
function trim(strn) {
	return strn.replace(/^\s*(.*\S|.*)\s*$/, '$1');
}
