var siteRoot = document.getElementsByTagName('base')[0].getAttribute('href');



function defaultValueTrigger(placeholder, text) {
	if (text!='' && $(placeholder).val()!='') {
	} else {
		if ($(placeholder).val(text)=='') $(placeholder).val(text)
	}	
	}

function requiredEngine() {
	$("input[required=true]").each(function(n) {
		this.onkeyup=function() {
			if(this.value==='') {
				$(this).css({"background-color":"#ff0000", "color":"white", "background-image":"none"});
			} else {
				$(this).css({"background-color":"#ffffff", "color":"#565656"});
			}
		}
	});

	$("#btnNext").click(function() {
		var result = true;
		$("input[required=true]").each(function(n) {
			if(this.value==='') {
				$(this).css({"background-color":"#ff0000", "color":"white", "background-image":"none"});
				result = false;
			}
		});  
        if (result) {
            $('select[disabled]').removeAttr('disabled');
        }
        return result;
	});
}


function onepropertyChange(productId, categoryId) {
	$('#org_cellfex_product_priceTag'+productId).html('<img src="/lib/ajax-loader.gif" width="14" height="14" alt="Loading..." />');
	$('#org_cellfex_product_image'+productId).attr('src', '/lib/ajax-loader.gif');
	$('#org_cellfex_product_imageNormal'+productId).attr('src', '/lib/ajax-loader.gif');
	$('#org_cellfex_product_number'+productId).html('<img src="/lib/ajax-loader.gif" width="14" height="14" alt="Loading..." />');
	var propertyProductNr = $('#firstProperty'+productId+' option:selected').val();
	readPrice('#org_cellfex_product_priceTag'+productId, categoryId+'.'+productId+'.'+propertyProductNr);
	readNumber1('#org_cellfex_product_number'+productId, propertyProductNr);
	readImage('#org_cellfex_product_image'+productId, categoryId+'.'+productId+'.'+propertyProductNr);
	readImageLarge('#org_cellfex_product_imageLarge'+productId, categoryId+'.'+productId+'.'+propertyProductNr);
	readImageNormal('#org_cellfex_product_imageNormal'+productId, categoryId+'.'+productId+'.'+propertyProductNr);
	$('#org_cellfex_product_buyForm'+productId+' input[name=org_cellfex_order_productNumber]').val(propertyProductNr);
}

function twopropertiesChange(productId, categoryId) {
	var firstProperty = $('#firstProperty'+productId+' option:selected').text();
	$('.secondPropertyFor'+productId).hide();
	$('.secondPropertyFor'+productId+' option[rel='+firstProperty+']').parent().show();
	secondPropertyChange(productId, categoryId);
}

function secondPropertyChange(productId, categoryId) {
	$('#org_cellfex_product_priceTag'+productId).html('<img src="/lib/ajax-loader.gif" width="14" height="14" alt="Loading..." />');
	$('#org_cellfex_product_image'+productId).attr('src', '/lib/ajax-loader.gif');
	$('#org_cellfex_product_imageNormal'+productId).attr('src', '/lib/ajax-loader.gif');
	$('#org_cellfex_product_number'+productId).html('<img src="/lib/ajax-loader.gif" width="14" height="14" alt="Loading..." />');
	var firstProperty = $('#firstProperty'+productId+' option:selected').text();
	var activeSecondProperty = $('.secondPropertyFor'+productId+' option[rel='+firstProperty+']').parent();
	var propertyProductNr = $('.secondPropertyFor'+productId+' option[rel='+firstProperty+']:selected').val();
	readPrice('#org_cellfex_product_priceTag'+productId, categoryId+'.'+productId+'.'+propertyProductNr);
	readNumber1('#org_cellfex_product_number'+productId, propertyProductNr);
	readImage('#org_cellfex_product_image'+productId, categoryId+'.'+productId+'.'+propertyProductNr);
	readImageLarge('#org_cellfex_product_imageLarge'+productId, categoryId+'.'+productId+'.'+propertyProductNr);
	readImageNormal('#org_cellfex_product_imageNormal'+productId, categoryId+'.'+productId+'.'+propertyProductNr);
	$('#org_cellfex_product_buyForm'+productId+' input[name=org_cellfex_order_productNumber]').val(propertyProductNr);
}

function readPrice(targetObject,id) {
	$.get(siteRoot+'no/product-dynamic-price/'+id+'/', function(data){
		$(targetObject).html(data);
	});
}

function readNumber(targetObject,id) {
	$.get(siteRoot+'no/product-dynamic-number/'+id+'/', function(data){
		$(targetObject).html(data);
	});
}

function readNumber1(targetObject,id) {
	$(targetObject).html(id);
}

function readImage(targetObject,id) {
	$.get(siteRoot+'no/product-dynamic-image/'+id+'/', function(data) {
		$(targetObject).attr('src', data);
	});
}

function readImageLarge(targetObject,id) {
	$.get(siteRoot+'no/product-dynamic-image-large/'+id+'/', function(data) {
		$(targetObject).attr('href', data);
	});
}

function readImageNormal(targetObject,id) {
	$.get(siteRoot+'no/product-dynamic-image-small/'+id+'/', function(data) {
		$(targetObject).attr('src', data);
	});
}

$(document).ready(function() {
	requiredEngine();
		
	$("#formUpdate #paymentCountry option[value='NO']").attr('selected','selected');
		$("#formUpdate #deliveryCountry option[value='NO']").attr('selected','selected');

		if (($("#deliveryName").val()==$("#paymentName").val()) && 
		 ($("#deliveryStreet").val()==$("#paymentStreet").val()) && 
		 ($("#deliveryCity").val()==$("#paymentCity").val()) && 
		 ($("#deliveryZip").val()==$("#paymentZip").val()) && 
		 ($("#deliveryState").val()==$("#paymentState").val()) && 
		 ($("#deliveryCountry").val()==$("#paymentCountry").val())) {

			 $("#deliveryAsPayment").attr('checked','checked');
			 $("#deliveryAddressFields input").each(function(n) {
			 	$(this).attr('readonly', 'readonly');
			});
		    $("#deliveryCountry").attr('disabled', 'disabled');
		 }
		 
		 

	$("#paymentAddressFields input").each(function(n) {
		this.onkeyup=function() {
			if ($("#deliveryAsPayment").attr('checked')) {
				if ($(this).attr('id')=='paymentName') $('#deliveryName').val(this.value);
				if ($(this).attr('id')=='paymentStreet') $('#deliveryStreet').val(this.value);
				if ($(this).attr('id')=='paymentCity') $('#deliveryCity').val(this.value);
				if ($(this).attr('id')=='paymentZip') $('#deliveryZip').val(this.value);
				if ($(this).attr('id')=='paymentState') $('#deliveryState').val(this.value);
				if ($(this).attr('id')=='paymentCountry') $('#deliveryCountry').val(this.value);
			}
		}
	});
		$("#deliveryAsPayment").click(function() {
			if ($("#deliveryAsPayment").attr('checked')) {
				$("#deliveryName").val($("#paymentName").val());
				    $("#deliveryName").attr('readonly', 'readonly');
					$("#deliveryName").css('color', '#888888');
					$("#deliveryName").css({"background-color":"#ffffff", "color":"black"});
				$("#deliveryStreet").val($("#paymentStreet").val());
				    $("#deliveryStreet").attr('readonly', 'readonly');
					$("#deliveryStreet").css('color', '#888888');
					$("#deliveryStreet").css({"background-color":"#ffffff", "color":"black"});
				$("#deliveryCity").val($("#paymentCity").val());
				    $("#deliveryCity").attr('readonly', 'readonly');
					$("#deliveryCity").css('color', '#888888');
					$("#deliveryCity").css({"background-color":"#ffffff", "color":"black"});
				$("#deliveryZip").val($("#paymentZip").val());
				    $("#deliveryZip").attr('readonly', 'readonly');
					$("#deliveryZip").css('color', '#888888');
					$("#deliveryZip").css({"background-color":"#ffffff", "color":"black"});
				$("#deliveryState").val($("#paymentState").val());
				    $("#deliveryState").attr('readonly', 'readonly');
					$("#deliveryState").css('color', '#888888');
					$("#deliveryState").css({"background-color":"#ffffff", "color":"black"});
				$("#deliveryCountry").val($("#paymentCountry").val());
				    $("#deliveryCountry").attr('disabled', 'disabled');
					$("#deliveryCountry").css('color', '#888888');
					$("#deliveryCountry").css({"background-color":"#ffffff", "color":"black"});
		    } else {
			    $("#deliveryName").removeAttr('readonly');
					$("#deliveryName").css('color', '#000000');
			    $("#deliveryStreet").removeAttr('readonly');
					$("#deliveryStreet").css('color', '#000000');
			    $("#deliveryCity").removeAttr('readonly');
					$("#deliveryCity").css('color', '#000000');
			    $("#deliveryZip").removeAttr('readonly');
					$("#deliveryZip").css('color', '#000000');
			    $("#deliveryState").removeAttr('readonly');
					$("#deliveryState").css('color', '#000000');
			    $("#deliveryCountry").removeAttr('disabled');
					$("#deliveryCountry").css('color', '#000000');
		    }
	});
});



$(document).ready(function() {
	$('#paymentTypefaktura').hide();
	if ($('#module_31').html()!="CREDIT_CARD" && $('#module_31').html()!='') {
		$('#paymentTypefaktura').show();
	}
});





