var previousCell = null;
var previousAnchor = null;

//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 OpenClose(anchor, id) {
  var cell = document.getElementById( "item" + id );
  if ( cell != null ){
    if ( anchor.className == "Open"  ) {
      anchor.className = "Close";
      cell.style.display = "none";
    } else {
      anchor.className = "Open";
      cell.style.display = "block";
    }

    if(previousCell != null && previousCell != cell ) previousCell.style.display = "none";
    if(previousAnchor != null && previousAnchor != anchor ) previousAnchor.className = "Close";

    previousCell = cell;
    previousAnchor = anchor;
  }
  return false;
}

function closeAlert( AlertObject ) {
	Element = document.getElementById( AlertObject );
	
	$( Element ).slideUp();
}

function switchPayment( IDElement, IDValue, NameElement, NameValue ) {
	
	/**
	 * Changing the value inside of the hidden field for posting
	 */
	var IDObject = document.getElementById( IDElement );
	if ( IDObject ) {
		IDObject.value = IDValue;
	}
	
	/**
	 * Changing the layout of the chosen paymentmethod
	 */
	var PaymentmethodHolder = document.getElementById( 'Paymentmethods' );
	
	if ( PaymentmethodHolder ) {
		/**
		 * Setting all default
		 */
		var Paymentmethods = getChilds( PaymentmethodHolder );

		if ( Paymentmethods != false ) {
			for( i = 0; i < Paymentmethods.length; i++ ) {
				var Child = Paymentmethods[ i ];
				Child.className = 'PaymentMethod';
			}
		}
		
		/**
		 * Changing color of the active element
		 */
		var ActiveElement = document.getElementById( NameElement + IDValue );
		if ( ActiveElement ) {
			ActiveElement.className = 'PaymentMethodActive';
		}
		
		/**
		 * Changing the value which show the chosen payment method
		 */
		var NameObject = document.getElementById( NameElement );
		
		if( NameObject ) {
			NameObject.innerHTML = '<b>De geselecteerde betalingsmethode is ' + NameValue + '</b>';
		} else {
			alert( 'Name object could not be set with value ' + NameValue );
		}
	}	
}

function getChilds( PaymentmethodHolder ) {
	/**
	 * If the ID excists 
	 */
	if ( PaymentmethodHolder ) {

		/* Getting all chulds inside the Element of ID */
		var Childs = new Array();
		
		for ( var i = 0; i < PaymentmethodHolder.childNodes.length; i++) {
			var Child = PaymentmethodHolder.childNodes[ i ];
			if ( Child.tagName == "DIV" ) {
				Childs.push(Child);
			}
		}

		/* If there are childs, then it is workable, and we give them back */
		if( Childs.length ) {
			return Childs;
		} else {
			return false;
		}
	}
}

// selects a delivery type
function selectDeliveryType( DeliveryTypeID, DifferentAddressTypeID ) {
	document.getElementById( 'DeliveryType' ).value = DeliveryTypeID;
	if( DeliveryTypeID == DifferentAddressTypeID ) {
		
		document.getElementById( 'Bezorgadres' ).style.display = 'block';
		
	} else {
		
		document.getElementById( 'Bezorgadres' ).style.display = 'none';
		
	}	
	document.getElementById( 'ErrorMeldingDisplay' ).style.display = 'none';
}

// checks if a delivery type has been choosen
function checkDelivery() {
	if( document.getElementById( 'DeliveryType' ).value == 0 ) {
		document.getElementById( 'BezorgingMelding' ).style.display = 'block';
	} else {
		document.getElementById( 'DeliveryForm' ).submit();
	}
}

// checks if the algemene voorwaarden have been accepted
function checkVoorwaarden() {
	if( document.getElementById( 'AlgemeneVoorwaarden' ).checked != true ) {
		document.getElementById( 'AlgemeneVoorwaardenMelding' ).style.display = 'block';
	} else {
		document.getElementById( 'VoorwaardenForm' ).submit();
	}
}

// order annuleren
function orderAnnuleren() {
	if( document.getElementById( 'AnnuleerOrderBevestigen' ).style.display == 'block' ) {
		document.getElementById( 'AnnuleerOrderBevestigen' ).style.display = 'none';
		document.getElementById( 'AnnuleerOrder' ).style.display = 'block';
	} else {
		document.getElementById( 'AnnuleerOrderBevestigen' ).style.display = 'block';
		document.getElementById( 'AnnuleerOrder' ).style.display = 'none';
	}	
}

function displayWinkelmandKort() {
	document.getElementById( 'WinkelmandInhoudKort' ).style.display = 'block';
}

function closeWinkelmandKort() {
	document.getElementById( 'WinkelmandInhoudKort' ).style.display = 'none';
}

function displayWaardechequeInfo( div, type ) {
	if( type == 'out' ) {
		document.getElementById( div ).style.display = 'none';
	} else {
		document.getElementById( div ).style.display = 'block';
	}	
}

$(document).ready(function() {
	$('div.BasketInfoHolder').bind('mouseenter', function() {
		$('#BasketMailInfo').show();
	}).bind('mouseleave', function() {
		$('#BasketMailInfo').hide();
	});	
	
	$('div.Informatieaanvraag img').bind('click', function() {
		if( $('div#RequestInfo').hasClass('Inactive') ) {
			$('div#RequestInfo').show();
			// open form
			$('div#RequestInfo').animate({
				'height' : '280px'
			  }, 1500, function() {
			    // Animation complete.
				  $(this).addClass( 'Active' );
				  $(this).removeClass( 'Inactive' );
			  });
		} else {			
			// close form
			CloseRequestInfoForm();
		}	
	});	
});

function SubmitInfoRequest() {
    $.ajax( {
        type: "POST",
        url: AbsPath + 'includes/Components/RequestInfo/Submit.php',
        dataType: 'json',
        data: { ProductID : $('input[name=ProductID]').val(), 
    			SenderName : $('input[name=SenderName]').val(), 
    			SenderPhone : $('input[name=SenderPhone]').val(), 
    			SenderEmail : $('input[name=SenderEmail]').val(), 
    			SenderComment : $('textarea[name=SenderComment]').val() },
    			/*SenderOK : $('input:checkbox:checked[name=SenderOK]').val()*/
        async: false,
        error: function(xhr, status, errorThrown) {
             alert('Unable to load'+errorThrown);
        }, 
        success: function( response ) {
            //console.log(response);
            $('#RequestInfoForm').html( response );
        }
    } );	
}

function CloseRequestInfoForm() {
	$('div#RequestInfo').animate({
		'height' : '0px'
	  }, 1500, function() {
	    // Animation complete.
		  $(this).addClass( 'Inactive' );
		  $(this).removeClass( 'Active' );
		  $('div#RequestInfo').hide();
	  });
}

function ToggleBasketLog() {
	if( $('div.BasketLogContent').hasClass('Active') ) {
		$('div.BasketLogContent').hide();
		$('div.BasketLogContent').removeClass( 'Active' );
	} else {
		$('div.BasketLogContent').show();
		$('div.BasketLogContent').addClass( 'Active' );
	}
}

$(document).ready(function() {
	$( 'input.AccountTypeSwitch' ).bind('change', function( element ) {
		var CurrentValue = $(this).val(); 
		ToggleAccountSwitch( CurrentValue );
	});
	$('#FormCountry').bind('change', function( element ) {
		if( $('#FormCountry').val() != 167
			&& $('#FormCountry').val() != 33
			&& $('#FormCountry').val() != 10 ) {
			if( !$('#FormBtwNummer').hasClass( 'required' ) ) {
				if( $('div.AccountBusiness').hasClass('Active') ) {
					$('#FormBtwNummer').addClass( 'required' );
					$('#FormBtwNummerMandatory').html( '*' );					
				}	
			}
		} else {
			$('#FormBtwNummer').removeClass( 'required' );
			$('#FormBtwNummerMandatory').html( '' );
		}		
	});
});

function ToggleAccountSwitch( AccountType ) {
	if( !$('div.AccountDefault').hasClass('Active') ) {
		$('div.AccountDefault').show();
		$('div.AccountDefault').addClass( 'Active' );
	}
	
	if( AccountType == 1 ) {
		if( $('div.AccountBusiness').hasClass('Active') ) {
			$('div.AccountBusiness').hide();
			$('div.AccountBusiness').removeClass( 'Active' );
			$('#FormBedrijfsnaam').removeClass( 'required' );
			$('#FormBtwNummer').removeClass( 'required' );
			$('#FormBtwNummerMandatory').html( '' );
			$('#FormBedrijfsnaam').removeClass( 'error' );
			$('#FormBtwNummer').removeClass( 'error' );
		}		
	} else {
		if( !$('div.AccountBusiness').hasClass('Active') ) {
			$('div.AccountBusiness').show();
			$('div.AccountBusiness').addClass( 'Active' );
			$('#FormBedrijfsnaam').addClass( 'required' );
			if( $('#FormCountry').val() != 167
				&& $('#FormCountry').val() != 33
				&& $('#FormCountry').val() != 10 ) {
				$('#FormBtwNummer').addClass( 'required' );
				$('#FormBtwNummerMandatory').html( '*' );
			}			
		}		
	}
}
