// JavaScript Document
	//general functions
	openMenuList = new Array();
function sendParam(paramname,param){
//	alert(param);
	$.get(iurl,{asetparam:paramname, 'paramvalue[]':param,PHPSESSID:sessid});
	}
	function init()
{
  document.onmousemove = getMousePos; // update(event) implied on NS, update(null) implied on IE
  if(window.Event && document.captureEvents)
	document.captureEvents(Event.MOUSEMOVE);
	getMousePos();
}
var currentVersion = null;
var productVersionAttributeCounter = null;
	var posX = 0;
	var posY = 0;
			
			function getMousePos(e)
			{
				if (!e)
				var e = window.event||window.Event;
				
				if('undefined'!=typeof e.pageX)
				{
				posX = e.pageX;
				posY = e.pageY;
				}
				else
				{
				posX = e.clientX + document.body.scrollLeft;
				posY = e.clientY + document.body.scrollTop;
				
				}
			}

	function getPage(id){
	agent.call('_ajax.php','getpagecontents','displaypagecontents',id);
	}
	function displaypagecontents(contents){
		if(contents != 'false'){
			$("#ajaxContent").empty();
			$("#ajaxContent").append(contents);
			
			$("#ajaxContentContainer").fadeTo('normal',0.9);
return(false);			
		}
	}
	function hide(field){
		if(getElement(field)){
			target = getElement(field);
			target.style.display='none';
			target.style.visibility='hidden'
			}
		}
	function display(field){
		if(getElement(field)){
			//alert('displaying '+field);
			target = getElement(field);
			target.style.display='block';
			target.style.visibility='visible';
			}
		}
		
  // client functions  
  	function checkForUserEmail(email){
		  agent.call('_ajax.php','checkifuseremailexists','callback_email',email);
	}
    function checkForUser(email,pass) {
		//debug alert('tsekataan sun tunnarit');
       agent.call('_ajax.php','checkuserlogin','callback_login',email,pass);
    }
    function getProductInformation(pid){
	//	alert('calling getproduct with id '+pid);
		agent.call('_ajax.php','getProductInformation','callback_productinfo',pid);
		}
	function getImage(iid,size){
		generateItemBox('Ladataan kuvaa..');
		agent.call('_ajax.php','getImage','showImage',iid,size);
		}
	// client callback functions
	 function callback_email(result) {
	//	 alert(result);
		if(result!='false' && result){
			//N&auml;ytet&auml;&auml;n salasanakentt&auml;
			
			hide('NewUserPasswordFields');
			display('LoginPasswordField');
			getElement('LoginPasswordField').focus();
			//alert('S&auml;hköpostiosoite löytyi tietokannasta - '+result);
			}
		else{
			//N&auml;ytet&auml;&auml;n salasana-kent&auml;t jos k&auml;ytt&auml;j&auml; haluaa tallentaa tietonsa
			hide('LoginPasswordField');
			display('NewUserPasswordFields');
			//alert('Uusi s&auml;hköpostiosoite');
			}
    }
    function callback_login(resultset) {
		if(resultset && resultset!='false'){
			//T&auml;ytet&auml;&auml;n kaavakkeen tiedot, juhlaa \o/
			if(resultset.sender != 'undefined')  getElement('order_sender').value = resultset.sender;
			if(resultset.phone && resultset.phone != 'undefined')   getElement('phone').value = resultset.phone;
			if(resultset.address != 'undefined') getElement('order_address').value = resultset.address;
			//getElement('').value = resultset.;
			}
		else alert('K&auml;ytt&auml;j&auml;tunnus ja salasana eiv&auml;t t&auml;sm&auml;nneet');
    }
	function callback_productinfo(resulthtml){
		//alert('got result?'+resulthtml);
		
		if(resulthtml){
			showItem(resulthtml);
			
			}
	}
	function showImage(resulthtml){
		if(resulthtml && resulthtml != 'false')
			showItem(resulthtml);
		}
/* Item Display */
			
			
	
function setItemPosition(itemID){
			//	getMousePosition();
				//alert(posX+' - '+posY);
				getElement(itemID).style.pixelLeft = posX - 150;
				getElement(itemID).style.pixelTop = posY-200;
				getElement(itemID).style.top = posY-200;
				getElement(itemID).style.left = posX-150;
			//	alert(getElement(itemID).style.pixelLeft);
				}
function showItem(contents){
		generateItemBox(contents);
			if(getElement('imageContainerDiv').style.visibility=='hidden')
			setItemPosition('imageContainerDiv');
			display('imageContainerDiv');
			}
function generateItemBox(contents){
				getElement('ImageDiv').innerHTML=contents;
			
				}
var targetAttributeSelector = null;
function displayImage(src,target,width,alt){
//	alert('displayImage:'+src+','+target+','+width);
	if(arguments[4]){ 
		$('.propertyInfoOnImage a').attr('href',arguments[4]);
		$('#'+target).empty().html('<a href="'+arguments[4]+'" class="thickbox"><img border="0" src="'+src+'" alt="'+alt+'"></a>');
		tb_init('a.thickbox, area.thickbox, input.thickbox');
	}
else	$('#'+target).empty().html('<img src="'+src+'" alt="'+alt+'">');
}
function createNewAttributeName(attribute){
	$('#propertySelector_'+attribute).hide();
	$('#newAttributeField_'+attribute).show();
}
function addProductAttribute(attribute,attributename){
	var targetAttributeSelector = 'attributenameselect';
	$('#'+targetAttributeSelector).addOption(attributename,attributename);
	$('#'+targetAttributeSelector).selectOptions(attributename,true);
		$('#propertySelector_'+attribute).show();
		$('#newAttributeField_'+attribute).hide();
}
function removePAttribute(id){
	$('#attributerow_'+id).remove();
	$('#productversionform').append('<input type="hidden" name="removeattributeidlist[]" value="'+id+'">');
	}
function cancelNewAttribute(p){
	var version = v;
	var attribute = p;
	$('#propertySelector_'+attribute).show();
	$('#newAttributeField_'+attribute).hide();
}
function CreateAttributeLine(counterStart){

	if(productVersionAttributeCounter ==null) 
		productVersionAttributeCounter = counterStart;
	else productVersionAttributeCounter++;
	var newname = $('#attributenameselect').selectedValues();
//	if(typeof newname =='object') newname = newname[0];
	var newline = '<tr><td><input class="productVersionAttributeInput" type="text" name="attributename_'+productVersionAttributeCounter+'" value="'+newname[0]+'"></td>'+
	'<td><input type="text" name="attributevalue_'+productVersionAttributeCounter+'" value="'+$('#attributevalueselect').val()+'"></td>'+
	'<td><input id="attrimage'+productVersionAttributeCounter+'" type="hidden" name="attributeimage_'+productVersionAttributeCounter+'2" value="" />'+
   '<div id="attrimagepreview'+productVersionAttributeCounter+'"></div>'+
   '<a href="javascript:" onclick="selectImage(\'attrimage'+productVersionAttributeCounter+'\',\'attrimagepreview'+productVersionAttributeCounter+'\'); return(false);">Choose</a></td>'+
	'</tr>';
	$('#productAttributes').append(newline);
	$('#attributevalueselect').val('');
}
function createNewProductVersion(counterStart,productID,versionID){
	if(currentVersion==null) currentVersion=counterStart;
	else currentVersion++;
	$('#productVersions').append('<tr><td id="version'+currentVersion+'"></td></tr>');
	$('#version'+currentVersion).load('htmlContent/newVersionForm.php?pid='+productID+'&v='+currentVersion+'&clone='+versionID,versionformloaded);
	}
	function versionformloaded(){
	$('.newAttributeInputField').hide();
	}
function openImageAddView(){
	$('#imageBankDiv').load('index.php?admin_page=imageAdd',function(){
			$('#imageBankDiv').fadeIn();
			 });
	}
function updateImageAddView(folder){
	$('#imageAddCategoryField').load('index.php?admin_page=imageAdd&selectimageaddfolder='+folder);
}
function addSelectedImage(imageid){
	$('#'+targetFieldId).val(imageid);
	targetFieldId=null;
	$('#imageBankDiv').fadeOut();
	if(displayField!=null){
			$('#'+displayField).load('index.php?admin_page=imageAdd&selectedimage='+imageid+'&size=150');
		displayField=null;
	}
	
}
var targetFieldId = null;
var displayField = null;
function selectImage()
	{
	targetFieldId = arguments[0];
	if(arguments[1])  displayField = arguments[1];
	 openImageAddView();
	}
	
var productversionprice = null;
var sendattributes = new Array();
var pricequery = null;
var quantityquery = null;
var itemprice = new Array();

function calctotalprice(q,pid)
	{
	if(!itemprice[pid] && arguments[2]) itemprice[pid] = arguments[2];
	if(itemprice[pid] && itemprice[pid]!='undefined')
		{
		$('#totalpricediv_'+pid).html(itemprice[pid]*q+'&euro;');
		}
	}

function getProductVersionPrice(productid)
	{
		
	sendattributes = '';
	var datatosend = $('#productAttributes_'+productid+' select[name*=product_attribute],#productAttributes_'+productid+' input[name*=product_attribute]').serialize();
	if(pricequery && pricequery!=null)
	pricequery.abort();
	pricequery = $.getJSON( "getversionprice.php?"+datatosend, { 'product':productid }, function( data)
		{
		itemprice[productid]=data.price;
		$("#versionPrice_"+productid).empty().append( data.priceinfo );
		$("#priceAddInfo_"+productid).empty().append( data.priceinfo );
		calctotalprice($('#productquantity_'+productid).val(),productid);
		});
}

function getProductVersionQuantity(productid)
	{
	sendattributes = '';
	var datatosend = $('#productAttributes_'+productid+' select[name*=product_attribute],#productAttributes_'+productid+' input[name*=product_attribute]').serialize();
	if(quantityquery && quantityquery != null)
		quantityquery.abort();
	quantityquery =	$.get( "getversionquantity.php?"+datatosend, {'product':productid }, function(data ){
		$("#productAvailability_"+productid).empty().append( data );

		});
}


function fillRegistrationFromCustomerInfo(){
	$('#registration_firstname').val($('#customer_firstname').val());
	$('#registration_lastname').val($('#customer_lastname').val());
	$('#registration_email').val($('#customer_email').val());
	$('#registration_phone').val($('#customer_phone').val());
//	$('').val($('').val());
}
function myrandom() {
var  min = arguments[0];
var max = arguments[1];
    return Math.floor(Math.random() * (max - min + 1)) + min;
}
var currentLayer = 1;

