function externalLinks() {
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") &&
		anchor.getAttribute("rel") == "external")
		anchor.target = "_blank";
	}
}
function navigate(theUrl)
{
    document.location.href = theUrl;
}
function addToField(field, code)
{
	document.getElementById(field).value += code+ ' ';
}
function showImage(object, source){
	$('#'+object).attr("src", source);
}
function confirmDelete(str){
	if(str == null){
		if(confirm('Are you sure that you want to delete this item?')){
			return true;
		}
		else{
			return false;
		}
	}
	else{
		if(confirm(str)){
			return true;
		}
		else{
			return false;
		}
	}	
}
function onEnterSubmit(e, form){
	if(e){
		e = e 
	} 
	else {
		e = window.event
	} 

	if(e.which){ 
		var keycode = e.which
	} 
	else {
		var keycode = e.keyCode 
	}
	if(keycode == 13) {
		form.submit();
	}
}
function open_url(page, target){
	$(document).ready(function() {
		$('#'+target).fadeOut('fast', function(){ setup_urlcontent(page, target);});
	});
}
function setup_urlcontent(page, target){
	$.ajax({
		type: "GET",
		url: page,
		cache: false,
		dataType: 'json',
		success: function(html){
			// Empty content
			$('#'+target).empty();
			// New content
	    	$("#"+target).append(html.content);
            $('#'+target).fadeIn('fast');

	  	},
	  	error: function(){
	  		if(page == '/error/inline'){
	  			alert("You request ended up in an endless loop\n Please try again later");
	  		}
	  		else{
	  			open_url('error/inline', 'content');
	  		}
	  	}
	});
}
function update_from_cart(productid, sizeid, value){
	$(document).ready(function() {
		// Eventuele oude interval stoppen
	    window.clearInterval(self.$intervalId); 
	    // Stel interval waardes in
	    self.$intervalValue = 1;
	    self.$intervalMaxValue = 60; 
	    // Interval starten (1 ms)
	    self.$intervalId = window.setInterval(function(){	       
	        // Controleer timer
	        if(self.$intervalValue == self.$intervalMaxValue) {
	        	if(value < 1){
	        		$('#layer_error_msg').html('Product quantity has be larger then "0". Press the delete button on the left to delete the product from your shopping cart');
	        		$('#layer_error_msg').slideDown('slow', function(){
	        			setTimeout(function(){ $('#layer_error_msg').slideUp('slow');},3000);
	        		});	        		
	        	}
	        	else if ( isNaN( value ) ){
	        		$('#layer_error_msg').html('Entered quantity "'+value+'" is not a valid number.');
	        		$('#layer_error_msg').slideDown('slow', function(){
	        			setTimeout(function(){ $('#layer_error_msg').slideUp('slow');},3000);
	        		});	 
	        	}
	        		
	        	if(value > 0){
	        		$.ajax({
	        			type: "POST",
	        			url: '/ajax/updatefromcart',
	        			cache: false,
	        			dataType: 'json',
	        			data: ({	product : productid,
	        						size : sizeid,
	        						quantity : value
	        					}),
	        			success: function(json){
                            // Set totals
                            $('#mycart_totalitems').html(json.mycart_totalitems);
                            $('#mycart_totalcost').html(json.mycart_totalcost);
                            // Set item values
                            $('#product_cost'+productid+'_'+sizeid).html(json.product_cost);

                            if(json.maxproductsreached != null){
                                // Result message
                                $('#layer_error_msg').html('You can order a maximum of "'+json.maxproductsreached+'" items of this product. The product quantity has been updated to "1" item');
                                $('#layer_error_msg').slideDown('slow', function(){
                                    setTimeout(function(){ $('#layer_error_msg').slideUp('slow');
                                    $('#mycart_'+productid+'_'+sizeid).val(1);
                                    
                                    },3000);
                                });
                            }
                            else{   
                                // Result message
                                $('#layer_success_msg').html('Product quantity updated succesfully');
                                $('#layer_success_msg').slideDown('slow', function(){
                                    setTimeout(function(){ $('#layer_success_msg').slideUp('slow');},3000);
                                });
                            }
	        		  	}
	        		});		
	        		
	        	}	        		
    			// Stop interval;
            	return window.clearInterval(self.$intervalId);
	        }
	        // Verhoog timerwaarde
	        self.$intervalValue++;
	    }, 1); 
	});
}
function delete_from_cart(productid, sizeid){
	if(confirm('Are you sure that you want to remove this product from your shopping cart?')){
		$.ajax({
			type: "POST",
			url: '/ajax/removefromcart',
			cache: false,
			dataType: 'json',
			data: ({	product : productid,
						size : sizeid
					}),
			success: function(json){
				// Set totals
				$('#mycart_totalitems').html(json.mycart_totalitems);
				$('#mycart_totalcost').html(json.mycart_totalcost);
				// Remove layer
                if(json.mycart_totalitems == 0){
					$('#productrow'+productid+'_'+sizeid).slideUp("slow",function() {
						$('#productrow'+productid+'_'+sizeid).remove();
						$('#shoprow_head').hide(1000);
						$('#shoprow_buttons').hide(1000);						
						$('#shoprow_noitems').slideDown(1000);
					});	
				}
				else{
					$('#productrow'+productid+'_'+sizeid).slideUp("slow",function() {
						$('#productrow'+productid+'_'+sizeid).remove();
					});
				}
		  	}
		});		
	}
	else{
		return false;
	}
}
function add_to_cart(productid){
	if($('#productsize'+productid).val() != null && $('#productsize'+productid).val() != ''){
		var resultmessage = 'The product has been added to your shopping cart';
		$.ajax({
			type: "POST",
			url: '/ajax/addtocart',
			cache: false,
			dataType: 'json',
			data: ({	product : productid,
						size : $('#productsize'+productid).val()
					}),
			success: function(json){
				if(json.maxorderreached != null){
					resultmessage = 'This product is \"Out of Stock\"';
				}
				else{
					$('#mycart_totalitems').html(json.mycart_totalitems);
					$('#mycart_totalcost').html(json.mycart_totalcost);
				}
				// Set result message
				$.cursorMessage(resultmessage, 
								{	offsetY:-70,
									offsetX:-100,
									hideTimeout:1000
								});	
		  	}
		});
	}
	else{
		var resultmessage = 'Please select your size';
		$.cursorMessage(resultmessage, 
						{	offsetY:-70,
							offsetX:-100,
							hideTimeout:2000
						});		
	}	
	
}
function checkout_billingdata(){
	jQuery(document).ready(function($) {
		if($('#usebilling').is(':checked')){			
			$.ajax({
				type: "POST",
				url: '/ajax/checkoutdata',
				cache: false,
				dataType: 'json',
				data: ({	step : 1
						}),
				success: function(json){
					$('#firstname').val(json.firstname);
					$('#lastname').val(json.lastname);
					$('#company').val(json.company);
					$('#address').val(json.address);
					$('#city').val(json.city);
					$('#state').val(json.state);
					$('#postalcode').val(json.postalcode);
					$('#country'+json.country).attr('selected', 'selected');
			  	}
			});
		}
		else{
			$('#firstname').val('');
			$('#lastname').val('');
			$('#company').val('');
			$('#address').val('');
			$('#city').val('');
			$('#state').val('');			
			$('#postalcode').val('');
			$('#country'+$('#country').val()).attr('selected', '');
		}
	});
}
// Init
window.onload = externalLinks;
