function sack(file) {
	this.xmlhttp = null;

	this.resetData = function() {
		this.method = "POST";
  		this.queryStringSeparator = "?";
		this.argumentSeparator = "&";
		this.URLString = "";
		this.encodeURIString = true;
  		this.execute = false;
  		this.element = null;
		this.elementObj = null;
		this.requestFile = file;
		this.vars = new Object();
		this.responseStatus = new Array(2);
  	};

	this.resetFunctions = function() {
  		this.onLoading = function() { };
  		this.onLoaded = function() { };
  		this.onInteractive = function() { };
  		this.onCompletion = function() { };
  		this.onError = function() { };
		this.onFail = function() { };
	};

	this.reset = function() {
		this.resetFunctions();
		this.resetData();
	};

	this.createAJAX = function() {
		try {
			this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e1) {
			try {
				this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e2) {
				this.xmlhttp = null;
			}
		}

		if (! this.xmlhttp) {
			if (typeof XMLHttpRequest != "undefined") {
				this.xmlhttp = new XMLHttpRequest();
			} else {
				this.failed = true;
			}
		}
	};

	this.setVar = function(name, value){
		this.vars[name] = Array(value, false);
	};

	this.encVar = function(name, value, returnvars) {
		if (true == returnvars) {
			return Array(encodeURIComponent(name), encodeURIComponent(value));
		} else {
			this.vars[encodeURIComponent(name)] = Array(encodeURIComponent(value), true);
		}
	}

	this.processURLString = function(string, encode) {
		encoded = encodeURIComponent(this.argumentSeparator);
		regexp = new RegExp(this.argumentSeparator + "|" + encoded);
		varArray = string.split(regexp);
		for (i = 0; i < varArray.length; i++){
			urlVars = varArray[i].split("=");
			if (true == encode){
				this.encVar(urlVars[0], urlVars[1]);
			} else {
				this.setVar(urlVars[0], urlVars[1]);
			}
		}
	}

	this.createURLString = function(urlstring) {
		if (this.encodeURIString && this.URLString.length) {
			this.processURLString(this.URLString, true);
		}

		if (urlstring) {
			if (this.URLString.length) {
				this.URLString += this.argumentSeparator + urlstring;
			} else {
				this.URLString = urlstring;
			}
		}

		// prevents caching of URLString
		this.setVar("rndval", new Date().getTime());

		urlstringtemp = new Array();
		for (key in this.vars) {
			if (false == this.vars[key][1] && true == this.encodeURIString) {
				encoded = this.encVar(key, this.vars[key][0], true);
				delete this.vars[key];
				this.vars[encoded[0]] = Array(encoded[1], true);
				key = encoded[0];
			}

			urlstringtemp[urlstringtemp.length] = key + "=" + this.vars[key][0];
		}
		if (urlstring){
			this.URLString += this.argumentSeparator + urlstringtemp.join(this.argumentSeparator);
		} else {
			this.URLString += urlstringtemp.join(this.argumentSeparator);
		}
	}

	this.runResponse = function() {
		eval(this.response);
	}

	this.runAJAX = function(urlstring) {
		if (this.failed) {
			this.onFail();
		} else {
			this.createURLString(urlstring);
			if (this.element) {
				this.elementObj = document.getElementById(this.element);
			}
			if (this.xmlhttp) {
				var self = this;
				if (this.method == "GET") {
					totalurlstring = this.requestFile + this.queryStringSeparator + this.URLString;
					this.xmlhttp.open(this.method, totalurlstring, true);
				} else {
					this.xmlhttp.open(this.method, this.requestFile, true);
					try {
						this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
					} catch (e) { }
				}

				this.xmlhttp.onreadystatechange = function() {
					switch (self.xmlhttp.readyState) {
						case 1:
							self.onLoading();
							break;
						case 2:
							self.onLoaded();
							break;
						case 3:
							self.onInteractive();
							break;
						case 4:
							self.response = self.xmlhttp.responseText;
							self.responseXML = self.xmlhttp.responseXML;
							self.responseStatus[0] = self.xmlhttp.status;
							self.responseStatus[1] = self.xmlhttp.statusText;

							if (self.execute) {
								self.runResponse();
							}

							if (self.elementObj) {
								elemNodeName = self.elementObj.nodeName;
								elemNodeName.toLowerCase();
								if (elemNodeName == "input"
								|| elemNodeName == "select"
								|| elemNodeName == "option"
								|| elemNodeName == "textarea") {
									self.elementObj.value = self.response;
								} else {
									self.elementObj.innerHTML = self.response;
								}
							}
							if (self.responseStatus[0] == "200") {
								self.onCompletion();
							} else {
								self.onError();
							}

							self.URLString = "";
							break;
					}
				};

				this.xmlhttp.send(this.URLString);
			}
		}
	};

	this.reset();
	this.createAJAX();
}


var ajax = new sack();

function getCityList(sel)
{
	var countryCode = sel.options[sel.selectedIndex].value;
	document.getElementById('subcat').options.length = 0;	// Empty city select box
	if(countryCode.length>0){
		ajax.requestFile = 'ajax_cat.php?subcatId='+countryCode;	// Specifying which file to get
		ajax.onCompletion = createCities;	// Specify function that will be executed after file has been found
		ajax.runAJAX();		// Execute AJAX function
	}
}

function createCities()
{
	var obj = document.getElementById('subcat');
	eval(ajax.response);	// Executing the response from Ajax as Javascript code	
}

var ajaxx = new sack();

 function getGenreList(sel)
{
	var subcatCode = sel.options[sel.selectedIndex].value;
	var catCode = document.getElementById('cat')[document.getElementById('cat').selectedIndex].value;
	
	document.getElementById('genre').options.length = 0;	// Empty city select box
	if(subcatCode.length>0){
		ajaxx.requestFile = 'ajax_gen.php?catId='+catCode+'&genreId='+subcatCode;	// Specifying which file to get
		ajaxx.onCompletion = createGenres;	// Specify function that will be executed after file has been found
		ajaxx.runAJAX();		// Execute AJAX functio
	}
}

function createGenres()
{
	var obj = document.getElementById('genre');
	eval(ajaxx.response);	
}

function getSelectedValue(filu, name, name2)
{
	var value = document.getElementById(name)[document.getElementById(name).selectedIndex].value;
	var value2 = document.getElementById(name2)[document.getElementById(name2).selectedIndex].value;	
	ajaxx.requestFile = filu+'?value='+catCode+'&value2='+value;	// Specifying which file to get
	ajaxx.runAJAX();		// Execute AJAX functio
}

function LoadPage(page,usediv,loadtext) {
        // Set up request varible
        try {xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");}  catch (e) { alert("Error: Could not load page.");}
        //Show page is loading
        document.getElementById(usediv).innerHTML = '<p align=center><b>'+loadtext+'<b></p>';
        //scroll to top
        scroll(0,500);
        //send data
        xmlhttp.onreadystatechange = function(){
                //Check page is completed and there were no problems.
                if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
                       //Write data returned to page
                       document.getElementById(usediv).innerHTML = xmlhttp.responseText;
                }
        }
        xmlhttp.open("GET", page);
        xmlhttp.send(null);
        //Stop any link loading normaly
        return false;
}

/* checkall */
function checkAll(field)
{
for (i = 0; i < field.length; i++)
	field[i].checked = true ;
}

function uncheckAll(field)
{
for (i = 0; i < field.length; i++)
	field[i].checked = false ;
}
/* loppuu */

/* Show Div */
function toggleDiv( whichLayer )
{
  var elem, vis;
  if( document.getElementById ) // this is the way the standards work
    elem = document.getElementById( whichLayer );
  else if( document.all ) // this is the way old msie versions work
      elem = document.all[whichLayer];
  else if( document.layers ) // this is the way nn4 works
    elem = document.layers[whichLayer];
  vis = elem.style;
  // if the style.display value is blank we try to figure it out here
  if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)
    vis.display = (elem.offsetWidth!=0&&elem.offsetHeight!=0)?'block':'none';
  vis.display = (vis.display==''||vis.display=='block')?'none':'block';
}
function hideDiv(szDivID, iState) 
{
if(document.layers) 
{
document.layers[szDivID].visibility = iState ? "show" : "hide";
}
else if(document.getElementById) 
{
var obj = document.getElementById(szDivID);
obj.style.visibility = iState ? "visible" : "hidden";
}
else if(document.all) 
{
document.all[szDivID].style.visibility = iState ? "visible" : "hidden";
}
}

function goLite(FRM,BTN)
{
   window.document.forms[FRM].elements[BTN].style.backgroundColor = "#FFFF99";
}

function goDim(FRM,BTN)
{
   window.document.forms[FRM].elements[BTN].style.backgroundColor = "#CCFF66";
}

var f=document.forms(0); 
    // Boolen to track if error found 
var foundErr; 
    // Form element index number which the first error occured. 
var focusOn; 
     
function check_form() { 
	foundErr = false; focusOn = -1; 
        // Username field must be at least 6 chars. 
        if (f.user.value.length<6) { 
            alert ("Username too short."); 
            foundErr = true; focusOn = 0; 
	} 
     
	// Password field must be at least 6 chars. 
        if (f.pass.value.length<6) { 
            alert("Password too short"); 
            foundErr = true; 
            if (focusOn==-1) focusOn=1; 
        } 
     
        //Has any error occured? 
        if (foundErr) { 
            //Yes. Focus on which the first occurred. 
            f.elements.focus(focusOn); 
        } else { 
            // No. Submit the form. 
            f.submit(); 
        } 
}

/// uutisten xml haku ///
var CheckNews;

function alustaCheckNews()
{
	if(window.ActiveXObject)
	{
		// Jos kÃ¤ytetÃ¤Ã¤n Internet Exploreria:
		CheckNews = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else
	{
		// Jos kÃ¤ytetÃ¤Ã¤n muita selaimia:
		CheckNews = new XMLHttpRequest();
	}
}

function suoritaCheckNews()
{
	//var id = ids;

	alustaCheckNews();

	CheckNews.onreadystatechange = kasitteleCheckNews;

	CheckNews.open("GET", "news.xml", true);

	//CheckNews.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

	CheckNews.send(null);
}

function kasitteleCheckNews()
{
	var i = 0;
	var text = new Array();
	// Tarkistetaan, onko pyyntÃ¶ suoritettu kokonaan
	if(CheckNews.readyState == 4)
	{
		// Tarkistetaan, onko pyynnÃ¶n suoritus onnistunut:
		if(CheckNews.status == 200)
		{
		//alert("jou");
			while(CheckNews.responseXML.getElementsByTagName("id")[i] != null)
			{
				var titlet = CheckNews.responseXML.getElementsByTagName("title");
				var title = titlet[i].childNodes[0].nodeValue;

				var addedd = CheckNews.responseXML.getElementsByTagName("added");
				var added = addedd[i].childNodes[0].nodeValue;

				var bodyt = CheckNews.responseXML.getElementsByTagName("body");
				var body = bodyt[i].childNodes[0].nodeValue;

				text[i] = '<div class="news"><a href="javascript: klappe_news(\'a'+i+'\')"><img border="0" src="pic/minus.gif" id="pica'+i+'" alt="Show/Hide">'+added+' <b>'+title+'</b></a><div id="ka'+i+'" style="display: block;">'+body+'</div></div>';

				//document.getElementById("news"+i).innerHTML = '<div class="news"><a href="javascript: klappe_news("a'+i+'")"><img border="0" src="pic/minus.gif" id="pica'+i+'" alt="Show/Hide"> '+added+' <b>'+title+'</b></a></div>\n';
				//document.getElementById("news1").style.display == 'none';
				
				i++;
			}
			document.getElementById("news1").innerHTML = text[0];
			//document.news1 = text;
		}
		else
		{
			alert(CheckNews.statusText);
		}
	}
}

function confirmSubmit(msg)
{
var agree=confirm(msg+"\n\nOletko varma että haluat jatkaa?");
if (agree)
	return true ;
else
	return false ;
}

