/*
*  Extens Solucions S.L.
*  common.js
*/

var cal = new calendar();
var acr;

function initNewsSection(nnews, nn, id){
	acr = new acord();
	for(x = 0; x < nnews; x++){
		acordx = document.getElementById('wn' + x);
		acordx.style.height = acordx.offsetHeight + 'px';
		//acordx.style.display = (x == nn) ? 'block' : 'none';
		/*node = acordx.parentNode;
		if(node.childNodes[0].className == 'neu') 
			node.childNodes[0].className == 'neu';
		*/
	}
	
	if(id) document.location.href = "#n" + id;
}

function trim(str){
	var trimed, rg, i = 0;
	while ((i < str.length) && (str.charAt(i) == ' ')) i++;
	trimed = str.substring(i, str.length);
	rg = trimed;
	i = rg.length;
	while ((i > 0) && (rg.charAt(i-1) == ' ')) i--;
	trimed = rg.substring(0,i);
	return trimed;
}

function showBodyNew(atag, ncon){
	var nw = document.getElementById(ncon);	
	if(atag.innerHTML != "Amagar"){
		acr.active(ncon);
 		atag.innerHTML = "Amagar";
	}else{
		acr.active(ncon);
		atag.innerHTML = "Mostrar";
	}	
}

function external(){
	if(document.getElementsByTagName){
		var bk = document.getElementById('kuko');
  		if(bk){
  			var i, a = bk.getElementsByTagName('a'); 
	  		for (i in a){
	  			if (a[i].getAttribute('href') && a[i].getAttribute('rel') == 'external') a[i].target = '_blank';
	  		}
		}
	}
}

function wvideo(url){ window.open(url, '_blank', 'toolbar=no, resizable=no, copyhistory=no, status=no, width=450, height=350'); }

function spring(url){ window.open(url, '_blank'); }

function springw(atag, width, heigth){ window.open(atag.href, '_blank', 'toolbar=no, resizable=yes, copyhistory=no, status=no, width=' + width + ', height=' + height); }

function submitForm(fm){
	
	if(fm.name.value.length <= 0){
		document.getElementById('lb' + fm.name.id).className = 'red';
		fm.name.focus();
		return false;
	}else{
		document.getElementById('lb' + fm.name.id).className = 'bck';
	}

	if(fm.nachname.value.length <= 0){
		document.getElementById('lb' + fm.nachname.id).className = 'red';
		fm.nachname.focus();
		return false;
	}else{
		document.getElementById('lb' + fm.nachname.id).className = 'bck';
	}
	
	if(fm.email.value.length <= 0){
		document.getElementById('lb' + fm.email.id).className = 'red';
		fm.email.focus();
		return false;
	}else{
		document.getElementById('lb' + fm.email.id).className = 'bck';
	}
	
	if(fm.komentaren.value.length <= 0){
		document.getElementById('lb' + fm.komentaren.id).className = 'red';
		fm.komentaren.focus();
		return false;
	}else{
		document.getElementById('lb' + fm.komentaren.id).className = 'bck';
	}

	if(!fm.legal.checked){
		remarkBox();
		return false;
	}

	fm.submit();
}

function clearForm(fm){
	for(x = 0; x < fm.length; x++){
		if(fm.elements[x].type == "select-one")
			fm.elements[x].selectedIndex = 0;
		else if(fm.elements[x].type != "button" && fm.elements[x].type != "submit")
			fm.elements[x].value = "";
	}
}

function remarkBox(){
	document.getElementById('ichazkept').style.border = '1px solid #FF0000';
	setTimeout("document.getElementById('ichazkept').style.border = '1px solid #FFFFFF'", 500);
	setTimeout("document.getElementById('ichazkept').style.border = '1px solid #FF0000'", 1000);
	setTimeout("document.getElementById('ichazkept').style.border = '1px solid #FFFFFF'", 1500);
	setTimeout("document.getElementById('ichazkept').style.border = '1px solid #FF0000'", 2000);	
	setTimeout("document.getElementById('ichazkept').style.border = '1px solid #FFFFFF'", 2500);
}

function calendar(year, month, day){

	var cdate;
	var bgsel;
	var fsel;
	var fnom;
	var bgnom;
	var msrecord;
	
	this.initCalendar = function(year, month, day, norecord){
		var self = this;
		bgsel = "#FAEC75";
		fsel = "#06A1DB";
		fnom = bgnom = "#FFFFFF";
		if(isNaN(year) || isNaN(month) || isNaN(day)){ 
			cdate = new Date();
			msrecord = null;
		}else{
			cdate = new Date(year, month, day);
			msrecord = (norecord) ? null : cdate.valueOf();
		}
		record = cdate.valueOf();
		document.getElementById("yBackward").onclick = function(){ self.yearBackward(); }
		document.getElementById("mBackward").onclick = function(){ self.monthBackward(); }
		document.getElementById("mForward").onclick = function(){ self.monthForward(); }
		document.getElementById("yForward").onclick = function(){ self.yearForward(); }
		buildCalendar(cdate.getFullYear(), cdate.getMonth(), cdate.getDate());
	}

	this.yearForward = function(){
		cdate.setYear(cdate.getFullYear() + 1);
		buildCalendar(cdate.getFullYear(), cdate.getMonth(), cdate.getDate());	
	}
	
	this.yearBackward = function(){
		cdate.setYear(cdate.getFullYear() - 1);
		buildCalendar(cdate.getFullYear(), cdate.getMonth(), cdate.getDate());			
	}
	
	this.monthForward = function(){
		if((cdate.getMonth() + 1) > 11){
			cdate.setFullYear(cdate.getFullYear() + 1);
			cdate.setMonth(0);
		}else{
			cdate.setMonth(cdate.getMonth()+1);
		}
		buildCalendar(cdate.getFullYear(), cdate.getMonth(), cdate.getDate());
	}
	
	this.monthBackward = function(){
		if((cdate.getMonth() - 1) < 0){
			cdate.setFullYear(cdate.getFullYear() - 1);
			cdate.setMonth(11);
		}else{
			cdate.setMonth(cdate.getMonth()-1);
		}
		buildCalendar(cdate.getFullYear(), cdate.getMonth(), cdate.getDate());
	}
	
	function getDaysInMonth(month, year){
		var ndays = 0;
		var date = new Date(year, month, 1);
		var month_days = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
		if(month == 1){
			if(((year % 4 == 0) && (year % 100 != 0)) || ( year % 400 == 0)){
				return 29;
			}
		}
		return month_days[month];
	}
	
	function buildCalendar(year, month, date){
		var month_names = new Array('Gen','Feb','Mar','Abr','Mai','Jun','Jul','Ago','Set','Oct','Nov', 'Des');
		var todate = new Date();
		var today = todate.getDate();
		var tomonth = todate.getMonth();
		var toyear = todate.getFullYear();
		
		var wdate = new Date(year, month, date);
		var mswdate = wdate.valueOf();
		var day = wdate.getDate();
		var month = wdate.getMonth();
		var year = wdate.getFullYear();

		amonth = document.getElementById("mname");
		amonth.innerHTML = month_names[month] + "&nbsp;" + String(year).substring(2,4);
		amonth.href = "noticies.php?month=" + (month + 1) + "&date=" + (mswdate / 1000);
		
		var dtmp = new Date(year, month, 1);
		
		var offset = null;
		
		switch(dtmp.getDay()){
			case 1: offset = 6; break; 	// Di
			case 2: offset = 0; break;	// Dm
			case 3: offset = 1; break;	// Dx
			case 4: offset = 2; break;	// Dj
			case 5: offset = 3; break;	// Dv
			case 6: offset = 4; break;	// Ds
			case 0: offset = 5; break;	// Dm
		}		
		
		if(month == 0){
			var p_month = 11;
			var n_month = month + 1;
			var p_year = year - 1;
			var n_year = year;
		}else if(month == 11){
			var p_month = month - 1;
			var n_month = 1;
			var p_year = year;
			var n_year = year + 1;
		}else{
			var p_month = month - 1;
			var n_month = month + 1;
			var p_year = year;
			var n_year = year;
		}
	
		var p_month_days = getDaysInMonth(p_month, p_year);
		var month_current = getDaysInMonth(month, year); // quants de dies te el mes actual

		var start_offset = p_month_days - offset;
		var end_offset =  42 - (offset + month_current);
		var dc = 1, dn = 1;	
		var strdate = null;
		
		table = document.getElementById('calendar');
		tbody = table.getElementsByTagName('tbody');
		trs = tbody[0].getElementsByTagName('tr');

		for(x = 0; x < 6; x++){
			tds = trs[x].getElementsByTagName('td');
			for(y = 0; y < 7; y++){
				if(start_offset <= p_month_days){
					strdate = start_offset + "/" + (p_month + 1) + "/" + p_year;
					dtmp = new Date(p_year, p_month, start_offset);
					tds[y].innerHTML = "<a class=\"om\" href=\"noticies.php?date=" + (dtmp.getTime() / 1000) + "\" title=\"" + strdate + "\">" + start_offset + "</a>";
					start_offset++;
				}else if(dc <= month_current){
					strdate = dc + "/" + (month + 1) + "/" + year;
					dtmp = new Date(year, month, dc);
					if(dtmp.valueOf() == mswdate && msrecord == mswdate){
						tds[y].style.backgroundColor = bgsel;
						tds[y].style.color = fsel;
						tds[y].innerHTML = "<a title=\"" + strdate + "\">" + dc + "</a>";
					}else{
						tds[y].style.backgroundColor = bgnom;
						tds[y].style.color = fnom;
						tds[y].innerHTML = "<a class=\"cm\" href=\"noticies.php?date=" + (dtmp.getTime() / 1000) + "\" title=\"" + strdate + "\">" + dc + "</a>";
					}
					dc++;
				}else{
					strdate = dn + "/" + (n_month + 1) + "/" + n_year;
					dtmp = new Date(n_year, n_month, dn);
					tds[y].innerHTML = "<a class=\"om\" href=\"noticies.php?date=" + (dtmp.getTime() / 1000) + "\" title=\"" + strdate + "\">" + dn + "</a>";			
					dn++;	
				}
			}
		}
	}
}

acord = function(){ 
	this.steps = 12;
	this.duration = 600;
	this.stepCounter = 0;
	this.sizec = null;
	this.sizeo = null;
	this.stepGrow = null;
	this.interval = this.duration / this.steps;
	this.panc = null;
	this.grows = false;	

	acord.prototype.active = function (idac){
		this.panc = document.getElementById(idac);
		if(this.panc.style.display == 'none'){
			this.sizec = 0;
			this.sizeo = parseInt(this.panc.style.height);
			this.panc.style.height = 0 + 'px';
			this.panc.style.display = 'block';
			this.stepGrow = this.sizeo / this.steps;	
			this.grows = true;
		}else{
			clientHeight = this.panc.offsetHeight;
			this.sizeo = clientHeight;
			this.sizec = clientHeight;
			this.panc.style.height = this.sizec + 'px';
			this.panc.style.display = 'block';
			this.stepGrow = this.sizeo / this.steps;
			this.grows = false;
		}
		this.stepAnimation();
	}
	
	acord.prototype.stepAnimation = function (){
		++this.stepCounter;
		this.animate();
		(this.stepCounter >= this.steps) ? this.terminated() : this.start();
	}
	
	acord.prototype.start = function (){
		var self = this;
		setTimeout(function() { self.stepAnimation(); }, this.interval);
	}
	
	acord.prototype.downArrow = function(){ }
	
	acord.prototype.animate = function (){
		this.sizec = this.grows ? (this.sizec + this.stepGrow) : (this.sizec - this.stepGrow);		
		this.panc.style.height = this.sizec + "px";
	}
	
	acord.prototype.terminated = function(){
		if(!this.grows) this.panc.style.display = "none";
		this.panc.style.height = this.sizeo + 'px';
		this.stepCounter = 0;
	}
} 


