// 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,'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;
}


/**
 * Get ModPoll answer from it's form and sends it to ModPoll via XHR interface 
 * @author TH
 * @return boolean
 */
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;
	}
			
}

// 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');
		});
	}
};




/**
 * castVote	
 */
function castVote(id,action) {
	new Request({
		url:'./?module=Comments&mode=Vote&action='+action+'&id='+id,
		method:'get',
		onSuccess:function(responseText) {
			$$('.comments-votes-'+id+'').set('html','&nbsp;');
			$('comments-points-'+id).set('html',responseText);
		}
	}).send();
	
};

/**
 * ratingsSubmit
 */
function ratingsSubmit() {
	$('ratingsform').send('./?module=Comments&mode=Ratings');
	$('ratings').empty();
	window.alert('Arvostelu tallennettu.');
};


window.addEvent('domready', function() {
	$$('a[rel=external]').each(function(link) {
		link.addEvent('click', function(event){
			window.open(this.href);
			return false;
		});
	});
});

