//** Copyright Tuunix Oy 2009
// Kopiointi kielletty
// verkkokaupan polku korjattava tähän, tai muuttujaksi

/*verkkokaupn popuppi */
function verkkokauppapopup(tuoteid, optiot) {
			if(!optiot){ optiot='';}
			window.open("vk_tuote.php?tuote_id="+tuoteid+"&opt="+optiot,"","resizable=1 toolbar=no, resizable=yes, scrollbars=yes, height=420,width=400, screenX=160,screenY=100,top=100,left=160");
			}
			
			
function isInt (i) 
	{
	return (i % 1) == 0;
	}

function addtocart(product_id)  /* Tätä kutsutaan add2cart napilla, parametrinä product_id */
	{
       if(product_id=='-1' || product_id=='-2')
		{
		var tyhjays=confirm('Haluatko varmasti tyhjätä korin?')
		if (tyhjays==false)return false;
		}
	   
	   if (isInt(product_id))
			{
			/* tulevaisuuden varalle, voi olla tarkastuksia tässä */
			check_product_id(product_id);
			}
		else alert("Tuotten ID ei ollut oikein");
    return 0;
	}
	
function check_product_id(product_id) 
{
	//optiotuote 
	var optiontext='';
	if(document.getElementById('optio'+product_id))
		{
		
		var optionindex = document.getElementById('optio'+product_id).selectedIndex;
		optiontext = document.getElementById('optio'+product_id)[optionindex].value;
		optiontext=decodeURI(optiontext);
		optiontext='&optiontext='+optiontext;
		
		/*alert(optiontext);*/
		
		}





	//clearTableRows("restable");
	var httpRequest = null;
	if (window.XMLHttpRequest) 
	{ //firefoxille
		httpRequest = new XMLHttpRequest();
		if (httpRequest.overrideMimeType) 
			{
			httpRequest.overrideMimeType('text/xml');
			}
	} 
	else if (window.ActiveXObject) 
	{ //iexplorer
		try 
			{
			httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
			} 
		catch (e) 
			{
			try
				{
				httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
				} 
			catch (e) 
				{}
			}
	}

	if (!httpRequest) 
		{
			alert('XMLHTTP :n luonti epäonnistui');
			return false;
		}
	
		
	var cart_url = "verkkokauppa/mini_ajax.php" + "?product_id=" + product_id +optiontext;
	
	var paluuarvo='';
	
	httpRequest.abort();
	httpRequest.open("GET", cart_url, true);
	httpRequest.onreadystatechange=function() 
	{
    if(httpRequest.readyState == 4) 
		{
		//var paluuarvo2= httpRequest.responseText;
		//alert (paluuarvo2);
		//paluuarvo = httpRequest.responseXML.getElementsByTagName('Message')[0].firstChild.data;
		var response_status = httpRequest.responseXML.getElementsByTagName('Status')[0].firstChild.data;
		//paluuarvo = httpRequest.responseXML.getElementsByTagName('Message')[0].firstChild..nodeValue;
		var x=httpRequest.responseXML.getElementsByTagName("Koriviesti");
		paluuarvo=(x[0].getElementsByTagName("Message")[0].childNodes[0].nodeValue);
		
		response_status=parseInt(response_status); //intiksi
		
		
		
		switch(response_status)
			{
			case 1:
			alert('Tuote on lisätty koriin');
			break;
			
			case 10:
			alert("Tuote on lisätty koriin\nPääset kassalle klikkaamalla ostoskoria sivun ylhäältä\nVoit muokata tuotemääriä kassalla");
			/* kelataan sivun alkuun */
			scroll(0,0);
			break;
			
			case 2:
			alert('Tuotetta ei ollut varastossa');
			break;
			case 3:
			alert('Tuote ei ole tällä hetkellä saatavilla');
			break;
			case 4:
			alert('Tuote ID väärin');
			break;
			case 5:
			alert('Ostoskori on tyhjätty');
			break;
			case 6:
			alert('Tuote poistettiin korista');
			break;
			
			default:
			alert('Tuntematon virhe');
			break;
			}//switch
		
		//kirjoitetaan koridiviin
		if(document.getElementById('minicart').style.display !='block' )
			{
			//tooltip teksti lisätään
			document.getElementById('minicart').style.display = 'block';
			}
		//document.getElementById('minicart').innerHTML =paluuarvo;
		paluuarvo=paluuarvo +'<span>Näytä ostoskori</span>';
		//alert(paluuarvo);
		document.getElementById('showcartlink').innerHTML =paluuarvo;
		}
	}
	httpRequest.send(null);
return 0;
}
/* check_product_id LOPPU */

/*luokan lisäys */
function addcategory(myselectid)
{

var myselect=document.getElementById(myselectid)

var kategorianimi=prompt("Anna nimi valinnalle,\nEi erikoismerkkejä!","");

if(kategorianimi.length > 30)kategorianimi=kategorianimi.substr(0, 30);
if(kategorianimi != '')
	{
	try
		{
		 //myselect.add(new Option(kategorianimi, "4"), null) //add new option to end of "sample"
		 myselect.add(new Option(kategorianimi, "0"), myselect.options[0]) //add new option to beginning of "sample"
		 myselect.options[0].value=kategorianimi;
		}
	catch(e)
		{ //in IE, try the below version instead of add()
		 //myselect.add(new Option("New Last Option", "4")) //add new option to end of "sample"
		 myselect.add(new Option(kategorianimi, "0"), 0) //add new option to beginning of "sample"
		 myselect.options[0].value=kategorianimi;
		}
		
	myselect.options[0].selected=kategorianimi;
	}


}
/* addcategory LOPPU */

function backnappi()
{ 
   var myurl = "verkkokauppa"; 
   if(document.referrer.indexof(myurl) != -1) 
    {  document.location.href=document.referrer;  } 
  else 
    {  document.location.href='index.php';  } 
}

/* kassasivun formiin */

function kassa1lomakemuuttui()
{
var submitnappi=document.getElementById('kassa1submitnappi');
submitnappi.value='Tallenna muutokset';

var saajatkaa=document.getElementById('saajatkaa1');
saajatkaa.value='0';

}



function flashh(s){
	document.getElementById(s).style.backgroundColor="#ff0000";	
}

function flashhoff(s){
	document.getElementById(s).style.backgroundColor="#ffffff";	
}

function vilkuta(divid)
{
flashh(divid);
var dividunique=divid;
//setTimeout(flashhoff(divid),700);	
setTimeout(function(){flashhoff(dividunique);},800); 
}


var vkvalidaatioalert=false;
function validate_form1()
{
var jatkaform=true;

arvo=document.getElementById('maksutapa').value;
if (arvo==0||arvo==""){
vilkuta('maksutapa');
jatkaform=false;
}

arvo=document.getElementById('toimitustapa').value;
if (arvo==0||arvo==""){
vilkuta('toimitustapa');
jatkaform=false;
}

if(vkvalidaatioalert==true && jatkaform==false)alert("Et valinnut kaikkia kenttiä!");
if(jatkaform==false)vkvalidaatioalert=true;
return jatkaform;

}


/* hallintapuolella */
function moveDown(itemlistid) {
var itemList=document.getElementById(itemlistid);
if (itemList.length == -1) {
return false; // List is empty
alert('Lista on tyhjä!');
}
else if (itemList.length == 0) {
return false; // List is empty
alert('Lista on tyhjä! 0');
}

var selected = itemList.selectedIndex;
if (selected == -1) {
alert('Ei valittuna mitään');
return false; // Nothing selected
}

//Convert from an HTMLCollection to an Array
var optionsOrginal = new Array(itemList.options.length);
for (i = 0; i < itemList.options.length; i++) {
optionsOrginal[i] = itemList.options[i];
}

var optionsFinal = new Array();
var selectedItems = new Array();

while (optionsOrginal.length != 0){
var hd = optionsOrginal.shift(); //Shift the first element off the array

if (hd.selected) {
selectedItems.push(hd); //Push the item onto the selected elements
} else {
optionsFinal.push(hd); //Push the iten to the result array
optionsFinal = optionsFinal.concat(selectedItems); //Then concat the selected items
selectedItems.length = 0; //Reset the selected items
}
}
optionsFinal = optionsFinal.concat(selectedItems); //We fell off the end make sure to add any selected items

//Copy it back to the HTMLCollection
for (i = 0; i < optionsFinal.length; i++) {
itemList.options[i] = optionsFinal[i];
}


return false;
} 


