function hover(rowobj,classitem,action)
{
	if(action==1)rowobj.className=classitem;
	else rowobj.className=classitem+'hover';
}
function ShowItem (itemID) {
  var x = document.getElementById(itemID);
  if (x)
    x.style.visibility = "visible";
  return true;
}
function HideItem (itemID) { 
  var x = document.getElementById(itemID);
  if (x)
     x.style.visibility = "hidden";
  return true;
}
function Export(val)
{
		document.getElementById('search').value='Pesquisar';
		document.getElementById('exportoption').value=val;
		document.frmlist.submit();
}
function IsNum(val)

 {

   if (val.value.length != 0) 

    {

		

      for (i = 0; i < val.value.length; i++) 

	  {

		  var ch = val.value.charAt(i);

		  if ((ch >= "0" && ch <= "9") || (ch == ".") ||  (ch==')') || (ch=='(')) 

		  {

			continue;

		  } 

		  else

		  { 

		  val.value='';

		  alert("Por favor introduza valores numéricos apenas.");

		  val.focus();

		  return false;

		 }

	 }

	 return true;

   }

 

}
function toround(original)
{
	return Math.round(original*100)/100;
}
function hideDiv() { 
if (document.getElementById) { // DOM3 = IE5, NS6 
document.getElementById('hideshow').style.visibility = 'hidden'; 
} 
else { 
if (document.layers) { // Netscape 4 
document.hideshow.visibility = 'hidden'; 
} 
else { // IE 4 
document.all.hideshow.style.visibility = 'hidden'; 
} 
} 
}

function showDiv() { 
if (document.getElementById) { // DOM3 = IE5, NS6 
document.getElementById('hideshow').style.visibility = 'visible'; 
} 
else { 
if (document.layers) { // Netscape 4 
document.hideshow.visibility = 'visible'; 
} 
else { // IE 4 
document.all.hideshow.style.visibility = 'visible'; 
} 
} 
} 


function GotoPage(Pagename) 
{
	location.href=Pagename;
}
//=============================================================================================================================
//THIS FUNCTION POSTS FORM FOR NEXT PAGE ....
//THIS FUNCTION POSTS FORM FOR NEXT PAGE ....
//=============================================================================================================================
function NextPage()
{
   
  document.frmlist.pageNumber.value = eval(document.frmlist.pageNumber.value) + 1;
 
   document.frmlist.submit();
}
//===========================================================================================================================
//=============================================================================================================================
//THIS FUNCTION POSTS FORM FOR PREVIOUS PAGE ....
//THIS FUNCTION POSTS FORM FOR PREVIOUS PAGE ....
//=============================================================================================================================
function PrePage()
{
   
	document.frmlist.pageNumber.value = eval(document.frmlist.pageNumber.value) - 1  ;
	document.frmlist.submit();
}

//=============================================================================================================================
//=============================================================================================================================
//THIS FUNCTION POSTS FORM FOR SPECIFIED PAGE NUMBER....
//THIS FUNCTION POSTS FORM FOR SPECIFIED PAGE NUMBER....
//=============================================================================================================================
function NextPageLink(val)
{
   
	document.frmlist.pageNumber.value = eval(val)-1  ;
	document.frmlist.submit();
}
//=============================================================================================================================
//========function SubmitForPage used to submit form ==========================================================================
//=============================================================================================================================
function SubmitForPage(FormName, ControlName, ControlValue, FormAction)
{
	ControlName.value = ControlValue;
	FormName.action = FormAction;
	FormName.submit();
}
//=============================================================================================================================
//THIS FUNCTION POSTS FORM FOR ORDER BY....
//THIS FUNCTION POSTS FORM FOR ORDER BY....
//=============================================================================================================================
function OrderPage(OrderBy)
{
	
	document.frmlist.pageNumber.value = eval(document.frmlist.pageNumber.value);
	
	if(document.frmlist.orderby.value.search(OrderBy)<0)
		document.frmlist.orderby.value = OrderBy;
	else
	{
		if(document.frmlist.orderby.value.search(' desc')<0)
			document.frmlist.orderby.value = OrderBy +' desc';
		else
			document.frmlist.orderby.value = OrderBy;
	}
	document.frmlist.submit();
}



function OrderFunction(orderstr,type)
{
   var temp=Array();
   text=document.frmlist.orderby.value;
   if(text!='')
   {
	   
	   i=text.indexOf(orderstr)
	   if(i+1 > 0)
	   {
		   if(type=='asc')text=text.replace(orderstr+' desc',orderstr+' asc');
		   else text=text.replace(orderstr+' asc',orderstr+' desc');
	   		document.frmlist.orderby.value=text;
	   }
	   else
	   {
		 temp=text.split(',');
		   if(temp.length >= 3)
		   {
			   temp=temp.reverse();
			   temp.pop();
			   temp=temp.reverse();
		   }
			temp[temp.length]=orderstr+' '+type;
	  	 document.frmlist.orderby.value=temp.join(',');	   
	   }
   }
   else 
   {   
   	temp[temp.length]=orderstr+' '+type;
  	 document.frmlist.orderby.value=temp.join(',');
   }
   document.frmlist.submit();
}


function setvalue(tar,con,val)
{
	con.value=val;
	tar.submit();
}
function fillvalue(val)
{
	document.getElementById('ins_budget_number1').value=val;
	document.getElementById('ins_budget_number2').value=val;
	document.getElementById('ins_budget_number3').value=val;
		document.getElementById('ins_budget_number4').value=val;
	document.getElementById('ins_budget_number5').value=val;
	document.getElementById('ins_budget_number6').value=val;
}

function calculatenetvalue()
{
	var netalu=document.getElementById('aluminum_value').value;
	var netass=document.getElementById('accessories_value').value;
	var netraw=document.getElementById('raw_aluminum_value').value;
	var total_value=0;
	var total_net_value=0;
	if(document.getElementById('aluminum_value').value!='')
	{
		if(document.getElementById('aluminum_discount').value!='')
		{
			if(document.getElementById('aluminum_discount').value*1 > document.getElementById('max_alu').value){alert('Por favor introduza um desconto para Alumínio inferior ao máximo permitido ('+document.getElementById('max_alu').value+'%)');document.getElementById('aluminum_discount').value='';}
			else
			netalu=(document.getElementById('aluminum_value').value - (document.getElementById('aluminum_value').value*document.getElementById('aluminum_discount').value)/100);
		}
		if(document.getElementById('aluminum_discount1').value!='')
		{
		if(document.getElementById('aluminum_discount1').value*1 > document.getElementById('max_alu1').value){alert('Por favor introduza um segundo desconto para Alumínio inferior ao máximo permitido ('+document.getElementById('max_alu1').value+'%)');document.getElementById('aluminum_discount1').value=''}
		else
			netalu=(netalu - (netalu*document.getElementById('aluminum_discount1').value)/100);
		}
		total_value=total_value+(document.getElementById('aluminum_value').value)*1;
		total_net_value=total_net_value+netalu*1;	
	}
	document.getElementById('aluminum_net_value').value=toround(netalu);
	if(document.getElementById('accessories_value').value!='')
	{
		if(document.getElementById('accessories_discount').value!='')
		{
		if(document.getElementById('accessories_discount').value*1 > document.getElementById('max_ass').value){alert('Por favor introduza um desconto para Acessórios inferior ao máximo permitido ('+document.getElementById('max_ass').value+'%)');document.getElementById('accessories_discount').value=''}
		else
			netass=(document.getElementById('accessories_value').value - (document.getElementById('accessories_value').value*document.getElementById('accessories_discount').value)/100);
		}
		if(document.getElementById('accessories_discount1').value!='')
		{
			if(document.getElementById('accessories_discount1').value*1> document.getElementById('max_ass1').value){alert('Por favor introduza um segundo desconto para Acessórios inferior ao máximo permitido ('+document.getElementById('max_ass1').value+'%)');document.getElementById('accessories_discount1').value=''}
			else
			netass=(netass - (netass*document.getElementById('accessories_discount1').value)/100);
		}
		total_value=total_value+(document.getElementById('accessories_value').value)*1;
		total_net_value=total_net_value+netass*1;
	}
	document.getElementById('accessories_net_value').value=toround(netass);
	
	if(document.getElementById('raw_aluminum_value').value!='')
	{
		if(document.getElementById('raw_aluminum_discount').value!='')
		{
		if(document.getElementById('raw_aluminum_discount').value*1 > document.getElementById('max_raw').value){alert('Por favor introduza um desconto para Chapas inferior ao máximo permitido ('+document.getElementById('max_raw').value+'%)');document.getElementById('raw_aluminum_discount').value=''}
		else
			netraw=(document.getElementById('raw_aluminum_value').value - (document.getElementById('raw_aluminum_value').value*document.getElementById('raw_aluminum_discount').value)/100);
		}
		if(document.getElementById('raw_aluminum_discount1').value!='')
		{
			if(document.getElementById('raw_aluminum_discount1').value*1 > document.getElementById('max_raw1').value){alert('Por favor introduza um segundo desconto para Chapas inferior ao máximo permitido ('+document.getElementById('max_raw1').value+'%)');document.getElementById('raw_aluminum_discount1').value=''}
		else
			netraw=(netraw - (netraw*document.getElementById('raw_aluminum_discount1').value)/100);
		}
		total_value=total_value+(document.getElementById('raw_aluminum_value').value)*1;
		total_net_value=total_net_value+netraw*1;	
	}
	document.getElementById('raw_aluminum_net_value').value=toround(netraw);
	document.getElementById('total_value').value=toround(total_value);
	document.getElementById('total_net_value').value=toround(total_net_value);
} 
function CopyData(flag,nid,pid)
{
	if(flag==0)
	{
		document.getElementById('ref_name'+nid).value=document.getElementById('ref_name'+pid).value;
		document.getElementById('ref_suppliers'+nid).value=document.getElementById('ref_suppliers'+pid).value;
		document.getElementById('ref_systems'+nid).value=document.getElementById('ref_systems'+pid).value;
		document.getElementById('ref_budget_value'+nid).value=document.getElementById('ref_budget_value'+pid).value;
		document.getElementById('ref_proposed_value'+nid).value=document.getElementById('ref_proposed_value'+pid).value;


	}
	else
	{
		document.getElementById('ins_name'+nid).value=document.getElementById('ins_name'+pid).value;
		document.getElementById('ins_suppliers'+nid).value=document.getElementById('ins_suppliers'+pid).value;
		document.getElementById('ins_systems'+nid).value=document.getElementById('ins_systems'+pid).value;
		document.getElementById('ins_budget_value'+nid).value=document.getElementById('ins_budget_value'+pid).value;
		document.getElementById('ins_proposed_value'+nid).value=document.getElementById('ins_proposed_value'+pid).value;

	}
	
}
function UploadValidImageFile()
{
   var error = 0;
   var message =''; 
   val=document.getElementById('photo');
 if(val.value!="")
	 { 
       //check the extension of the uploaded file
       Ext=val.value.substring(val.value.lastIndexOf('.')+1);
	   Ext=Ext.toLowerCase();
	   
		 if(Ext!='jpg' && Ext!='gif' && Ext!='png' && Ext!='jpeg' && Ext!='pjpeg')
		  {
		    error=1;
			message=message + "Please upload only jpg,gif,png,jpeg,pjpeg format file.\n";
		    
		  }
	 }
     
	 if(error==1)
       {
	     alert(message);
		 return false;
	   }
	else
	  {
	     return true;
	   } 
 }
