/* fogantyuk */
function getHandles(catName,id,fitting_question){
	var catid = parseInt(catName.substring(catName.lastIndexOf("_")+1));
    if(fitting_question==undefined){
        fitting_question = id;
    }
    
    if(!fitting_question){
        shoxBox('Would like us to install your order?<br> If yes, please select your fitting option on the next page, enter fitting, then add to your cart when you return to this page. If you choose no the product will be added to your basket.<br /><br /><a href="javascript: saveAccessoriesQuantity(\''+catid+'\',\''+catName+'\')">Yes</a>&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;<a href="javascript: void(0)" onclick="getHandles(\'' + catName + '\',' + id + ',' + true + ')">No'); return false;
    }
    
	var all = 0;
	var products = $("#" + catName).serialize();
	products = products.split(/&separator=separator/);
	for(var i=0; i<products.length;i++){
		var quantity = parseInt(products[i].substring(products[i].lastIndexOf("q=")+2));
		if(!isNaN(quantity) && quantity>0){
			$.post("add_basket.php", products[i]);
			all++;
		}
	}
	if(all > 0)
	{
		document.getElementById("viewbasket").style.display = "";
		shoxBox('Successfully added to Basket!');
	}
	else
	{
		document.getElementById("viewbasket").style.display = "none";
		shoxBox('Please set a quantity first!');
	}
}

/* szinmintak */
function getColourSample(formName){
	
	var all = 0;
	var products = $("#" + formName).serialize();
	
	arrProducts = products.split(/&/);
	
	if(arrProducts.length > 2)
	{
		var temp = arrProducts[0]+"&"+arrProducts[1]+"&q=1&flag=1";
		for(var i=2;i<=arrProducts.length-1;i++)
		{
			color = arrProducts[i].split("=");
			temp += "&Colour"+(i-1)+"="+color[1];
		}
		products = temp;
		
		$.post("add_basket.php", products,
		function(){
			document.getElementById("viewbasket").style.display = "";
			shoxBox('Successfully added to Basket!');
		});
		
	}
	else
	{
		document.getElementById("viewbasket").style.display = "none";
		shoxBox('Please select at least one colour!');
	}
}

/* egyeb termekek */
function getAccssories(formName,fitting_question){
    var prodid = parseInt(formName.substring(formName.lastIndexOf("_")+1));
	if(eval('document.'+formName+'.height != null')) // ha van height mezo akkor ellenorzes
	{
		var value_height = Number(eval('document.'+formName+'.height.value'));
		
		if(isNaN(value_height) || value_height < 1)  // if there isn't a number in height field
		{
			eval('document.'+formName+'.height.value = ""');
			document.getElementById("viewbasket").style.display = "none";
			shoxBox('Please fill the Height field first!');
			return false;
		}
		else
		{
			str_height = "&height="+value_height;
		}
	}
	else
	{
		str_height = "";
	}
	
	if(eval('document.'+formName+'.width != null')) // ha van width mezo akkor ellenorzes  
	{
		var value_width = Number(eval('document.'+formName+'.width.value'));
		
		if(isNaN(value_width) || value_width < 1)  // if there isn't a number in width field
		{
			eval('document.'+formName+'.width.value = ""');
			document.getElementById("viewbasket").style.display = "none";
			shoxBox('Please fill the Width field first!');
			return false;
		}
		else
		{
			str_width = "&width="+value_height;
		}
	}
	else
	{
		str_width = "";
	}
	
	if(eval('document.'+formName+'.long != null')) // ha van long mezo akkor ellenorzes  
	{
		var value_long = Number(eval('document.'+formName+'.long.value'));
		
		if(isNaN(value_long) || value_long < 1)  // if there isn't a number in long field
		{
			eval('document.'+formName+'.long.value = ""');
			document.getElementById("viewbasket").style.display = "none";
			shoxBox('Please fill the Long field first!');
			return false;
		}
		else
		{
			str_long = "&long="+value_long;
		}
	}
	else
	{
		str_long = "";
	}
	
	if(eval('document.'+formName+'.lengt != null')) // ha van length mezo akkor ellenorzes  
	{
		var value_length = Number(eval('document.'+formName+'.lengt.value'));
		
		if(isNaN(value_length) || value_length < 1)  // if there isn't a number in length field
		{
			eval('document.'+formName+'.lengt.value = ""');
			document.getElementById("viewbasket").style.display = "none";
			shoxBox('Please fill the Length field first!');
			return false;
		}
		else
		{
			str_length = "&length="+value_length;
		}
	}
	else
	{
		str_length = "";
	}
    
    if(!fitting_question){
        /*var php = window.location.href.substring(window.location.href.lastIndexOf("/")+1);
        php = php.substring(0,php.indexOf(".php"));*/
        
        shoxBox('Would like us to install your order?<br> If yes, please select your fitting option on the next page,  enter fitting, then add to your cart when you return to this page.  If you choose no the product will be added to your basket.<br /><br /><a href="javascript: _submit(\'fitting.php?' + (!isNaN(prodid) ? 'prodid=' + prodid + '&' : '') +  ' returnto=accessories.php\',\'' + formName + '\')">Yes</a>&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;<a href="javascript: void(0)" onclick="getAccssories(\'' + formName + '\',' + true + ')">No'); return false;
    }
	
	var products = $("#" + formName).serialize(); 
	var product_id = eval('document.'+formName+'.prodid.value'); 
	var color = !eval( 'document.'+formName+'.primed_only.checked' );
	var price = false;
	
	/* a kuldendo adatokban a prodid-t atnevezem id-re /ez kell az add basket-hez/ */
	products = products.replace("prodid","id");
	
	/* a kuldendo adatokbol kitorlom a Price_Primed-t es ha nem festetett rendelt akkor beallitom a kuldendo arat */
	var fieldname = "Price_Primed";
	if(eval('document.'+formName+'.'+fieldname+' != null'))  
	{
		var fieldvalue = eval('document.'+formName+'.'+fieldname+'.value');
		products = products.replace(fieldname+"="+fieldvalue,"");
		if(!color)
		{
			var price = fieldvalue;
		}
	}
	
	/* a kuldendo adatokbol kitorlom a Price_Coloured-t es ha festetett rendelt akkor beallitom a kuldendo arat */
	var fieldname = "Price_Coloured";
	if(eval('document.'+formName+'.'+fieldname+' != null'))  
	{
		var fieldvalue = eval('document.'+formName+'.'+fieldname+'.value');
		products = products.replace(fieldname+"="+fieldvalue,"");
		if(color)
		{
			var price = fieldvalue;
		}
	}
		
	/* cserelem a dupla & es _ karaktereket &-re illetve urse space-re, valamint ha van, akkor torlom a karakterlanc elejen es vegen levo & jeleket */
	products = products.replace(/&{2,}/g,'&');
	if(products.charAt(0) == '&')
	{
		products = products.substr(1);
	}
	if(products.substr(products.length - 1) == '&')
	{
		products = products.substr(0, products.length - 1);
	}
	
	var quantity = parseInt(products.substring(products.lastIndexOf("q=")+2));
	if(!isNaN(quantity) && quantity>0) // if the quantity is a number
	{
		if(price) // ha van ara a termeknek, vagyis nem a prices tablabol kell lekerni akkor atadom a basketnak
		{
			products += "&Price=" + price;
			
			$.post("add_basket.php", products,
			function(){
				document.getElementById("viewbasket").style.display = "";
				shoxBox('Successfully added to Basket!');
			});
		}
		else
		{
			datas = "id="+product_id+str_width+str_height+str_length+str_long+"&color="+color;
			$.post("calculate_price.php", datas,
			function(result){ // reult[0] = true /false; result[1] = lekerdezett ar / hibauzenet
				result = result.split("&");
				
				if(result[0] == 'true') // ha sikeres volt az ar lekerdezes
				{
					products += "&Price=" + result[1];
					
					$.post("add_basket.php", products,
					function(){
						document.getElementById("viewbasket").style.display = "";
						shoxBox('Successfully added to Basket!');
					});
				}
				else // ha nem volt sikeres az ar lekerdezes
				{
					document.getElementById("viewbasket").style.display = "none";
					shoxBox(result[1]);
				}
			});
		}
	}
	else
	{
		document.getElementById("viewbasket").style.display = "none";
		shoxBox('Please set a quantity first!');
	}   
}

/* ajtok es fiokok */
function getDoors(formName, paint_id, paint_price, hinge_id, hinge_price,fitting_question)
{
    if(fitting_question==undefined){
        fitting_question = hinge_id;
    }

	
	/* ha nincs kitoltve a magassag mezo akkor hibauzenet*/
	if(eval('document.'+formName+'.height != null')) 
	{
		var value_height = parseInt(eval('document.'+formName+'.height.value'));
		
		if(isNaN(value_height) || value_height < 1) 
		{
			eval('document.'+formName+'.height.value = ""');
			document.getElementById("viewbasket").style.display = "none";
			shoxBox('Please fill the Height field first!');
			return false;
		}
	}
	
	/* ha nincs kitoltve a szelesseg mezo akkor hibauzenet*/
	if(eval('document.'+formName+'.width != null'))  
	{
		var value_width = parseInt(eval('document.'+formName+'.width.value'));
		
		if(isNaN(value_width) || value_width < 1)
		{
			eval('document.'+formName+'.width.value = ""');
			document.getElementById("viewbasket").style.display = "none";
			shoxBox('Please fill the Width field first!');
			return false;
		}
		else
		{
			/* ha van minimum szelesseg megadva akkor azt is at kell adni feldolgozasra */
			var fieldname = "minwidth";
			if(eval('document.'+formName+'.'+fieldname+' != null'))  
			{   
				var min_width = eval('document.'+formName+'.'+fieldname+'.value');
				if(Number(min_width) > value_width)
				{
					document.getElementById("viewbasket").style.display = "none";
					shoxBox('This size is not possible with framed doors please order a Plain & Flat doors!');
					return false;
				}
			}
		}
	}
    
        
	
	var all = 0; // ajtoknal ha nem csak egy quantity mezo van /kulonbozo osztasu ajtok leteznek/ akkor ez tarolja az osszes darabszamot. ha 0 akkor nem rendeltek egy darabot sem -> hibauzenet
	var door = false; // ha a termek ajto akkor kesobb true lesz
	var products = $("#" + formName).serialize();
	var product_id = eval('document.'+formName+'.prodid.value'); 
	var is_color = !eval( 'document.'+formName+'.primed_only.checked' );
	
	/* a kuldendo adatokbol kitorlom a kategoria id-t es a termek id nevet atnevezem id-re /ez kell az add basket-hez/ */
	var fieldname = "id";
	var fieldvalue = eval('document.'+formName+'.'+fieldname+'.value');
	products = products.replace("id="+fieldvalue,"");
	products = products.replace("prodid","id");
	
	/* ajtoknal /fioknal nincs zsaner/ a kuldendo adatokbol kitorlom a zsanerokat mert az nem opciokent lesz elkuldve, hanem kulon termmekkent */
	var fieldname = "quantity_of_hinges_per_pair";
	if(eval('document.'+formName+'.'+fieldname+' != null'))  
	{
		var fieldvalue = eval('document.'+formName+'.'+fieldname+'.value');
		products = products.replace(fieldname+"="+fieldvalue,"");
	}
	
	/* a kuldendo adatokbol kitorlom a festekeket mert az nem opciokent lesz elkuldve, hanem kulon termmekkent */
	var fieldname = "tin_of_touch_up_paint";
	if(eval('document.'+formName+'.'+fieldname+' != null'))  
	{
		var fieldvalue = eval('document.'+formName+'.'+fieldname+'.value');
		products = products.replace(fieldname+"="+fieldvalue,"");
	}
	
	/* a kuldendo adatokbol kitorlom a minimum szelesseget mert erre nincs szukseg a tovabbiakban */
	var fieldname = "minwidth";
	if(eval('document.'+formName+'.'+fieldname+' != null'))  
	{
		var fieldvalue = eval('document.'+formName+'.'+fieldname+'.value');
		products = products.replace(fieldname+"="+fieldvalue,"");
	}
		
	/* cserelem a dupla & es _ karaktereket &-re illetve urse space-re, valamint ha van, akkor torlom a karakterlanc elejen es vegen levo & jeleket */
	products = products.replace(/&{2,}/g,'&');
	if(products.charAt(0) == '&')
	{
		products = products.substr(1);
	}
	if(products.substr(products.length - 1) == '&')
	{
		products = products.substr(0, products.length - 1);
	}
	
	datas = "id="+product_id+"&width="+value_width+"&height="+value_height+"&color="+is_color; 
	
	$.post("calculate_price.php", datas,
	function(result){  // result[0] = true /false; result[1] = lekerdezett ar / hibauzenet
		result = result.split("&");
		
		if(result[0] == 'true') // ha sikeres volt az ar lekerdezes 
		{
            if(!fitting_question){
                shoxBox('Would like us to install your order?<br> If yes, please select your fitting option on the next page,  enter fitting, then add to your cart when you return to this page. If you choose no the product will be added to your basket.<br /><br /><a href="javascript:void(0)" onclick="_submit(\'fitting.php\',\''+formName+'\');">Yes</a>&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;<a href="javascript: void(0)" onclick="getDoors(\'' + formName + '\',' + paint_id + ',' + paint_price + ',' + hinge_id + ',' + hinge_price + ',' + true + ')">No'); return false;
            }
			var price = result[1];
			var fieldvalue = Number(eval('document.'+formName+'.q.value'));
			
			if(!isNaN(fieldvalue) && fieldvalue > 0)
			{
				var quantity = fieldvalue;
			}
			else
			{
				eval('document.'+formName+'.q.value = ""');
				document.getElementById("viewbasket").style.display = "none";
				shoxBox('Please set a quantity first!');
				return false;
			}
			
			products += "&Price=" + price;
			$.post("add_basket.php", products);
			
			/* ha meg lett adva a festeknel darabszam akkor az kuldes kulon termekkent */
			if(eval('document.'+formName+'.tin_of_touch_up_paint != null'))  
			{
				var fieldvalue = Number(eval('document.'+formName+'.tin_of_touch_up_paint.value'));
				if(!isNaN(fieldvalue) && fieldvalue > 0)
				{
					var color = eval('document.'+formName+'.color.value');  
					strProducts = "id=" + paint_id + "&Price=" + paint_price + "&q=" + fieldvalue + "&color=" + color; 
					$.post("add_basket.php", strProducts); 
				}
			}
			
			/* ha meg lett adva a zsanernal darabszam akkor az kuldes kulon termekkent */
			if(eval('document.'+formName+'.quantity_of_hinges_per_pair != null'))  
			{
				var fieldvalue = Number(eval('document.'+formName+'.quantity_of_hinges_per_pair.value'));
				if(!isNaN(fieldvalue) && fieldvalue > 0)
				{
					strProducts = "id=" + hinge_id + "&Price=" + hinge_price + "&q=" + fieldvalue; 
					$.post("add_basket.php", strProducts); 
				}
			}
			
			/* minden ok, popup ablak megjelenitese */
			document.getElementById("viewbasket").style.display = "";
			shoxBox('Successfully added to Basket!');
		}
		else // ha nem volt sikeres az ar lekerdezes
		{
			document.getElementById("viewbasket").style.display = "none";
			shoxBox(result[1]);
		} 
	});  
}


function shoxBox(msg)
{
	var availHeight;
	var availWidth;
	
	document.getElementById("msg").innerHTML = msg;
 
	if(typeof(window.innerWidth) == 'number'){
		availHeight = window.innerHeight;
		availWidth = window.innerWidth;
	}else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)){
		availHeight = document.documentElement.clientHeight;
		availWidth = document.documentElement.clientWidth;
	}else if(document.body && (document.body.clientWidth || document.body.clientHeight)){
		availHeight = document.body.clientHeight;
		availWidth = document.body.clientWidth;
	}
 
	var indicatorWidth = 300;
	var indicatorHeight = 200;
	var left = (availWidth/2) - (indicatorWidth/2);
	var top = (availHeight/2) - (indicatorHeight/2);
	var indicator = document.getElementById('addbasketbox');
	indicator.style.top=top+f_scrollTop()+"px";
	indicator.style.display="block";
}

function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}

function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

function changeColor(obj,formName,bigimgId,primedImg)
{    
	eval('document.'+formName+'.color.value = obj.title');
	if(document.getElementById(bigimgId) != null)
	{
		if(!primedImg)
		{
			document.getElementById(bigimgId).src = obj.src.replace("mini","large");
		}
		else
		{
			document.getElementById(bigimgId).src = "admin1/image/large/" + primedImg;
		}
	}
	if(obj.name == 'primed_only')
	{
		obj.disabled = true;
        
        eval('document.'+formName+'.tin_of_touch_up_paint.disabled = true');
        eval('document.'+formName+'.tin_of_touch_up_paint.value = ""');
       
	}
	else
	{
		eval('document.'+formName+'.primed_only.checked = false');
		eval('document.'+formName+'.primed_only.disabled = false');
        eval('document.'+formName+'.tin_of_touch_up_paint.disabled = false');


	}
}

function CreateBookmarkLink() {

 title = document.title; 

 url = window.location.href;

	if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url,"");
	} else if( window.external ) { // IE Favorite
		window.external.AddFavorite( url, title); }
	else if(window.opera && window.print) { // Opera Hotlist
		return true; }
 }

 function convert(obj, unit)
 {
	 if(unit == 'inch')
	 {
		 value = Number(obj.value) * 25.4;
		 if(isNaN(value))
		 {
			 obj.value = "";
			 document.getElementById("mm").value = "";
		 }
		 else
		 {
			document.getElementById("mm").value = value.toFixed(2);
		 }
	 }
	 
	 if(unit == 'mm')
	 {
		 value = Number(obj.value) * (1 / 25.4);
		 if(isNaN(value))
		 {
			 obj.value = "";
			 document.getElementById("inch").value = "";
		 }
		 else
		 {
			document.getElementById("inch").value = value.toFixed(2);
		 }
	 }
 }
 
 function checkQuantity(element){
    var value = parseInt(element.value);
    if(isNaN(value)){
        value = 0;
    }
    else if(value<0){
        value = 0
    }
    element.value = value;
 }
 
 function checkWidthHeight(thisval,type){
    var thisval = parseInt(thisval);
    if(isNaN(thisval)) thisval = 0;
    var dHeight = parseInt(document.form_7.height.value);
    var dWidth = parseInt(document.form_7.width.value);
    if(isNaN(dHeight)) dHeight = 0;
    if(isNaN(dWidth)) dWidth = 0;
   
    if(type=="height"){
       if(dWidth>=2400 && thisval>750){
            alert('The maximum height you can enter is: 750mm');
            document.form_7.height.value = 0;
            
       }        
    }
    
    else{
        if(dHeight>=2400 && thisval>750){
            alert('The maximum width you can enter is: 750mm');
            document.form_7.width.value = 0;
            
       }    
    }
 }
 
  function checkWidthHeightDoor(thisval,type){
    var thisval = parseInt(thisval);
    if(isNaN(thisval)) thisval = 0;
    var dWidth = parseInt(document.formDoor.width.value);
    if(isNaN(dWidth)) dWidth = 0;
    
    if(type=="height" && thisval>2400){
        alert('The maximum height you can enter is: 2400mm');
        document.formDoor.height.value = 0;    
    }
    else if(type=="width" && thisval>1000){
        alert('The maximum width you can enter is: 1000mm');
        document.formDoor.width.value = 0;    
    }
    
    if(dWidth>=611 && dWidth<=1000 && thisval>360){
        alert('The maximum height you can enter is: 360mm');
        document.formDoor.height.value = 0;
    } 

    
    
 }
 
 function checkWidthHeightDrawer(thisval,type){
    var thisval = parseInt(thisval);
    if(isNaN(thisval)) thisval = 0;
    
    if(type=="height" && thisval>360){
        alert('The maximum height you can enter is: 360mm');
        document.formDrawer.height.value = 0;    
    }
    else if(type=="width" && thisval>1000){
        alert('The maximum width you can enter is: 1000mm');
        document.formDrawer.width.value = 0;    
    }
    
    
 }
 
 function checkInfils(thisobj){
    var thisval = parseInt(thisobj.value);
    if(isNaN(thisval)) thisval = 0;
    
    if(thisval>2400){
        alert('The maximum is: 2400mm');
        thisobj.value = 0;    
    }
 
 }
 
  function checkPlinth(thisobj){
    var thisval = parseInt(thisobj.value);
    if(isNaN(thisval)) thisval = 0;
    
    if(thisval>200){
        alert('The maximum is: 200mm');
        thisobj.value = 0;    
    }
 
 }
 
 function saveAccessoriesQuantity(prodid,formName){
    var inputs = $('input');
    var prodids = new Array();
    var quantitys = new Array();
    var db = 0;
    var db2 = 0;
    for(var i=0; i<inputs.length;i++){
        if(inputs[i].name == "id"){
            prodids[db++] = inputs[i].value;
        }
        if(inputs[i].name == "q"){            
            quantitys[db2++] = inputs[i].value;
        }
        
        
    }
    
    var print = "";
    for(var i=0;i<prodids.length;i++){
        print += prodids[i] + "=" + quantitys[i] + "\n";
    }
    $.post("handle.php?saveQ",{"quantity" : print},function(retval){
        _submit('fitting.php?' + (!isNaN(prodid) ? 'prodid=' + prodid + '&' : '') +  'returnto=handle.php',formName);
        //window.location = 'fitting.php?returnto=handle.php'
    });
 }
