
/**
 * JQUERY
 */
$("document").ready(function(){
	
	tb_init('a.thickbox, area.thickbox, input.thickbox');//pass where to apply thickbox
	imgLoader = new Image();// preload image
	imgLoader.src = tb_pathToImage;
	
	/*
	 * Order data form validation
	 */
	checkForm();
	
	$('form#orderform input, :radio').keyup(function(){
		checkForm();
	});
	$('form#orderform input, :radio').change(function(){
		checkForm();
	});
	$('form#orderform input, :radio').click(function(){
		checkForm();
	});

	$("input[name=ander_factuur_adres]").click(function(){
		
		if($("input[name=ander_factuur_adres]:checked").length == 1)
		{
			$("div#anderadres").show("medium");
		}
		else
		{
			$("div#anderadres").hide("fast");
		}
		
	});
	
	
	/*
	 * Search form control
	 */
	
	// Disable empty fields on page load
	if ($("div#searchform input[name='artist']").val() != "" || $("div#searchform input[name='title']").val() != "" || $("div#searchform select").val() != "")
	{
		$("div#searchform input, div#searchform select").each(function(){
			if ($(this).val() == "") {
				//$(this).attr("disabled", "true");
				$(this).addClass("disabled");
				$(this).prev("label").addClass("disabled");
			}
		});
	}
	
	// Select field value on focus
	$("div#searchform input, form#orderform input").focus(function(){
		$(this).select();
	}); 
		
	// Disable other fields if one is filled
	$("div#searchform input").keyup(function(){
		searchFormDisable($(this));
	});
	
	$("div#searchform select").change(function(){
		searchFormDisable($(this));
	});
	
});


/**
 * FUNCTIONS
 */


/*
 * Shopcart check stock before ordering
 */
function checkStock()
{
	var sMsg = "";
	var sGlue;
	var foo;
	
	$.get("/webwinkel/ajax_checkstock.php", function(d){
	
		// Some include in the ajax file ads a newline (\n) to the response
		d = d.replace(/\s*\n\s*/g,'');
				
		if (d != 'false')
		{
			var aTitles = d.split(";;");
			var len = aTitles.length;
			
			for (i in aTitles) {
			
				if (i < 1) 
					sGlue = "";
				else 
					if (i == len - 1) 
						sGlue = " en ";
					else 
						sGlue = ", "
				
				
				sMsg += sGlue + "<strong>" + aTitles[i] + "</strong>";
				;
				
			}
			
			if (len > 1) 
				sGlue = " zijn ";
			else 
				sGlue = " is "
			
			sMsg += sGlue;
			
			$("div#hiddenModalContent span.msg").html(sMsg);
			$("a.thickbox").click();
		}
		else
		{
			window.location = "/webwinkel/uwgegevens/"
		}
	});
}
/*
 * Order data form validation
 */

function submitForm()
{	
	if(checkForm(true))
	{
		document.getElementById('orderform').submit();
		return true;
	}
	else
	{
		alert('Velden met een * zijn verplicht om in te vullen.');
		return false;
	}
}
function checkForm(submitted)
{
	var validated = true;
	
	$('form#orderform label').each(function(){
		var str = $(this).html();
		
		if(str.indexOf("*") > 0)
		{
			
			
			if($(this).next().attr("type") == "text")
			{
				if($(this).next().val() == "")
				{
					validated = false;
					
					if (submitted) 
					{
						$(this).css("color", "#E95100");
					}
				}
				else
				{
					$(this).css("color", "");
				}
			}
			
			if($(this).next().attr("type") == "radio")
			{
				var name = $(this).next().attr("name")
				
				if($("input[name='"+name+"']:checked").length < 1)
				{
					validated = false;
					if (submitted) 
					{
						$(this).css("color", "#E95100");
					}
				}
				else
				{
					$(this).css("color", "");
				}
			}
			
			if($("input[name='Auto']").attr('checked') == false)
			{
				validated = false;
				if (submitted) 
				{
					$("input[name='Auto']").parent().css("color", "#E95100");
				}
			}
			else
			{
				$("input[name='Auto']").parent().css("color", "");
			}
		}
	});
	
	if(validated == true)
	{
		$("a#submitform").removeClass("next_grey_alt");
	}
	else
	{
		$("a#submitform").addClass("next_grey_alt");
	}
	
	return validated;
}

function highlightForm(fieldsCSV)
{
	var aFields = fieldsCSV.split(",");
	
	for (i in aFields)
	{
		$("form#orderform input[name='"+aFields[i]+"']").css("color", "#ff0000");
		$("form#orderform input[name='"+aFields[i]+"']").keydown(function(){
			$(this).css("color","#000000");
		});
	}
}

/*
 * Disables search form fields when one field is in focus
 */
function searchFormDisable(oDomobject)
{
	if (oDomobject.val() != "") {
		var current = oDomobject.attr('name');
		
		oDomobject.removeClass("disabled");
		oDomobject.prev("label").removeClass("disabled");
		
		$("div#searchform input, div#searchform select").each(function(){
			if ($(this).attr("name") !== "search" && $(this).attr('name') !== current) {
				$(this).val("");
				$(this).addClass("disabled");
				$(this).prev("label").addClass("disabled");
			}
		});
	}
	else
	{
		$("div#searchform input, div#searchform select").removeClass("disabled");
		$("div#searchform input, div#searchform select").prev("label").removeClass("disabled");
	}
}

/*
 * Validate the searchform
 */
function checkSearchForm()
{
	if(document.searchform.title.value != "" || document.searchform.artist.value != "" || document.searchform.genre.value != "")
	{
		return true;
	}
	else
	{
		alert("Vul a.u.b. een titel of een artiest in, of kies een genre.");
		return false;
	}
}


