
function signupValidate(){
	//validate form
	var errors=0;
	var validFields=new Array("first_name", "surname", "telephone", "email", "address","postcode");
	for (i=0;i<validFields.length;i++){
		if(!doesHaveValue(validFields[i])){
			
			errors++;			
		}		
	}
	if(errors>0){
		alert('The form fields indicated in red must be filled out before submitting the form');	
	}else{
		document.getElementById('signup').submit();
		
	}
}

function doesHaveValue(id){
	if(document.getElementById(id)){
		if(document.getElementById(id).value){
			document.getElementById(id).style.border="1px solid #999999";
			return true;
		}else{
			document.getElementById(id).style.border="1px solid red";
			return false;	
		}
	}else{
		alert(id);
		return false;
	}	
}

//if(jQuery){
	 $(document).ready(function() {
		if($("#addproduct").length>0){
			$("#addproduct").click(addProductRow);
			$(".removeProduct").live('click',removeProductRow);
			$("#saveproducts").click(saveProducts);
			$("#productgridDiv").sortable();
			$("#productgridDiv input").live('focus',removeTick);
		}
		//add links

		//if(!$('.duoEditArea')){
			$('.productimagebox img').each(function(){
				var href=$(this).attr('src').replace(/-?rs-[0-9]+x[0-9]+(\.jpg\/)?/i,'');
				$(this).wrap('<a rel="lightboxGal" href="'+href+'" ></a>');
				$(this).parent().parent().append('<div class="zoom"></div>');
			});	
		//}

		$('.productimagebox a').lightBox();
	});
//}

function addProductRow(event){
	var rowHTML=$('#productgridDiv li').first().html();
	$('#productgridDiv').append("<li>"+rowHTML+"</li>");
	//reset id of copied row
	$('#productgridDiv li form').last().children('input').first().val('');
}

function removeProductRow(event){
	if(confirm('Are you sure you want to delete this product variation\nThis cannot be undone')){
		//get id
		var prodid=$(event.target).parent().children('input').first().val();
		if(!prodid){
			$(event.target).parent().parent().remove();//just remove unsaved without id
		}else{
			//send command to server
			$.ajax({url: '/duocms/kinetic/deleteProduct/'+prodid,success: 
				function(data) {
					//remove from form
					$(event.target).parent().parent().remove();
	 			}
			});	
		}
	}
}



function saveProducts(event){
	var forms=$('#productgridDiv li form');
	var form;
	for(var i=0;i<forms.length;i++){
		form=forms[i];
		$(form).parent().addClass('saved');
		$.post("/duocms/kinetic/saveProduct/", ($(form).serialize()+"&order="+i),
		function(data){
			if(data.info.id){
				$(form).last().children('input').first().val(data.info.id);
			}
			
		},"json");
	}
	
}

function removeTick(event){
	var tick=$(event.target).parent().parent();
	$(tick).removeClass('saved');
}

function updatePrice(item,path){
	var bits=item.value.split('|');
	var price=bits[1];
	var productid=bits[0];
	document.getElementById('buynow_new').innerHTML='\
	<span class="buynow_price" id="buynow_price">&pound;'+price+'</span>\
	<a href="/duocms/basket/add/'+productid+'/1'+path+'">click here to buy online now</a>\
	';

}

