// JavaScript Document funcoes comuns de javascrip vão aqui
function vdata( strValue ) {
  var objRegExp = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/

  if(!objRegExp.test(strValue))
    return false; 
  else{
    var strSeparator = strValue.substring(2,3) 
    var arrayDate = strValue.split(strSeparator); 

    var arrayLookup = { '01' : 31,'03' : 31, '04' : 30,'05' : 31,'06' : 30,'07' : 31,
                        '08' : 31,'09' : 30,'10' : 31,'11' : 30,'12' : 31}
    var intDay = parseInt(arrayDate[0]);

    if(arrayLookup[arrayDate[0]] != null) {
      if(intDay <= arrayLookup[arrayDate[0]] && intDay != 0)
        return true; 
    }

    var intYear = parseInt(arrayDate[2]);
    var intMonth = parseInt(arrayDate[1]);
    if( ((intYear % 4 == 0 && intDay <= 29) || (intYear % 4 != 0 && intDay <=28)) && intDay !=0)
      return true; 
  }
  return false; 
}

function checkemail(str){
var testresults;
var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
if (filter.test(str))
testresults=true;
else{
testresults=false;
}
return (testresults);
}

function naovazio( strValue ) {
   var strTemp = strValue;
   strTemp = trimAll(strTemp);
   if(strTemp.length > 0){
     return true;
   }
   return false;
}

function pviiClassNew(obj, new_style) {
    obj.className = new_style;
}

function favoritos(){
var bookmarkurl="http://www.ideias-frescas.pt"
var bookmarktitle="Ideias Frescas, design e multimédia, Lda."

if (document.all)
window.external.AddFavorite(bookmarkurl,bookmarktitle)
}

function trimAll( strValue ) {
 var objRegExp = /^(\s*)$/;

    if(objRegExp.test(strValue)) {
       strValue = strValue.replace(objRegExp, '');
       if( strValue.length == 0)
          return strValue;
    }

   objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
   if(objRegExp.test(strValue)) {
       strValue = strValue.replace(objRegExp, '$2');
    }
  return strValue;
}

function filterType(field, ext) { 
	if (field.value.indexOf('.' + ext) == -1) { 
		return false; 
	} 
	else {
		return true; 
	}
} 

function show_pop(mypage, myname, w, h, scroll) {
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable=no'
	win = window.open(mypage, myname, winprops)
	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

function show_pop2(mypage, myname, w, h, scroll) {
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable=yes'
	win = window.open(mypage, myname, winprops)
	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

function textCounter(field,cntfield,maxlimit) {
	if (field.value.length > maxlimit) {
		field.value = field.value.substring(0, maxlimit);
	}
	else {
		cntfield.value = maxlimit - field.value.length;		
	}
}

function isValid(parm,val) {
  if (parm == "") return false;
  for (i=0; i<parm.length; i++) {
    if (val.indexOf(parm.charAt(i),0) == -1) return false;
  }
  return true;
}

function  validateNumeric( strValue ) {
  var numb = '0123456789';
  return isValid(strValue,numb);
}

var timer_id;
function scroll_iframe(frm,inc,dir) {
  if (timer_id) clearTimeout(timer_id);
  if (window.frames[frm]) {
    if (dir == "v") window.frames[frm].scrollBy(0, inc);
    else window.frames[frm].scrollBy(inc, 0);
    timer_id = setTimeout("scroll_iframe('" + frm + "'," + inc + ",'" + dir + "')", 20);
  }
}

function stopScroll() { if (timer_id) clearTimeout(timer_id); }

function set_campo(obj,u) {
	if (naovazio(obj.value) == false) {
		if (u == "u") {
			obj.value = "name";
		}
		else {
			obj.value = "email";
		}
	}
}

function set_campo2(obj) {
	if (obj.value == "name") {
		obj.value = "";
	}
	if (obj.value == "email") {
		obj.value = "";
	}
}

function validar() {
	if ( (document.mailform.nome.value == "nome") || (document.mailform.email.value == "email")) {
		alert ("The input data seems to be invalid.\nPlease try again!");
		return false;
	}
	else {
		if (checkemail(document.mailform.email.value) == false) {
			alert ("The input data seems to be invalid.\nPlease try again!");
			return false;
		}
		else {
			return true;
		}
	}
}
