/***************************************************************************************************************************************
* Stack-Em Functions
***************************************************************************************************************************************/

// Initialize variables

//var promoStatus=0;	// default promo status to 0 - if promo code successfully entered, then will become set to 1 (global var)


/***************************************************************************************************************************************
* PROMO CODE ENTER BUTTON STUFF
***************************************************************************************************************************************/
function testPromoCode() {

	// see if a valid promo code was entered
	thispromocode=document.getElementById('promocode').value.toLowerCase();		// get promocode (forced to Lower Case)

	// FULL06
	if(thispromocode=='full06') {
		promostatus=1;			// promo accepted
		promoamount=0.1;		// 5%
		document.getElementById('promotablerow').innerHTML='<B>Promotional discount of 5%</B>';
		document.getElementById('promoheading1').innerHTML='';
		domaths();
		alert("Your Promotional Code '"+thispromocode+"' was successful\nYour will now receive a promotional discount of 5%");
		}

	// ROBI06
	if(thispromocode=='robi06') {
		promostatus=1;			// promo accepted
		promoamount=0.05;		// 5%
		document.getElementById('promotablerow').innerHTML='<B>Promotional discount of 5%</B>';
		document.getElementById('promoheading1').innerHTML='';
		domaths();
		alert("Hello to the Frederic Robinson Brewery\n\nYour Promotional Code '"+thispromocode+"' was successful\nYour will now receive a promotional discount of 5%");
		}

	// EZE08
	if(thispromocode=='eze08') {
		promostatus=1;			// promo accepted
		promoamount=0.05;		// 5%
		document.getElementById('promotablerow').innerHTML='<B>Promotional discount of 5%</B>';
		document.getElementById('promoheading1').innerHTML='';
		domaths();
		alert("Your Promotional Code '"+thispromocode+"' was successful\nYour will now receive a promotional discount of 5%");
		}


	return true;
	
}

/***************************************************************************************************************************************
* PAY BY CHEQUE - TOGGLE BUTTON TEXT - HIDE/SHOW PAYMENT FORM
***************************************************************************************************************************************/
function toggleChequeButton() {

	var thischequestatus=document.getElementById('paybycheque').checked;
	if(thischequestatus==true) {
		// CHECKED - PAY BY CHEQUE
		document.mf.paymentmethod.value='cheque';
		document.mf.submitform.value='Pay by cheque >';
		document.mf.target="proforma";		// cheque payment to go to new browser window with pro forma on view
		showaddress('addresstable');
	} else {
		// NOT CHECKED - PAY USING PAYPAL
		document.mf.paymentmethod.value='paypal';
		document.mf.submitform.value='Pay with PayPal >';
		document.mf.target="_self";		// cheque payment to go to new browser window with pro forma on view
		hideaddress('addresstable');
	}
	
}

/***************************************************************************************************************************************
* SHOW ADDRESS TABLE
***************************************************************************************************************************************/
function showaddress(id)
{
     if (document.getElementById(id).style.display == 'none')
     {
          document.getElementById(id).style.display = 'block';
     }
}

/***************************************************************************************************************************************
* HIDE ADDRESS TABLE
***************************************************************************************************************************************/
function hideaddress(id)
{
     if (document.getElementById(id).style.display == 'none')
     {
          document.getElementById(id).style.display = 'none';
     }
     else
     {
          document.getElementById(id).style.display = 'none';
     }
}

/***************************************************************************************************************************************
* ADDRESS FORM VALIDATION CODE
***************************************************************************************************************************************/
function addressValidate2(){
	
	//	only do this if the address form is visible (pay by cheque is checked)
	errs=false;		// become global once used
	errtext="";		// become global once used
	var thischequestatus=document.getElementById('paybycheque').checked;
	if(thischequestatus==true) {
		
		var cname = document.getElementById('cname');
		var caddr1 = document.getElementById('caddr1');
		var ctowncity = document.getElementById('ctowncity');
		var ccounty = document.getElementById('ccounty');
		var cpostcode = document.getElementById('cpostcode');
		var ctel = document.getElementById('ctel');
		var cemail = document.getElementById('cemail');
		// check posted fields
		if(isEmpty(cname, "Please enter your name")) errs=true;
		if(isEmpty(caddr1, "Please enter at least the first line of your address")) errs=true;
		if(isEmpty(ctowncity, "Please enter your town/city")) errs=true;
		if(isEmpty(ccounty, "Please enter your county")) errs=true;
		if(isEmpty(cpostcode, "Please enter your postcode")) errs=true;
		if(isEmpty(ctel, "Please enter your telephone number")) errs=true;
		//if(emailValidation(cemail, "Please enter a valid email address")) errs=true;

		// see if any errors occured
		if(errs==true) {
			alert(errtext);
			return false;
		} else {
			// no errors found
			return true;
		}
		
	}
	
}

function isEmpty(elem, helperMsg){
	if(elem.value.length == 0){
		errtext+=helperMsg+"\n";
		if(errs==false) elem.focus();
		return true;
	}
	return false;
}

function isNumeric(elem, helperMsg){
	var numericExpression = /^[0-9]+$/;
	if(elem.value.match(numericExpression)){
		return true;
	}else{
		errtext+=helperMsg+"\n";
		if(errs==false) elem.focus();
		return false;
	}
}

function isAlphabet(elem, helperMsg){
	var alphaExp = /^[a-zA-Z]+$/;
	if(elem.value.match(alphaExp)){
		return true;
	}else{
		errtext+=helperMsg+"\n";
		if(errs==false) elem.focus();
		return false;
	}
}

function isAlphanumeric(elem, helperMsg){
	var alphaExp = /^[0-9a-zA-Z]+$/;
	if(elem.value.match(alphaExp)){
		return true;
	}else{
		errtext+=helperMsg+"\n";
		if(errs==false) elem.focus();
		return false;
	}
}

function lengthRestriction(elem, min, max){
	var uInput = elem.value;
	if(uInput.length >= min && uInput.length <= max){
		return true;
	}else{
		errtext+=("Please enter between " +min+ " and " +max+ " characters\n");
		if(errs==false) elem.focus();
		return false;
	}
}

function madeSelection(elem, helperMsg){
	if(elem.value == "Please Choose"){
		errtext+=helperMsg+"\n";
		if(errs==false) elem.focus();
		return false;
	}else{
		return true;
	}
}

function emailValidation(elem, helperMsg){
	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	if(elem.value.match(emailExp)){
		return true;
	}else{
		errtext+=helperMsg+"\n";
		if(errs==false) elem.focus();
		return false;
	}
}

/***************************************************************************************************************************************
* 
***************************************************************************************************************************************/

