/*
 * Copyright (c) P3 2008.  All rights reserved.
 * Designed by P3 Technolgies.
 * For distribution only with valid licence
 * Version 1.0
 * Author: John Paul Hayes
 * Date: 1st Oct 2008 13:36:31
 * File Name: basket.js
 */
 
function hasWhiteSpace(s) 
{
     reWhiteSpace = new RegExp(/^\s+$/);

     // Check for white space
     if (reWhiteSpace.test(s)) {
          return 0;
     }
	return 1;
}

function inputValidation()
{
	resetErrors();
	
	// Conditions to show the error markers on the form.
	if(document.getElementById('VOUCHER_FOR').value == '' || hasWhiteSpace(document.getElementById('VOUCHER_FOR').value) == 0) {
		$("#errVOUCHER_FOR").show();
	}
	if(document.getElementById('VOUCHER_FROM').value == '' || hasWhiteSpace(document.getElementById('VOUCHER_FROM').value) == 0) {
		$("#errVOUCHER_FROM").show();
	}
	if(document.getElementById('MESSAGE').value == '' || hasWhiteSpace(document.getElementById('MESSAGE').value) == 0) {
		$("#errMESSAGE").show();
	}
	if(document.getElementById('TITLE').value == '' || hasWhiteSpace(document.getElementById('TITLE').value) == 0) {
		$("#errTITLE").show();
	}
	if(document.getElementById('NAME').value == '' || hasWhiteSpace(document.getElementById('NAME').value) == 0) {
		$("#errNAME").show();
		
	}
	if(document.getElementById('SURNAME').value == '' || hasWhiteSpace(document.getElementById('SURNAME').value) == 0) {
		$("#errSURNAME").show();
		
	}
	if(document.getElementById('EMAIL').value == '' || hasWhiteSpace(document.getElementById('EMAIL').value) == 0) {
		$("#errEMAIL").show();
		
	}
	if(document.getElementById('STREET').value == '' || hasWhiteSpace(document.getElementById('STREET').value) == 0) {
		$("#errSTREET").show();
		
	}
	if(document.getElementById('TOWN').value == '' || hasWhiteSpace(document.getElementById('TOWN').value) == 0) {
		$("#errTOWN").show();
		
	}
	if(document.getElementById('COUNTY').value == '' || hasWhiteSpace(document.getElementById('COUNTY').value) == 0) {
		$("#errCOUNTY").show();
		
	}
	if(document.getElementById('CONTACT_NUMBER').value == '' || hasWhiteSpace(document.getElementById('CONTACT_NUMBER').value) == 0) {
		$("#errCONTACT_NUMBER").show();
		
	}
	
	// Condition to check need to continue or stop
	// If not blank and no spaces - continue
	// else stop and inform.
	if(	(document.getElementById('VOUCHER_FOR').value != '' && hasWhiteSpace(document.getElementById('VOUCHER_FOR').value) == 1)
		&& (document.getElementById('VOUCHER_FROM').value != '' && hasWhiteSpace(document.getElementById('VOUCHER_FROM').value) == 1) 
		&& (document.getElementById('MESSAGE').value != '' && hasWhiteSpace(document.getElementById('MESSAGE').value) == 1)
		&& (document.getElementById('TITLE').value != '' && hasWhiteSpace(document.getElementById('TITLE').value) == 1)
		&& (document.getElementById('NAME').value != '' && hasWhiteSpace(document.getElementById('NAME').value) == 1) 
		&& (document.getElementById('SURNAME').value != '' && hasWhiteSpace(document.getElementById('SURNAME').value) == 1)
		&& (document.getElementById('EMAIL').value != '' && hasWhiteSpace(document.getElementById('EMAIL').value) == 1)
		&& (document.getElementById('STREET').value != '' && hasWhiteSpace(document.getElementById('STREET').value) == 1)
		&& (document.getElementById('TOWN').value != '' && hasWhiteSpace(document.getElementById('TOWN').value) == 1)
		&& (document.getElementById('COUNTY').value != '' && hasWhiteSpace(document.getElementById('COUNTY').value) == 1)
		&& (document.getElementById('CONTACT_NUMBER').value != '' && hasWhiteSpace(document.getElementById('CONTACT_NUMBER').value) == 1)
	   ) {

		$('.GiftVoucherBasket').load(
			'/giftvoucher/basket.php',
			{
				func:'Checkout_step2',
				VOUCHER_FOR: document.getElementById('VOUCHER_FOR').value,
				VOUCHER_FROM: document.getElementById('VOUCHER_FROM').value,
				MESSAGE: document.getElementById('MESSAGE').value,
				TITLE: document.getElementById('TITLE').value,
				NAME: document.getElementById('NAME').value,
				SURNAME: document.getElementById('SURNAME').value,
				EMAIL: document.getElementById('EMAIL').value,
				STREET: document.getElementById('STREET').value,
				TOWN: document.getElementById('TOWN').value,
				COUNTY: document.getElementById('COUNTY').value,
				CONTACT_NUMBER: document.getElementById('CONTACT_NUMBER').value
			},
			function (data) {
				$('.GiftVoucherRight').load(
					'/giftvoucher/basket.php',
					{
						func:'Checkout_step3'
					}
				);
			}
		);
		
		return true;
	}
	else {
		$('.errNotification').show();
		return false;
	}
}

function resetErrors()
{
	$('.errNotification').hide();
	$('.errors').hide();
}

function resetForm()
{
	$('input').val("");
}

function cc_validate()
{
	if(emailCheck()) {
		if(validate()) {
			return true;			
		}
		else {
			return false;
		}
	}
	else {
		return false;
	}
}


function showDeliveryMethod(delivery,count,product_id)
{
	var del = delivery; 
	
	if(del == 'POSTAL') {
		$('#'+count+'_DELIVERY_POSTAL_'+product_id).show();
		$('#'+count+'_DELIVERY_EMAIL_'+product_id).hide();
		$('#'+count+'_EMAIL_DELIVERY_'+product_id).val(''); // clear the field
	}
	else if(del == 'EMAIL') {
		$('#'+count+'_DELIVERY_POSTAL_'+product_id).hide();
		$('#'+count+'_DELIVERY_POSTAL_NAME_'+product_id).val(''); // clear the field
		$('#'+count+'_DELIVERY_POSTAL_ADDRESS_'+product_id).val(''); // clear the field
		$('#'+count+'_DELIVERY_EMAIL_'+product_id).show();
	}
}

function viewBasket()
{
	$('#checkoutform_head').show();
	$('#checkoutform_tail').hide();
	$('div.GiftVoucherRight').load('/giftvoucher/basket.php',{func:'ViewBasket'},function(){alert("viewing basket");});
}

function removeItem(itemID) {}

function clearBasket()
{
	$('.GiftVoucherBasket').load('/giftvoucher/basket.php',{func:'BasketDelete'});
	$('.GiftVoucherRight').load('/giftvoucher/basket.php',{func:'listPromoted'});
}

function checkOut()
{
	$('#checkoutform_head').show();
	//$('.GiftVoucherRight').css({'margin-top':"0px"});
	$('.GiftVoucherRight').load(
			'/giftvoucher/basket.php',
			{
				func:'Checkout_step1'
			}
		);
	$('#checkoutform_tail').show();
}

function emailCheck()
{
	if(document.getElementById('EMAIL').value == '') {
		alert("You must fill in all fields to complete purchase.");
		$('#noEmail').show();
		return false;
	}
	return true;
}
