/**
 * articleSave
 */
function articleSave(id,articleUrl,articleHeadline) {
	new Request({
		url:'./serveAjax.php?mode=Articles&action=Save&id='+id+'&articleUrl='+articleUrl+'&articleHeadline='+articleHeadline,
		method:'get',
		onSuccess:function() {
			$('articleSave').setStyle('display','block');
		}
	}).send();
};


/**
 * castVote
 */
function castVote(id,action) {
	new Request({
		url:'./serveAjax.php?mode=Vote&action='+action+'&id='+id,
		method:'get',
		onSuccess:function(responseText) {
			$$('.comments-votes-'+id+'').set('html','&nbsp;');
			$('comments-points-'+id).set('html',responseText);
		}
	}).send();
};


/**
 * cppfBoxedTabs
 */
function cppfBoxedTabs(name) {
	var i=1;
	$$('#'+name+' .tab-content').each(function(el) {
		if(i==1) {
			el.setStyle('display','block');
		} else {
			el.setStyle('display','none');
		}
		i++;
	});
	$$('#'+name+' td.tab','#'+name+' td.tab-selected').addEvent('click',function() {
		var tabold=$$('#'+name+' td.tab-selected').removeClass('tab-selected').addClass('tab');
		var tabnew=this.removeClass('tab').addClass('tab-selected');
		var title=this.title;
		$$('#'+name+' .tab-content').each(function(el) {
			if(el.getProperty('title')==title) {
				el.setStyle('display','block');
			} else {
				el.setStyle('display','none');
			}
		});
	});
};


/**
 * 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);
};


/**
 * ratingsSubmit
 */
function ratingsSubmit() {
	$('ratingsform').send('./serveAjax.php?mode=Ratings');
	$('ratings').empty();
	window.alert('Arvostelu tallennettu.');
};


/**
 * 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');
		});
	}
};

// checkCheckBoxes
function checkCheckBoxes(form,group1,group2,val) {
	if(group1!="") {
		for(i=0,n=form.elements.length;i<n;i++) {
			if(form.elements[i].alt==group1) {
				if(val==1) {
					form.elements[i].checked=true;
				} else {
					form.elements[i].checked=false;
				}
			}
		}
	}
	if(group2!="") {
		for(i=0,n=form.elements.length;i<n;i++) {
			if(form.elements[i].alt==group2) {
				if(val==1) {
					form.elements[i].checked=true;
				} else {
					form.elements[i].checked=false;
				}
			}
		}
	}
};



/**
 * Old school XHR hacks of LT cart
 * @author Timo Hanhirova <timo.hanhirova@coronaria.fi>
 */

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://devel.laakariportaali.fi/';
var siteUrl = '';

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 loadpage(page_request, containerid, fade){
	if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1)) {
		document.getElementById(containerid).innerHTML=page_request.responseText;
		if (fade == 1)
			fadeIn(containerid,0,10);
	}
}

function loadselect(page_request, containerid){
	if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1)) {	
		//document.getElementById(containerid).innerHTML=page_request.responseText;

		if (window.ActiveXObject){
			var xmlDocument = new ActiveXObject("Microsoft.XMLDOM");
			xmlDocument.loadXML(page_request.responseText);
			options = xmlDocument.getElementsByTagName("rootnode")[0].getElementsByTagName("opt");
		} else {
			var xmlDocument = page_request.responseXML;
			//alert(xmlDocument.getElementsByTagName("rootnode")[0]);
			options = xmlDocument.getElementsByTagName("rootnode")[0].getElementsByTagName("opt");

		}
		document.getElementById(containerid).options.length = 0; //clear;
		for (i=0;options.length>i;i++) {
			//alert(options[i].childNodes[0].nodeValue);
			document.getElementById(containerid).options[i] = new Option(options[i].childNodes[0].nodeValue,options[i].getAttribute('value'));
		}
		
		if (containerid == 'selectProductsSpeciality') {
			if (document.getElementById(containerid).options[1].value == '0213123') { //no speciality
				window.location = '?page=1176230&mode=3&spid=0213123&pid='+selectedValue;
			}
		}
	}
}

function fadeIn(elementId,opacity,speed) {
    
	if (opacity <= 100) {
      		setOpacity(document.getElementById(elementId), opacity);
      		opacity += 10;
      		window.setTimeout("fadeIn('"+elementId+"',"+opacity+", "+speed+")", speed);
    	}

}


function setOpacity(obj, opacity) {
  opacity = (opacity == 100)?99.999:opacity;
  
  // IE/Win
  obj.style.filter = "alpha(opacity:"+opacity+")";
  
  // Safari<1.2, Konqueror
  obj.style.KHTMLOpacity = opacity/100;
  
  // Older Mozilla and Firefox
  obj.style.MozOpacity = opacity/100;
  
  // Safari 1.2, newer Firefox and Mozilla, CSS3
  obj.style.opacity = opacity/100;
}

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';
	return true;
}

function addToCart (mode, itemId, pAmount, speciality) {
	//active the graphics....
	if (speciality == '') {
		alert("Sinun on valittava erikoisala");
	}
	else {		
		el1 = document.getElementById('box_shoppingcart_content');
		el1.className = 'box_shoppingcart_content_active';

		var page_request = ajaxinit();
		if (!page_request)
			return false;
		document.getElementById('box_shoppingcart_content').innerHTML=loadstatustext_cool;
		page_request.onreadystatechange=function(){
			loadpage(page_request, 'box_shoppingcart_content', 0);	
		}
		page_request.open('GET', siteUrl+'serveAjax.php?cart=1&action=add&pamount='+pAmount+'&pid='+itemId+'&speciality='+speciality, true);
		page_request.send(null);
	}	
	return true;
}

function removeFromCart (itemId, speciality) {
	//active the graphics....
		var page_request = ajaxinit();
		if (!page_request)
				return false;
		//document.getElementById('box_shoppingcart_content').innerHTML=loadstatustext_cool;
		page_request.onreadystatechange=function(){
				loadpage(page_request, 'box_shoppingcart_content', 0);
		}

		page_request.open('GET', siteUrl+'serveAjax.php?cart=1&action=remove&pid='+itemId+'&speciality='+speciality, true);
		page_request.send(null);

	return true;
}

function emptyCart () {
	//active the graphics....
	document.getElementById('box_shoppingcart_content').className = 'box_shoppingcart_content_unactive';	
	
	var page_request = ajaxinit();
	if (!page_request)
		return false;	
	document.getElementById('box_shoppingcart_content').innerHTML=loadstatustext_cool;
	
	page_request.onreadystatechange=function(){
		loadpage(page_request, 'box_shoppingcart_content', 0);
	}
	page_request.open('GET', siteUrl+'serveAjax.php?cart=1&action=removeall', true);
	page_request.send(null);
	return true;
}


function populateSpecialityProductSelect () {
		
	var page_request = ajaxinit();
	if (!page_request)
		return false;	
	document.getElementById('selectSpecialitysProduct').options.length = 0; //clear;
	document.getElementById('selectSpecialitysProduct').options[0] = new Option('ladataan...','0');
	
	selectedValue = document.getElementById('selectSpecialitysSpeciality').options[document.getElementById('selectSpecialitysSpeciality').selectedIndex].value;
	
	page_request.onreadystatechange=function(){
		loadselect(page_request, 'selectSpecialitysProduct');
	}
	page_request.open('GET', siteUrl+'serveAjax.php?spid='+selectedValue, true);
	page_request.send(null);
	return true;
}
	
function populateProductSpecialitySelect () {
	//PURKKAVIRITELMÄ
	
	var page_request = ajaxinit();
	if (!page_request)
		return false;	
	document.getElementById('selectProductsSpeciality').options.length = 0; //clear;
	document.getElementById('selectProductsSpeciality').options[0] = new Option('ladataan...','0');
	
	selectedValue = document.getElementById('selectProductsProduct').options[document.getElementById('selectProductsProduct').selectedIndex].value;
	
	page_request.onreadystatechange=function(){
		loadselect(page_request, 'selectProductsSpeciality');
	}
	page_request.open('GET', siteUrl+'serveAjax.php?pid='+selectedValue, true);
	page_request.send(null);
	
	return true;
}