//Tabbladen bij productinformatie
var previoustab = document.getElementById( 'Tab1' );
var previoustext = document.getElementById( 'Text1' );

function ChangeClass( tab, text ) {
	/**
	 * @var tab that needs to be activated
	 * @var text related to tab that needs to be shown 
	 */				
	var tab = document.getElementById( tab );
	var text = document.getElementById( text );
					
	/* check to see if htmlObject is available */
	if (tab != null ){
		if ( tab.className == 'Inactive'  ) {
			/* if the current classname of tab and text is inactive then make this tab and text active */
			tab.className = 'Active';
			text.className = 'Active';
	    }
		// if the previous tab is set and active make it inactive
	   	if( previoustab != null && previoustab != tab ) { 
		   	previoustab.className = 'Inactive';
		} else {
			document.getElementById( 'Tab1' ).className = 'Inactive';
		}
	 	// if the previous text is set and active make it inactive
	    if( previoustext != null && previoustext != text ) {
		    previoustext.className = 'Inactive';
	    } else {
	    	document.getElementById( 'Text1' ).className = 'Inactive';
	    }
	    // set the current tab and text in previous tab and text
	    previoustab = tab;
	    previoustext = text;
	}
	return false;
}

function toggleOrderBox() {
	
	//Get element that we want to toggle
	var CloseElement = document.getElementById( 'OrderBox' );
	
	//If element was found
	if ( CloseElement ) {
		if ( CloseElement.style.display == 'none' ) {
			//Element is close, then display
			CloseElement.style.display = 'block';
			//Don't continue to next, return
			return false;
		} 

		if ( CloseElement.style.display == 'block' ) {
			//If the element is block, then set 'none'
			CloseElement.style.display = 'none';
			//Don't continue code
			return false;
		}
	}
}

function submitOrder( AbsPath ) {
	var ScriptURL = AbsPath + 'includes/SpecialPages/Shop/Product/Ajax.php';

	if ( document.OrderForm.productHoeveelheid ) {
		var Quantity = document.OrderForm.productHoeveelheid.options[document.OrderForm.productHoeveelheid.selectedIndex].value;
		
		if ( Quantity == 'null' )
			Quantity = 1;
	} else {
		var Quantity = 1;
	}
	
	if ( document.OrderForm.productKleur ) {
		var Color = document.OrderForm.productKleur.options[document.OrderForm.productKleur.selectedIndex].value;
	} else {
		var Color = 'null';
	}
	
	if ( document.OrderForm.productMaat ) {
		var Size = document.OrderForm.productMaat.options[document.OrderForm.productMaat.selectedIndex].value;
	} else {
		var Size = 'null';
	}
	
	/**
	 * Getting the HTTP Object
	 */
	var httpObject = getHTTPObject();
		
	/**
	 * Setting the total price (exclusive)
	 */
	//Opening the connection
	httpObject.open( "POST", ScriptURL, false );
	httpObject.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );
	
	//Setting up params and sending them
	var Params = 'Action=1&searchID=' + SearchID + '&productMaat=' + Size + '&productKleur=' + Color + '&productHoeveelheid=' +  Quantity + '&productID=' + ProductID;
	
	httpObject.send( Params );
	response = httpObject.responseText;
	
	//Based on the ajax response we will determin if it went ok or not.
	if ( response == 'true' ) {
		//HIDE STEP 1
		var OrderBoxContentStap1 = document.getElementById( 'OrderBoxContentStap1' );
		OrderBoxContentStap1.style.display = 'none';
		//SHOW STEP 2
		var OrderBoxContentStap2 = document.getElementById( 'OrderBoxContentStap2' );
		OrderBoxContentStap2.style.display = 'block';
		
		/*
		//Opening the connection
		httpObject.open( "POST", ScriptURL, false );
		httpObject.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );
		
		//Setting up params and sending them
		var Params = 'Action=2';
		httpObject.send( Params );
		
		var PriceLeft = httpObject.responseText;
		alert(PriceLeft);
		
		var DeliveryStep1 = document.getElementById( 'DeliveryStep1' );
		var DeliveryStep2 = document.getElementById( 'DeliveryStep2' );
		
		var Winkelmand_Stap1 = document.getElementById( 'Winkelmand_Stap1' );
		var Winkelmand_Stap2 = document.getElementById( 'Winkelmand_Stap2' );
		
		alert(Winkelmand_Stap1);
		alert(Winkelmand_Stap2);

		if ( DeliveryStep1 && DeliveryStep2 ) {
			
			if ( PriceLeft > 0 ) {
				var StillToOrder = document.getElementById( 'StillToOrder' );
				var Winkelmand_StillToOrder = document.getElementById( 'Winkelmand_StillToOrder' );

				//Formatting price
				PriceLeft = new Number( PriceLeft );
				PriceLeftFormatted = PriceLeft.toFixed( 2 );
				
				var PriceLeftFormatted = PriceLeftFormatted.replace( ".", ",");
				
				if ( StillToOrder && Winkelmand_StillToOrder ) {
					StillToOrder.innerHTML = PriceLeftFormatted;
					Winkelmand_StillToOrder.innerHTML = PriceLeftFormatted;
				}
				
				DeliveryStep1.className = 'Show';
				DeliveryStep2.className = 'Hide';
				
				Winkelmand_Stap1.className = 'Show';
				Winkelmand_Stap2.className = 'Hide';
			} else {
				DeliveryStep1.className = 'Hide';
				DeliveryStep2.className = 'Show';
				
				Winkelmand_Stap1.className = 'Hide';
				Winkelmand_Stap2.className = 'Show';
			}
		}
		*/
		/**
		 * Return totalprice (including the taxes and sending costs) and the total amount of products
		 */
		//Opening the connection
		httpObject.open( "POST", ScriptURL, false );
		httpObject.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );
		
		//Setting up params and sending them
		var Params = 'Action=4';
		httpObject.send( Params );		
		
		// alert(httpObject.responseText);
		var Information = eval( "(" + httpObject.responseText + ")" );
		
		if ( Information.TotalPrice && Information.Products ) {
			var Winkelmand_Q = document.getElementById( 'Winkelmand_Q' );
			var Winkelmand_TPI = document.getElementById( 'Winkelmand_TPI' );
			var Winkelmand_V = document.getElementById( 'Winkelmand_V' );
			var Winkelmand_BtnPay = document.getElementById('Winkelmand_BtnPay');
			
			if(Winkelmand_Q!=undefined){
				Winkelmand_Q.innerHTML = Information.Products;
			}
			
			if(Winkelmand_TPI!=undefined) {
				var TotalPrice = new Number( Information.TotalPrice );
				Winkelmand_TPI.innerHTML = TotalPrice.toFixed( 2 ).replace( ".", ",");
			}
			
			if(Winkelmand_V!=undefined) {
				var ForwardingCharges = new Number( Information.ForwardingCharges);
				Winkelmand_V.innerHTML = ForwardingCharges.toFixed( 2 ).replace( ".", ",");
			}			
			
			if(Winkelmand_BtnPay!=undefined){				
				if(parseInt(Information.Products)>0){
					Winkelmand_BtnPay.style.display = 'block';
				}else{					
					Winkelmand_BtnPay.style.display= 'none';
				}
			}
		}		
		return true;
	} else if ( response == 'false' ) {
		//Not a valid request
		alert( ErrorMelding );
		return false;
	} else {
		alert( response );
		return false;
	}
}

function resetOrderBox() {	
	//HIDE STEP 2
	var OrderBoxContentStap2 = document.getElementById( 'OrderBoxContentStap2' );
	if ( OrderBoxContentStap2 ) {
		OrderBoxContentStap2.style.display = 'none';
	}
	
	//SHOW STEP 1
	var OrderBoxContentStap1 = document.getElementById( 'OrderBoxContentStap1' );
	if ( OrderBoxContentStap1 ) {
		OrderBoxContentStap1.style.display = 'block';
	}
	
	resetSelectBoxes();
	//First remove and hide all sizes etc.
	hideSizeSpan();
	showSelectBoxByName( 'productMaat' );
	removeAllSizeOptions();
	
	//Load the sizes again
	loadSizes();
	
	return false;
}

function hideSizeSpan() {
	var MaatText = document.getElementById( 'MaatText' );
	if ( MaatText ) {
		MaatText.className = 'Hide';
	}
}

function showSizeSpan() {
	var MaatText = document.getElementById( 'MaatText' );
	if ( MaatText ) { 
		if ( document.OrderForm.productMaat ) {
			MaatText.innerHTML = document.OrderForm.productMaat.options[document.OrderForm.productMaat.selectedIndex].text;
		} else {
			MaatText.innerHTML = 'One size';
		}
		MaatText.className = 'Show';
	}
}

function hideNietLeverbaar() {
	var NietLeverbaar = document.getElementById( 'NietLeverbaar' );
	if ( NietLeverbaar ) {
		NietLeverbaar.className = 'Hide';
	}
}

function showNietLeverbaar() {
	var NietLeverbaar = document.getElementById( 'NietLeverbaar' );
	if ( NietLeverbaar ) { 
		NietLeverbaar.className = 'Show';
	}
}

function resetSelectBoxes() {

	//RESET THE VALUES
	if ( document.OrderForm.productHoeveelheid ) {
			document.OrderForm.productHoeveelheid.options[ 0 ].selected = true;
	}
	
	if ( document.OrderForm.productKleur ) {
		if ( document.OrderForm.productKleur.options.length > 2 ) 
			document.OrderForm.productKleur.options[ 0 ].selected = true;
	}
	
	if ( document.OrderForm.productMaat ) {
		if ( document.OrderForm.productMaat.options.length > 2 ) 
			document.OrderForm.productMaat.options[ 0 ].selected = true;
	}	
}

function showSelectBoxByName( Name ){
	var SelectBox = document.getElementsByName( Name )[0];
	if ( SelectBox ) {
		SelectBox.className = 'Show';
	}
}

function hideSelectBoxByName( Name ){
	var SelectBox = document.getElementsByName( Name )[0];
	
	if ( SelectBox ) {
		SelectBox.className = 'Hide';
	}
}

//Get the HTTP Object
function getHTTPObject(){
	if( window.ActiveXObject ) { 
		// Object in IE7+, Firefox, Chrome, Opera, Safari
		return new ActiveXObject( "Microsoft.XMLHTTP" );
	} else if( window.XMLHttpRequest ) { 
		// Object in IE6, IE5
		return new XMLHttpRequest();
	} else {
		//No AJAX in this browser :-(
		alert( "Your browser does not support AJAX." );
		return null;
	}
}

function loadVoorraad() {
	removeVoorraad();
	
	/**
	 * Getting the HTTP Object
	 */
	var httpObject = getHTTPObject();
	//Script base
	var ScriptURL = AbsPath + 'includes/SpecialPages/Shop/Product/Ajax.php';
	
	if ( document.OrderForm.productMaat ) {
		var Maat = document.OrderForm.productMaat.options[document.OrderForm.productMaat.selectedIndex].value;
	} else {
		var Maat = 'NULL';
	}
	
	if ( document.OrderForm.productMaat ) {
		var Kleur = document.OrderForm.productKleur.options[document.OrderForm.productKleur.selectedIndex].value;
	} else {
		var Kleur = 'NULL';
	}
	
	if ( ( Kleur == 'NULL' && Maat == 'NULL' ) || ( Kleur > 0 && Maat > 0 ) ) {		
		/**
		 * Setting the total price (exclusive)
		 */
		//Opening the connection
		httpObject.open( "POST", ScriptURL, false );
		httpObject.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );
		
		//Setting up params and sending them
		var Params = 'Action=5&searchID=' + SearchID + '&productMaat=' + Maat + '&productKleur=' + Kleur + '&productID=' + ProductID ;
		
		httpObject.send( Params );
		
		var Voorraad = httpObject.responseText;
		var ProductHoeveelheid = document.OrderForm.productHoeveelheid;
		
		showSelectBoxByName( 'productHoeveelheid' );
		hideNietLeverbaar();
		
		if ( ProductHoeveelheid ) {
			if ( Voorraad ) {
			
				//If the stock is more than 10, only show 10, else show stock
				Voorraad = ( Voorraad > 10 ) ? 10 : Voorraad; 
			
				for ( var i = 1; i <= Voorraad; i++ ) {
				
					var Option = document.createElement( 'option' );
					Option.text = i;
					Option.value = i;
										
					try {
						ProductHoeveelheid.add( Option, null );
					} catch ( e ) {
						ProductHoeveelheid.add( Option );
					}
				}
				
				if ( Voorraad > 0 ) {
					ProductHoeveelheid.selectedIndex = 1;				
				}
				
				var ProductHoeveelheidOptions = document.OrderForm.productHoeveelheid.options;
				
				if ( ProductHoeveelheidOptions.length == 1 ) {
					//Niet leverbaar
					hideSelectBoxByName( 'productHoeveelheid' );
					showNietLeverbaar();
				} else {
					//Dit wordt hierboven ook al gedaan, voor de zekerheid nogmaals
					hideNietLeverbaar();
					showSelectBoxByName( 'productHoeveelheid' );
					
					//Add the onlick function for submitting an order
					var SubmitButton = document.getElementById( 'SubmitButton' );
					SubmitButton.className = 'ShowAnchor';
					SubmitButton.onclick = function(){
						submitOrder( AbsPath ); 
						return false; 
					}
				}
			} else {
				//Geen voorraad
				alert( 'Product niet op voorraad' );
			}
		} else {
			//Element verwijderd
		}
		
	} else {
		showSelectBoxByName( 'productHoeveelheid' );
		hideNietLeverbaar();
	}
}

function loadSizes() {
	/**
	 * Getting the HTTP Object
	 */
	var httpObject = getHTTPObject();
	//Script base
	var ScriptURL = AbsPath + 'includes/SpecialPages/Shop/Product/Ajax.php';
	
	//Opening the connection
	httpObject.open( "POST", ScriptURL, false );
	httpObject.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" );
	
	if ( document.OrderForm.productKleur ) {
		var ColorSelectBox = document.OrderForm.productKleur;
		if ( ColorSelectBox ) {
			var Color = ColorSelectBox.options[document.OrderForm.productKleur.selectedIndex].value;
		}
	}
	
	//Setting up params and sending them
	var Params = 'Action=3&productID=' + ProductID + '&ColorID=' + Color;
	
	httpObject.send( Params );
	response = httpObject.responseText;
	
	if ( response != false ) {
		var response = eval( response );

		removeAllSizeOptions();
		createAllSizeOptions( response );
			
		var MaatSelectBox = document.OrderForm.productMaat;
		
		if ( MaatSelectBox ) {
			var Options = MaatSelectBox.options;
	
			if ( Options ) {
				//Hide the selectbox if there is no more than 1 option
				
				if ( Options.length == 2 ) {
					hideSelectBoxByName( 'productMaat' );
					showSizeSpan();
				} else {
					showSelectBoxByName( 'productMaat' );
					hideSizeSpan();
				}
			}
		} else {
			//Geen selectbox, 1 maat, 1 kleur
		}
	} else {
		//response is false
		alert( ErrorMelding );
	}
}

function removeAllSizeOptions() {
	//Removing all options
	if ( document.OrderForm.productMaat ) {

		var Length = document.OrderForm.productMaat.options.length;
		
		for ( var i = ( Length -1 ); i >= 0 ; i-- ) {
			
			var Option = document.OrderForm.productMaat.options[ i ];

			if ( Option.value != 'null' ) {
				document.OrderForm.productMaat.remove( i );
			}
		}
	}
	
	removeVoorraad();
}

function removeVoorraad() {
	
	showSelectBoxByName( 'productHoeveelheid' );
	hideNietLeverbaar();
	
	if ( document.OrderForm.productHoeveelheid ) {
		//Checking the length
		var Length = document.OrderForm.productHoeveelheid.options.length;
		//Removing all options
		for ( var i = ( Length -1 ); i >= 0 ; i-- ) {
			
			var Option = document.OrderForm.productHoeveelheid.options[ i ];

			if ( Option.value != 'null' ) {
				document.OrderForm.productHoeveelheid.remove( i );
			}
		}
		
		//Default selectview
		showSelectBoxByName( 'productHoeveelheid' );
		
		//Remove the onlick function for submitting an order
		var SubmitButton = document.getElementById( 'SubmitButton' );
		SubmitButton.onclick = function(){}
		SubmitButton.className = 'HideAnchor';
	}
}

function createAllSizeOptions( responseObject ) {
	var MaatSelectBox = document.OrderForm.productMaat;
	if ( MaatSelectBox ) {
		var Options = MaatSelectBox.options;
	} else {
		//1 maat, 1 kleur
	}
	
	if ( MaatSelectBox ) {
		if ( Options ) {
			//Creating all options
			for ( var i = 0; i < responseObject.length; i++ ) {
				
				var Option = document.createElement( 'option' );
				Option.text = responseObject[ i ].maatLang;
				Option.value = responseObject[ i ].maatID;
				
				if ( responseObject.length == 1 ) {
					Option.selected = true;
					Option.defaultSelected = true;
				}
				
				try {
					MaatSelectBox.add( Option, null );
				} catch ( e ) {
					MaatSelectBox.add( Option );
				}
			}
		} 
		
		//Opnieuw hier ophalen i.v.m. toevoeging van options hierboven
		var Options = MaatSelectBox.options;
	
		if ( Options.length == 2 ) {
			hideSelectBoxByName( 'productMaat' );
			MaatSelectBox.selectedIndex = 1;
			showSizeSpan();
		} else {
			showSelectBoxByName( 'productMaat' );
			hideSizeSpan();
		}
	} else {
		showSizeSpan();
	}
	
	loadVoorraad();
}

function preloadSizes() {
	var Color = '';
	
	if( document.OrderForm == undefined)
		return;
	
	if ( document.OrderForm.productKleur ) {
		Color = document.OrderForm.productKleur.options[document.OrderForm.productKleur.selectedIndex].value;
	} else {
		Color = null;
	}
	
	if ( Color != null && Color != 'null' ) {
		loadSizes( document.OrderForm.productKleur );
	} else {
		//Er zijn meerdere kleuren, klant moet nog zelf een kleur kiezen
	}
	
	if ( !Color ) {
		loadVoorraad();		
	}
}

$(document).ready( function() {
	preloadSizes();	
} );
