function setDefaultStyle(fld)
{
	fld.value = "";
	fld.style.background = 'White';
	fld.focus();
	document.getElementById('validAnswer').innerHTML = "";
}


function validateFormOnSubmit(theForm) {
	var reason = "";
	
	reason += validateEmpty(theForm.answer);
	    
	if (reason != "") {
	  document.getElementById('validAnswer').innerHTML = reason;
	 
	  return false;
	}
	
	return true;
}

function validateEmpty(fld) {
   var error = "";
 
   if (fld.value.length == 0) {
       fld.style.background = 'Yellow'; 
       error = "Vastaus puuttuu.\n"
   } else {
       fld.style.background = 'White';
   }
   return error;   
}


/**
 * Suggests URL in societies form, using value of field 'urlAlias'. May be useful in some other place too
 * @author TH
 * @return string
 */
function cleanUri (txt) {
	tmpval = txt;
	tmpval = tmpval.toLowerCase();
	tmpval = tmpval.replace(/ï¿½/g,'a');
	tmpval = tmpval.replace(/ï¿½/g,'a');
	tmpval = tmpval.replace(/ï¿½/g,'o');
	tmpval = tmpval.replace(/ /g,'-');
	tmpval = tmpval.replace(/\(/g,'');
	tmpval = tmpval.replace(/\)/g,'');
	return tmpval;
} 



// fadeColours
function fadeColours(elementId,red,green,blue,speed) {

	if(red==255 && green==255 && blue==255) {
		document.getElementById(elementId).style.backgroundColor="transparent";
		return true;
	}

	document.getElementById(elementId).style.backgroundColor="rgb("+red+","+green+","+blue+")";

	red=red + Math.ceil((246 - red)/10);
	green=green + Math.ceil((163-green)/10);
	blue=blue + Math.ceil((0-blue)/10);

	setTimeout("fadeColours('"+elementId+"',"+red+","+green+","+blue+","+speed+")",speed);
	//document.getElementById(elementId).style.backgroundColor = '#f6a300';
};


// popUpWin
function popUpWin(url,name,w,h) {
	var x=(screen.width/2-w/2);
	var y=(screen.height/2-h/2);
	window.open(url,name,"toolbar=no,location=no,directories=no,status=no,menuvar=no,resizable=yes,scrollbars=yes,width="+w+",height="+h+",screenX="+x+",screenY="+y+",left="+x+",top="+y);
};

function deleteItem(message,url) {
	var action=window.confirm(message);
	if(action==true) {
		window.location=url;
	} else {
		history.go(-1);
	}
};

function checkBlogForm() {

	formErrors = false;    

	
	if (document.post.title.value.length < 2 ) {
			formErrors = "Otsikko pitää olla vähintään 3 merkkiä pitkä.";
	}

	if (document.post.description_short.value.length < 2 ) {
			formErrors = "Lyhyt kuvaus puuttuu!";
	}	
	
	if (document.post.description_short.value.length > 300 ) {
			formErrors = "Lyhyt kuvaus liian pitkä! (max. 300 merkkiä)";
	}	
	
	if (formErrors) {
		alert(formErrors);
		return false;
	} else {
		//bbstyle(-1);
		//formObj.preview.disabled = true;
		//formObj.submit.disabled = true;
		
		/*return true;*/
		document.post.submit();
	}
}


//AJAX

var bustcachevar=0; //bust potential caching of external pages after initial request? (1=yes, 0=no)
var loadstatustext_cool="<p style=\"font-family:verdana,arial,sans-serf;color:gray;margin-left:auto;margin-right:auto;padding:4px;\"><img src='http://images.coronaria.fi/?id=4188303' /> ladataan...</p>"
var loadstatustext="<br /><br /><p style=\"display:block;font-family:verdana,arial,sans-serf;color:#DDDDDD;text-align:center;\">lataa...</p>";

var siteUrl = 'http://'+window.location.host+'/';


////NO NEED TO EDIT BELOW////////////////////////
var loadedobjects=""
var defaultcontentarray=new Object()
var bustcacheparameter=""

function ajaxinit () {
	if (window.XMLHttpRequest) // if Mozilla, Safari etc
		return (new XMLHttpRequest());
	else if (window.ActiveXObject){ // if IE
		try {
			return ( new ActiveXObject("Msxml2.XMLHTTP"));
		} 
		catch (e){
			try{
				return ( new ActiveXObject("Microsoft.XMLHTTP"));
			}
			catch (e){}
		}
	}
	else {
		return false;
	}
}

function ajaxpage(url, containerid, targetobj){
	var page_request = ajaxinit();
	if (!page_request) {
		return false;
	}
	//var ullist=targetobj.parentNode.parentNode.getElementsByClass(document, 'tab', 'div')
	var ullist=targetobj.parentNode.parentNode.getElementsByTagName("li");

	for (var i=0; i<ullist.length; i++) {
		if (ullist[i].className == 'righttab_unactive' || ullist[i].className == 'righttab_active')
			ullist[i].className="righttab_unactive";  //deselect all tabs
		if (ullist[i].className == 'lefttab_unactive' || ullist[i].className == 'lefttab_active' )
			ullist[i].className="lefttab_unactive";
	}

	if (targetobj.parentNode.className == "righttab_unactive")
		targetobj.parentNode.className="righttab_active";  //highlight currently clicked on tab
	if (targetobj.parentNode.className == "lefttab_unactive")
		targetobj.parentNode.className="lefttab_active";  //highlight currently clicked on tab
		
	if (url.indexOf("#default")!=-1){ //if simply show default content within container (verus fetch it via ajax)
		document.getElementById(containerid).innerHTML=defaultcontentarray[containerid];
		return;
	}
	document.getElementById(containerid).innerHTML=loadstatustext;
	//fadeIn(containerid,0,5);
	page_request.onreadystatechange=function(){
		loadpage(page_request, containerid, 1);
	}
	if (bustcachevar) //if bust caching of external page
		bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime();
	page_request.open('GET', url+bustcacheparameter, true);
	page_request.send(null);
}

function loadpage(page_request, containerid, fade){
	if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1)) {
		//document.getElementById(containerid).style.visibility = 'hidden';
		document.getElementById(containerid).innerHTML=page_request.responseText;
		if (fade == 1) {
			setOpacity(document.getElementById(containerid), 0);
			//document.getElementById(containerid).style.visibility = 'visible';
			fadeIn(containerid,0,10);
		} else {
			document.getElementById(containerid).style.visibility = 'visible';
		}
	}
}

function orderNewsletter(email, radioSet) {
	//active the graphics....
	for (i=0;i<radioSet.length;i++){
	if (radioSet[i].checked==true)
		radioValue = radioSet[i].value;
	}

	var page_request = ajaxinit();
	if (!page_request)
		return false;	
	document.getElementById('newsletter').innerHTML=loadstatustext_cool;
	
	page_request.onreadystatechange=function(){
		loadpage(page_request, 'newsletter', 0);
	}
	page_request.open('GET', siteUrl+'serveAjax.php?newsletter&action=add&email='+email+'&type='+radioValue, true);
	page_request.send(null);
}

function cancelNewsletter(email) {
	//active the graphics....

	var page_request = ajaxinit();
	if (!page_request)
		return false;	
	document.getElementById('newsletter').innerHTML=loadstatustext_cool;
	
	page_request.onreadystatechange=function(){
		loadpage(page_request, 'newsletter', 0);
	}
	page_request.open('GET', siteUrl+'serveAjax.php?newsletter&action=remove&email='+email, true);
	page_request.send(null);
}

function showNewsletterIndex() {
	//active the graphics....

	var page_request = ajaxinit();
	if (!page_request)
		return false;	
	document.getElementById('newsletter').innerHTML=loadstatustext_cool;
	
	page_request.onreadystatechange=function(){
		loadpage(page_request, 'newsletter', 0);
	}
	page_request.open('GET', siteUrl+'serveAjax.php?newsletter&action', true);
	page_request.send(null);
}

/**
 * Suggests URL in societies form, using value of field 'urlAlias'. May be useful in some other place too
 * @author TH
 * @return boolean
 */
function suggestUri (el) {
	tmpval = el.value;
	tmpval = tmpval.toLowerCase();
	tmpval = tmpval.replace(/�/g,'a');
	tmpval = tmpval.replace(/�/g,'a');
	tmpval = tmpval.replace(/�/g,'o');
	tmpval = tmpval.replace(/ /g,'-');
	tmpval = tmpval.replace(/\(/g,'');
	tmpval = tmpval.replace(/\)/g,'');
	document.getElementById('urlAlias').value = tmpval;
}


function sendPollAnswer(form) {
	var page_request = ajaxinit();
	if (!page_request)
		return false;	
	form = document.getElementById('modpollform');
	quickpoll = form.quickpoll;
	questionid = form.questionid.value;
	
	page_request.onreadystatechange=function(){
		loadpage(page_request, 'ModPoll', 0);
	}

	//
	//alert(form);
	len = quickpoll.length;
	//quickpollValue='';
	for (i = 0; i < len; i++) {
		
		if (quickpoll[i].checked) {
				quickpollValue = quickpoll[i].value;
		}
    }
    
    if (quickpollValue == undefined) {
    	return false;
    }
  
	document.getElementById('ModPoll').innerHTML=loadstatustext_cool;
	
	parameters = 'quickpoll='+quickpollValue+'&questionid='+questionid;
	//alert(parameters);
	page_request.open('POST', siteUrl+'.?module=ModPoll', true);
	page_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	page_request.setRequestHeader("Content-length", parameters.length);
	page_request.setRequestHeader("Connection", "close");
	page_request.send(parameters);
	
	if (ajaxinit()) {
		return false;
	} else {
		return true;
	}
			
}

function sendArticlePollAnswer(form) {
	var page_request = ajaxinit();
	if (!page_request)
		return false;	
	form = document.getElementById('modarticlepollform');
	quickpoll = form.quickpoll;
	articlequestionid = form.articlequestionid.value;
	articlepoll = form.articlepoll.value;
	articlepollid = form.articlepollid.value;
	
	page_request.onreadystatechange=function(){
		loadpage(page_request, 'articlePollContent', 0);
	}

	//
	//alert(form);
	len = quickpoll.length;
	//quickpollValue='';
	for (i = 0; i < len; i++) {
		
		if (quickpoll[i].checked) {
				quickpollValue = quickpoll[i].value;
		}
    }
    
    if (quickpollValue == undefined) {
    	return false;
    }
  
	document.getElementById('articlePollContent').innerHTML=loadstatustext_cool;
	
	parameters = 'quickpoll='+quickpollValue+'&articlequestionid='+articlequestionid+'&articlepoll='+articlepoll+'&articlepollid='+articlepollid;
	//alert(parameters);
	page_request.open('POST', siteUrl+'.?module=ModPoll', true);
	page_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	page_request.setRequestHeader("Content-length", parameters.length);
	page_request.setRequestHeader("Connection", "close");
	page_request.send(parameters);
	
	if (ajaxinit()) {
		return false;
	} else {
		return true;
	}
			
}





// resize
function resize(method) {
	if(method=="larger") {
		$$('#cppfArticleContent').each(function(el) {
			el.setStyle('font-size',(el.getStyle('font-size').toInt()+1)+'px');
		});
		$$('#cppfArticleContent a').each(function(el) {
			el.setStyle('font-size',(el.getStyle('font-size').toInt()+1)+'px');
		});
		$$('#cppfArticleContent h1').each(function(el) {
			el.setStyle('font-size',(el.getStyle('font-size').toInt()+1)+'px');
		});
		$$('#cppfArticleContent h2').each(function(el) {
			el.setStyle('font-size',(el.getStyle('font-size').toInt()+1)+'px');
		});
		$$('#cppfArticleContent h3').each(function(el) {
			el.setStyle('font-size',(el.getStyle('font-size').toInt()+1)+'px');
		});
		$$('#cppfArticleContent h4').each(function(el) {
			el.setStyle('font-size',(el.getStyle('font-size').toInt()+1)+'px');
		});
		$$('#cppfArticleContent p').each(function(el) {
			el.setStyle('font-size',(el.getStyle('font-size').toInt()+1)+'px');
		});
	}
	if(method=="smaller") {
		$$('#cppfArticleContent').each(function(el) {
			el.setStyle('font-size',(el.getStyle('font-size').toInt()-1)+'px');
		});
		$$('#cppfArticleContent a').each(function(el) {
			el.setStyle('font-size',(el.getStyle('font-size').toInt()-1)+'px');
		});
		$$('#cppfArticleContent h1').each(function(el) {
			el.setStyle('font-size',(el.getStyle('font-size').toInt()-1)+'px');
		});
		$$('#cppfArticleContent h2').each(function(el) {
			el.setStyle('font-size',(el.getStyle('font-size').toInt()-1)+'px');
		});
		$$('#cppfArticleContent h3').each(function(el) {
			el.setStyle('font-size',(el.getStyle('font-size').toInt()-1)+'px');
		});
		$$('#cppfArticleContent h4').each(function(el) {
			el.setStyle('font-size',(el.getStyle('font-size').toInt()-1)+'px');
		});
		$$('#cppfArticleContent p').each(function(el) {
			el.setStyle('font-size',(el.getStyle('font-size').toInt()-1)+'px');
		});
	}
};

/**
 * ACC - Article Collection Content
 */
window.addEvent('domready',function(){
	var reqACC_1 = new Request.HTML({url:'/?module=ArticleCollection&tab=20&saveTab=1', 
		onSuccess: function(html) {
			// unactivate
			$$('#articleCollection div.menu a').set('class', '');
			// activate
			$('ACC_tab20').set('class', 'a');
			// get content
			$('ACC').set('text', '');
			$('ACC').adopt(html);
		}
	});
	
	var reqACC_2 = new Request.HTML({url:'/?module=ArticleCollection&tab=30&saveTab=1', 
		onSuccess: function(html) {
			// unactivate
			$$('#articleCollection div.menu a').set('class', '');
			// activate
			$('ACC_tab30').set('class', 'a');
			// get content
			$('ACC').set('text', '');
			$('ACC').adopt(html);
		}
	});
	
	var reqACC_3 = new Request.HTML({url:'/?module=ArticleCollection&tab=40&saveTab=1', 
		onSuccess: function(html) {
			// unactivate
			$$('#articleCollection div.menu a').set('class', '');
			// activate
			$('ACC_tab40').set('class', 'a');
			// get content
			$('ACC').set('text', '');
			$('ACC').adopt(html);
		}
	});
	
	var reqACC_4 = new Request.HTML({url:'/?module=ArticleCollection&tab=50&saveTab=1', 
		onSuccess: function(html) {
			// unactivate
			$$('#articleCollection div.menu a').set('class', '');
			// activate
			$('ACC_tab50').set('class', 'a');
			// get content
			$('ACC').set('text', '');
			$('ACC').adopt(html);
		}
	});
	
	var reqACC_5 = new Request.HTML({url:'/?module=ArticleCollection&tab=60&saveTab=1', 
		onSuccess: function(html) {
			// unactivate
			$$('#articleCollection div.menu a').set('class', '');
			// activate
			$('ACC_tab60').set('class', 'a');
			// get content
			$('ACC').set('text', '');
			$('ACC').adopt(html);
		}
	});
	
	$('ACC_tab20').addEvent('click', function() {
		reqACC_1.send();
	});
	
	$('ACC_tab30').addEvent('click', function() {
		reqACC_2.send();
	});
	
	$('ACC_tab40').addEvent('click', function() {
		reqACC_3.send();
	});
	
	$('ACC_tab50').addEvent('click', function() {
		reqACC_4.send();
	});
	
	$('ACC_tab60').addEvent('click', function() {
		reqACC_5.send();
	});
});


/**
 * External link direction
 */
window.addEvent('domready', function() {
	$$('a[rel=external]').each(function(link) {
		link.addEvent('click', function(event){
			window.open(this.href);
			return false;
		});
	});	
});





window.addEvent('domready',function(){

	// nlInfo
	$('nlInfo').addEvent('click',function(e){
		var target=$('newsletter');
		$('nlMode').set('value','info');
		$('nlForm').set('send',{onComplete:function(response){
			target.empty().set('html',response);
		}});
		$('nlForm').send();
	});

	// nlOrder
	$('nlOrder').addEvent('click',function(e){
		var target=$('newsletter');
		$('nlMode').set('value','order');
		$('nlForm').set('send',{onComplete:function(response){
			target.empty().set('html',response);
		}});
		$('nlForm').send();
	});

	// nlCancel
	$('nlCancel').addEvent('click',function(e){
		var target=$('newsletter');
		$('nlMode').set('value','remove');
		$('nlForm').set('send',{onComplete:function(response){
			target.empty().set('html',response);
		}});
		$('nlForm').send();
	});

});




/**
 * Alert-like easy message box with SqueezeBox
 * @author TH
 * @extends SqueezeBox
 */ 
SqueezeBox.msg = 	 
	function(responseText) {
		 this.initialize({
			handler:'string',
		size:{
			x:300,
			y:75
		}}
	);
	this.setContent('string', '<div style=\"margin-top:22px;text-align:center\"><strong>'+responseText)+'</strong></div>';
	this.showContent();
};

/**
 * instance LoadBox
 * Lays loading msg box over given element
 * @author TH
 */
var LoadBox = {
	presets: {
		opacity: 0.4,
		loadingText: 'ladataan'
	},
	render: function(el, conf) {
		conf = (conf != undefined && typeof(conf)=='object')?$merge(this.presets,conf):this.presets;
		if (Browser.Engine.trident) {
			elb = el;
			for (var posX = 0, posY = 0; el; el = el.offsetParent ) {
			      posX += el.offsetLeft;
			      posY += el.offsetTop;
			}
			el = elb;
			posY = posY + (el.getSize().y/2-13);
			posX = posX + (el.getSize().x/2-39);
			//el.set('html','<div style="margin:8px;"><img src=\"http://images.coronaria.fi/?id=4188303\" alt=\"\" /> '+conf.loadingText+'</div>');
		} else {
			coords = el.getCoordinates(document.body);
			posY = coords.top + (coords.height/2-13);
			posX = coords.left + (coords.width/2-39);
		}
		//If argument conf is defined, we merge it with presets. If not, let's revert to presets
		el.getChildren().set('opacity',conf.opacity);
		
		box = new Element('div', {
			'class': 'load-box',
			'styles':{
				'top': posY,
				'left': posX 
			},
			'html':'<img src=\"http://images.coronaria.fi/?id=4188303\" alt=\"\" /> '+conf.loadingText
		});
		box.inject(el,'top');
		
	}
};