/*
 * @filename /var/js/common.js
 *
 * @author Media Vision Sp. z o.o. [www.mv.pl]
 * @copyright (c) 2010 Copyright by Cruella Fashion
 */

// Show Primary Navigation Second Layer
function showPrimaryNavigationSecondLayer(refElement) {
  refElement.parentNode.getElementsByTagName('ul').item(0).style.display='block';
}

// Hide Primary Navigation Second Layer
function hidePrimaryNavigationSecondLayer(refElement) {
  refElement.parentNode.getElementsByTagName('ul').item(0).style.display='none';
}

$(document).ready(function() {
	
	$('#btn-do_koszyka').click(function(){
		
		if($('#isizeid').val()=='Rozmiar')
		{
			alert('Wybierz rozmiar produktu');
			return false;	
		}
		
		$('#addToCartForm').submit();
		return false;
	});
	
	$('.addedImage').click(function(){
		
		var link = $(this).attr('id');
		var orginalLink = link.replace('medium','orginal');
		
		$('#mainProductPhoto').attr('src',$(this).attr('id'));
		$('#mainProductPhotoLink').attr('href',orginalLink);
	});
	
	$('#manufacturersList').change(function(){
		document.location=prepareUrl($(this));
	});
	
	$('#sortByList').change(function(){
		document.location=prepareUrl($(this));
	});
	
	$('#categoriesList').change(function(){
		document.location=prepareUrl($(this));
	});
	
	$('#ilogin-bottom').focus(function(){
		if($(this).val()=='e-mail')
		{
			$(this).val('');
		}
	});
	
	$('#ipassword-bottom').focus(function(){
		if($(this).val()=='hasło')
		{
			$(this).val('');
		}
	});	
	
	$('#btn-prev').click(function(){
		startChangeProduct();
	}); 
	
	$('#btn-next').click(function(){
		startChangeProduct();
	});
	
	$('#check-code').click(function(){
		
		$('#check-code-result').html('');
		
		var code = $('#input-code').attr('value');
		
		var options = { 
		   	type: 			"POST",
		   	url: 			"/inc/ajax.php",
		   	data: 			"function=checkCode&code=" + code,
	        success:       	function(msg){
	        	
				if (msg=='OK')
				{
					$('#check-code-result').html('<b style="color: green;">OK</b>');
				}
				else
				{
					$('#check-code-result').html('<b style="color: red;">Błąd</b>');
				}
	        }
	    };
		
	 	$.ajax(options);
	 	
	});
	
});

function startChangeProduct()
{
	var currentId = $('#ajaxProductPhoto').attr('class');
		
	var options = { 
	   	type: 			"POST",
	   	url: 			"/inc/ajax.php",
	   	data: 			"function=getMainPageProduct&currentId=" + currentId,
        success:       	changeProduct,
        dataType:		"xml"
    };
	
 	$.ajax(options);
}

function changeProduct (responseXML)
{
	
	var error	= $('error',responseXML).text();
	var name 	= $('name',responseXML).text();
	var price 	= $('price',responseXML).text();
	var id 		= $('id',responseXML).text();
	var src		= $('src',responseXML).text();
	
	if (error==0)
	{
		$('#ajaxProductName').html(name);
		$('#ajaxProductPrice').html(price + '<span>pln</span>');
		$('#ajaxProductPhoto').attr('class',id).attr('src',src);
		$('#btn-buy').attr('href','/product.html?productId=' + id);
		
		$('#ajaxProductPhoto').show('slow');
	}
}

function showPhoto()
{
}

function prepareUrl (variable)
{
	
	var varName 		= variable.attr('name');
	var varValue 		= variable.val();
	var url 			= document.location.href;
	
	urlParts = url.split('?');
	
	if (urlParts.length==2)
	{
		var isLong  	= 1;
		var isVar 		= 0;
		var address	 	= urlParts[0];
		var vars 		= urlParts[1];
        var params 		= vars.split("&");   
        var locationUrl = '';
        
        if (params.length>0)
        {
        	for (i=0; i<params.length; i++)
        	{
        		paramsParts = params[i].split('=');
        		
        		if (paramsParts[0]==varName)
        		{
        			locationUrl += varName + '=' + varValue;
        			
        			var isVar = 1;
        		}
        		else
        		{
        			if (paramsParts[0]=='page')
        			{
        				continue;
        			}
        			else
        			{        			
        				locationUrl += paramsParts[0] + '=' + paramsParts[1];
        			}
        		}
        		
        		if (i+1<params.length)
        		{
        			locationUrl +='&';
        		}
        	}
        }
        
        if (isVar==0)
        {
        	locationUrl += '&' + varName + '=' + varValue;
        }
	}
	
	if (isLong)
	{
		var newLocation = address+'?'+locationUrl;
	}
	else
	{
		var newLocation = url += '?' + varName + '=' + varValue;
	}
	
	return newLocation;
}

function checkOrderForm ()
{
	var error = false;
	
	if ($('#iname').val()=='') 
	{
		$('#iname').css('background','yellow');
		error = true;
	}
	else
	{
		$('#iname').css('background','#F0C3E2');	
	}
	
	if ($('#isurname').val()=='') 
	{
		$('#isurname').css('background','yellow');
		error = true;
	}
	else
	{
		$('#isurname').css('background','#F0C3E2');	
	}
	
	if ($('#iphone').val()=='') 
	{
		$('#iphone').css('background','yellow');
		error = true;
	}
	else
	{
		$('#iphone').css('background','#F0C3E2');	
	}
	
	if ($('#iemail').val()=='') 
	{
		$('#iemail').css('background','yellow');
		error = true;
	}
	else
	{
		$('#iemail').css('background','#F0C3E2');	
	}
	
	if ($('#istreet').val()=='') 
	{
		$('#istreet').css('background','yellow');
		error = true;
	}
	else
	{
		$('#istreet').css('background','#F0C3E2');	
	}
	
	if ($('#istreet_no').val()=='') 
	{
		$('#istreet_no').css('background','yellow');
		error = true;
	}
	else
	{
		$('#istreet_no').css('background','#F0C3E2');	
	}
	
	if ($('#istreet_flat_no').val()=='') 
	{
		$('#istreet_flat_no').css('background','yellow');
		error = true;
	}
	else
	{
		$('#istreet_flat_no').css('background','#F0C3E2');	
	}
	
	if ($('#ipost_code').val()=='') 
	{
		$('#ipost_code').css('background','yellow');
		error = true;
	}
	else
	{
		$('#icode').css('background','#F0C3E2');	
	}
	
	if ($('#icity').val()=='') 
	{
		$('#icity').css('background','yellow');
		error = true;
	}
	else
	{
		$('#ipost_city').css('background','#F0C3E2');	
	}

	if ($('#ishipping:checked').val() == null) 
	{
		alert('Wybierz rodzaj dostawy');
		error = true;
	}
	
	if ( !($('#ipayment-1:checked').val()==1 || $('#ipayment-2:checked').val()==2) )
	{
		alert('Wybierz rodzaj płatności');
		error = true;		
	}
	
	if ($('#iconfirm:checked').val() == null) 
	{
		alert('Musisz zaakceptować regulamin');
		error = true;
	}
	
	if (error)
	{
		return false;	
	}
	
	return true;
}

function checkRemindForm ()
{
	if ($('#iemail-remind').val()=='') 
	{
		$('#iemail-remind').css('background','yellow');
		
		alert('Uzupełnij adres email');
		return false;
	}
	
	return true;
}

