
var msgRequest = null;
var buyWasClicked = false;

//-----------------------------------------------------
//Subscriber Client Functions
//-----------------------------------------------------

//Signup a new subscriber
function purchaseSignup(){

	try
    {
        
        ValidateInput('purchaseSignup');

		return true;
		
    }catch(err){

		return false;

    }

}	

//Signup a new subscriber
function purchaseConfirm()
{
	try
    {
        ValidateInput('purchaseConfirm');

		return true;	
    }
	catch(err)
	{
		return false;
    }
}	



function setPurchaseStatusTrackingDisplay(){

    //Set X Position for Order Status Preview
    objPreview = document.getElementById("view_order_status_preview");
  
    var objClassObjectPreview = new ClassObject(objPreview);
    
    objClassObjectPreview.determineXYPos();

    objPreviewSignup = document.getElementById("view_order_status_preview_signup");
    
    var objClassObjectPreviewSignup = new ClassObject(objPreviewSignup); 

    objClassObjectPreviewSignup.setXYForObject(objClassObjectPreview.getXPos() + 130, objClassObjectPreview.getYPos() + 40);
		

}




function gotoProductDetails(url){

	if(buyWasClicked == false){
		
		window.location.href = url;	
		
	}else{

		buyWasClicked = false;
		
	}
	
}


function purchaseOptionPaymentCalc(){

	//Get Payment Method
	var paymentMethod = document.getElementById('payment')[document.getElementById('payment').selectedIndex].innerHTML;
	var total_surcharge_cost = 0.00;

	//Get Sub total
	var purchaseSubTotal = document.getElementById('purchase_subtotal_price').innerHTML + "";
	var purchaseDeliveryTotal = document.getElementById('purchase_total_delivery_price').value + "";

	//Get Delivery Total
	purchaseSubTotal = purchaseSubTotal.replace("$","");

	if(paymentMethod == 'Credit Card (Visa/Mastercard)'){

		purchaseSubTotal = parseFloat(purchaseSubTotal);

		if(purchaseDeliveryTotal != ''){
	
			purchaseSubTotal = purchaseSubTotal + parseFloat(purchaseDeliveryTotal);
	
		}

		total_surcharge_cost = (purchaseSubTotal * .016);

	}

	document.getElementById('purchase_total_surcharge_price').value = total_surcharge_cost.toFixed(2);
	document.getElementById('purchase_total_surcharge_price_display').innerHTML = '$' + total_surcharge_cost.toFixed(2);

	purchaseOptionTotalCalc();

}	




function purchaseOptionPostageCalc(){

	var deliveryMethod = document.getElementById('delivery')[document.getElementById('delivery').selectedIndex].innerHTML;

	var total_price = document.getElementById('purchase_subtotal_price').innerHTML;
	total_price = total_price.replace("$","");

	var total_weight = document.getElementById('purchase_total_delivery_weight').innerHTML;
	total_weight = total_weight.replace(" Units","");

	var total_delivery_cost = '0.00';

	switch(deliveryMethod){
		
		case 'Couriers Please - Perth Metro (Next Business Day after dispatch)':

			if (total_price > 500 && total_weight < 50){

				total_delivery_cost = '0.00';
				break;
			
			}

				if (total_weight < 25){

					total_delivery_cost = '6.00';
					break;
					
				}
	
				if (total_weight < 50){
			
					total_delivery_cost = '12.00';
					break;
			
				}
	
				if (total_weight < 75){
			
					total_delivery_cost = '18.00';
					break;
	
				}else{
			
					total_delivery_cost = '24.00';
					break;
	
				
				
			}

		break;
		
		
		
		
		case 'Couriers Please - Perth Outer Suburbs (Next Business Day after dispatch)':
		
			if (total_price > 1000 && total_weight < 25){

				total_delivery_cost = '0.00';
				break;
			
			}

				if (total_weight < 25){
	
					total_delivery_cost = '9.50';
					break;
					
				}
	
				if (total_weight < 50){
			
					total_delivery_cost = '19.00';
					break;
			
				}
	
				if (total_weight < 75){
			
					total_delivery_cost = '28.50';
					break;
	
				}else{
			
					total_delivery_cost = '38.00';
					break;
	
				
				
			}

		break;
		
		
		
		
		case 'Couriers Please - WA Country Zone 1 (1-3 Business Days after dispatch)':
		
			if (total_weight < 25){

				total_delivery_cost = '19.50';
				break;
			
			}

				if (total_weight < 50){
	
					total_delivery_cost = '39.00';
					break;
					
				}
	
				if (total_weight < 75){
			
					total_delivery_cost = '58.50';
					break;
			
									
				}else{
			
					total_delivery_cost = '78.00';
					break;

				
				
			}

		break;
		
		
		
		
		case 'Couriers Please - WA Country Zone 2 (1-3 Business Days after dispatch)':
		
			if (total_weight < 25){

				total_delivery_cost = '23.00';
				break;
			
			}

				if (total_weight < 50){
	
					total_delivery_cost = '46.00';
					break;
					
				}
	
				if (total_weight < 75){
			
					total_delivery_cost = '69.00';
					break;
			
			
				}else{
			
					total_delivery_cost = '92.00';
					break;
				
					
				
					
			}

		break;
		



		case 'Couriers Please - WA Country Zone 3 (2-4 Business Days after dispatch)':
		
			if (total_weight < 25){

				total_delivery_cost = '26.50';
				break;
			
			}

				if (total_weight < 50){
	
					total_delivery_cost = '53.00';
					break;
					
				}
	
				if (total_weight < 75){
			
					total_delivery_cost = '79.50';
					break;
			
			
				}else{
			
					total_delivery_cost = '106.00';
					break;
					
				
					
			}

		break;




		case 'Couriers Please - WA Country Zone 4 (3-6 Business Days after dispatch)':
		
			if (total_weight < 25){

				total_delivery_cost = '33.50';
				break;
			
			}

				if (total_weight < 50){
	
					total_delivery_cost = '67.00';
					break;
					
				}
	
				if (total_weight < 75){
			
					total_delivery_cost = '100.50';
					break;
			
			
				}else{
			
					total_delivery_cost = '134.00';
					break;

				}
				
		

		break;
		
		
		
		
		case 'Australia Post - Australia Wide Express Parcel (1-3 Business Days after dispatch)':

			if (total_weight < 3){

				total_delivery_cost = '11.00';
				break;
		
			}
			
			if (total_weight < 5){
					
				total_delivery_cost = '19.00';
				break;
		
			}else{
			
				total_delivery_cost = 'Unavail.';
				break;
				
			}

		break;



		case 'Couriers Please - Interstate Metro (3-6 Business Days after dispatch)':
		
			if (total_weight < 15){

				total_delivery_cost = '23.00';
				break;
			
			}

				if (total_weight < 25){
	
					total_delivery_cost = '30.00';
					break;
					
				}
	
				if (total_weight < 50){
			
					total_delivery_cost = '43.00';
					break;
			
			
				}else{
			
					total_delivery_cost = '60.00';
					break;

				}
				
		

		break;




		case 'Couriers Please - Interstate Country (5-8 Business Days after dispatch)':
		
			if (total_weight < 15){

				total_delivery_cost = '35.00';
				break;
			
			}

				if (total_weight < 25){
	
					total_delivery_cost = '55.00';
					break;
					
				}
	
				if (total_weight < 50){
			
					total_delivery_cost = '75.00';
					break;
			
			
				}else{
			
					total_delivery_cost = '100.00';
					break;

				}
				
		

		break;





		case 'Couriers Please - Interstate Rural (6-10 Business Days after dispatch)':
		
			if (total_weight < 15){

				total_delivery_cost = '45.00';
				break;
			
			}

				if (total_weight < 25){
	
					total_delivery_cost = '69.00';
					break;
					
				}
	
				if (total_weight < 50){
			
					total_delivery_cost = '100.00';
					break;
			
			
				}else{
			
					total_delivery_cost = '140.00';
					break;

				}
				
		

		break;




		default:

			total_delivery_cost = 'Unavail.';
		
	}

	total_delivery_cost = total_delivery_cost + "";

	if(total_delivery_cost == 'Unavail.'){

		document.getElementById('purchase_total_delivery_price').value = '0.00';
		document.getElementById('purchase_total_delivery_price_display').innerHTML = total_delivery_cost;


	}else{

		document.getElementById('purchase_total_delivery_price').value = total_delivery_cost;
		document.getElementById('purchase_total_delivery_price_display').innerHTML = '$' + total_delivery_cost;

	}

	purchaseOptionTotalCalc();

	purchaseOptionPaymentCalc();

}	



function purchaseOptionTotalCalc(){


	var purchaseTotal = '';
	var purchaseSubTotal = document.getElementById('purchase_subtotal_price').innerHTML + "";
	var purchaseDeliveryTotal = document.getElementById('purchase_total_delivery_price').value + "";
	var purchaseSurchargeTotal = document.getElementById('purchase_total_surcharge_price').value + "";

	purchaseSubTotal = purchaseSubTotal.replace("$","");
	purchaseDeliveryTotal = purchaseDeliveryTotal.replace("$","");
	purchaseSurchargeTotal = purchaseSurchargeTotal.replace("$","");

	purchaseTotal = parseFloat(purchaseSubTotal);

	if(purchaseDeliveryTotal != ''){

		purchaseTotal = purchaseTotal + parseFloat(purchaseDeliveryTotal);

	}


	if(purchaseSurchargeTotal != ''){

		purchaseTotal = purchaseTotal + parseFloat(purchaseSurchargeTotal);

	}

	document.getElementById('purchase_total_price').innerHTML = '$' + purchaseTotal.toFixed(2);


}	


function retrievePurchase(id){
	
	if(confirm("Retrieving this saved purchase will empty your current shopping cart.\nAre you sure you want to continue?")){
	
		var url = window.location.href + "";
		
		url = url.substring(0,url.lastIndexOf("/") + 1);

		window.location.href	= url + "page_purchase_engine.php?action=retrievepurchases&invoice_id=" + id;
		
	}
	
}

function deleteSavedPurchase(id){
	
	if(confirm("Are you sure you want to delete this saved shopping cart?")){
	
		var url = window.location.href + "";
		
		url = url.substring(0,url.lastIndexOf("/") + 1);

		window.location.href	= url + "page_purchase_engine.php?action=deletesavedpurchases&invoice_id=" + id;
		
	}
	
}


function purchaseProduct(id, stock)
{
	switch(stock){
	
		case "0":
			if(confirm("This product is not in stock. Do you still want to add it to your order?")!=1){
			
			return false;
			
			}
			break;
		
		case "Email":
			if(confirm("This product may not be in stock. Do you still want to add it to your order?")!=1){
		
				return false;
		
			}
			break;
		
		case "Avail":
		    if(confirm("This product is by order from the supplier. It will take 2-3 days to get in, assuming the supplier has stock. Do you still want to add it to your order?")!=1){
		
				return false;
			}
			break;
	}
}


//Submit Payment Details
function submitPayment()
{
	try
    {
        ValidateInput('purchasePayment');
    }
	catch(err)
	{
		return false;
    }

    //Display Loading Div
    displayLoadingDivSimple('loading_div_payment');

	//Display Loading Messsage
	$('#payment_message').html("Payment Processing...");
	
	var postData = {};
	postData.member_id 				= $('#member_id').val();
	postData.member_first_name 		= $('#member_first_name').val();
	postData.member_surname 		= $('#member_surname').val();
	postData.payment_cc_name 		= $('#payment_cc_name').val();
	postData.payment_cc_number		= $('#payment_cc_number1').val() + $('#payment_cc_number2').val() + $('#payment_cc_number3').val() + $('#payment_cc_number4').val();
	postData.payment_cc_expiry_month= $('#payment_cc_expiry_month').val();
	postData.payment_cc_expiry_year	= $('#payment_cc_expiry_year').val();

	//Format Total Price
	var purchaseTotal = $('#purchase_total_price').html();
	purchaseTotal = purchaseTotal.replace("$","");
	purchaseTotal = parseFloat(purchaseTotal);
	purchaseTotal = purchaseTotal * 100;

	purchaseTotal = purchaseTotal.toFixed(0);

	postData.purchase_total_price 	= purchaseTotal;

	$.post("/post_data/post_data_purchase.php?mode=submitpaymentdetails", postData, function(data){

		hideLoadingDivSimple('loading_div_payment', 100);

		//Hide Loading Messsage
		$('#payment_message').html("");

		if (data == 'success')
		{
			window.location = "/purchase_form?purchase_view_mode=complete&member_id=" + document.getElementById('member_id').value;
		}
		else
		{
			alert('An error has occured. Please contact sales@vtechindustries.com.au\n' + data);
		}

	});

}

function paymentCreditCardTabber()
{
	$('input#payment_cc_number1').keyup(function(){

		if( 4 == $(this).val().length )
		{
			$('input#payment_cc_number2').focus();
		}

	});

	$('input#payment_cc_number2').keyup(function(){

		if( 4 == $(this).val().length )
		{
			$('input#payment_cc_number3').focus();
		}

	});

	$('input#payment_cc_number3').keyup(function(){

		if( 4 == $(this).val().length )
		{
			$('input#payment_cc_number4').focus();
		}

	});
}

function purchaseImageRollover()
{
	$('table#purchaseSummaryContainer table.purchaseSummaryBody').hover(function(){

		$('img#image_preview').attr('src', '/imagesProduct/' + $(this).attr('imagePath'));

	},
	function(){

		$('img#image_preview').attr('src', 'images/icons/purchase_120x120.png')

	});
}

$(document).ready(function(){


	if( 1 == $('input#payment_cc_number1').length )
	{
		paymentCreditCardTabber();
	}

	if( 1 == $('table#purchaseSummaryContainer').length )
	{
		purchaseImageRollover();
	}

});