//Cookies
function SetCookie(cookieName,cookieValue,nDays) {
	var today = new Date();
	var expire = new Date();
	if (nDays==null || nDays==0) nDays=1;
	expire.setTime(today.getTime() + 3600000*24*nDays);
	
	document.cookie = cookieName+"="+escape(cookieValue)+ ";expires="+expire.toGMTString()+";path=/";
}

function SetSessionCookie(cookieName,cookieValue) {
	document.cookie = cookieName+"="+escape(cookieValue);
}

function GetCookie(cookieName) {
	var search = cookieName + "="
	var returnvalue = "";
	if (document.cookie.length > 0) {
		offset = document.cookie.indexOf(search)
		if (offset != -1) { 
			offset += search.length
			end = document.cookie.indexOf(";", offset);
			if (end == -1) end = document.cookie.length;
			returnvalue = unescape(document.cookie.substring(offset, end))
		}
	}
	return returnvalue;
}

function DeleteCookie(cookieName){
	var cookie_date = new Date ( );
	cookie_date.setTime (cookie_date.getTime() - 1);
	document.cookie = cookieName += "=; expires=" + cookie_date.toGMTString();
}
//END Cookies

//LanguageSetter
function setLanguage(dropdown){
	LanguageID = dropdown.options[dropdown.selectedIndex].value;
	SetCookie('lang',LanguageID,365)	
	postdata = "lang="+LanguageID;
	
	var url = "/index.php?setlang";
	
	xmlHttpSetLang = makeHttp();
	xmlHttpSetLang.onreadystatechange = function() {
		if(checkReadyState(xmlHttpSetLang))
		{
			answer = xmlHttpSetLang.responseText;
			document.getElementById('languagetext').innerHTML = answer;
		}
	}
	SendPOSTRequest(xmlHttpSetLang,url,postdata);
}
//END LanguageSetter

//ShowHide
function ShowHide(id, allDivs, subdivs){
	if(document.getElementById(id).style.display == "none"){
		for(i=0; i<allDivs.length; i++){
			document.getElementById(allDivs[i]).style.display = "none";
		}
		if(subdivs != 0){
			for(i=1; i<=subdivs; i++){
				document.getElementById(id+'_'+i).style.display = "none";
			}
		}
		document.getElementById(id).style.display = "block";
	}
	else if(document.getElementById(id).style.display == "block"){document.getElementById(id).style.display = "none";}
	else if(document.getElementById(id).style.display == ""){document.getElementById(id).style.display = "none";}
	else{}
}

function ShowHideSub(id, toshow, total){
	if(document.getElementById(id+'_'+toshow).style.display == "none"){
		for(i=1; i<=total; i++){
			document.getElementById(id+'_'+i).style.display = "none";
		}
		document.getElementById(id+'_'+toshow).style.display = "block";
	}
	else if(document.getElementById(id+'_'+toshow).style.display == "block"){document.getElementById(id+'_'+toshow).style.display = "none";}
	else if(document.getElementById(id+'_'+toshow).style.display == ""){document.getElementById(id+'_'+toshow).style.display = "none";}
	else{}
}

function Toggle(id){
	if(document.getElementById(id).style.display == "none"){
		document.getElementById(id).style.display = "block";
	}
	else if(document.getElementById(id).style.display == "block"){document.getElementById(id).style.display = "none";}
	else if(document.getElementById(id).style.display == ""){document.getElementById(id).style.display = "none";}
	else{}
}
//END ShowHide

//Div Center
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll)
	return arrayPageScroll;
}

function getPageSize(){

	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else {
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;
	if (self.innerHeight) {
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) {
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}

	if(xScroll < windowWidth){
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
	return arrayPageSize;
}

function OverlayHeight(id){
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();
	var arrayPageScrollOffSet = GetScrollOffset();
	document.getElementById(id).style.height = (arrayPageSize[1] + 'px');
}

function CenterPicker(pickerid, height, width){
	arrayPageSize = getPageSize();
	arrayPageScroll = getPageScroll();
	arrayPageScrollOffSet = GetScrollOffset();
	
	var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - 35 - height) / 2);
	var lightboxLeft = ((arrayPageSize[0] - 40 - width) / 2);
	if(document.getElementById(pickerid)){
		document.getElementById(pickerid).style.top = (lightboxTop < 0) ? "0px" : lightboxTop + "px";
		document.getElementById(pickerid).style.left = (lightboxLeft < 0) ? "0px" : lightboxLeft + "px";
		document.getElementById(pickerid).style.height = height + "px";
		document.getElementById(pickerid).style.width = width + "px";
	}
}
function UpdateOverlays(){
	for(i=0; i<overlays.length; i++){
		OverlayHeight(overlays[i][0]);
		if(document.getElementById(overlays[i][0]+'inner')){
			CenterPicker(overlays[i][0]+'inner', overlays[i][1], overlays[i][2]);
		}
		else if(document.getElementById(overlays[i][0]+'Inner')){
			CenterPicker(overlays[i][0]+'Inner', overlays[i][1], overlays[i][2]);
		}
	}
	UpdateOverlaysInterval = window.setTimeout("UpdateOverlays()", 100);
}

//END Div Center
function GetScrollOffset(){
	var x,y;
	if (self.pageYOffset) // all except Explorer
	{
		x = self.pageXOffset;
		y = self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
		// Explorer 6 Strict
	{
		x = document.documentElement.scrollLeft;
		y = document.documentElement.scrollTop;
	}
	else if (document.body) // all other Explorers
	{
		x = document.body.scrollLeft;
		y = document.body.scrollTop;
	}
	ScrollOffset = new Array(x,y)
	return ScrollOffset;
}

function viewport(){
	var x,y;
	if (self.innerHeight) // all except Explorer
	{
		x = self.innerWidth;
		y = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
	{
		x = document.documentElement.clientWidth;
		y = document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		x = document.body.clientWidth;
		y = document.body.clientHeight;
	}
	return (x - 30) + 'px';
}

//Ajax init
function makeHttp()
{
	if(window.XMLHttpRequest)
	{
		return new XMLHttpRequest();
	}
	else if(window.ActiveXObject)
	{
		return new ActiveXObject("MSXML2.XMLHTTP");
	}
	else{
		return false;
	}
}

function SendGETRequest(ajaxobj,url_query)
{
	ajaxobj.open("GET",url_query,true);
	ajaxobj.send(null);
}

function SendPOSTRequest(ajaxobj,url_query,post_data)
{
	ajaxobj.open("POST",url_query,true);
	ajaxobj.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	ajaxobj.send(post_data);
}

function checkReadyState(obj)
{
	if(obj.readyState == 0) {}
	if(obj.readyState == 1) {}
	if(obj.readyState == 2) {}
	if(obj.readyState == 3) {}
	if(obj.readyState == 4)
	{
		if(obj.status == 200)
		{
			return true;
		}
		else if(obj.status == 404){}
		else{}
	}
}
//END Ajax init

//Color Picker
function HSV_TO_RGB (H, S, V){

	var RGB = Array()
	if(S == 0){
		var R = V * 255;
		var G = V * 255;
		var B = V * 255;
	}
	else{
		var var_H = H * 6;
		var var_i = Math.floor( var_H );
		var var_1 = V * ( 1 - S );
		var var_2 = V * ( 1 - S * ( var_H - var_i ) );
		var var_3 = V * ( 1 - S * (1 - ( var_H - var_i ) ) );

		if (var_i == 0) { var_R = V ; var_G = var_3 ; var_B = var_1 ; }
		else if (var_i == 1) { var_R = var_2 ; var_G = V ; var_B = var_1 ; }
		else if (var_i == 2) { var_R = var_1 ; var_G = V ; var_B = var_3 ; }
		else if (var_i == 3) { var_R = var_1 ; var_G = var_2 ; var_B = V ; }
		else if (var_i == 4) { var_R = var_3 ; var_G = var_1 ; var_B = V ; }
		else { var_R = V ; var_G = var_1 ; var_B = var_2 ; }

		var R = Math.round(var_R * 255);
		var G = Math.round(var_G * 255);
		var B = Math.round(var_B * 255);
	}
	RGB = Array(R,G,B);
	return RGB;
}

function PrintOutColorTable(xmax, ymax){

	hue = 0;
	value = 0;
	h_inc = 1/(ymax);
	s_inc = 1/(xmax/2);
	v_inc = 1/(xmax/2);

	document.write('<table cellspacing="0" cellpadding="0" border="0" style="cursor:pointer;cursor:hand">');
	document.write('<tr style="height:15px;width:184px;"><td style="border: 1px solid #000000;height:15px;width:184px;" id="smartpreview"></td></tr>');
	document.write('</table>');
								
	document.write('<table cellspacing="0" cellpadding="0" border="0" style="cursor:pointer;cursor:hand">');
	document.write('<tr><td><table cellspacing="0" cellpadding="0" border="0">');

	for (y = 0; y < ymax; y++){
		document.write('<tr height="2">');
		for (x = 0; x < xmax/2; x++){
			rgb = HSV_TO_RGB(hue, 1, value);
		    color = (((rgb[0] * 256) + rgb[1]) * 256) + rgb[2];
		    colorHex = '000000' + color.toString(16).toUpperCase();
		    colorHex = colorHex.substring(colorHex.length-6,colorHex.length);
		    cellCode = ' onMouseDown="SetColor(\'#' + colorHex + '\');" onMouseOver="ChangeColor(\'#' + colorHex + '\')"'
		    document.write('<td width="2" style="background-color: #' + colorHex + ';" ' + cellCode + '></td>');
			value += v_inc;
		}
		value = 0;
		hue += h_inc;
		document.write('</tr>');
	}

	document.write('</table></td><td><table cellspacing="0" cellpadding="0" border="0">');

	hue = 0;
	sat = 1;
	for (y = 0; y < ymax; y++){
		document.write('<tr height="2">');
		for (x = xmax/2; x < xmax; x++){
			rgb = HSV_TO_RGB(hue, sat, 1);
		    color = (((rgb[0] * 256) + rgb[1]) * 256) + rgb[2];
		    colorHex = '000000' + color.toString(16).toUpperCase();
		    colorHex = colorHex.substring(colorHex.length-6,colorHex.length);
		    cellCode = ' onMouseDown="SetColor(\'#' + colorHex + '\');" onMouseOver="ChangeColor(\'#' + colorHex + '\')"'
		    document.write('<td width="2" style="background-color: #' + colorHex + ';" ' + cellCode + '></td>');
			sat -= s_inc;
		}
		sat = 1;
		hue += h_inc;
		document.write('</tr>');
	}
	document.write('</table></td><tr></table>');

}