/*
	Jono's mods
*/


	// ===================================================================================
	/*
		The following function handles all events on objects that we want to have happen automagically
	*/
	// ===================================================================================
	function addClickHandlers() {	
		
		// ===================================================================================
		/*
			Emailsubmit is the button for the newsletter subscribe component
		*/
		// ===================================================================================

		function isValidEmailAddress(emailAddress) {
			var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
			return pattern.test(emailAddress);
		}

		$('#emailsubmit').click(function()
			{
				if($("#emailname").val().length == 0 | $("#emailemail").val().length == 0){
					alert('Please ensure both your name and email are entered.');
				} else if(!isValidEmailAddress($("#emailemail").val())) {
					alert('Please enter a valid email address.');
				} else {
					$("#emailsubmit").val('Please wait...');
					$.get(
						'cmCart_xhr.lasso?',
						{
							action:		'subscribe',
							cmd:		'sub',
							nameis:		$("#emailname").val(),
							emailis:	$("#emailemail").val()
						},
						function(data) {
							$("#emailsubmit").val('Subscribe');
							$("#subscribesaved").fadeIn();
							$("#emailname").val('');
							$("#emailemail").val('');
							var t = setTimeout('$("#subscribesaved").fadeOut()',3000);
						}
					);
				
				}
			});


		// ===================================================================================
		/*
			Functionselect is the admin dropdown to determine what information group we want to see
		*/
		// ===================================================================================
		$("#functionselect").change(function()
			{
				$("#admincontentedit").html('');
				$("#adminloading").show();
				$.get(
					'/cmCart_files/_xml/lists.lasso?',
					{
						action:		'get',
						cmd:		$("#functionselect").val()
					},
					function(xml) {
						$("#admincontent").html($("return", xml).text());
						$('#listdata').dataTable();

					}
				);
				var l = setTimeout('$("#adminloading").fadeOut()',3000);
				
			}
		);




		$(".selectingNewImg1").change(function()
			{ 
				$("#show_img1").attr('src',$("#selectingNewImg1").val()); 
				$("#pr_img1").val($("#selectingNewImg1").val()); 
			}
		);
		$(".selectingNewImg2").change(function()
			{ 
				$("#show_img2").attr('src',$("#selectingNewImg2").val()); 
				$("#pr_img2").val($("#selectingNewImg2").val()); 
			}
		);
		$(".selectingNewImg3").change(function()
			{ 
				$("#show_img3").attr('src',$("#selectingNewImg3").val()); 
				$("#pr_img3").val($("#selectingNewImg3").val()); 
			}
		);
		
		$('.grouppropimg').click(function()
			{
				var i = $(this).parent();
				if($(this).attr('src') == '/cmCart_files/img/add.png'){
					i.prependTo( $("#targeted-list") );
					$(this).attr('src','/cmCart_files/img/delete.png');
				} else {
					i.prependTo( $("#source-list") );
					$(this).attr('src','/cmCart_files/img/add.png');
				}
			}
		);
		$('.grouppropimg2').click(function()
			{
				var i = $(this).parent();
				if($(this).attr('src') == '/cmCart_files/img/add.png'){
					i.prependTo( $("#targeted-list2") );
					$(this).attr('src','/cmCart_files/img/delete.png');
				} else {
					i.prependTo( $("#source-list2") );
					$(this).attr('src','/cmCart_files/img/add.png');
				}
			}
		);


		
		$(".cartDel-btn").click(function(){
				var x = $(this).attr('id');
				x = x.substr(13);
				document.location.href = '?cmd=remove&item='+x+'&'+session.sessionParam;		
		});

	
		$("#qrSidebar li a").click( function() {
			var u = $(this).parent().children('ul');
			var s = $(this).parent().siblings();
			if(u.hasClass('open')) {
				u.addClass('closed');
				u.removeClass('open');
			} else {
				u.addClass('open');
				u.removeClass('closed');
				s.children('ul').each( function() {
					if($(this).hasClass('open')){
					 $(this).addClass('closed');
					 $(this).removeClass('open');
					}
				});
			}
		});
		
		$('.defPopup').hover(
			function() {
				var i = $(this).attr('title');
				var d = '#defPopupdiv'+i;
				$(d).css('display','inline');
			},
			function() {
				var i = $(this).attr('title');
				var d = '#defPopupdiv'+i;
				$(d).css('display','none');			
			}
		);

	}
	
	
	// ===================================================================================
	/*
		$(document).ready(addClickHandlers); triggers the initialization of automagical events from objects
	*/
	// ===================================================================================

	$(document).ready(addClickHandlers);
/* 	$(document).ready(cart.fixFormElements()); */
	
	
	// ===================================================================================
	/*
		function deleteProduct deletes the product and reloads the list
		requires product group id as well as product id. 
		the group id must match the product id's
	*/
	// ===================================================================================
	function deleteProduct(id,groupid){
		if (confirm("Are you sure you want to delete this item?")) {
			$("#adminloading").show();
			$.get(
				'/cmCart_files/_xml/lists.lasso?',
				{
					action:		'delete',
					cmd:		'product,'+groupid,
					id:			id
				},
				function(xml) {
					$("#admincontent").html($("return", xml).text());
					$('#listdata').dataTable();
				}
			);
			var l = setTimeout('$("#adminloading").fadeOut()',1000);
		}
	}
	
	
	// ===================================================================================
	/*
		function deleteItem deletes the product and reloads the list
		requires product group id as well as product id. 
		the group id must match the product id's
	*/
	// ===================================================================================	
	function deleteItem(id,cmd){
		$("#adminloading").show();
		$.get(
			'/cmCart_files/_xml/lists.lasso?',
			{
				action:		'delete',
				cmd:		cmd,
				id:			id
			},
			function(xml) {
				$("#admincontent").html($("return", xml).text());
				$('#listdata').dataTable();
			}
		);
		var l = setTimeout('$("#adminloading").fadeOut()',1000);
	}

	// ===================================================================================
	/*
		function loadItem loads ready for edit... or in the case of 0, for add.
	*/
	// ===================================================================================	
	function loadItem(id,cmd){
		$("#admincontentedit").html('Loading... ');
		$.get(
			'/cmCart_files/_xml/load_edits.lasso?',
			{
				action:		'load',
				cmd:		cmd,
				id:			id
			},
			function(xml) {
				$("#admincontentedit").html($("return", xml).text());
				
				// initialize DOM objects to be acted on
				addClickHandlers();
				
				// initialize any intended sortable objects
				$('.sortable').sortable();
				if(cmd == 'download'){
					new Ajax_upload('#pfilename_new', {
						action: '/cmCart_files/_xml/uplfile.lasso?',
						name: 'download',
						onComplete : function(response){
							$("#pfilename").text(response);
						}	
					});
				} else {
					new Ajax_upload('#pr_img1_new', {
						action: '/cmCart_files/_xml/uplfile.lasso?',
						name: 'file1',
						onComplete : function(response){
							$("#pr_img1").val(response);
							$("#show_img1").attr('src','/cmCart_files/repo/articlegfx/list/'+response); 
						}	
					});
					new Ajax_upload('#pr_img2_new', {
						action: '/cmCart_files/_xml/uplfile.lasso?',
						name: 'file2',
						onComplete : function(response){
							$("#pr_img2").val(response);
							$("#show_img2").attr('src','/cmCart_files/repo/articlegfx/detail/'+response); 
						}	
					});
					new Ajax_upload('#pr_img3_new', {
						action: '/cmCart_files/_xml/uplfile.lasso?',
						name: 'file3',
						onComplete : function(response){
							$("#pr_img3").val(response);
							$("#show_img3").attr('src','/cmCart_files/repo/articlegfx/spec/'+response); 
						}	
					});
				}


			}
		);
	}
	
	function cancelEdit() {
		$("#admincontentedit").html('');
	}

	// ===================================================================================
	/*
		function saveGroup saves!
	*/
	// ===================================================================================	
	function saveGroup(id){
		
		var props = '';
		$("#targeted-list > li > .props2view").each(function (i) {
			props += this.value + ',';
		});
/* alert(props);		 */
		$.get(
			'/cmCart_files/_xml/lists.lasso?',
			{
				action:		'save',
				cmd:		'group',
				id:			id,
				groupname:	$("#groupname").val(),
				groupcode:	$("#groupcode").val(),
				groupbase:	$("#groupbase").val(),
				props:		props
			},
			function(xml) {
				$("#admincontent").html($("return", xml).text());
				$('#listdata').dataTable();
				$("#admincontentedit").html('');
			}
		);
	}

	// ===================================================================================
	/*
		function saveProp saves!
	*/
	// ===================================================================================	
	function saveProp(id){
		$.get(
			'/cmCart_files/_xml/lists.lasso?',
			{
				action:		'save',
				cmd:		'property',
				id:			id,
				pname:		$("#pname").val(),
				pdatatype:	$("#pdatatype").val(),
				punit:		$("#punit").val()				
			},
			function(xml) {
				$("#admincontent").html($("return", xml).text());
				$('#listdata').dataTable();
				$("#admincontentedit").html('');
			}
		);
	}

	// ===================================================================================
	/*
		function saveDL saves!
	'pname' 		= string,
	'pdescription' 	= string,
	'ptag' 			= string,
	'pfilename' 	= string,
	'pupdatedate' 	= string,

	*/
	// ===================================================================================	
	function saveDL(id){
		$.get(
			'/cmCart_files/_xml/lists.lasso?',
			{
				action:			'save',
				cmd:			'downloads',
				id:				id,
				pname:			$("#pname").val(),
				pdescription:	$("#pdescription").val(),
				ptag:			$("#ptag").val(),
				pfilename:		$("#pfilename").text()				
			},
			function(xml) {
				$("#admincontent").html($("return", xml).text());
				$('#listdata').dataTable();
				$("#admincontentedit").html('');
			}
		);
	}
	
	
	
	// ===================================================================================
	/*
		function saveProduct saves!
	*/
	// ===================================================================================	
	function saveProduct(id){

		var props = '';
		$("#targeted-list > li > .props2view").each(function (i) {
			props += this.value + ',';
		});
/* alert(props); */

		var props2 = '';
		$("#targeted-list2 > li > .props2view").each(function (i) {
			props2 += this.value + ',';
		});
/* alert(props2); */

/* theseprops */
		var productproparray = '';
		$(".theseprops").each(function (i) {
			productproparray += this.id + '=' + this.value + '|';
		});
/* alert(productproparray); */

/*
		$("#thiscmd").val('product,'+$("#pr_artgroup_id").val());
		$("#thisrelproducts").val(props);
		$("#thisdownloads").val(props2);
		$("#thisproperties").val(productproparray);

		$("#productform").submit();
*/

		$.post(
			'/cmCart_files/_xml/lists.lasso?',
			{
				action:			'save',
				cmd:			'product,'+$("#pr_artgroup_id").val(),
				id:				id,
				pr_artno:		$("#pr_artno").val(),
				pr_name:		$("#pr_name").val(),
				pr_artgroup_id:	$("#pr_artgroup_id").val(),
				pr_description:	$("#pr_description").val(),
				pr_img1:		$("#pr_img1").val(),
				pr_img2:		$("#pr_img2").val(),
				pr_img3:		$("#pr_img3").val(),
				relproducts:	props,
				downloads:		props2,
				properties:		productproparray
			},
			function(xml) {
				$("#admincontent").html($("return", xml).text());
				$('#listdata').dataTable();
				$("#admincontentedit").html('');
			}
		);
	}
	
	function sortme(groupid){
		$("#admincontentedit").html('');
		$("#adminloading").show();
		$.get(
			'/cmCart_files/_xml/lists.lasso?',
			{
				action:		'sort',
				cmd:		'product,'+groupid
			},
			function(xml) {
				$("#admincontent").html($("return", xml).text());
				$('.sortable').sortable();
			}
		);
		var l = setTimeout('$("#adminloading").fadeOut()',3000);		
	}
	function cancelsort(groupid){
		$("#admincontentedit").html('');
		$("#adminloading").show();
		$.get(
			'/cmCart_files/_xml/lists.lasso?',
			{
				action:		'get',
				cmd:		'product,'+groupid
			},
			function(xml) {
				$("#admincontent").html($("return", xml).text());
			}
		);
		var l = setTimeout('$("#adminloading").fadeOut()',3000);		
	}
	function savesort(groupid){
		var sortorder = '';
		$("#targeted-list > li > .props2view").each(function (i) {
			sortorder += this.value + ',';
		});
/* alert(sortorder); */

		$("#admincontentedit").html('');
		$("#adminloading").show();
		$.get(
			'/cmCart_files/_xml/lists.lasso?',
			{
				action:		'sorting',
				cmd:		'product,'+groupid,
				sortorder:	sortorder
			},
			function(xml) {
				$("#admincontent").html($("return", xml).text());
			}
		);
		var l = setTimeout('$("#adminloading").fadeOut()',3000);		

	}


	function productSearch(groupid){
		//load area1
		$.get(
			'cmCart_xhr.lasso?' + session.sessionParam,
			{
				action:		'article',
				cmd:		'search',
				section:	'area1',
				searchtype:	$("#searchtype").val(),
				searchval:	$("#searchval").val()
			},
			function(data) {
				$("#area1").hide().html(data).fadeIn();
 				$("#area2").hide();
 				$("#area3").hide();
				$('.revealDetailArea').click(function(){
					$('#area2').show('fast');
					$('#area3').show('fast');
				});
			}
		);
	
	
	}
	
	function sendcart(action){
		if(
			//$("#emailname").val().length > 2 && 
			$("#email").val().length > 2 &&
			$("#email").val().indexOf('@') > 1
			){
				$("#emailcmd").val(action);
				$("#emailform").submit();
				if("#signup:checked") {
					var firstlastname = $("#firstname").val() + " " + $("#lastname").val();
					$.get(
						'cmCart_xhr.lasso?',
						{
							action:		'subscribe',
							cmd:		'sub',
							nameis:		firstlastname,
							emailis:	$("#email").val()
						},
						function(data) {
						}
					);
				}
				alert("Your message has been sent.");
		} else {
			alert('Please enter a valid email address.');
		}
	}

	function cartclick(artno,groupnav){
/*
				alert(artno);
				alert(groupnav);
*/
				document.location.href='/'+groupnav+'?artno='+artno;
				
	}

	function  cmDropDown(radio,divid) {
		alert('in cmdropdown');
		var cmVal = radio.value;
		$.post('/cmCart_files/_xml/qrDropdown.lasso', {cm:cmVal}, function(data){
			$(divid).html(data);
		});
		
	}