var page_array = new Array();
var invoice_array = new Array();
var billship = {};
var regions = {};
var cart_status = new Array();
var cart_button = new Array();

// Slides for the pages
var slds = {};
var last = 1;

// Slides for north american regions
var region_slides = {};
var myAccordion;

function show_return(){
	$('forgotlogin').setStyle('display','none');
	$('returnlogin').setStyle('display','block');
}
function show_forgot_login(){

	$('forgotlogin').setStyle('display','block');
	$('returnlogin').setStyle('display','none');
}
function output_file( filename ){

	$('filename').value = filename;
	$('output_file_form').submit();

}

function sld( show ){
	
	// Hide the yes no bit on step 6 and 7
	if( show >= 6 ){
		
		$('yesno-header').addClass('hide');
		$('location-header').removeClass('hide');		
		
	} else {
		
		$('yesno-header').removeClass('hide');		
		$('location-header').addClass('hide');		
		
	// if
	}
	
	$$('div.slide').each( function(e){
		
		if( e.id.replace(/question_div_/,'') != show ){
			$('step_' + e.id.replace(/question_div_/,'') ).removeClass('large');
			slds[ 'question_div_' + e.id.replace(/question_div_/,'') ].slideOut();

		// if
		}

	// if
	});

	// Set what step we're on
	$('step_' + show ).addClass('large');
	slds[ 'question_div_' + show ].slideIn();

// function
}

// This function completes the form submission with SEO friendly string. If javascript isn't enable, search still works
window.addEvent('domready', function(d){

	// This is for the checkout page
	if( $("cartproceed") ){
	
		$("proceed_with_co").addEvent('click', function(){
			
			var myFx = new Fx.Scroll(window).toElement('proceed_with_co');

			});
	
		// if
		}
	
	// This is for the account page
	if( $("history") ){
		
		// Crea the pages
		page_array[page_array.length] = 'status';
		page_array[page_array.length] = 'billing';
		page_array[page_array.length] = 'history';

	// if
	}
	
	if( $('generate') ){
		
				
		// Click on the steps functionality
		$('step').getChildren('span').each( function(e){
										
			e.addEvent('click', function(f){
										 
										 if( this.id.replace(/step_/,'') != 'step' ){
										 
											 sld( this.id.replace(/step_/,'') );
											 
										 // if
										 }
										 
										 });
		
		// each
		});

		// Create the slides
		$$('div.slide').each( function(e){
									
			if( e.id.replace(/question_div_/,'') == 1 ){
		
				slds[e.id] = new Fx.Slide(e.id);
				
			} else {
				
				slds[e.id] = new Fx.Slide(e.id).hide();
				
			// if
			}
			
			$( e ).getParent('div').addClass('span-20 last');

		// each
		});

		// Here's the accordion
		myAccordion = new Accordion($('accordion'), 'input.toggler', 'div.toggle', {
			start:'all-close',
			alwaysHide: true,
			opacity: true,
			onComplete: height_check
		});
		
		// When unselected checkbox, clear out options
		$$('input.toggler').each( function(e){ 	
									 
									 // Add an event handler for clicking on
									 e.addEvent('click',function(f){
										   
										  var count = 0;
										   
										  // assign a height to the wrapper div cause it's in an accordion
										  $$('input.toggler').each( function(e){ 
													
																		if( e.checked ){
																			
																			 count++
																			 
																		// if
																		}
													
																	});
										  if($('question_div_7')){
										  	$('question_div_7').getParent('div').setStyle('height','1250px');
										  }else{
											$('question_div_6').getParent('div').setStyle('height','1250px');											  											
										  }
										  // if
										  //}
											  
										  										   
										 // If we're unselected to see if we should uncheck it or not (not if it's closed)
										 if( !this.checked ){
											 
											var opened = true;
											 
											// Loop through the elements to see if we should deselect all ro not
											myAccordion.elements.each(function(element,i){
												
												// Check if this selected element is hidden or not											
												if( e.id.replace(/continent_/,'') == element.id && element.getStyle('visibility') == "hidden" ){
												
													opened = false;

												// if
												}
												
											});
											
											// If the slide is open deselect all
											if( opened ){
											
											   //Clear everything out
											   $('all_' + this.id.replace(/continent_/,'') ).setProperty('checked',false);
											   $('all_' + this.id.replace(/continent_/,'') ).fireEvent('click');
											   
											} else {
											
												f = new Event(f).stop();
												
											// if
											}
											 
										 // If the item is checked already
										 } 
										 
										});

									});
		
		// Get all the all checkboxed
		$$('.all').each(function(e){
								 
								 e.addEvent('click',function(f){
								 
									 if( this.checked ){
										 
										 tmp = true;
										 
									 } else {
										 
										 tmp = false;
										 
									 // uf
									 }
									 
									 // Loop through the elements that are in the container
									 $$('input.checkbox_' + this.id.replace(/all_/,'') ).each( function(f){ 	
																								   
																								   f.setProperty('checked',tmp);
																								   
																								   });
									 
									   // If we're in north america clear out the regions too
									   if( this.id.replace(/all_/,'') == 4 ){
																						 // remove all the clicked items
											 $('country_190').fireEvent('click');
											 $('country_191').fireEvent('click');
											 
										// if
									   }	

								 });

							 });
		
		// Get all the all checkboxed
		$$('.all_region').each(function(e){
								 
								 e.addEvent('click',function(f){
								 
									 if( this.checked ){
										 
										 tmp = true;
										 
									 } else {
										 
										 tmp = false;
										 
									 // uf
									 }
									 
									 // Loop through the elements that are in the container
									 $$('input.checkbox_region_' + this.id.replace(/all_/,'') ).each( function(f){ 	
																								   
																								   f.setProperty('checked',tmp);
																								   
																								   });

								 });

							 });
		
		// click functions for north america and canada
		$$('input#country_190,input#country_191').each( function(e) {
																 
										region_slides[ e.id.replace(/country_/,'') ] = new Fx.Slide('region_' + e.id.replace(/country_/,''),{
																																duration: 'short',
																													  			onStart: height_check
																													  }).hide();
										
										e.addEvent('click',function(f){
													   
											     // Check if goig in or out
												 if( e.checked && !region_slides[ e.id.replace(/country_/,'') ].open ){
													 
													 // slide in
													 region_slides[ e.id.replace(/country_/,'') ].slideIn();
												 
												 // Out
												 } else {
													 
													 // slide out
													 region_slides[ e.id.replace(/country_/,'') ].slideOut();
													 
													 // remove all the clicked items
													 $('all_region_' + e.id.replace(/country_/,'') ).set('checked',false);
													 $('all_region_' + e.id.replace(/country_/,'') ).fireEvent('click');
													 
												 // uf
												 }

										});
										
										$('region_' + e.id.replace(/country_/,'') ).getParent('div').addClass('span-15 last');

						   });
	
	// if
	}

// domread function
});

// Checks the overall height of the generate page
function height_check(){
	
	if ( myAccordion.elements[0].getStyle('height').replace(/px/,'') > 0 ){
		
		//if both Canada and US are checked
		if( $('country_190').checked && $('country_191').checked ){
			
			$('caus').getParent('div').setStyle('height', '1100px' );
			if($('question_div_7')){
				$('question_div_7').getParent('div').setStyle('height','1400px');
			}
			else{
				$('question_div_6').getParent('div').setStyle('height','1400px');				
			}
			//$('step-4').setStyle('display','block');
		
		//if Canada is checked
		} else if( $('country_190').checked && !$('country_191').checked ){
		
			$('caus').getParent('div').setStyle('height', '600px' );
			if($('question_div_7')){
				$('question_div_7').getParent('div').setStyle('height','900px');
			}
			else{
				$('question_div_6').getParent('div').setStyle('height','900px');				
			}
		
		//if US is checked
		} else if( !$('country_190').checked && $('country_191').checked ){
		
			$('caus').getParent('div').setStyle('height', '900px' );
			if($('question_div_7')){
				$('question_div_7').getParent('div').setStyle('height','1200px');
			}
			else{
				$('question_div_6').getParent('div').setStyle('height','1200px');				
			}
		// if
		
		//if none are checked
		} else {
			
			$('caus').getParent('div').setStyle('height', '400px' );
			if($('question_div_7')){
				$('question_div_7').getParent('div').setStyle('height','700px');
			}
			else{
				$('question_div_6').getParent('div').setStyle('height','700px');				
			}
			
		// if
		}
		
	} else if ( myAccordion.elements[1].getStyle('height').replace(/px/,'') > 0 ){
		
		$('caus').getParent('div').setStyle('height', '0px' );
			if($('question_div_7')){
				$('question_div_7').getParent('div').setStyle('height','750px');
			}
			else{
				$('question_div_6').getParent('div').setStyle('height','750px');				
			}

	// if
	} else if ( myAccordion.elements[2].getStyle('height').replace(/px/,'') > 0 ){
		
		$('caus').getParent('div').setStyle('height', '0px' );
			if($('question_div_7')){
				$('question_div_7').getParent('div').setStyle('height','700px');
			}
			else{
				$('question_div_6').getParent('div').setStyle('height','700px');				
			}

	// if
	} else if ( myAccordion.elements[3].getStyle('height').replace(/px/,'') > 0 ){
		
		$('caus').getParent('div').setStyle('height', '0px' );
			if($('question_div_7')){
				$('question_div_7').getParent('div').setStyle('height','550px');
			}
			else{
				$('question_div_6').getParent('div').setStyle('height','550px');				
			}

	// if
	} else if ( myAccordion.elements[4].getStyle('height').replace(/px/,'') > 0 ){
		
		$('caus').getParent('div').setStyle('height', '0px' );
			if($('question_div_7')){
				$('question_div_7').getParent('div').setStyle('height','550px');
			}
			else{
				$('question_div_6').getParent('div').setStyle('height','550px');				
			}

	// if
	} else if ( myAccordion.elements[5].getStyle('height').replace(/px/,'') > 0 ){
		
		$('caus').getParent('div').setStyle('height', '0px' );
			if($('question_div_7')){
				$('question_div_7').getParent('div').setStyle('height','800px');
			}
			else{
				$('question_div_6').getParent('div').setStyle('height','800px');				
			}

	// if
	} else {
		
		$('caus').getParent('div').setStyle('height', '0px' );
			if($('question_div_7')){
				$('question_div_7').getParent('div').setStyle('height','300px');
			}
			else{
				$('question_div_6').getParent('div').setStyle('height','300px');				
			}

	// if
	}
// if
}

// Page the individual function on a page
function page( layer ){

	for(i=0;i<page_array.length;i++){
	
		if( layer != page_array[i] ){

			$( page_array[i] ).setStyle('display','none');
		
		} else {
		
			$( page_array[i] ).setStyle('display','block');
		
		// if
		}
	
	// for
	}

// function
}

// Login from the checkout process
function login(){
	
	// Loop through the object
	for( var i=0;i<response.variables.meta.length;i++){

		var account_id = response.variables.meta[i].account_id;
		
		if( response.variables.meta[i].name == "billing_region" ){
			
			regions["billing"] = response.variables.meta[i].value;
			
		// if
		}
		
		if( response.variables.meta[i].name == "shipping_region" ){
			
			regions["shipping"] = response.variables.meta[i].value;
			
		// if
		}

		// check if the element exists
		if( $('account_meta.' + response.variables.meta[i].name) ){
		
			$('account_meta.' + response.variables.meta[i].name).value = response.variables.meta[i].value;
			
		// if
		}
		
	// for
	}
	
	// Now assign the correct 
	$$('input.account_id').each( function(element){
			
			element.value = response.variables.account_id;
			
	// each
	});

	//Show the billing page
	page('billing');

// function
}

// Set this as the account id
function register(){
	
	// Now assign the correct 
	$$('input.account_id').each( function(element){

			element.value = response.variables.account_id;
			
	// each
	});
	
	//Show the billing page
	page('billing');

// function
}