// JavaScript Document
function myLogin(myForm){
		if(myForm.user.value.length > 0 && myForm.password.value.length > 0){
				myForm.submit();
		}
		else{
			messgageBox("Please fill both fields");
		}
}

function submitTableCreation(table1Cols,table2Cols){

	strCheck = new Array((table1Cols + table2Cols)*3);
	var i, count = 0;
	
	if(table1Cols > 0){
		for(i = 1; i <= table1Cols; i++){
			strCheck[count] = "1_label" + i + "";
			strCheck[count+1] = '';
			strCheck[count+2] = 'R';
			count = count + 3;
		}
	}
	
	var elems1 = count / 3;

	if(table2Cols > 0){
		for(j = 1; j <= table2Cols; j++){
			strCheck[count] = "2_label" + j + "";
			strCheck[count+1] = '';
			strCheck[count+2] = 'R';
			count = count + 3;
		}
	}
	
	var elems2 = (count / 3) - elems1;

	MM_validateFormTables(strCheck,elems1,elems2);
	
	var res = document.MM_returnValue;

	if(res)
		return true;
	else
		return false;
}


function open_w(nome_ficheiro, nome_janela, w, h){
	window.open(nome_ficheiro, nome_janela, "width="+ w +", height="+ h +", scrollbars=no, resizable=no, top=0, left=0");
}

function closeWindow(){
	window.close();
}

function myConfirm(form_name){
	window.open("confirm_delete.php?form_name="+form_name, null, "width=320, height=125 resizable=no, menubar=no, status=no, titlebar=no, toolbar=no, top=50, left=50");
}

function messgageBox(message){
	window.open("message_box.php?message="+message, null, "width=300, height=125 resizable=no, menubar=no, status=no, titlebar=no, toolbar=no, top=50, left=50");
}

function isEmpty( variable ){
	if(variable.length == 0 ){
		return true;
	}
	return false;
}

function checkMandatoryFields(valuesArray, myForm){
	for(i = 0; i < valuesArray.length; i++){
		if(isEmpty( valuesArray[i].value ) ){
			messgageBox("All fields with <b>red</b> font are mandatory");
			return;
		}
	}
	if(myForm != null){
		myForm.submit();
	}
	return true;
}
function checkDates(firstDate, startHour, startMinute, secondDate, endHour, endMinute){
 var day,month,year;
 var date1,date2;
 day=myParseInt(firstDate.substr(8,2));
 month=myParseInt(firstDate.substr(5,2))-1;
 year= parseInt(firstDate.substr(0,4));
 startHour = myParseInt(startHour);
 startMinute = parseInt(startMinute);
 
 date1 = new Date(year, month, day, startHour, startMinute); 

 
 day=myParseInt(secondDate.substr(8,2));
 month=myParseInt(secondDate.substr(5,2))-1;
 year=parseInt(secondDate.substr(0,4));
 endHour = myParseInt(endHour);
 endMinute = myParseInt(endMinute);

 date2 = new Date(year, month, day, endHour, endMinute); 
 
 if(date1>date2){
  	return false;
 }
 return true;
}

function myParseInt(number){
	if(parseInt(number) == 0){
		return parseInt(number.substr(1,2));
	}
	return parseInt(number);
}

function checkPassword(password, confirm_password){}

function hideDisplay(){
	if(document.all.menu.style.display == "none"){
		document.all.control_panel.style.display = "none";
		document.all.menu.style.display = "inline";
	}
	else{
		document.all.control_panel.style.display = "inline";
		document.all.menu.style.display = "none";
	}
}

function open_w(nome_ficheiro, nome_janela, w, h){
		window.open(nome_ficheiro, nome_janela, "width="+ w +", height="+ h +", scrollbars=yes, resizable=no, top=0, left=0");
}

function verifica_img(myform){
	
  <!--var r, re, texto; -->
  var texto;
  texto=myform.file.value;
   if(texto.length!=0){
		re = ".jpg";
		r = texto.search(re);
		if(r>0){
			return true;
		}else{
			alert("The picture must be a .jpg");	
			return false;
		}
 	}
}
function check_date(ano, mes, dia){
	if((ano%4)==0){
		if(mes==2){
			if(dia>29){
				return 1;
			}else{
				return 0;
			}
		}else if((mes==4)||(mes==6)||(mes==9)||(mes==11)){
			if(dia>30){
				return 1;
			}else{
				return 0;
			}
		}else{
			return 0;
		}
	}else{
		if(mes==2){
			if(dia>28){
				return 1;
			}else{
				return 0;
			}
		}else if((mes==4)||(mes==6)||(mes==9)||(mes==11)){
			if(dia>30){
				return 1;
			}else{
				return 0;
			}
		}else{
			return 0;
		}	
	}
}

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_validateFormColumns() { //v4.0
  
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateFormColumns.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+' must contain an e-mail address.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\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+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } 
  
  var number = document.getElementById("numberTable").value;

  if(number == 1){
	if(document.getElementById("colNumber1").value <= 0)
		errors += '- '+ "Number of Columns for table 1 " +' has to be bigger than zero.\n';
  }
  if(number == 2){
	if(document.getElementById("colNumber1").value <= 0)
		errors += '- '+ "Number of Columns for table 1 " +' has to be bigger than zero.\n'; 
	if(document.getElementById("colNumber2").value <= 0)
		errors += '- '+ "Number of Columns for table 2 " +' has to be bigger than zero.\n';
  }
  
  if (errors) alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}

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+' must contain an e-mail address.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\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+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } if (errors) alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}

function MM_validateFormTables(elems,elemsNumber1,elemsNumber2){ //v4.0

  var i,p,q,nm,test,num,min,max,errors='';
  var args = elems;
  var count = 0;
  var argValues = new Array(Math.ceil(args.length/3));

  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+' must contain an e-mail address.\n';
      		}
			if (!isNaN(val)) 
				errors+='- '+nm+' must not contain just numbers.\n';

		}else if (test.charAt(0) == 'R') 
			errors += '- '+nm+' is required.\n'; 
	}
	argValues[count] = val;
	count++;
  }

  if(repeatedValues(argValues, 0, elemsNumber1 - 1))
  		errors += '- its not possible to have equal names on the same table.\n';

  if(repeatedValues(argValues, elemsNumber1, argValues.length - 1))
 		errors += '- its not possible to have equal names on the same table.\n';

  if (errors) 
  	alert('The following error(s) occurred:\n'+errors);
	
  document.MM_returnValue = (errors == '');
}

function repeatedValues(elems, init, end){

	for(var i = init; i <= end - 1; i++){
		for(var j = i + 1; j <= end; j++){
			if(elems[i] != "" && elems[j] != ""){
				if(elems[i] == elems[j])
					return true;
			}
		}
	}
	
	return false;
}

function confirmDelete(myform,id){
	if(confirm("Delete selected item(s)?")){
		myform.dummy.value = "submit";
		myform.xId.value = id;
		myform.submit();
	}else{
		myform.dummy.value = "";
	}
}

function confirmDelete2(myform,idElem,idTable,id){
	if(confirm("Delete selected item(s)?")){
		myform.dummy.value = "Delete";
		myform.xId.value = id;
		myform.xIdElem.value = idElem;
		myform.xTable.value = idTable;
		myform.submit();			
	}else{
		myform.dummy.value = "";
	}
}


function confirmOrder(myform){	
		myform.dummy.value = "nosubmit";
		myform.submit();				
}
function confirmTransfer(myform){
		myform.dummy.value = "transfer";
		myform.submit();	
}
function confirmGerar(myform){
		myform.dummy.value = "Gerar";
		myform.submit();	
}