function _$(id) {
	var object = document.getElementById(id);
	if(object == null && typeof(object) == "undefined") {object = null;}
	return object;
}

function submitForm(object) {
	var formNode = getFormNode(object);
	if(formNode != null && typeof(formNode) != "undefined" && formNode.nodeName.toLowerCase() == "form"){ formNode.submit(); }
	function getFormNode(object){
	while(object.nodeName.toLowerCase() != 'form'){object=object.parentNode;}
	return object;
	}
}	
/**
* Check if required fields are set from contact page
*/
function submitContactForm(object) {
	var formNode = getFormNode(object);
	var error = false;
	
	if(formNode != null && typeof(formNode) != "undefined" && formNode.nodeName.toLowerCase() == "form") {
		showHideError("title", "errorTitle");
		showHideError("firstName", "errorFirstName");
		showHideError("lastName", "errorLastName");
		showHideError("category", "errorCategory");
		checkPhoneField();
		checkEmailField();
		
		if(!error){
			formNode.submit();
		}
	}
	function checkEmailField() {
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		var value = _$("eMail").value;
		if(value == "" || !reg.test(value)) {
			_$("errorMail").style.display = "";
			_$("errorMail").parentNode.className= "error";
			error = true;
		}else{
			hideError("Mail");
		}
	}
	function checkPhoneField() {
		if( _$("phone").value == " " || _$("phone").value=="") {
			_$("errorphone").style.display = "";
			_$("errorphone").parentNode.className= "error";
			error = true;
		}else{
			hideError("phone");
		}
	}
	function showHideError(id, field) {
		if(_$(id).value == "") {
			_$(field).style.display = "";
			_$(field).parentNode.className= "error";
			error = true;
		} else {
			_$(field).style.display = "none";
			_$(field).parentNode.className= "";
		}
	}
	function hideError(field) {
		_$("error" + field).style.display = "none";
		_$("error" + field).parentNode.className= "";
	}
	function getFormNode(object) {
		while(object.nodeName.toLowerCase() != 'form') {
			object = object.parentNode;
		}
		return object;
	}
}
/**
*/
function userRegisterForm(object) {
	var formNode = getFormNode(object);
	var err = false;
	if(formNode != null && typeof(formNode) != "undefined" && formNode.nodeName.toLowerCase() == "form"){
	
		var re = /([\w\s])*\d+([\w\s])*/
		var value = $("#shippingAddres").val();
		if( value.search(re) ){
			if( $("#shippingAddres").parent().find(".missingNumber").hasClass('hide')){
				err = true;
			}
			$("#shippingAddres").parent().find(".missingNumber").removeClass('hide');
		}else{
			$("#shippingAddres").parent().find(".missingNumber").addClass('hide');
		}
	
		var value = $("#billingAddres").val();
		if( value.search(re) && $("#billingAddres").val()!="" ){
			if( $("#billingAddres").parent().find(".missingNumber").hasClass('hide')){
				err = true;
			}
			$("#billingAddres").parent().find(".missingNumber").removeClass('hide');
		}else{
			$("#billingAddres").parent().find(".missingNumber").addClass('hide');
		}

		if(!err){
			formNode.submit();
		}
		return !err;
	
	}
}
function getFormNode(object){
while(object.nodeName.toLowerCase() != 'form'){object=object.parentNode;}
return object;
}
/**
* Check if a payment provider and shipping provider and terms of conditions is selected
*/
function submitConfirmOrderForm(object) {
	var formNode = getFormNode(object);
	if(formNode != null && typeof(formNode) != "undefined" && formNode.nodeName.toLowerCase() == "form"){
		var err = false;
		
		$("#termsConditionsError").hide();
		$("#paymentError").hide();
		if( $("#shippingError").length >= 1 )
			$("#shippingError").hide();
		
		if( $('#acceptTerms:checked').attr('checked') == undefined ){
			err = true;
			$("#termsConditionsError").show();
		}
		
		// check for paymentMethod
		if( $("#paymentError").length >= 1 ){
			if( $("input[name='paymentMethod']:checked").val() == "" || $("input[name='paymentMethod']:checked").val() == undefined) {
				err = true;
				$("#paymentError").show();
			}
		}
		
		// check for valid shippingMethod
		if( $("#shippingError").length >= 1 ){
			if( $("input[name='shippingmethod']:checked").val() == "" || $("input[name='shippingmethod']:checked").val() == undefined) {
				err = true;
				$("#shippingError").show();
			}
		}
		if(!err){
			formNode.submit();
		}
		return !err;
	}
}
function showTermsOfConditions() {
	if(_$("p80popup") && _$("p80dimmedOverlay")) {
		_$("p80popup").style.display = "";
		_$("p80dimmedOverlay").style.display = "";
	}
}
function hideTermsOfConditions() {
	if(_$("p80popup") && _$("p80dimmedOverlay")) {
		_$("p80popup").style.display = "none";
		_$("p80dimmedOverlay").style.display = "none";
	}
}

// Searchbox handler
function searchBoxHandler( object ){
	
	// Set color to black
	object.style.color="#000000";
	
	if( object.value == "Search" ){
		object.value ='';
	}
	
	// Attach the onblur effent
	object.onblur=function(){
		// When the searchbox is empty, set the default color and value
		if( $("#searchbox .input").val()=="" ){
			$("#searchbox .input").css("color", "#CCCCCC");
			$("#searchbox .input").val('Search');
		}
	}
}

function toggleFlexDetails( field ){
	if( document.getElementById( field+'_q').checked ){
		$('#'+field).parent().show();
	}else{
		$('#'+field).parent().hide();
	}
}

function setStates( type ){
	// US
	if( $("#"+type+"Country").val()==700 ){
		// show the dropdownlist
		$("#"+type+"State").parent().show();
		
		// display all items first (to prevent dubble spans
		$("#"+type+"State span").each(function() {
			var opt = $(this).find('option').show();
			$(this).replaceWith(opt)
        });
		
		// hide all elements
		$("#"+type+"State option").wrap('<span>');
		$("#"+type+"State span").hide();
		
		// show the US elements
		$("#"+type+"State .700").unwrap();
		
		// set value to null
		$("#"+type+"State").val('');
		
	// Canada
	}else if( $("#"+type+"Country").val()==699 ){
		// show the dropdownlist
		$("#"+type+"State").parent().show();
		$("#"+type+"State span").each(function() {
			var opt = $(this).find('option').show();
			$(this).replaceWith(opt)
        });
		$("#"+type+"State option").wrap('<span>');
		$("#"+type+"State span").hide();
		$("#"+type+"State .699").unwrap();
		$("#"+type+"State").val('');
		
	} // other
	else{
		// show the dropdownlist
		$("#"+type+"State").parent().hide();
		$("#"+type+"State span").each(function() {
			var opt = $(this).find('option').show();
			$(this).replaceWith(opt)
        });
		$("#"+type+"State option").wrap('<span>');
		$("#"+type+"State span").hide();
		$("#"+type+"State .outsideUS").unwrap();
		$("#"+type+"State").val('outsideUS');
	}
}

// jQuery unwrap function
$.fn.unwrap = function() {
  this.parent(':not(body)')
    .each(function(){
      $(this).replaceWith( this.childNodes );
   });
  return this;
};

function switchFAQstate(object, cat, label){
	if( object.parent().find(".answer").css('display')=='none' ){
		object.parent().find(".answer").fadeIn();
		object.addClass('open');
		// Google tracking
		pageTracker._trackEvent(cat,'Open', label);
	}else{
		object.parent().find(".answer").fadeOut();
		object.removeClass('open');
		// Google tracking
		pageTracker._trackEvent(cat,'Close', label);
	}
}
$(function(){
   $(".support_an").live('click', function() {
	   if ($(this).attr("class") == "support_an") {
		   pageTracker._trackEvent('SupportRating', $(this).attr("action"), $(this).attr("label"), parseInt($(this).attr("value")));
		   $(this).parent().children().hide();
		   $(this).siblings('.support_ty').show();
		   $(this).siblings('.support_ty').css('cursor','default');
	   }
   });
});

function updatePrice( mainObjectName ){
	var selectedRadio = $("#productGroep_" + mainObjectName).find("input:radio:checked").val();
	var newPrice = $("#product_" + selectedRadio + " > :input[name=Price]").val();
	var basePrice = 0;
	$("#productGroep_" + mainObjectName + " .included_product > :input[name=Price]").each(function(){
		basePrice =+ parseFloat($(this).val());
	});
	$("#productGroep_" + mainObjectName +" .price span").html( (parseFloat(basePrice)+parseFloat(newPrice)) );
}
function submitProductGroup( mainObjectName ){		
	var siteAccess = 'nl_dut';
	var url = '/index.php/'+siteAccess+'/shop/addProductGroup/'+mainObjectName+'/1/' + $("#productGroep_" + mainObjectName).find("input:radio:checked").val()
	window.location = url;
}

function updatePriceChoice( productId ){
//	var selectedProductId = $("#productListItems").find("input:radio:checked").val();
	$("#submitForm .price").html( $("#submitForm span.symbol").html() + ' ' + $("#productData_"+productId+" > :input[name=Price]").val() ); 
}
function submitProductChoice( productId ){
	var selectedProductId = $("#productListItems").find("input:radio:checked").val();
	$("#submitForm > :input[name=ActionAddToBasket]").val( $("#productData_"+selectedProductId+" > :input[name=ActionAddToBasket]").val() ); 
	$("#submitForm > :input[name=ContentNodeID]").val( $("#productData_"+selectedProductId+" > :input[name=ContentNodeID]").val() ); 
	$("#submitForm > :input[name=ContentObjectID]").val( $("#productData_"+selectedProductId+" > :input[name=ContentObjectID]").val() ); 
	$("#submitForm").submit();
}






