//test ie netscape
var ns = (navigator.appName.indexOf("Netscape") != -1);
var ie = (navigator.appName.indexOf("Microsoft Internet Explorer") != -1);
/*
var ns = (document.layers) ? 1 : 0;
var ie = (document.all) ? 1: 0;*/

var docObj, doc;

if (ie) docObj = "document.all.";
else if (ns) docObj = "document.";

if (ie) doc = document.all;
else if (ns) doc = document;

//expr regulares
var 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];
		
	return null
}

function changeListOrder(selectObj){
	var order = selectObj.options[selectObj.selectedIndex].value;
	var url = setQueryString(location.href, "order", null);
	
	location.href = url + 'order=' + order;
}

function requestQuerystring(theUrl, key){
	queryPos = theUrl.indexOf("?");
	keyPos = -1;
	
	if (queryPos != -1){
		queryArray = theUrl.substr(queryPos).split("&");
		
		for(i=0; i<queryArray.length; i++){
			keyPos = queryArray[i].indexOf(key + "=");
		
			if (keyPos != -1){
				return queryArray[i].substr(queryArray[i].indexOf("=")+1);
			}
		}
	}
	
	return ""
}

function setQueryString(theUrl, query, value){
	queryPos = theUrl.indexOf("?");
	queryArray = theUrl.substr(queryPos).split("&");
	queryString = null;
	queryList = "";
	queryFind = false;
	
	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;
				queryFind = true;
			}
			
			queryList += queryArray[i] + "&";
		}
		
		if  (!queryFind) queryList += query + "=" + value;
	}
	
	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 surveyVote(page, answer_id){
	location.href = page + '&x=vote&ans=' + answer_id;

}

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();
	}
	
	return true
}

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++){
		charA = string.charCodeAt(i);
			
		if (!((charA >= 48 && charA <= 57)  ||
			  (charA >= 65 && charA <= 90)  || 
			  (charA >= 97 && charA <= 122) || 
			  (charA == 95) || (charA == 150)))
			return false;
	}
	
	return true;
}

//funções do sistema em  MySql//

function GoToPageEx(strQueryString, strUrl, bReturn, strProp){
	if (strUrl == null) strUrl = location.href;
	
	if (strUrl.indexOf('?') != -1)
		strUrl = strUrl.substr(0, strUrl.indexOf('?'));

	if (bReturn == null)
		location.href = strUrl + '?' + strQueryString;
	else 
		if(bReturn == 'popup')
			 window.open(strUrl + '?' + strQueryString, '', strProp);	
		else return strUrl + '?' + strQueryString;
	
	return void(0);
}

function GoToPrevPage(nPrevPage, strSearch, strClientId){
	GoToPageEx('pg=' + nPrevPage + '&src=' + strSearch + '&cid=' + strClientId);
}

function GoToNextPage(nNextPage, strSearch, strClientId){
	GoToPageEx('pg=' + nNextPage + '&src=' + strSearch + '&cid=' + strClientId);
}

function CleanSearch(strClientId, strQString){
	GoToPageEx(strQString + '=' + strClientId);
}


//fim das funções do sistema em MySql//

function refreshMenu(){
	if (top.frames["admleftFrame"] == null) return;
	top.frames["admleftFrame"].location.href = "../adm.Articles.Menu.asp?rv=1";
}

function getObject(strObjectName){
	for (i=0; i<document.all.length; i++){
		if (document.all[i].id == strObjectName || document.all[i].name == strObjectName) return document.all[i];
	}
	
	return null;
}

function ResizeToFit(nHandler){
	var height = document.body.scrollHeight;
	var image_width = document.all.img1.width;	
	
	try{
		window.resizeTo(image_width+23, height+65);
		window.focus();
	}catch(x){
		location.reload();
	}
}

var arrToolBox = Array();
var strToolObject = "";

function inicializeToolFlash(strObjectName){
	strToolObject = strObjectName;
	
	if (ie) document.attachEvent("onreadystatechange", inicializeToolFlashFn);
	else if (ns) window.addEventListener("load", inicializeToolFlashFn, true);
	
}

function inicializeToolFlashFn(){
	var i, oFlash, oHidden
	var strJs;
	
	for (i=0; i<document.all.length; i++){
		if (document.all[i].id == "toolBarFlash"){
			oFlash = document.all[i];

			oHiddenName = getFlashHiddenValue(oFlash, "toolBarHiddenName");
			oHiddenJs = getFlashHiddenValue(oFlash, "toolBarHiddenJs")//oFlash.parentNode.parentNode.all.toolBarHiddenJs;
			
			if (oHiddenName != null && oHiddenJs != null){
				oFlash.SetVariable("strToolName", oHiddenName.value);
				oFlash.SetVariable("strToolJs", oHiddenName.value);
				oFlash.play();
			}
		}
	}
	
	return true
}

function getFlashHiddenValue(oFlash, strName){
	var i, oParent
	
	if (ie){
		return eval("oFlash.parentNode.parentNode.all." + strName);
	}else if (ns){
		oParent = oFlash.parentNode.parentNode;
		
		for (i=0; i<oParent.childNodes.length; i++){
			if (oParent.childNodes[i].id == strName) return oParent.childNodes[i];
		}
		
	}
	
	return null
}

function setLayerInMiddle(strLayerName, strHorizontal, strVertical){
	var oLayer = document.getElementById(strLayerName);
	
	if (oLayer == null) return false;
	
	var nWindowHeight = document.body.offsetHeight;
	var nWindowWidth = document.body.offsetWidth;
	var nLayerHeight = oLayer.clientHeight;
	var nLayerWidth = oLayer.clientWidth;
	var nLayerTop = 0;
	var nLayerLeft = 0;
	
	if (strHorizontal == "left") nLayerLeft = 0;
	else if (strHorizontal == "center") nLayerLeft = nWindowWidth/2 - nLayerWidth/2;
	else if (strHorizontal == "right") nLayerLeft = nWindowWidth - nLayerWidth;
	
	if (strVertical == "top") nLayerTop = 0;
	else if (strVertical == "middle") nLayerTop = nWindowHeight/2 - nLayerHeight/2;
	else if (strVertical == "bottom") nLayerTop = nWindowHeight - nLayerHeight;
	
	//alert(oLayer.clientHeight +" "+ oLayer.clientWidth)
	oLayer.style.position = "absolute";
	oLayer.style.visibility = "visible";
	
	if (ie){
		oLayer.style.left = nLayerLeft;	
		oLayer.style.top = nLayerTop;
	}else if (ns){
		oLayer.style.left = 0;	
		oLayer.style.top = 0;
	}
	
	return true
}

function OpenRedirectPage(page_path, link_id){
	var url = page_path + "?id=" + link_id;
	window.open(url, '', '');
}

function SearchPreviousPage(nAbsPage){
	var urlWithoutPage = GetCurrentUrl(Array("pg"), 
									   Array(String(nAbsPage-1)) );
	location.href = urlWithoutPage;
}

function SearchNextPage(nAbsPage){
	var urlWithoutPage = GetCurrentUrl(Array("pg"), 
									   Array(String(nAbsPage+1)) );
	
	location.href = urlWithoutPage;
}



function GetCurrentUrl(keys, values){ 
	var strUrl = location.href.replace(location.search, "");
	var strSearch = location.search;
	var strNewSearch = location.search;
	var re = /[?|&](\w+)=(\w+)/g;
	var found = Array();
	var flag = false;
	
    while ((arr = re.exec(strSearch)) != null)
		found.push(arr)
	
	for (i=0; i<keys.length; i++){
		for (j=0; j<found.length; j++){
			if (found[j][1] == keys[i]){
				strNewSearch = strNewSearch.replace(found[j][1] + "=" + found[j][2], found[j][1] + "=" + values[i]);
				flag = true
			}
		}
			
		if (!flag) strNewSearch += "&" + keys[i] + "=" + values[i];
		
		flag = false;
	}
	
	return strUrl + strNewSearch;
}

function sendStandaloneSearch(){
	var oForm = document.getElementById("frmStdForm");
	var oTextBox = document.getElementById("txtStdSearch");
	//var oSelectBox = document.getElementById("txtStdOrder");
	//var oSelectBox2 = document.getElementById("txtStdLinks");
	var oButton = document.getElementById("btnStdButton");
	
	if (oForm == null || oTextBox == null || oButton == null/* || oSelectBox2 == null*/) return false;
	
	oTextBox.disabled = true;
	oButton.disabled = true;
	
	var action = oForm.action;
	
	action += "?srcword=" + oTextBox.value //+ 
			 // "&srclink=" + oSelectBox2.options[oSelectBox2.selectedIndex].value;
			//  alert(oSelectBox2.options[oSelectBox2.selectedIndex].value)
	oForm.action = action;
	
	return true	
}

function showImageTypeNavigator(image_id){
	window.open('art_includes/artigos.images.navigator.asp?id=' + image_id, 'sitemais_image_navigator', 'scrollbars=no, status=yes');
}


