// +--------------------+
// | FUNÇÕES JAVASCRIPT |
// +--------------------+

// +-----------+
// | VALIDAÇÃO |
// +-----------+
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' deverá conter um endereço de e-mail.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' deverá conter um numero.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' deverá conter um numero entre '+min+' e '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' é obrigatório.\n'; }
  } if (errors) alert('Os seguintes erros ocorreram:\n'+errors);
  document.MM_returnValue = (errors == '');
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

// +---------------+
// | FIM VALIDAÇÃO |
// +---------------+

function Delete(target) {
  if(confirm("Confirma exclusão?")) location.href = target;
}

// +--------------------+
// | PREVIEW DE IMAGENS |
// +--------------------+

function Preview(URL) {
	timeout = 5; // Close window after __ number of seconds?
	windowprops = "left=0,top=0,width=0,height=0";
//	windowprops = "width=0, height=0, top=" + ((screen.height-200)/2) + ", left=" + ((screen.width-200)/2)
	text = "<html><head><title>Preview</title></head><script type='text/javascript' src='/tw_includes/js_site.js'></script><body bgcolor='white'";
	if (timeout != 0) text +=" onLoad=\"resizeWindow(document.images[0]);setTimeout('window.close()', " + timeout*1000 + ");\"";
	text += "><center><img src='../../img/up/" + URL + "' border='1'>";
	if (timeout != 0) text +="<br><font face='verdana, arial, helvetica' size='1'>Fechará sozinho<br>em " + timeout + " segundos.</font>";
	text += "<br><input type='button' onClick='javascript:window.close();' value='Fechar Janela'>" // style='font-size:11px'
	text += "</center></body></html>";
	preview = window.open("", "preview", windowprops);
	preview.document.open();
	preview.document.write(text);
	preview.document.close();
}

function resizeWindow(imageSource) {
  var width = (imageSource.width + 50);
  var height =  (imageSource.height + 150);
  self.resizeTo(width,height);
}

function BrowseImages(Campo) {
  var winl = (screen.width - 600) / 2;
  var wint = (screen.height - 300) / 2;
  window.open("../imagesDB/browseImages.asp?Campo="+Campo,"BrowseLinks","width=600,height=300,scrollbars=yes,resizable=yes,titlebar=0,left="+winl+",top="+wint);
}

function BrowseAlbum(Campo, pasta) {
  var winl = (screen.width - 600) / 2;
  var wint = (screen.height - 300) / 2;
  window.open("../imagesDB/browseImages.asp?Campo="+Campo+"&subpasta="+pasta,"BrowseLinks","width=600,height=300,scrollbars=yes,resizable=yes,titlebar=0,left="+winl+",top="+wint);
}

function SelectImage(Campo, FileName) {
  eval("opener.document.forms[0]." + Campo + ".value = FileName;")
  window.close();
}

// +------------------------+
// | FIM PREVIEW DE IMAGENS |
// +------------------------+

function URL(endereco) {
  location.href = endereco;
}

function Editar(Id) {
  location.href = "Editar.asp?Id=" + Id;
}

function Excluir(Id) {
  if(confirm("Confirma exclusão?")) location.href = "Excluir.asp?Id=" + Id;
}

var checkflag = "false";
function check(field) {
if (checkflag == "false") {
	for (i = 0; i < field.length; i++) {
		field[i].checked = true;}
	checkflag = "true";
	return "Desmarcar todos"; }
else {
	for (i = 0; i < field.length; i++) {
		field[i].checked = false; }
	checkflag = "false";
	return "Marcar todos"; }
}

function Confirma()
{
var agree=confirm("Deseja continuar?");
if (agree)
	return true ;
else
	return false ;
}

function Enquete(Id) {
  var winl = (screen.width - 400) / 2;
  var wint = (screen.height - 450) / 2;
  window.open("enquete.asp?Id="+Id,"Enquete","width=400,height=450,scrollbars=no,resizable=no,titlebar=0,left="+winl+",top="+wint);
}

function DiaDia(Id) {
  var winl = (screen.width - 400) / 2;
  var wint = (screen.height - 450) / 2;
  window.open("DiaDia/Default.asp?Id="+Id,"","width=400,height=450,scrollbars=yes,resizable=no,titlebar=0,left="+winl+",top="+wint);
}

function DiaDia2(Id) {
  var winl = (screen.width - 400) / 2;
  var wint = (screen.height - 450) / 2;
  window.open("DiaDia.asp?id="+Id,"","width=400,height=450,scrollbars=yes,resizable=no,titlebar=0,left="+winl+",top="+wint);
}

function Indique() {
  var winl = (screen.width - 330) / 2;
  var wint = (screen.height - 380) / 2;
  window.open("indique.asp","","width=330,height=380,scrollbars=no,resizable=no,titlebar=0,left="+winl+",top="+wint);
}

function copiar() {
	var rng = document.selection.createRange();
	rng.execCommand("copy");
	alert('Informativo copiado com sucesso!\nPressione Ctrl+V para colar.');
}
function selectAll(theField) {
	var tempval=eval("document."+theField)
	tempval.focus()
	tempval.select()
	copiar()
}

