//test ie netscape
var ns = (document.layers) ? 1 : 0;
var ie = (document.all) ? 1: 0;

if (ie) docObj = "document.all.";
else if (ns) docObj = "document.";

if (ie) doc = document.all;
else if (ns) doc = document;

//expr regulares
email_regxp = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;

function ActiveLinkInText(url){
	if (url.indexOf('http://') == -1) url = 'http://'+url;
	
	window.open(url, '', '');
}

function SetBlur(){
	this.blur();	
}

function goToPage(page_name, query_string, target){
	if (target == '_blanc')
		window.open(page_name +"?"+ query_string, "", "");
	else
		location.href=page_name +"?"+ query_string;
}


function keyPerformSubmit(query_string, page_name){
		if (document.all){
			if (event.keyCode == 13)
				submitSearchForm(query_string, page_name);
		}
}

function submitSearchForm(query_string, page_name){
	var formObj = document.all.mForm
	
	formObj.action = page_name + "?" + query_string;
	formObj.target = "_self";
	formObj.method = "POST";
	formObj.submit();
}

function findObject(object_name){
	var docElements = eval(docObj.substr(0, docObj.length-1));

	for (i=0; i<docElements.length; i++)
		if (docElements[i].name == object_name) return docElements[i];
}

function changeListOrder(selectObj){
	var order = selectObj.options[selectObj.selectedIndex].value;
	var url = setQueryString(location.href, "order", null);
	
	location.href = url + 'order=' + order;
}

function setQueryString(theUrl, query, value){
	queryPos = theUrl.indexOf("?");
	queryArray = theUrl.substr(queryPos).split("&");
	queryString = null;
	queryList = "";
	
	if (value == null){
		for(i=0; i<queryArray.length; i++)
			if (queryArray[i].indexOf(query + "=") == -1) queryList += queryArray[i] + "&";
	}else{
		for(i=0; i<queryArray.length; i++){
			if (queryArray[i].indexOf(query + "=") != -1) queryArray[i] = query + "=" + value;
			
			queryList += queryArray[i] + "&";
		}
	}
	
	return theUrl.substr(0, theUrl.indexOf("?")) + queryList;
}

function alertFuncArgs(args){
	var x = '';

	for (i=0; i<args.length; i++)
		x += args[i] + '\n';
		
	alert(x);
}

function ToodleSchedule(toodle, page){
	if (toodle == 1) toodle = 0
	else             toodle = 1

	location.href = page + '&sdt=' + toodle;
}

function DownloadFile(page){
	location.href = page + '&x=dwn';
}

function surveyVote(page, radio_name, survey_id){
	var vote = -1

	for (i=0; i<doc.length; i++){
		if (doc[i].name == 'ans'+survey_id){
			if (doc[i].checked == true)	vote = doc[i].value;
		}
	}
	
	if (vote == -1){
		alert("Faça seu voto");
	}else{
		location.href = page + '&x=vote&ans=' + vote;
	}
}

function surveyResults(page){
	location.href = page;
}

function GoBack(){
	history.back();
}

function sendFeedBackForm(form_name, array, query_string){
	var formObj = eval('document.all.' + form_name);
	var object, object_value, error_string = "";
	
	if (array != null){
		for (i=0; i<array.length; i++){
			for (j=0; j<document.all.length; j++){
				field_name = array[i][0];
				field_mode = array[i][1];
			
				//localizando objeto
				if (document.all[j].name == field_name){
					object = document.all[j];

					//validar apenas campos de texto
					if (object.selectedIndex == null){
					
						//validando campo vazio
						
						if (object.value.length == 0)
							error_string += "O campo "+ object.name + " deve ser preenchido\n";
						else if (field_mode == "email"){
						
							//validando o e-mail se necessário
							if (!email_regxp.test(object.value)) 
								error_string += "O campo "+ object.name + " deve conter um e-mail válido\n";
						}
					}
				}
			}
		}
	}

	if (error_string.length > 0){
		alert("O formulário não foi preenchido corretamente:\n" + error_string);
		return false
	}else if (formObj != null){
		formObj.action = 'artigos.painel.asp?' + query_string + '&x=sx';
		formObj.submit();
	}
}

function sendToFriendValidate(formObj){//\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
	var sender_name = formObj.txtNome.value;
	var sender_mail = formObj.txtEmail.value;
	var friend_name = formObj.txtAmigo.value;
	var friend_mail = formObj.txtEmailAmigo.value;
	var message     = formObj.txtComentarios.value;
	var mail_expr   = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
	var error = '';
	
	if (sender_name.length <= 3) error += 'O campo nome deve ser preenchido\n';

	if (sender_mail.length <= 3) error += 'O campo e-mail deve ser preenchido\n';
	else if (!mail_expr.test(sender_mail)) error += 'O campo e-mail deve conter um e-mail válido\n';
	
	if (friend_name.length <= 3) error += 'O campo nome do amigo deve ser preenchido\n';
	
	if (friend_mail.length <= 3) error += 'O campo e-mail do amigo deve ser preenchido\n';
	else if (!mail_expr.test(friend_mail)) error += 'O campo e-mail do amigo deve conter um e-mail válido\n';
	
	if (error.length > 0){
		alert('Há erros no preenchimento do formulário:\n' + error);
		return false;
	}
	
	return true;
}

function openImage(page_path, type, artId){
	var url = page_path + "?" + type + "=" + artId;
	window.open(url, 'imgWnd', 'width=200, height=200, left=100, top=100');
}

function setMaxChar(textObj, maxChar, fieldLabel){
	if (textObj.value.length > maxChar){
		alert("O campo " + fieldLabel + " não pode ter mais que " + maxChar + " caracteres.");
		textObj.value = textObj.value.substr(0, maxChar);
	}
}

function KeySendForm(js){
	if (document.all){
		if (event.keyCode == 13)
			eval(js);
	}
}

function charTest(string){
	for (i=0; i<string.length; i++){
		char = string.charCodeAt(i);
			
		if (!((char >= 48 && char <= 57)  ||
			  (char >= 65 && char <= 90)  || 
			  (char >= 97 && char <= 122) || 
			  (char == 95) || (char == 150)))
			return false;
	}
	
	return true;
}





