///////////////////////////////////////
//////////////// DATE /////////////////
///////////////////////////////////////

//global vars

var _sep="/";


//DATE;

var dat = new Date();

var lm = dat.getMonth()+1;
if(lm<10)
	lm = '0'+lm;

var ly = dat.getFullYear();

var ld = dat.getDate();
if(ld<10)
	ld = '0'+ld;

var today = ld+_sep+lm+_sep+ly;

//GENERAL FUNCTIONS

function strZero( n , c){var s=""+n;var ng=false;if( s.substr(0,1)=='-') { ng=true;c--;s=s.substr(1)}while(s.length<c)s='0'+s;s=s.substr(0,c);if(ng)s='-'+s;return s}

function isctrl(k){ return k==46 || k==8 || ( k>=35&&k<=40&&k!=39) || k==9 || k==13}

function isdts(k){ return k==190||k==189||k== 55||k==45||k==47} // separador ./-

function isDigit(k){ return k >=48 && k <=57 }

 

function d_kp(e)
{
	if(!e&&window.event)e=window.event;
	if(e){_k=e.keyCode}
		if( e.which ) _k=e.which;
	var r=false;
	if( isctrl(_k) || isDigit(_k) )r=true;
	if( isdts(_k) ) r=true;
	if(!r)
	{
		e.cancelBubble=true;
	}
	return r;
}

function d_ku(e)
{
	if(!e&&window.event)e=window.event;
	if(e){_k=e.keyCode}
		if( e.which ) _k=e.which;
	var co=null;
	if ( e.srcElement ){co=e.srcElement}
	else if ( e.target ){co=e.target}
	var _max = -1;
	if ( co && co.getAttribute("maxlength")!=null) _max = parseInt( co.getAttribute("maxlength") );
	if( _max >-1 && _i.value.length==_max && _k >=48 && _k <=57 ) 
	{
		if ( co && co.getAttribute("nextfield") != null )
		{
			var nf = document.getElementById( co.getAttribute("nextfield"));
			if(nf)nf.focus();
		}
	}
	var _v=_i.value;
	if( _v.length==1 && '.-/'.indexOf(_v)>-1 ) 
	{
		_v= today;//strZero(ld,2)+_sep+strZero(lm,2)+_sep+strZero(ly,4);
		_i.value=_v;
		_i.select();
		try{if(co ){document.getElementById( "a"+co.id.split("_")[2] ).tabIndex =-1}}catch(e){}
	}
	else if( _i.value.length == 5 && (_k!=46 &&_k!=8 ) )
	{
		var c4=_i.value.substr(3,1);
		var c5=_i.value.substr(4,1);
		if( c5=='-'||c5=='.'||c5=='/'){_v=_v.substr(0,3)+'0'+c4 }
		try{if(co ){document.getElementById( "a"+co.id.split("_")[2] ).tabIndex =-1}}catch(e){}
		_v=_v+_sep+strZero(ly,4);
		_i.value=_v;
		if(_i.createTextRange){
			var t=_i.createTextRange();
			t.moveStart("character",6);
			t.select();
		}else if(_i.setSelectionRange){
			_i.setSelectionRange(6,_i.value.length);
		}
	}
	else if( _i.value.length == 2 && (_k!=46 &&_k!=8 ) )
	{
		try{if(co ){document.getElementById( "a"+co.id.split("_")[2] ).tabIndex =-1}}catch(e){}
		var c0=_i.value.substr(0,1);
		var c1=_i.value.substr(1,1);
		if( c1=='-'||c1=='.'||c1=='/'){_v='0'+c0 }
		_v=_v+_sep+strZero(lm,2)+_sep+strZero(ly,4);
		
		_i.value=_v;
		if(_i.createTextRange){
			var t=_i.createTextRange();
			t.moveStart("character",3);
			t.select();
		}else if(_i.setSelectionRange){
			_i.setSelectionRange(3,_i.value.length);
		}
	}
}

function d_in( o ){_i=o;_i.vla=_i.value }

function d_out(e)
{
	var toret=true;
	if(!e&&window.event)e=window.event;
	var co=null;
	if ( e.srcElement ){co=e.srcElement}
	else if ( e.target ){co=e.target}
	val = co.value; 
	if( val.length > 0 )
	{
		var t = val.split(_sep);
		if(!t||t.length<3) 
		{
			toret=false;
		}
		else
		{
			if(t[0].substr(0,1)=='0') t[0]=t[0].substr(1,1);
			if(t[1].substr(0,1)=='0') t[1]=t[1].substr(1,1);
			var dia = parseInt(t[0]);
			var mes = parseInt(t[1])-1;
			var ano = parseInt(t[2]);
			var d = new Date( ano , mes , dia );
			if ( isNaN(dia) || isNaN(mes) || isNaN(ano) || !(d.getFullYear()==ano&&d.getMonth()==mes&&dia==d.getDate()))
			{
				toret=false;
			}
			else{ lm = d.getMonth()+1;ly = d.getFullYear();ld = d.getDay()}
		}
		
		if(!toret)
		{
			co.value='';
			if( co.vla ){ co.value=co.vla;co.select();co.focus()}else co.focus(); 
		}
	}	
}

///////////////////////////////////////
//////////////// DATE /////////////////
///////////////////////////////////////



function cleanDataFields()
{
	if(document.getElementById("UserInformation1_txtNumeroAndar") != null)
		document.getElementById("UserInformation1_txtNumeroAndar").value = "";
	if(document.getElementById("UserInformation1_txtApelido") != null)
		document.getElementById("UserInformation1_txtApelido").value = "";
	if(document.getElementById("UserInformation1_txtBI") != null)
		document.getElementById("UserInformation1_txtBI").value = "";	
	if(document.getElementById("UserInformation1_txtCartaConducao") != null)
		document.getElementById("UserInformation1_txtCartaConducao").value = "";
	if(document.getElementById("UserInformation1_txtConfPassword") != null)
		document.getElementById("UserInformation1_txtConfPassword").value = "";
	if(document.getElementById("UserInformation1_txtCp3") != null)
		document.getElementById("UserInformation1_txtCp3").value = "";
	if(document.getElementById("UserInformation1_txtCp4") != null)
		document.getElementById("UserInformation1_txtCp4").value = "";
	if(document.getElementById("UserInformation1_txtDataNascimento") != null)
		document.getElementById("UserInformation1_txtDataNascimento").value = "";
	if(document.getElementById("UserInformation1_txtUsername") != null)
		document.getElementById("UserInformation1_txtUsername").value = "";
	if(document.getElementById("UserInformation1_txtFax") != null)
		document.getElementById("UserInformation1_txtFax").value = "";
	if(document.getElementById("UserInformation1_txtLocalidade") != null)
		document.getElementById("UserInformation1_txtLocalidade").value = "";
	if(document.getElementById("UserInformation1_txtVia") != null)
		document.getElementById("UserInformation1_txtVia").value = "";
	if(document.getElementById("UserInformation1_txtNacionalidade") != null)
		document.getElementById("UserInformation1_txtNacionalidade").value = "";
	if(document.getElementById("UserInformation1_txtNIF") != null)
		document.getElementById("UserInformation1_txtNIF").value = "";
	if(document.getElementById("UserInformation1_txtNome") != null)
		document.getElementById("UserInformation1_txtNome").value = "";
	if(document.getElementById("UserInformation1_txtPassword") != null)
		document.getElementById("UserInformation1_txtPassword").value = "";
	if(document.getElementById("UserInformation1_txtTelefone") != null)
		document.getElementById("UserInformation1_txtTelefone").value = "";
	if(document.getElementById("UserInformation1_txtTelemovel") != null)
		document.getElementById("UserInformation1_txtTelemovel").value = "";
	if(document.getElementById("UserInformation1_ddlVia") != null)
		document.getElementById("UserInformation1_ddlVia").value = "";
	if(document.getElementById("UserInformation1_rblSexo_0") != null)
		document.getElementById("UserInformation1_rblSexo_0").checked = "";
	if(document.getElementById("UserInformation1_rblSexo_1") != null)
		document.getElementById("UserInformation1_rblSexo_1").checked = "";
	if(document.getElementById("UserInformation1_chkPrivacidade") != null)
		document.getElementById("UserInformation1_chkPrivacidade").checked = false;
}

function numbersOnly(myfield, e, dec)
{
	var key;
	var keychar;

	if (window.event)
		key = window.event.keyCode;
	else if (e)
		key = e.which;
	else
		return true;
	keychar = String.fromCharCode(key);

	// control keys
	if ((key==null) || (key==0) || (key==8) ||
		(key==9) || (key==13) || (key==27) )
	return true;

	// numbers
	else if ((("0123456789").indexOf(keychar) > -1))
	return true;
	
	else if (((keychar == ".")||(keychar == ",")) && dec && (dec==true))
	{
		return true;
	}
	else
		return false;
}


  function numbersOnly_Paste(objEvent, dec) {
	
     var strPasteData = window.clipboardData.getData("Text"); 

     for(i=0;i<strPasteData.length;i++)
     {
		if((strPasteData.charAt(i) == '.'
			|| strPasteData.charAt(i) ==',')
			&& dec==true)
			return true;
		else if(isNaN(strPasteData.charAt(i)))
			return false;
     }  
}

function numbersOnly_dragDrop(dec) {
	
	var strDragDrop = window.event.dataTransfer.getData("TEXT")

     for(i=0;i<strDragDrop.length;i++)
     {
		if((strDragDrop.charAt(i) == '.'
			|| strDragDrop.charAt(i) ==',')
			&& dec==true)
			return true;
		else if(isNaN(strDragDrop.charAt(i)))
			return false;
     }   
}

function showLayerUserInformation1(layerName)
{
	if (document.getElementById) // Netscape 6 and IE 5+
	{
		var targetElement = document.getElementById('UserInformation1_' + layerName);
		targetElement.style.display = 'block';
	}
}

function hideLayerUserInformation1(layerName)
{
	if (document.getElementById) 
	{
		var targetElement = document.getElementById('UserInformation1_' + layerName);
		targetElement.style.display = 'none';
	}
}

function abreJanela( windowURL, id, windowParams )
{
    var janela=window.open( windowURL, id, windowParams )
	janela.focus()
}

function PopupImage(img){
	image= new Image();
	image.src=(img);
	openWindow(img);
}
function openWindow(img){
	if((image.width!=0)&&(image.height!=0)){
		viewImage(img);
	}
	else{
	stuff="openWindow('"+img+"')";
	stuff1=setTimeout(stuff,20);
	}
}
function viewImage(img){
	w=image.width+0;
	h=image.height+0;
	dims="toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width="+w+",height="+h;
	
	myWin=window.open(img,"",dims);

	if (myWin) { 
		myWin.document.writeln("<HTML><HEAD><TITLE>Detalhe da Foto</TITLE></HEAD>"); 
		myWin.document.writeln("<BODY BGColor=#FFFFFF LeftMargin=0 TopMargin=0 MarginWidth=0 MarginHeight=0>"); 
		myWin.document.write("<IMG Src="+img); 
		if (w!=0) { myWin.document.write(" Width="+w) } 
		if (h!=0) { myWin.document.write(" Height="+h) } 
		myWin.document.writeln(">"); 
		myWin.document.writeln("</BODY></HTML>"); 
		myWin.document.close; 
	}  
	return myWin; 
}

var checkArray = new Array();
var totalSelected = 0;

function validateMaxSelectCheckBox(controlId)
{
	if(controlId.checked == true)
		totalSelected = (totalSelected*1) + 1;
	else
		totalSelected = (totalSelected*1) - 1;

	if(totalSelected*1 == 3)
	{
		for(i=0; i<checkArray.length; i++)
		{
			obj1 = document.getElementById(checkArray[i]);
			
			if(!(obj1.checked))
			{
				obj1.disabled = !(obj1.disabled);
			}
		}
	}
	else if(totalSelected*1 < 3)
	{
		for(i=0; i<checkArray.length; i++)
		{
			obj1 = document.getElementById(checkArray[i]);
			if(!(obj1.checked) && obj1.disabled)
			{
				obj1.disabled = !(obj1.disabled);
			}
		}
	}
}

function SetCarro(valoresNecessarios, txtHiddenPerfil1, txtHiddenPerfil2, txtHiddenPerfil3)
{
	perfil1 = document.getElementById(txtHiddenPerfil1);
	perfil2 = document.getElementById(txtHiddenPerfil2);
	perfil3 = document.getElementById(txtHiddenPerfil3);
	var hasDoneBusiness = false;
	if(perfil1.value == valoresNecessarios && hasDoneBusiness == false)
	{
		perfil1.value = "";
		hasDoneBusiness = true;
	}
	else if(perfil2.value == valoresNecessarios && hasDoneBusiness == false)
	{
		perfil2.value = "";
		hasDoneBusiness = true;
	}
	else if(perfil3.value == valoresNecessarios && hasDoneBusiness == false)
	{
		perfil3.value = "";
		hasDoneBusiness = true;
	}
	
	if(perfil1.value.length == 0 && hasDoneBusiness == false)
	{
		perfil1.value = valoresNecessarios;
		hasDoneBusiness = true;
	}
	else if(perfil2.value.length == 0 && hasDoneBusiness == false)
	{
		perfil2.value = valoresNecessarios;
		hasDoneBusiness = true;
	}
	else if(perfil3.value.length == 0 && hasDoneBusiness == false)
	{
		perfil3.value = valoresNecessarios;
		hasDoneBusiness = true;
	}
}

function PopupWindowConfigurador( webpage , marcaQ, marca, gamaQ, gama, modeloQ, modelo, perfilQ, perfil)
{
	var url = webpage + marcaQ + "=" + marca + "&" + gamaQ + "=" + gama + "&" + modeloQ + "=" + modelo + "&" + perfilQ + "=" + perfil;
	var res = window.open(url,"SIVAonline"); 
}

function PopupWindowConfContextPerfil( webpage , numChassisQ, numChassis)
{
	var url = webpage + numChassisQ + "=" + numChassis;
	var res = window.open(url,"SIVAonline"); 
}

function AddItem(tb_servicos ,chk , item)
{
	var tb = document.getElementById(tb_servicos);
	var ch = document.getElementById(chk);
	
	if(tb != null && ch != null)
	{
		var aux = tb.value;
		var aux2 = "";
		var temp = new Array();
		temp = aux.split(';');
		
		if(ch.checked)
		{
			if(temp.length > 1)
			{
				var valor = "0";
				for(i=0 ; i < (temp.length - 1) ; i=i+1)
				{
					if(temp[i].toString() == item)
						valor = "1";
				}
				if(valor == "0")
					tb.value = aux + item + ";";
			}
			else
				tb.value = item + ";";
		}
		else
		{
			if(temp.length > 1)
			{
				var valor = "0";
				for(i=0 ; i < (temp.length - 1) ; i=i+1)
				{
					if(temp[i].toString() != item)
						aux2 = aux2 + temp[i].toString() + ";";
				}
				tb.value = aux2;
			}
			else
				tb.value = "";
		}
	}
}

function ClickToThickbox(button)
{
	imgLoader = new Image();// preload image
	imgLoader.src = tb_pathToImage;
	
	var t = button.title || button.name || null;
	var a = button.href || button.alt;
	var g = button.rel || false;
	tb_show(t,a,g);
	button.blur();
}

function SetStock(textoStock, idTextBox, idBtn)
{
	obj1 = document.getElementById(idTextBox);
	obj1.value = textoStock;
	
	obj2 = document.getElementById(idBtn);
	//obj2.click();
	
	var btnName = idBtn.replace('_','$');
	__doPostBack(btnName,'');
}

function showThickbox(pagina)
{
	tb_show('', pagina, false);
}