// JavaScript Document
function toggleSubmenu(submenuId, show){
	var theSub=document.getElementById('submenu-'+submenuId);
	var theMenu=document.getElementById(submenuId);
	if(show==1){
		theSub.className='submenu';
		theMenu.className='menu-over';
	}
	else{
		theSub.className='submenu-hide';
		theMenu.className='';
	}
}

function toggleSubmenuH(submenuId, show){
	var theSub=document.getElementById('submenu-'+submenuId);
	var theMenu=document.getElementById(submenuId);
	if(show==1){
		theSub.className='submenu-h';
	}
	else{
		theSub.className='submenu-hide';
	}
}


$(function(){
		//Controll the main menu drop down list
		$('ul li',$('#mainMenu')).hover(
			function(){$(this).addClass('over');},
			function(){$(this).removeClass('over');}
			);
		
		$('ul li',$('#mainMenu')).click(
			function(){
					window.location.href=$('base').attr('href')+$('a',$(this)).attr('href');
				}
			);
		
		/**$('#top-menu a').hover(
			function(){$('.menu-item',$(this)).fadeOut('slow');},
			function(){$('.menu-item',(this)).fadeIn('slow');}
			);**/
		
		$('#top-menu a').each(function(i){
										   var currentUrl = window.location.href;
										   var position = parseInt(currentUrl.indexOf(($(this).attr('href'))))
										   if(position>0){
												$('.menu-item',$(this)).fadeOut();  
										   }else{
												$(this).hover(
														function(){$('.menu-item',$(this)).fadeOut('slow');},
														function(){$('.menu-item',(this)).fadeIn('slow');}
													  );   
										   }
									   });
		
		//Make the shipping details same as billing details
		$('#shippingController').click(function(){
			var theForm=document.forms['cart'];
			if(theForm.elements['controller'].checked){
				theForm.elements['dFirstName'].value=theForm.elements['bFirstName'].value;
				theForm.elements['dLastName'].value=theForm.elements['bLastName'].value;
				theForm.elements['dCompany'].value=theForm.elements['bCompany'].value;
				theForm.elements['dAddress'].value=theForm.elements['bAddress'].value;
				theForm.elements['dAddress1'].value=theForm.elements['bAddress1'].value;				
				theForm.elements['dCity'].value=theForm.elements['bCity'].value;		
				theForm.elements['dState'].value=theForm.elements['bState'].value;		
				theForm.elements['dPostcode'].value=theForm.elements['bPostcode'].value;				
				theForm.elements['dCountry'].value=theForm.elements['bCountry'].value;		
				theForm.elements['dPhone'].value=theForm.elements['bPhone'].value;
			}else{
				theForm.elements['dFirstName'].value='';
				theForm.elements['dLastName'].value='';
				theForm.elements['dCompany'].value='';
				theForm.elements['dAddress'].value='';
				theForm.elements['dAddress1'].value='';
				theForm.elements['dCity'].value='';		
				theForm.elements['dState'].value='';		
				theForm.elements['dPostcode'].value='';				
				theForm.elements['dCountry'].value='';		
				theForm.elements['dPhone'].value='';
			}
		});
		
		//User registration form verification
		$('#loginForm').submit(function(){
			if($('#member_pass').attr('value')!=$('#member_pass1').attr('value')){
				alert('Passwords entered are not the same!');
				return false;
			}
		});
		
		//product images lightbox
		$('#allImages a').lightBox();
		
		//main menu vertical align
		$('li>a',$('#mainMenu')).each(function(i){
											   var height=27;
											   $(this).css('top',(height-$(this).height())/2+'px');
											   });
		
		//search box text
		$('#searchFor').focus(function(){
									   	if(!$(this).attr('value')){
											$(this).css('backgroundImage',"none");
										}
									   });
		
		$('#searchFor').blur(function(){
									   	if(!$(this).attr('value')){
											$(this).css('backgroundImage',"url('images/bg-search.gif')");
										}
									   });
		
		//calculators
		$('#cal1-cal').click(function(){
									  try{
									  		var reelWeight=parseFloat($('#cal1-weight').attr('value'));
											var width=parseFloat($('#cal1-width').attr('value'));
											var gsm=parseFloat($('#cal1-gsm').attr('value'));
											var result=(reelWeight*100000)/(gsm*width);
											$('#cal1-result').attr('value',result.toFixed(2));
									  }catch(e){
											alert('Error calculating, please refresh your browser and try again!');  
									  }
									  
									  });
		
		$('#cal2-cal').click(function(){
									  try{
									  		var sheetNo=parseFloat($('#cal2-sheetno').attr('value'));
											var gsm=parseFloat($('#cal2-gsm').attr('value'));
											var length=parseFloat($('#cal2-length').attr('value'));
											var width=parseFloat($('#cal2-width').attr('value'));
											var result=(length*width*gsm*sheetNo)/1000;
											$('#cal2-result').attr('value',result.toFixed(2));
									  }catch(e){
											alert('Error calculating, please refresh your browser and try again!');  
									  }
									  
									  });
		
		$('#cal3-cal').click(function(){
									  try{
											var gsm=parseFloat($('#cal3-gsm').attr('value'));
											var length=parseFloat($('#cal3-length').attr('value'));
											var width=parseFloat($('#cal3-width').attr('value'));
											var result=1000000/(length*width*gsm);
											$('#cal3-result').attr('value',result.toFixed(0));
									  }catch(e){
											alert('Error calculating, please refresh your browser and try again!');  
									  }
									  
									  });
		
		$('#cal4-cal').click(function(){
									  try{
									  		var sheetNo=parseFloat($('#cal4-sheetno').attr('value'));
											var weight=parseFloat($('#cal4-weight').attr('value'));
											var length=parseFloat($('#cal4-length').attr('value'));
											var width=parseFloat($('#cal4-width').attr('value'));
											var result=(weight*1000)/(length*width*sheetNo);
											$('#cal4-result').attr('value',result.toFixed(0));
									  }catch(e){
											alert('Error calculating, please refresh your browser and try again!');  
									  }
									  
									  });
		
		$('#cal5-cal').click(function(){
									  try{
											var gsm=parseFloat($('#cal5-gsm').attr('value'));
											var length=parseFloat($('#cal5-length').attr('value'));
											var width=parseFloat($('#cal5-width').attr('value'));
											var result=gsm*length*width;
											$('#cal5-result').attr('value',result.toFixed(2));
									  }catch(e){
											alert('Error calculating, please refresh your browser and try again!');  
									  }
									  
									  });
		
		//the converter
		$('#metric').change(function(){
									 setMetricList($(this).attr('value'));
									 });
		
		$('#metric-from, #metric-to').change(function(){
															convert(false);
														 });
		
		$('#data-from').keyup(function(){
									  	convert(false);
									   });
		
		$('#data-to').keyup(function(){
									  	convert(true);
									   });
		
		
		//Paper selector
		$('#paper-selector').click(function(){
											$('#paper-search-result').html('<img src="images/icon-loading.gif" alt="Searching..." />');
											$.ajax({
													   type: "GET",
													   url: "paper-selector.php",
													   data:$('#paper-selector-form').serialize(),
													   dataType: 'json',
													   success: function(json){
																	var data=json;
																	if(data.result==1){
																		$('#paper-search-result').html(data.papers);
																		$('#paper-search-result').show('slow');
																	}else{
																		alert(data.msg);	
																	}
																}
													 });
										 });
		
		$('#paper-selector-reset').click(function(){
											$('#paper-search-result').html('');
											$('input[@type=checkbox]').attr('checked','');
											$('select').attr('value','');
											});
		
});

//converter metric select function
function setMetricList(metric){
	if(metric=='length'){
		var metricOptions='<option value="thous-inch">Thous./inch</option>'+
                                           '<option value="micron">Micron</option>'+
                                   		   '<option value="inch">Inch</option>'+
                                           '<option value="mm">Millimetre</option>';
		
		//In IE, just replacing the options of the selec list is not ok.. 
		//it needs to be a full select list with select tag and wrap with an outer container to achieve the same effect.
		$('#metric-from-select').html('<select id="metric-from">'+metricOptions+'</select>');
		$('#metric-to-select').html('<select id="metric-to">'+metricOptions+'</select>');
				
		$('#metric-to').attr('value','micron');
	}else{
		var metricOptions='<option value="lb">Pound</option>'+
                                           '<option value="kg">Kilogram</option>';
		
		$('#metric-from-select').html('<select id="metric-from">'+metricOptions+'</select>');
		$('#metric-to-select').html('<select id="metric-to">'+metricOptions+'</select>');
		
		$('#metric-to').attr('value','kg');	
	}
	
	$('#data-from,#data-to').attr('value','');
	
	//rebind events
	$('#metric-from, #metric-to').change(function(){
															convert(false);
														 });
}

//do the convertion
function convert(dataToChange){
	var metric=$('#metric').attr('value');	
	
	if(!dataToChange){ //switch around from and to fields
		var metricFrom=$('#metric-from').attr('value');
		var metricTo=$('#metric-to').attr('value');
		var dataFrom=parseFloat($('#data-from').attr('value'));
		var dataTo=parseFloat($('#data-to').attr('value'));
		var updateId='#data-to';
	}else{
		var metricFrom=$('#metric-to').attr('value');
		var metricTo=$('#metric-from').attr('value');
		var dataFrom=parseFloat($('#data-to').attr('value'));
		var dataTo=parseFloat($('#data-from').attr('value'));
		var updateId='#data-from';
	}
	
	if(dataFrom>0){
		var result=0;
		
		switch(metricFrom){
			case 'thous-inch':
				switch(metricTo){
					case 'thous-inch':
						result=dataFrom;
						break;
					case 'micron':
						result=dataFrom*25.4;
						break;
					case 'mm':
						result=dataFrom*25.4*0.001;
						break;
					case 'inch':
						result=dataFrom*0.001;
						break;
				}
				break;
			case 'micron':
				switch(metricTo){
					case 'thous-inch':
						result=dataFrom/25.4;
						break;
					case 'micron':
						result=dataFrom;
						break;
					case 'mm':
						result=dataFrom*0.001;
						break;
					case 'inch':
						result=dataFrom*0.001/25.4;
						break;
				}
				break;
			case 'inch':
				switch(metricTo){
					case 'thous-inch':
						result=dataFrom*0.001;
						break;
					case 'micron':
						result=dataFrom*25.4/0.001;
						break;
					case 'mm':
						result=dataFrom*25.4;
						break;
					case 'inch':
						result=dataFrom;
						break;
				}
				break;
			case 'mm':
				switch(metricTo){
					case 'thous-inch':
						result=dataFrom/0.001/25.4;
						break;
					case 'micron':
						result=dataFrom/0.001;
						break;
					case 'mm':
						result=dataFrom;
						break;
					case 'inch':
						result=dataFrom/25.4;
						break;
				}
				break;
			case 'lb':
				switch(metricTo){
					case 'kg':
						result=dataFrom*0.454;
						break;
					case 'lb':
						result=dataFrom;
						break;
				}
				break;		
			case 'kg':
				switch(metricTo){
					case 'kg':
						result=dataFrom;
						break;
					case 'lb':
						result=dataFrom*2.205;
						break;
				}
				break;	
		}
		
		$(updateId).attr('value',result.toFixed(3));
	}
}


//Animate the product detail slideshow images.
$(function(){
	var imagesUrl=Array();
	var mainImage=$('img').filter('#mainImage');
	var current=0;
	var interval=null;
	var intervalTime=5000;
	var thumbnailImages=$('img').filter('.thumbnail');
	
	thumbnailImages.each(function(i){
		var theSrc=this.src;
		var otherImagesUrl=Array();
		
		otherImagesUrl[0]=theSrc.replace(/thumb_/,'medium_');
		otherImagesUrl[1]=theSrc.replace(/thumb_/,'');
		imagesUrl[i]=otherImagesUrl;
		
		$(this).click( function(){
				clearInterval(interval);
				mainImage.attr('src',imagesUrl[i][0]).fadeIn();
				$('#theLink').attr('href',imagesUrl[i][1]);
				
				current=i;
				if(imagesUrl.length>1)//start slide show for over 1 image.
					interval=setInterval(slideShow,intervalTime);
				//The thumbnail class switcher
				thumbnailImages.each(function(i){
									if(i!=current)
										$(this).removeClass('thumbnail-over');
									else
										$(this).addClass('thumbnail-over');
								 });

			});
		
		$(this).mouseover( function(){
			if(current!=i)
				$(this).addClass('thumbnail-over');
			}
		);	

		$(this).mouseout( function(){
			if(current!=i)
				$(this).removeClass('thumbnail-over');
			}
		);	
	});

	function slideShow(){
			if(current<(imagesUrl.length-1))
				current++;
			else
				current=0; 
			
			mainImage.attr('src',imagesUrl[current][0]).fadeIn();
			$('#theLink').attr('href',imagesUrl[current][1]);
			
			thumbnailImages.each(function(i){
									if(i!=current)
										$(this).removeClass('thumbnail-over');
									else
										$(this).addClass('thumbnail-over');
								 });
			
		}
	
	
	$(thumbnailImages.get(0)).addClass('thumbnail-over');
	
	if(imagesUrl.length>1) //start slide show for over 1 image.
		interval=setInterval(slideShow, intervalTime);
});

//modal box for environmental details
$(function(){
 	 //select all the a tag with name equal to modal  
     $('a[name=modal]').click(function(e) {  
         //Cancel the link behavior  
         e.preventDefault();  
         //Get the A tag  
         var id = $(this).attr('href');  
       
         //Get the screen height and width  
         var maskHeight = $(document).height();  
         var maskWidth = $(window).width();  
       
         //Set height and width to mask to fill up the whole screen  
         $('#mask').css({'width':maskWidth,'height':maskHeight});  
           
         //transition effect       
         $('#mask').fadeIn(1000);      
         $('#mask').fadeTo("slow",0.8);    
       
         //Get the window height and width  
         var winH = $(window).height();  
         var winW = $(window).width();  
                 
         //Set the popup window to center  
         $(id).css('top',  winH/2-$(id).height()/2);  
         $(id).css('left', winW/2-$(id).width()/2);  
       
         //transition effect  
         $(id).fadeIn(2000);   
       
     });  
       
     //if close button is clicked  
     $('.window .close').click(function (e) {  
         //Cancel the link behavior  
         e.preventDefault();  
         $('#mask, .window').hide();  
     });       
       
     //if mask is clicked  
     $('#mask').click(function () {  
         $(this).hide();  
         $('.window').hide();  
     });
});


/*** 
============================================
Sample Sheets Orders 
=============================================
**/
$(function(){
		   //initialize envets
		   boundDeleteSheetEvent();
		   boundEditSheetEvent();
		   
		   //sheets section
		   $('#btn-sheet').click(function(){
											//reset the paper selection
											resetAddSheetForm();
										  	$('#sec-sheet-form').slideToggle('slow');
										  });
		   
		   //sheets section
		   $('#add-more-sheet').click(function(){
											//reset the paper selection
											resetAddSheetForm();
										  	$('#sec-sheet-form').slideDown('slow');
										  });
		   
		   $('#sheet_size').change(function(){
											$('#c_sheet_height').attr('value','');
											$('#c_sheet_width').attr('value','');
											});
											
		    $('#sheet_qty').change(function(){
											$('#c_sheet_qty').attr('value','');
											});
											
			$('#c_sheet_height,#c_sheet_width').keyup(function(){
											$('#sheet_size').attr('value','');
											});
			
			$('#c_sheet_qty').keyup(function(){
											$('#sheet_qty').attr('value','');
											});
		   
		   $('#sheet_paper').change(function(){
											 	$.ajax({
													   type: "GET",
													   url: "order-samples.php",
													   data:"task=paper_change&paperId="+$('#sheet_paper').attr('value'),
													   dataType: 'json',
													   async: false, //make sure other action perform after result has been return.
													   success: function(json){
																	var data=json;
																	if(data.result==1){
																		$('#sheet_color').html(data.colors);
																		//$('#sheet_finish').html(data.finishes);
																		$('#sheet_weight').html('<option value="">Weight</option>');
																	}else{
																		alert(data.msg);	
																	}
																}
													 });
											 });
		   
		   //color changes
		   $('#sheet_color').change(function(){
											 	$.ajax({
													   type: "GET",
													   url: "order-samples.php",
													   data: "task=color_change&paperId="+$('#sheet_paper').attr('value')+"&colorId="+$('#sheet_color').attr('value'),
													   dataType: 'json',
													   async: false,
													   success: function(json){
														  			if(json.result==1){
																		$('#sheet_weight').html(json.weights);
																	}else{
																		alert(json.msg);	
																	}
													   			}
													   });
											 });
		   
		   
		   //save sheet
		   $('#sheet_save').click(function(){
										   	 	$.ajax({
													   type: "GET",
													   url: "order-samples.php",
													   data: $('#sheetForm').serialize(),
													   dataType: 'json',
													   success: function(json){
														   			if(json.result==1){
																		$('#sec-sheet-cart').html(json.sheets);
																		//rebind the envents
																		boundDeleteSheetEvent();
																		boundEditSheetEvent();
																		$('#add-more-sheet').click(function(){
																												resetAddSheetForm();
																												$('#sec-sheet-form').slideDown('slow');
																												});
																		$('#sec-sheet-form').slideUp('slow');
																	}else{
																		alert(json.msg);	
																	}
													   			}
													   });
										   });
		   
		   //cancel sheet
		   $('#sheet_cancel').click(function(){
											 	resetAddSheetForm();
												$('#sec-sheet-form').slideUp('slow');
											 });
		   
		   
		   });


//reset add sheet form
function resetAddSheetForm(){
	$('#sheet_paper').attr('value','');
	$('#sheet_paper').trigger('change');
	$('#sheet_size').attr('value','');
	$('#c_sheet_height').attr('value','');
	$('#c_sheet_width').attr('value','');
	$('#sheet_qty').attr('value','');
	$('#c_sheet_qty').attr('value','');
	$('#sheet_id').attr('value','');
}

//delete sheet bounding event
function boundDeleteSheetEvent(){
	//rebind the envents
	$('.delete-sheet').click(function(){
										var sheetId=parseInt($(this).attr('title'));
										$.ajax({
											   type: "GET",
											   url: "order-samples.php",
											   data: "task=delete_sheet&sheetId="+sheetId,
											   dataType: 'json',
											   success: function(json){
															if(json.result==1){
																$('#sheet-'+sheetId).remove();
															}else{
																alert(json.msg);
															}
														}
											   });
									});

}

//edit sheet bounding event
function boundEditSheetEvent(){
	//rebind the envents
	$('.edit-sheet').click(function(){
										var sheetId=parseInt($(this).attr('title'));
										$('#sheet-loading').css('display','block');
										$.ajax({
											   type: "GET",
											   url: "order-samples.php",
											   data: "task=edit_sheet&sheetId="+sheetId,
											   dataType: 'json',
											   success: function(json){
															if(json.result==1){
																$('#sheet_paper').attr('value',json.sheet.sheet_paper);
																$('#sheet_paper').trigger('change');
																$('#sheet_color').attr('value',json.sheet.sheet_color);
																$('#sheet_color').trigger('change');
																//$('#sheet_finish').attr('value',json.sheet.sheet_finish);
																$('#sheet_weight').attr('value',json.sheet.sheet_weight);
																$('#sheet_size').attr('value',json.sheet.sheet_size);
																$('#c_sheet_height').attr('value',json.sheet.c_sheet_height);
																$('#c_sheet_width').attr('value',json.sheet.c_sheet_width);
																$('#sheet_qty').attr('value',json.sheet.sheet_qty);
																if(json.sheet.c_sheet_qty>0){
																	$('#c_sheet_qty').attr('value',json.sheet.c_sheet_qty);
																}
																$('#sheet_id').attr('value',json.sheet.sheet_id);
																$('#sec-sheet-form').slideDown('slow');
															}else{
																alert(json.msg);
															}
															$('#sheet-loading').css('display','none');
														}
											   });
									});

}

/*** 
============================================
Sample Sheets Orders ==== END
=============================================
**/

/*** 
============================================
Mockup Orders 
=============================================
**/
$(function(){
		   //initialize envets
		   boundDeleteMockupEvent();
		   boundEditMockupEvent();
		   
		   //sheets section
		   $('#btn-mockup').click(function(){
											//reset the paper selection
											resetAddMockupForm();
										  	$('#sec-mockup-form').slideToggle('slow');
										  });
		   
		   
		   $('#add-more-mockup').click(function(){
											//reset the paper selection
											resetAddMockupForm();
										  	$('#sec-mockup-form').slideDown('slow');
										  });
		  	
		    $('#mockup_finish_size').change(function(){
											$('#c_mockup_finish_height').attr('value','');
											$('#c_mockup_finish_width').attr('value','');
											});
			
			$('#c_mockup_finish_height,#c_mockup_finish_width').keyup(function(){
																			    $('#mockup_finish_size').attr('value','');
																			   });
		   
		   //paper changes
		   $('#mockup_cover_paper').change(function(){
											 	$.ajax({
													   type: "GET",
													   url: "order-samples.php",
													   data:"task=paper_change&paperId="+$(this).attr('value'),
													   dataType: 'json',
													   async: false, //make sure other action perform after result has been return.
													   success: function(json){
																	var data=json;
																	if(data.result==1){
																		$('#mockup_cover_color').html(data.colors);
																		//$('#mockup_cover_finish').html(data.finishes);
																		$('#mockup_cover_weight').html('<option value="">Weight</option>');
																	}else{
																		alert(data.msg);	
																	}
																}
													 });
											 });
		   
		   $('#mockup_text_paper').change(function(){
											 	$.ajax({
													   type: "GET",
													   url: "order-samples.php",
													   data:"task=paper_change&paperId="+$(this).attr('value'),
													   dataType: 'json',
													   async: false, //make sure other action perform after result has been return.
													   success: function(json){
																	var data=json;
																	if(data.result==1){
																		$('#mockup_text_color').html(data.colors);
																		//$('#mockup_text_finish').html(data.finishes);
																		$('#mockup_text_weight').html('<option value="">Weight</option>');
																	}else{
																		alert(data.msg);	
																	}
																}
													 });
											 });
		   
		   $('#mockup_other_paper').change(function(){
											 	$.ajax({
													   type: "GET",
													   url: "order-samples.php",
													   data:"task=paper_change&paperId="+$(this).attr('value'),
													   dataType: 'json',
													   async: false, //make sure other action perform after result has been return.
													   success: function(json){
																	var data=json;
																	if(data.result==1){
																		$('#mockup_other_color').html(data.colors);
																		//$('#mockup_other_finish').html(data.finishes);
																		$('#mockup_other_weight').html('<option value="">Weight</option>');
																	}else{
																		alert(data.msg);	
																	}
																}
													 });
											 });
		   
		   //color changes
		   $('#mockup_cover_color').change(function(){
											 	$.ajax({
													   type: "GET",
													   url: "order-samples.php",
													   data: "task=color_change&paperId="+$('#mockup_cover_paper').attr('value')+"&colorId="+$(this).attr('value'),
													   dataType: 'json',
													   async: false,
													   success: function(json){
														  			if(json.result==1){
																		$('#mockup_cover_weight').html(json.weights);
																	}else{
																		alert(json.msg);	
																	}
													   			}
													   });
											 });
		   
		   $('#mockup_text_color').change(function(){
											 	$.ajax({
													   type: "GET",
													   url: "order-samples.php",
													   data: "task=color_change&paperId="+$('#mockup_text_paper').attr('value')+"&colorId="+$(this).attr('value'),
													   dataType: 'json',
													   async: false,
													   success: function(json){
														  			if(json.result==1){
																		$('#mockup_text_weight').html(json.weights);
																	}else{
																		alert(json.msg);	
																	}
													   			}
													   });
											 });
		   
		   $('#mockup_other_color').change(function(){
											 	$.ajax({
													   type: "GET",
													   url: "order-samples.php",
													   data: "task=color_change&paperId="+$('#mockup_other_paper').attr('value')+"&colorId="+$(this).attr('value'),
													   dataType: 'json',
													   async: false,
													   success: function(json){
														  			if(json.result==1){
																		$('#mockup_other_weight').html(json.weights);
																	}else{
																		alert(json.msg);	
																	}
													   			}
													   });
											 });
		   
		   
		   //save sheet
		   $('#mockup_save').click(function(){
										   	 	$.ajax({
													   type: "GET",
													   url: "order-samples.php",
													   data: $('#mockupForm').serialize(),
													   dataType: 'json',
													   success: function(json){
														   			if(json.result==1){
																		$('#sec-mockup-cart').html(json.mockups);
																		//rebind the envents
																		boundDeleteMockupEvent();
																		boundEditMockupEvent();
																		$('#add-more-mockup').click(function(){
																												resetAddMockupForm();
																												$('#sec-mockup-form').slideDown('slow');
																												});
																		$('#sec-mockup-form').slideUp('slow');
																	}else{
																		alert(json.msg);	
																	}
													   			}
													   });
										   });
		   
		   //cancel sheet
		   $('#mockup_cancel').click(function(){
											 	resetAddMockupForm();
												$('#sec-mockup-form').slideUp('slow');
											 });
		   
		   
		   });


//reset add sheet form
function resetAddMockupForm(){
	$('#mockup_qty').attr('value','');
	$('#mockup_finish_size').attr('value','');
	$('#mockup_binding').attr('value','');
	$('#mockup_orientation').attr('value','');
	$('#c_mockup_finish_height').attr('value','');
	$('#c_mockup_finish_width').attr('value','');
	
	$('#mockup_cover_paper').attr('value','');
	$('#mockup_cover_paper').trigger('change');
	$('#mockup_cover_pages').attr('value','');
	
	$('#mockup_text_paper').attr('value','');
	$('#mockup_text_paper').trigger('change');
	$('#mockup_text_pages').attr('value','');
	
	$('#mockup_other_paper').attr('value','');
	$('#mockup_other_paper').trigger('change');
	$('#mockup_other_pages').attr('value','');
	
	$('#mockup_id').attr('value','');
}

//delete mockup bounding event
function boundDeleteMockupEvent(){
	//rebind the envents
	$('.delete-mockup').click(function(){
										var mockupId=parseInt($(this).attr('title'));
										$.ajax({
											   type: "GET",
											   url: "order-samples.php",
											   data: "task=delete_mockup&mockupId="+mockupId,
											   dataType: 'json',
											   success: function(json){
															if(json.result==1){
																$('#mockup-'+mockupId).remove();
															}else{
																alert(json.msg);
															}
														}
											   });
									});

}

//edit mockup bounding event
function boundEditMockupEvent(){
	//rebind the envents
	$('.edit-mockup').click(function(){
										var mockupId=parseInt($(this).attr('title'));
										$('#mockup-loading').css('display','block');
										$.ajax({
											   type: "GET",
											   url: "order-samples.php",
											   data: "task=edit_mockup&mockupId="+mockupId,
											   dataType: 'json',
											   success: function(json){
															if(json.result==1){
																$('#mockup_qty').attr('value',json.mockup.mockup_qty);
																$('#mockup_binding').attr('value',json.mockup.mockup_binding);
																$('#mockup_orientation').attr('value',json.mockup.mockup_orientation);
																$('#mockup_finish_size').attr('value',json.mockup.mockup_finish_size);
																$('#c_mockup_finish_height').attr('value',json.mockup.c_mockup_finish_height);
																$('#c_mockup_finish_width').attr('value',json.mockup.c_mockup_finish_width);
																
																$('#mockup_cover_paper').attr('value',json.mockup.mockup_cover_paper);
																$('#mockup_cover_paper').trigger('change');
																$('#mockup_cover_color').attr('value',json.mockup.mockup_cover_color);
																$('#mockup_cover_color').trigger('change');
																$('#mockup_cover_finish').attr('value',json.mockup.mockup_cover_finish);
																$('#mockup_cover_weight').attr('value',json.mockup.mockup_cover_weight);
																$('#mockup_cover_pages').attr('value',json.mockup.mockup_cover_pages);
																
																$('#mockup_text_paper').attr('value',json.mockup.mockup_text_paper);
																$('#mockup_text_paper').trigger('change');
																$('#mockup_text_color').attr('value',json.mockup.mockup_text_color);
																$('#mockup_text_color').trigger('change');
																$('#mockup_text_finish').attr('value',json.mockup.mockup_text_finish);
																$('#mockup_text_weight').attr('value',json.mockup.mockup_text_weight);
																$('#mockup_text_pages').attr('value',json.mockup.mockup_text_pages);
																
																$('#mockup_other_paper').attr('value',json.mockup.mockup_other_paper);
																$('#mockup_other_paper').trigger('change');
																$('#mockup_other_color').attr('value',json.mockup.mockup_other_color);
																$('#mockup_other_color').trigger('change');
																$('#mockup_other_finish').attr('value',json.mockup.mockup_other_finish);
																$('#mockup_other_weight').attr('value',json.mockup.mockup_other_weight);
																$('#mockup_other_pages').attr('value',json.mockup.mockup_other_pages);
																
																$('#mockup_id').attr('value',json.mockup.mockup_id);
																$('#sec-mockup-form').slideDown('slow');
															}else{
																alert(json.msg);
															}
															$('#mockup-loading').css('display','none');
														}
											   });
									});

}

/**
=============================
Mockup END  =========
=============================
**/

/*** 
============================================
Envelopes Orders 
=============================================
**/
$(function(){
		   //initialize envets
		   boundDeleteEnvelopeEvent();
		   boundEditEnvelopeEvent();
		   
		   //envelope section
		   $('#btn-envelope').click(function(){
											//reset the paper selection
											resetAddEnvelopeForm();
										  	$('#sec-envelope-form').slideToggle('slow');
										  });
		   
		   $('#add-more-envelope').click(function(){
											//reset the paper selection
											resetAddEnvelopeForm();
										  	$('#sec-envelope-form').slideDown('slow');
										  });
		   
		   
		   //save envelope
		   $('#envelope_save').click(function(){
										   	 	$.ajax({
													   type: "GET",
													   url: "order-samples.php",
													   data: $('#envelopeForm').serialize(),
													   dataType: 'json',
													   success: function(json){
														   			if(json.result==1){
																		$('#sec-envelope-cart').html(json.envelopes);
																		//rebind the envents
																		boundDeleteEnvelopeEvent();
																		boundEditEnvelopeEvent();
																		$('#add-more-envelope').click(function(){
																												resetAddEnvelopeForm();
																												$('#sec-envelope-form').slideDown('slow');
																												});
																		$('#sec-envelope-form').slideUp('slow');
																	}else{
																		alert(json.msg);	
																	}
													   			}
													   });
										   });
		   
		   //cancel envelope
		   $('#envelope_cancel').click(function(){
											 	resetAddEnvelopeForm();
												$('#sec-envelope-form').slideUp('slow');
											 });
		   
		   
		   });


//reset add sheet form
function resetAddEnvelopeForm(){
	$('#envelope_type').attr('value','');
	$('#envelope_qty').attr('value','');
	$('#envelope_id').attr('value','');
}

//delete envelope bounding event
function boundDeleteEnvelopeEvent(){
	//rebind the envents
	$('.delete-envelope').click(function(){
										var envelopeId=parseInt($(this).attr('title'));
										$.ajax({
											   type: "GET",
											   url: "order-samples.php",
											   data: "task=delete_envelope&envelopeId="+envelopeId,
											   dataType: 'json',
											   success: function(json){
															if(json.result==1){
																$('#envelope-'+envelopeId).remove();
															}else{
																alert(json.msg);
															}
														}
											   });
									});

}

//edit sheet bounding event
function boundEditEnvelopeEvent(){
	//rebind the envents
	$('.edit-envelope').click(function(){
										var envelopeId=parseInt($(this).attr('title'));
										$('#envelope-loading').css('display','block');
										$.ajax({
											   type: "GET",
											   url: "order-samples.php",
											   data: "task=edit_envelope&envelopeId="+envelopeId,
											   dataType: 'json',
											   success: function(json){
															if(json.result==1){
																$('#envelope_id').attr('value',json.envelope.envelope_id);
																$('#envelope_type').attr('value',json.envelope.envelope_type);
																$('#envelope_qty').attr('value',json.envelope.envelope_qty);
																
																$('#sec-envelope-form').slideDown('slow');
															}else{
																alert(json.msg);
															}
															$('#envelope-loading').css('display','none');
														}
											   });
									});

}

/*** 
============================================
Sample Envelope Orders ==== END
=============================================
**/


/*** 
============================================
Sample Order
=============================================
**/
$(function(){
		   	currentDate=new Date();
			currentYear=currentDate.getFullYear();
			nextYear=currentYear+1;
		   	$("#sample_need_date").datepicker({dateFormat:'yy-mm-dd', showOn:'both', buttonImageOnly:true, buttonImage:'js/jquery.ui/datepicker/img/calendar2.gif', firstDay:1, yearRange: currentYear+':'+nextYear});
		    //save order
		   $('#btn_sample_save').click(function(){
										   	 	$.ajax({
													   type: "GET",
													   url: "order-samples.php",
													   data: $('#sampleForm').serialize()+"&sample_need_date="+$('#sample_need_date').attr('value'), //added because some how the date picker tamper with the result.
													   dataType: 'json',
													   success: function(json){
														   			if(json.result==1){
																		$('#order-form-area').slideUp('slow');
																		$('#order-form-confirm').slideDown('slow');
																	}else{
																		alert(json.msg);	
																	}
													   			}
													   });
										   });
		   
		   });