jQuery.noConflict();
jQuery(document).ready(function(){
	/* Hide empty "no sub-catalogs" and "no products" messages. */
	jQuery('.catalogueTable .catalogueItemNotFound').closest('table').addClass('HiddenTable');
	jQuery('.productTable .productItemNotFound').closest('table').addClass('HiddenTable');
	
	/* Hide distributors application form. */
	jQuery('#DistributorsApplication').hide();
	
	/* Set main image in large product layout lightbox link value. */
	jQuery('#LargeProductImage a').attr('href',jQuery('#LargeProductImage img').attr('src'));
	
	jQuery('#ShowAddRecipientForm').click(function(){
		jQuery('#LargeProductRecipientForm').removeClass('HiddenDiv');
	});
});

/*function customiseDisplay(isLoggedIn){
	var customerType = getCustomerTypeCookie();
	if( (isLoggedIn) && (customerType == 'Distributor') ){
		jQuery('#ShopMenu').addClass('HiddenDiv');
		jQuery('#DistributorMenu').removeClass('HiddenDiv');
		jQuery('#BlackMenu').addClass('HiddenDiv');
		jQuery('#BlackDistMenu').removeClass('HiddenDiv');
	} else {
		jQuery('#BlackMenu').removeClass('HiddenDiv');
		jQuery('#BlackDistMenu').addClass('HiddenDiv');
	}
}*/

function customiseAccountSummary(){
	var customerType = getCustomerTypeCookie();
	var loggedInStatus = getLoggedInCookie();
	if( (loggedInStatus == 'true') & (customerType == 'Distributor') ){
	}
	if( (loggedInStatus == 'true') & (customerType == 'Fundraiser') ){
		document.write('<li><a href="/_catalog/Fundraiser">Fundraiser Products</a></li>');
	}

}

function setDistributorCookie(){ setCustomerTypeCookie('Distributor'); }
function setFundraiserCookie(){ setCustomerTypeCookie('Fundraiser'); }
function setCustomerCookie(){ setCustomerTypeCookie('Customer'); }

function setCustomerTypeCookie(cookieValue){
	var cookieName = 'MORISHCUSTTYPE';
	var expireDays = 100;
	var expiryDate = new Date();
	expiryDate.setDate( expiryDate.getDate() + expireDays );
	document.cookie = cookieName + '=' + cookieValue + ';expires=' + expiryDate.toGMTString();
}

function getCustomerTypeCookie(){
	var cookieName = 'MORISHCUSTTYPE';
	var getCookieValue = '';
	if( document.cookie.length > 0 ){
		cookieStart = document.cookie.indexOf( cookieName + '=' );
		if( cookieStart != -1 ){
			cookieStart = cookieStart + cookieName.length + 1;
			var cookieEnd = document.cookie.indexOf(';',cookieStart);
			if( cookieEnd == -1 ){
				cookieEnd = document.cookie.length;
			}
		getCookieValue =  document.cookie.substring(cookieStart,cookieEnd);
		}
	}
	return getCookieValue;
}

function setLoggedInCookie(isLoggedIn){
	if( isLoggedIn == 1 ){
		var cookieValue = 'true';
	}else{
		var cookieValue = 'false';
	}
	var cookieName = 'MORISHLOGGEDIN';
	var expireDays = 100;
	var expiryDate = new Date();
	expiryDate.setDate( expiryDate.getDate() + expireDays );
	document.cookie = cookieName + '=' + cookieValue + ';expires=' + expiryDate.toGMTString();
}

function getLoggedInCookie(){
	var cookieName = 'MORISHLOGGEDIN';
	var getCookieValue = '';
	if( document.cookie.length > 0 ){
		cookieStart = document.cookie.indexOf( cookieName + '=' );
		if( cookieStart != -1 ){
			cookieStart = cookieStart + cookieName.length + 1;
			var cookieEnd = document.cookie.indexOf(';',cookieStart);
			if( cookieEnd == -1 ){
				cookieEnd = document.cookie.length;
			}
		getCookieValue =  document.cookie.substring(cookieStart,cookieEnd);
		}
	}
	return getCookieValue;
}

function hideShowCCFields(val){
	if( val == 1 ){
		jQuery('#CreditCardFields').removeClass('HiddenDiv');
	}else{
		jQuery('#CreditCardFields').addClass('HiddenDiv');
	}
}

function setBillingAddress(checked){
	if( checked ){
		document.getElementById('BillingAddress').value = document.getElementById('ShippingAddress').value; 
		document.getElementById('BillingCity').value = document.getElementById('ShippingCity').value; 
		document.getElementById('BillingState').value = document.getElementById('ShippingState').value; 
		document.getElementById('BillingZip').value = document.getElementById('ShippingZip').value; 
		document.getElementById('BillingCountry').value = document.getElementById('ShippingCountry').value; 
	}else{
		document.getElementById('BillingAddress').value = ''; 
		document.getElementById('BillingCity').value = ''; 
		document.getElementById('BillingState').value = 'Western Australia'; 
		document.getElementById('BillingZip').value = ''; 
		document.getElementById('BillingCountry').value = 'AU'; 
	}
}

function setHomeAddress(checked){
	if( checked ){
		document.getElementById('HomeAddress').value = document.getElementById('WorkAddress').value; 
		document.getElementById('HomeCity').value = document.getElementById('WorkCity').value; 
		document.getElementById('HomeState').value = document.getElementById('WorkState').value; 
		document.getElementById('HomeZip').value = document.getElementById('WorkZip').value; 
		document.getElementById('HomeCountry').value = document.getElementById('WorkCountry').value; 
	}else{
		document.getElementById('HomeAddress').value = ''; 
		document.getElementById('HomeCity').value = ''; 
		document.getElementById('HomeState').value = 'Western Australia'; 
		document.getElementById('HomeZip').value = ''; 
		document.getElementById('HomeCountry').value = 'AU'; 
	}
}

function showDistributorsApplication(){
	jQuery('#DistributorsApplication').toggle(400);
}

/**
*	function customAddToCart(catalogID,purchaseAction)
*	catalogID: ID of current catalog that the product is in.
*	purchaseAction: true = Buy Now, false = Add to Cart. 
**/
/**
*	function customAddToCart(catalogID,purchaseAction){
	*	var productDetails = collectAddress();
	*	if( productDetails != false ){
		*	if( jQuery('#HideCaptureDetails textarea.productTextarea').length > 0 ){
		*		jQuery('#HideCaptureDetails textarea.productTextarea').val(productDetails);
		*		var productTextareaID = jQuery('#HideCaptureDetails textarea.productTextarea').attr('id');
		*		var productID = (productTextareaID.split('_'))[1];
		*		AddToCart(catalogID,productID,'',4,purchaseAction); 
		*		return false;
		*	}else{
		*		alert('Error [Capture Details]\n' + 
		*			  'You are unable to purchase this product due to a system error.\n ' + 
		*			  'Please notify us of this error and the product in question.');
		*		return false;
	*		}
*		}else{
	*		return false;
*		}
*	}
**/
function collectAddress(){
	var recipientName = document.getElementById("RecipientName").value;
	var recipientAddress = document.getElementById("RecipientAddress").value;
	var recipientCity = document.getElementById("RecipientCity").value;
	var recipientState = document.getElementById("RecipientState").value;
	var recipientPostcode = document.getElementById("RecipientPostcode").value;
	var recipientCountry = document.getElementById("RecipientCountry").value;
	
	var formAlert = "";
	if(recipientName == ""){formAlert += "Please enter Recipient Name\n";}
	if(recipientAddress == ""){formAlert += "Please enter Recipient Address\n";}
	if(recipientCity == ""){formAlert += "Please enter Recipient City\n";}
	if(recipientState == ""){formAlert += "Please enter Recipient State\n";}
	if(recipientPostcode == ""){formAlert += "Please enter Recipient Postcode\n";}
	if(recipientCountry == ""){formAlert += "Please enter Recipient Country\n";}
	
	if(formAlert != ""){
		alert(formAlert);
		return false;
	}else{
		var collectedInfo = recipientName + "; " + recipientAddress + "; " + recipientCity + "; " + recipientState + "; " + recipientPostcode + "; " + recipientCountry;
		return collectedInfo;
	}
}
