// JavaScript Document

$(document).ready(function() {

///////////////////
//LOGIN FORM	
///////////////////
	$('#submit_form_login').click(function() {
		$('#form_login').submit();
	});
	
	
	$('#form_login').submit(function(){
		// submit the login form
		$('#form_error').hide();
		var hasError = false;
		
		// validate the form
		if($('#email').val().length<6 || $('#password').val().length<6){
			$('#form_error').html('You must enter your email and password.'); 
			$('#form_error').fadeIn();
			hasError=true;
		}
		
		
		if(!hasError) {
			$('#submit_form_login').html('<img src="assets/images/loader.gif" alt="Loading…" />');
			
			var formInput = $(this).serialize();
			//ADD CLASS,TYPE,FUNCTION
			formInput += '&class=order_system&function=check_login&type=json';
			
			$.post("../api/index.php",formInput, 
			function(data){
				//console.log(data.response);
				if(data.error==true){
					$('#form_error').html(data.msg); 
					$('#form_error').fadeIn();
					$('#submit_form_login').html('<img src="assets/images/next_orange.png" alt="Loading…" />');
				} else {
					//SET LOGIN COOKIES
					var formInput = '&class=order_system&function=set_login_cookie&type=html&userid='+data.data.userid+'&accountid='+data.data.accountid;
					$.post("../api/index.php",formInput,function(data2){location.href="myaccount";});
				}
					
			}, "json");
		} 
		
		return false;
	});
	
///////////////////
//FORGOT PASSWORD FORM	
///////////////////	
	
	$('#submit_form_send_password').click(function() {
		$('#form_send_password').submit();
	});
	
	
	$('#form_send_password').submit(function(){
		// submit the login form
		$('#form_error').hide();
		var hasError = false;
		
		// validate the form
		if($('#email').val().length<6){
			$('#form_error').html('You must enter your email address.'); 
			$('#form_error').fadeIn();
			hasError=true;
		}
		
		
		if(!hasError) {
			$('#submit_form_send_password').html('<img src="assets/images/loader.gif" alt="Loading…" />');
			
			var formInput = $(this).serialize();
			//ADD CLASS,TYPE,FUNCTION
			formInput += '&class=order_system&function=forgot_password&type=json';
			
			$.post("../api/index.php",formInput, 
			function(data){
				//console.log(data.response);
				if(data.error==true){
					$('#form_error').html(data.msg); 
					$('#form_error').fadeIn();
					$('#submit_form_send_password').html('<img src="assets/images/next_orange.png" alt="Loading…" />');
				} else {
					$('#form_error').html(data.msg); 
					$('#form_error').fadeIn();
					$('#submit_form_send_password').html('<img src="assets/images/next_orange.png" alt="Loading…" />');

				}
					
			}, "json");
		} 
		
		return false;
	});
///////////////////
//LOGO UPLOAD FORM	
///////////////////		
	
	if($('#logoupload').length != 0	){
	
		$("#logoupload").uploadify({
			'uploader'       : '../scripts/logo_uploadify.swf',
			'script'         : '../scripts/logouploadify.php',
			'cancelImg'      : '../assets/images/cancel.png',
			'folder'         : '../../logos',
			'queueID'        : 'fileStatus',
			'fileDesc'		 : 'Logo Files',
			'fileExt'		 : '*.jpg;*.jpeg;*.png;*.gif',
			'auto'           : true,
			'width'			 : '160',
			'height'		 : '35',
			'onComplete': function(event, queueID, fileObj, response, data) {
				 $('#fileStatus').html(fileObj.name +' uploaded.');
				 
				 //GET VALUES TO SAVE UPLOADED FILE TO DATABASE
				 var formInput = 'class=order_system&function=uploaded_logo&type=json';
				 formInput += '&filename='+response+'&userfilename='+fileObj.name+'&filesize='+fileObj.size+'&accountid='+getCookie('accountid')+'&userid='+getCookie('userid');
				 $.post("../../api/index.php",formInput, 
				function(data){
				if(data.error==true){
					$('#form_error').html(data.msg); 
					$('#form_error').fadeIn();
				} else {
					$('#form_error').html(data.msg); 
					$('#form_error').fadeIn();
					updateLogos();
				}
					
			}, "json");	
			}
		});
	}
///////////////////
//PAGE UPLOAD FORM	
///////////////////		
	
	if($('#pageupload').length != 0	){
	
		$("#pageupload").uploadify({
			'uploader'       : '../scripts/logo_uploadify.swf',
			'script'         : '../scripts/logouploadify.php',
			'cancelImg'      : '../assets/images/cancel.png',
			'folder'         : '../../page_images',
			'queueID'        : 'fileStatus',
			'fileDesc'		 : 'Image Files',
			'fileExt'		 : '*.jpg;*.jpeg;*.png;*.gif',
			'auto'           : true,
			'width'			 : '160',
			'height'		 : '35',
			'onComplete': function(event, queueID, fileObj, response, data) {
				$('#fileStatus').html(fileObj.name +' uploaded.');
				var lh = location.hash;
				if ( null == lh || "" == lh ) { lh='#1'};
				lh=lh.substring(1);
				jConfirm('You are about to overwrite Page '+lh+'?', 'Page Overwrite', function(r) {
    			if(r==true){
				 	pageid=$('#pageid_'+lh).html();
				 	//GET VALUES TO SAVE PAGE TO DATABASE
				 	var formInput = 'class=order_system&function=page_overwrite&type=json';
					formInput += '&filename='+response+'&userfilename='+fileObj.name+'&filesize='+fileObj.size+'&pageid='+pageid+'&accountid='+getCookie('accountid')+'&userid='+getCookie('userid')+'&bookid='+getCookie('bookid');
				 	$.post("../../api/index.php",formInput, 
					function(data){
						if(data.error==true){
							$('#form_error').html(data.msg); 
							$('#form_error').fadeIn();
						} else {
							$('#form_error').html(data.msg); 
							$('#form_error').fadeIn();
							location.reload(true);
						}
					
					}, "json");	
				}
				}) //end jconfirm
			}
		});
	}	
	
	
///////////////////
//UPLOAD FORM	
///////////////////		
	
	if($('#fileupload').length != 0	){
	
		$("#fileupload").uploadify({
			'uploader'       : '../scripts/uploadify.swf',
			'script'         : '../scripts/uploadify.php',
			'cancelImg'      : '../assets/images/cancel.png',
			'folder'         : '../pdf_transfer_out',
			'queueID'        : 'fileStatus',
			'fileDesc'		 : 'PDF Files',
			'fileExt'		 :'*.pdf',
			'auto'           : true,
			'width'			 : '367',
			'height'		 : '251',
			'onComplete': function(event, queueID, fileObj, response, data) {
				 $('#fileStatus').html(fileObj.name +' uploaded.');
				 
				 //GET VALUES TO SAVE UPLOADED FILE TO DATABASE
				 var formInput = 'class=order_system&function=uploaded_file&type=json';
				 formInput += '&filename='+response+'&userfilename='+fileObj.name+'&filesize='+fileObj.size+'&accountid='+getCookie('accountid')+'&userid='+getCookie('userid');
				 $.post("../../api/index.php",formInput, 
				function(data){
				if(data.error==true){
					$('#form_error').html(data.msg); 
					$('#form_error').fadeIn();
				} else {
					$('#form_error').html(data.msg); 
					$('#form_error').fadeIn();
					location.href="create_complete.php";//redirection
				}
					
			}, "json");	
			}
		});
	}
	
///////////////////
//PF#D UPLOAD FORM
///////////////////		
	
	if($('#pf3d_fileupload').length != 0	){
	
		$("#pf3d_fileupload").uploadify({
			'uploader'       : '../scripts/uploadify.swf',
			'script'         : '../scripts/uploadify.php',
			'cancelImg'      : '../assets/images/cancel.png',
			'folder'         : '../pdf_transfer_out',
			'queueID'        : 'fileStatus',
			'fileDesc'		 : 'PDF Files',
			'fileExt'		 :'*.pdf',
			'auto'           : true,
			'width'			 : '367',
			'height'		 : '251',
			'onComplete': function(event, queueID, fileObj, response, data) {
				 $('#fileStatus').html(fileObj.name +' uploaded.');
				 
				 //GET VALUES TO SAVE UPLOADED FILE TO DATABASE
				 var formInput = 'class=order_system&function=pf3d_uploaded_file&type=json';
				 formInput += '&filename='+response+'&userfilename='+fileObj.name+'&filesize='+fileObj.size+'&accountid='+getCookie('accountid')+'&userid='+getCookie('userid');
				 $.post("../../api/index.php",formInput, 
				function(data){
				if(data.error==true){
					$('#form_error').html(data.msg); 
					$('#form_error').fadeIn();
				} else {
					$('#form_error').html(data.msg); 
					$('#form_error').fadeIn();
					location.href="create_complete.php";//redirection
				}
					
			}, "json");	
			}
		});
	}	
	
	
///////////////////
//BUY CREDITS FORM	
///////////////////		
	if($('#credits').length != 0	){	
		$('#credits').keyup(function() {
 		 	//GET CREDIT VALUE
			var value=$('#credits').val();
			var cpc=0;
			value = value.replace(/,/g,"");
     		if (isNaN(value)||!value) {
				//set control back to zero
				value=0;
				$('#credits').val('');		
				$('#costpercredit').val(cpc);
				//ZERO IT OUT TOTAL
				totalvalue=value*cpc;
				$('#ccount').html(value);
				$('#priceper').html(cpc);
				$('#etotal').html(totalvalue);
			}else{
				//get costpercredit
				var formInput = 'class=order_system&function=get_costpercredit&type=json';
				formInput += '&credits='+value+'&accountid='+getCookie('accountid');
				$.post("../../api/index.php",formInput, 
				function(data){
					if(data.error==true){
						$('#form_error').html(data.msg); 
						$('#form_error').fadeIn();
						cpc=0
						value=0
						$('#credits').val(value);
					} else {
						cpc=data.msg;
						cpc=Math.round(cpc);
					}
					value=Math.round(value);
					$('#costpercredit').val(cpc);					
					totalvalue=value*cpc;
					$('#ccount').html(formatCurrency(value,1));
					$('#priceper').html(formatCurrency(cpc,0));
					$('#etotal').html(formatCurrency(totalvalue,1));
					//$('#credit_cost_summary').html('<p>'+formatCurrency(value,1)+' credits @ $'+cpc+'/credit.</p><p>*estimated cost to purchase '+formatCurrency(value,1)+' credits : $'+formatCurrency(totalvalue,1)+'<p><br /><br /></p>');
				},"json");
			}
		});
		/*
		$('#credits').spinbox({
			min: 1,    // Set lower limit or null for no limit.
  			max: 10000,  // Set upper limit or null for no limit.
  			step: 1, // Set increment size.);
		});
		*/
	}
	
	$('#submit_form_buy_credits').click(function() {
		$('#form_buy_credits').submit();
	});
	
	$('#form_buy_credits').submit(function(){
		// submit the login form
		$('#form_error').hide();
		var hasError = false;
		
		
		// validate the form
		if($('#credits').val()<1){
			$('#form_error').html('You must enter at least 1 credit.'); 
			$('#form_error').fadeIn();
			hasError=true;
		}else{
			//confirm the purchase
			$('#form_buy_credits').css('opacity',0.2);

			
			jConfirm('You are about to purchase '+$('#credits').val()+' credit(s)?', 'Purchase Confirmation', function(r) {
    			if(r==false){
			    	$('#form_error').html('Purchase Cancelled.'); 
			    	$('#form_error').fadeIn();				
					$('#form_buy_credits').css('opacity',1)	;
				}else{
					$('#form_buy_credits').css('opacity',1)	;

					$('#submit_form_buy_credits').html('<img src="../assets/images/loader.gif" alt="Loading…" />');
					//UPDATE THE DB OF PRUCHASE
					var formInput = $('#form_buy_credits').serialize();
					formInput += '&class=order_system&function=purchase_credits&type=json&accountid='+getCookie('accountid')+'&userid='+getCookie('userid');
					$.post("../../api/index.php",formInput, 
					function(data){
						//console.log(data.response);
						if(data.error==true){
							$('#form_error').html(data.msg); 
							$('#form_error').fadeIn();
							$('#submit_form_buy_credits').html('<img src="../assets/images/next_orange.png" alt="Loading…" />');
						} else {
							$('#form_error').html(data.msg); 
							$('#form_error').fadeIn();
							$('#submit_form_buy_credits').html('<img src="../assets/images/next_orange.png" alt="Loading…" />');
							location.href="credits_complete.php";//redirection
						}
					
					
					}, "json");
				}
			});
		}
		return false;
	});
///////////////////
//PROFILE EDITOR
///////////////////		
	$('#submit_form_update_profile').click(function() {
		$('#form_update_profile').submit();
	});
	
	$('#form_update_profile').submit(function(){
		// submit the login form
		$('#form_error').hide();
		var hasError = false;
		
		
		// validate the form
		if($('#email').val().length<1 || $('#name').val().length<1){
			$('#form_error').html('You must enter an Account Name and Email.'); 
			$('#form_error').fadeIn();
			hasError=true;
		}
		if($('#password').val().length<6 || $('#verify').val().length<6){
			$('#form_error').html('Your password must be at least 6 characters long.'); 
			$('#form_error').fadeIn();
			hasError=true;
		}
		if($('#password').val() != $('#verify').val()){
			$('#form_error').html('Your password and verification do not match.'); 
			$('#form_error').fadeIn();
			hasError=true;
		}
		
		
		if(!hasError) {
			$('#submit_form_update_profile').html('<img src="../assets/images/loader.gif" alt="Loading…" />');
			//UPDATE THE DB OF ACCOUNT NAME AND EMAIL OF USER
			var formInput = $('#form_update_profile').serialize();
			formInput += '&class=order_system&function=update_profile&type=json&accountid='+getCookie('accountid')+'&userid='+getCookie('userid');
			$.post("../../api/index.php",formInput, 
			function(data){
			//console.log(data.response);
				if(data.error==true){
					$('#form_error').html(data.msg); 
					$('#form_error').fadeIn();
					$('#submit_form_update_profile').html('<img src="../assets/images/next_orange.png" alt="Loading…" />');
				} else {
					$('#form_error').html(data.msg); 
					$('#form_error').fadeIn();
					$('#submit_form_update_profile').html('<img src="../assets/images/next_orange.png" alt="Loading…" />');
					location.href="profile_complete.php";//redirection
				}
			}, "json");
		}
		return false;
	});	
///////////////////
//EDIT DETAILS
///////////////////	
	if($('#shorttitle').length != 0	){	
		$('#shorttitle').keyup(function(){
       		if ($('#shorttitle').val().match(/[^a-zA-Z0-9/_]/g)) {
           		$('#shorttitle').val($('#shorttitle').val().replace(/[^a-zA-Z0-9/_]/g, ''));
       		}//end if
   	 	}); // end keyup
	}

	$('#submit_form_update_book').click(function() {
		$('#form_update_book').submit();
	});
	
	$('#form_update_book').submit(function(){
		// submit the login form
		$('#form_error').hide();
		var hasError = false;
		
		
		// validate the form
		if($('#title').val().length<1 || $('#shorttitle').val().length<1){
			$('#form_error').html('You must enter a Title and Short Title.'); 
			$('#form_error').fadeIn();
			hasError=true;
		}
		
		if($('#protect').is(':checked') && $('#password').val().length<5){
			$('#form_error').html('You must enter a password of at least 5 digits.'); 
			$('#form_error').fadeIn();
			hasError=true;
		}		
		
		if(!hasError) {
			$('#submit_form_update_book').html('<img src="../assets/images/loader.gif" alt="Loading…" />');
			//UPDATE THE DB OF ACCOUNT NAME AND EMAIL OF USER
			var formInput = $('#form_update_book').serialize();
			formInput += '&class=order_system&function=update_book_details&type=json&accountid='+getCookie('accountid')+'&bookid='+getCookie('bookid');
			$.post("../../api/index.php",formInput, 
			function(data){
			//console.log(data.response);
				if(data.error==true){
					$('#form_error').html(data.msg); 
					$('#form_error').fadeIn();
					$('#submit_form_update_book').html('<img src="../assets/images/next_orange.png" alt="Loading…" />');
				} else {
					$('#form_error').html(data.msg); 
					$('#form_error').fadeIn();
					$('#submit_form_update_book').html('<img src="../assets/images/next_orange.png" alt="Loading…" />');
					location.href="edit_book.php";//redirection
				}
			}, "json");
		}
		return false;
	});	

///////////////////
//ADD CUSTOMER & ADD PRINTER
///////////////////	
	if($('#shorttitle').length != 0	){	
		$('#shorttitle').keyup(function(){
       		if ($('#shorttitle').val().match(/[^a-zA-Z0-9/_]/g)) {
           		$('#shorttitle').val($('#shorttitle').val().replace(/[^a-zA-Z0-9/_]/g, ''));
       		}//end if
   	 	}); // end keyup
	}

	$('#submit_form_add_customer').click(function() {
		$('#form_add_customer').submit();
	});
	
	$('#form_add_customer').submit(function(){
		// submit the login form
		$('#form_error').hide();
		var hasError = false;
		
		
		// validate the form
		if($('#title').val().length<1 || $('#shorttitle').val().length<1){
			$('#form_error').html('You must enter a Customer Name and Short Name.'); 
			$('#form_error').fadeIn();
			hasError=true;
		}
		
		if(!hasError) {
			$('#submit_form_add_customer').html('<img src="../assets/images/loader.gif" alt="Loading…" />');
			//UPDATE THE DB OF ACCOUNT NAME AND EMAIL OF USER
			var formInput = $('#form_add_customer').serialize();
			formInput += '&class=order_system&function=add_customer&type=json&accounttype=1&accountid='+getCookie('accountid');
			$.post("../../api/index.php",formInput, 
			function(data){
			//console.log(data.response);
				if(data.error==true){
					$('#form_error').html(data.msg); 
					$('#form_error').fadeIn();
					$('#submit_form_add_customer').html('<img src="../assets/images/next_orange.png" alt="Loading…" />');
				} else {
					$('#form_error').html(data.msg); 
					$('#form_error').fadeIn();
					$('#submit_form_add_customer').html('<img src="../assets/images/next_orange.png" alt="Loading…" />');
					//alert($('#refer').val());
					location.href=$('#refer').val();//redirection
				}
			}, "json");
		}
		return false;
	});	
	
	
	$('#submit_form_add_printer').click(function() {
		$('#form_add_printer').submit();
	});
	
	$('#form_add_printer').submit(function(){
		// submit the login form
		$('#form_error').hide();
		var hasError = false;
		
		
		// validate the form
		if($('#title').val().length<1 || $('#shorttitle').val().length<1){
			$('#form_error').html('You must enter a Printer Name and Short Name.'); 
			$('#form_error').fadeIn();
			hasError=true;
		}
		
		if(!hasError) {
			$('#submit_form_add_printer').html('<img src="../assets/images/loader.gif" alt="Loading…" />');
			//UPDATE THE DB OF ACCOUNT NAME AND EMAIL OF USER
			var formInput = $('#form_add_printer').serialize();
			formInput += '&class=order_system&function=add_customer&type=json&accounttype=0&accountid=0'; //NO PARENTID
			$.post("../../api/index.php",formInput, 
			function(data){
			//console.log(data.response);
				if(data.error==true){
					$('#form_error').html(data.msg); 
					$('#form_error').fadeIn();
					$('#submit_form_add_printer').html('<img src="../assets/images/next_orange.png" alt="Loading…" />');
				} else {
					$('#form_error').html(data.msg); 
					$('#form_error').fadeIn();
					$('#submit_form_add_printer').html('<img src="../assets/images/next_orange.png" alt="Loading…" />');
					//alert($('#refer').val());
					location.href=$('#refer').val();//redirection
				}
			}, "json");
		}
		return false;
	});
	
/////////////////////
//CREATE FROM COPY
////////////////////
	
	
	$('#submit_form_copy').click(function() {
		$('#form_copy').submit();
	});
	
	$('#form_copy').submit(function(){
		// submit the login form
		$('#form_error').hide();
		var hasError = false;
		
		
		// validate the form
		if($('#books').val()<1){
			$('#form_error').html('You must select a book.'); 
			$('#form_error').fadeIn();
			hasError=true;
		}else{
			//confirm the purchase
			$('#form_copy').css('opacity',0.2);

			
			jConfirm('You are about to create a book from another book, You will be charged 1 credit?', 'Purchase Confirmation', function(r) {
    			if(r==false){
			    	$('#form_error').html('Purchase Cancelled.'); 
			    	$('#form_error').fadeIn();				
					$('#form_copy').css('opacity',1)	;
				}else{
					$('#form_copy').css('opacity',1)	;

					$('#submit_form_copy').html('<img src="../assets/images/loader.gif" alt="Loading…" />');
					//UPDATE THE DB OF BOOK
					var formInput = $('#form_copy').serialize();
					formInput += '&class=order_system&function=copy_book&type=json&accountid='+getCookie('accountid')+'&userid='+getCookie('userid');
					$.post("../../api/index.php",formInput, 
					function(data){
						//console.log(data.response);
						if(data.error==true){
							$('#form_error').html(data.msg); 
							$('#form_error').fadeIn();
							$('#submit_form_copy').html('<img src="../assets/images/next_orange.png" alt="Loading…" />');
						} else {
							$('#form_error').html(data.msg); 
							$('#form_error').fadeIn();
							$('#submit_form_copy').html('<img src="../assets/images/next_orange.png" alt="Loading…" />');
							location.href="copy_complete.php";//redirection -- book editing???
						}
					
					
					}, "json");
				}
			});
		}
		return false;
	});	
	

///////////////////
//SEND FOR REVIEW
///////////////////	
	$('#submit_form_send_review').click(function() {
		$('#form_send_review').submit();
	});
	
	$('#form_send_review').submit(function(){
		// submit the login form
		$('#form_error').hide();
		var hasError = false;
		
		
		// validate the form
		if($('#customers').val().length<1){
			$('#form_error').html('You must select a Customer.'); 
			$('#form_error').fadeIn();
			hasError=true;
		}
		
		if(!hasError) {
			$('#submit_form_send_review').html('<img src="../assets/images/loader.gif" alt="Loading…" />');
			//UPDATE THE DB OF ACCOUNT NAME AND EMAIL OF USER
			var formInput = $('#form_send_review').serialize();
			formInput += '&class=order_system&function=send_review&type=json&accountid='+getCookie('accountid')+'&userid='+getCookie('userid')+'&bookid='+getCookie('bookid');
			$.post("../../api/index.php",formInput, 
			function(data){
			//console.log(data.response);
				if(data.error==true){
					$('#form_error').html(data.msg); 
					$('#form_error').fadeIn();
					$('#submit_form_send_review').html('<img src="../assets/images/next_orange.png" alt="Loading…" />');
				} else {
					$('#form_error').html(data.msg); 
					$('#form_error').fadeIn();
					$('#submit_form_send_review').html('<img src="../assets/images/next_orange.png" alt="Loading…" />');
					//alert($('#refer').val());
					location.href="review_complete.php";//redirection
				}
			}, "json");
		}
		return false;
	});	

///////////////////
//PUBLISH
///////////////////	


	if($('#start_date').length != 0	){
		$("#start_date").datepicker({minDate: 0, maxDate: '+3M',onClose: function(dateText, inst) {
		$("#end_date" ).datepicker( "option", "minDate", dateText );
		//$("#end_date" ).datepicker( "option", "maxDate", '+15M' );
		}});
	}
	
	if($('#end_date').length != 0	){
		$("#end_date").datepicker({minDate: 0, maxDate: '+15M'});
	}
	
	
	$('#submit_form_publish').click(function() {
		$('#form_publish').submit();
	});
	
	$('#form_publish').submit(function(){
		// submit the login form
		$('#form_error').hide();
		var hasError = false;
		
		
		if(!hasError) {
			$('#submit_form_publish').html('<img src="../assets/images/loader.gif" alt="Loading…" />');
			//UPDATE THE DB OF ACCOUNT NAME AND EMAIL OF USER
			var formInput = $('#form_publish').serialize();
			formInput += '&class=order_system&function=publish&type=json&accountid='+getCookie('accountid')+'&userid='+getCookie('userid')+'&bookid='+getCookie('bookid');
			$.post("../../api/index.php",formInput, 
			function(data){
				if(data.error==true){
					$('#form_error').html(data.msg); 
					$('#form_error').fadeIn();
					$('#submit_form_publish').html('<img src="../assets/images/next_orange.png" alt="Loading…" />');
				} else {
					$('#form_error').html(data.msg); 
					$('#form_error').fadeIn();
					$('#submit_form_publish').html('<img src="../assets/images/next_orange.png" alt="Loading…" />');
					//alert($('#refer').val());
					location.href="publish_complete.php";//redirection
				}
			}, "json");
		}
		return false;
	});		
	function getCookie(c_name){
		if (document.cookie.length>0){
  			c_start=document.cookie.indexOf(c_name + "=");
  			if (c_start!=-1){
    			c_start=c_start + c_name.length+1;
    			c_end=document.cookie.indexOf(";",c_start);
    			if (c_end==-1) c_end=document.cookie.length;
    			return unescape(document.cookie.substring(c_start,c_end));
    		}
  		}
		return "";
	}
	
 function formatCurrency(strValue,cents)

 {

	strValue = strValue.toString().replace(/\$|\,/g,'');

	if (isNaN(parseFloat(strValue))) {

		strValue=0;

    }

	dblValue = parseFloat(strValue);



	blnSign = (dblValue == (dblValue = Math.abs(dblValue)));

	dblValue = Math.floor(dblValue*100+0.50000000001);

	intCents = dblValue%100;

	strCents = intCents.toString();

	dblValue = Math.floor(dblValue/100).toString();

	if(intCents<10)

		strCents = "0" + strCents;

	for (var i = 0; i < Math.floor((dblValue.length-(1+i))/3); i++)

		dblValue = dblValue.substring(0,dblValue.length-(4*i+3))+','+

		dblValue.substring(dblValue.length-(4*i+3));

	//return (((blnSign)?'':'-') + '$' + dblValue + '.' + strCents);

	strCents='.' + strCents;

	if(cents>0){

		strCents='';

	}

	return (((blnSign)?'':'-') + dblValue + strCents);

}







});

	function gCookie(c_name){
		if (document.cookie.length>0){
  			c_start=document.cookie.indexOf(c_name + "=");
  			if (c_start!=-1){
    			c_start=c_start + c_name.length+1;
    			c_end=document.cookie.indexOf(";",c_start);
    			if (c_end==-1) c_end=document.cookie.length;
    			return unescape(document.cookie.substring(c_start,c_end));
    		}
  		}
		return "";
	}


	function setBookID(bookid){
		// used by the flash book picker
		//submit to php to set cookie and then redirect
		var formInput = '&class=order_system&function=set_book_cookie&type=html&bookid='+bookid;
		$.post("../../api/index.php",formInput,function(data){location.href='edit_book.php';});
	}

	function getBookStatus(tableprefix){
	$.getJSON("http://flipbook3d.com/pdfauto/services/book_status.php?table_prefix="+tableprefix+"&type=json&callback=?", function(result){
			//alert(result.msg);
			return(result.msg);
  	})
	}

	function add_user(){
		jPrompt('Email Address:', 'format@email.com', 'Add Someone', function(r) {
    		if( r ){
				if(validate_email(r)){
					//GET ACCOUNT TO ADD USER
					customer=$('#customers').val();
					var formInput = '&class=order_system&function=add_user_customer&type=json&customerid='+customer+'&email='+r+'&accountid='+gCookie('accountid');
					$.post("../../api/index.php",formInput, 
					function(data){
						//console.log(data.response);
					if(data.error==true){
						$('#form_error').html(data.msg); 
						$('#form_error').fadeIn();
						$('#submit_form_update_profile').html('<img src="../assets/images/next_orange.png" alt="Loading…" />');
					} else {
						$('#form_error').html(data.msg); 
						$('#form_error').fadeIn();
						$('#submit_form_update_profile').html('<img src="../assets/images/next_orange.png" alt="Loading…" />');
						//RELOAD EMAIL IST
						load_email_list(customer);
					}
				}, "json");
			}else{
					jAlert('Please enter valid email address','Email Entry');
				}
			}
		});
	}
	
	
	function remove_user(userid){
		jConfirm('You are about to remove this user, This cannot be undone.', 'Remove User', function(r) {
    		if( r ){
				if( userid ){
					//REMOVE USER
					customer=$('#customers').val();
					var formInput = '&class=order_system&function=remove_user&type=json&userid='+userid;
					$.post("../../api/index.php",formInput, 
					function(data){
						//console.log(data.response);
						if(data.error==true){
							$('#form_error').html(data.msg); 
							$('#form_error').fadeIn();
							$('#submit_form_update_profile').html('<img src="../assets/images/next_orange.png" alt="Loading…" />');
						} else {
							$('#form_error').html(data.msg); 
							$('#form_error').fadeIn();
							$('#submit_form_update_profile').html('<img src="../assets/images/next_orange.png" alt="Loading…" />');
							//RELOAD EMAIL IST
							load_email_list(customer);
						}
					}, "json");
				}else{
					jAlert('There was a problem removing this user','User Remove');
				}
			}
		});
	}	
	
	function validate_email(text){
  		apos=text.indexOf("@");
  		dotpos=text.lastIndexOf(".");
  		if (apos<1||dotpos-apos<2){
    		return false;
		}else{
			return true;
		}
  	}

	function load_email_list(accountid){
		$('#email_list').load('email_list.php?accountid='+accountid,function(){$('#email_list').animate({opacity: 1}, 1000, function() {})});
	}
	
	function load_copy_book(bookid){
		//alert(bookid);
		if(bookid!=0){
			$('#copy_book').load('book_button.php?bookid='+bookid,function(){$('#copy_book').animate({opacity: 1}, 1000, function() {})});
		}else{
			$('#copy_book').html('');
		}
	}
	
	function add_customer(refer){
		location.href="add_customer.php?refer="+refer;//redirection
	}
	
	function add_printer(refer){
		location.href="add_printer.php?refer="+refer;//redirection
	}
	
	function updateLogos(){
		//alert('updateLogos Fired');
		var flash = getMovie('StyleEditor');
    	flash.update_logos();
		// for now reload this page, later get this to work, api call problem 
		window.location.reload(0);
	}
	
	 function getMovie(movieName) {
    	return document.getElementById(movieName);
 	}
	
	function openHelpMovie(movieName){
		Shadowbox.open({
				content:   movieName,
				player:     'qt',
				title:      'HELP',
				height: 	540,
				width:		960
		});	
	}
