//##################################################     T R I M     M E T H O D S     #############################
// http://www.developingskills.com/ds.php?article=jstrim&page=1
function strltrim() {
	//Match spaces at beginning of text and replace with a null string
	return this.replace(/^\s+/,'');
}

function strrtrim() {
	//Match spaces at end of text and replace with a null string
	return this.replace(/\s+$/,'');
}

function strtrim() {
	//Match spaces at beginning and end of text and replace with null strings
	//return this.replace(/^\s+/,'').replace(/\s+$/,'');
	return this.ltrim().rtrim();
}

String.prototype.ltrim = strltrim;
String.prototype.rtrim = strrtrim;
String.prototype.trim  = strtrim;

//########################################     O P E N S     A     N E W     W I N D O W     #########################
function NewWindow(url, winname, w, h, popunder) {
	try {
		var winl = (screen.availWidth  - w) / 2;
		var wint = (screen.availHeight - h) / 2;

		var win = window.open( url, winname, "width="+w+",height="+h+",resizable=1,menubar=0,scrollbars=1,toolbar=0,location=0,top="+wint+",left="+winl );
		if (popunder) win.blur();
		return win;
	}
	catch(e) {
		alert('window open failed');
		return false;
	}
}


//#####################################     V A L I D A T E S      D D     D E T A I L S     #########################
function checkDDlookup( oButton ) {
	var oForm = oButton.form;
	var retval = true;		// don't stop if we can't check things properly

	if (oForm) {
		var sortcode =	oForm.lookupDDsortCode1.value.trim() +
										oForm.lookupDDsortCode2.value.trim() +
										oForm.lookupDDsortCode3.value.trim();
		var accountno =	oForm.lookupDDaccountNumber.value.trim();

		if (accountno.match(/\w{7,9}/)==null) {
			retval = false;
			oForm.lookupDDaccountNumber.focus();
			oForm.lookupDDaccountNumber.select();
			alert( 'Please enter/correct your account number' );
		}
		else if (sortcode.match(/\d{6}/)==null) {
			retval = false;
			oForm.lookupDDsortCode1.focus();
			alert( 'Please enter/correct your sort code' );
		}
	}

	return retval;
}

//#####################################    FOR SINGLE PAGE SIGNUP ONLY   #########################

function paymentStep() {
	$('#cc_details').show()
	setCookie('ADDRESSDONE',1,1);
	
	}

// show either the uk (AFD) or non uk address form
function showAddress() {
	if ($('#signupMailingCountry').val() == 'GBR') {
		$("#ukaddress").show();
		$("#findme").removeAttr( 'disabled' );
		$("#lookupPostcode").removeAttr( 'disabled');
		$("#nonukaddress").hide(); 
		deleteCookie( 'NONUKADD', '/', '' );
		setCookie('UKADD',1,1);
	} else {
		$("#ukaddress").hide();
		$("#findme").attr( 'disabled', 'disabled');
		$("#lookupPostcode").attr( 'disabled', 'disabled');
		$("#nonukaddress").show(); 
		// Set cookie in case of refresh
		deleteCookie( 'UKADD', '/', '' );
		setCookie('NONUKADD',1,1);
	}

}

function hideDiffAddrFields() {
	$("#mailaddr").slideUp("normal");
}

function showDiffAddrFeildsUK() {
	//alert( $('#show_bill_addr').val()+ ' ' +$('#show_bill_addr2').val());
	$("#mailaddr").slideDown("normal");
	
	if (document.getElementById('show_bill_addr2').checked == false) {
		hideDiffAddrFields();	
		deleteCookie( 'billfields', '/', '' );
		// change value of hidden field so that signup obj knows whether to set mandatory fields for billing address
		// See process() function in signup class
		$('#diffbill').val('0');
		$('.show_bill_addr').val('0');
	} else {
		setCookie('billfields',1,1);
		$('#diffbill').val('1');
		$('.show_bill_addr').val('1');
	}
	
}

// not the same as above
function showDiffAddrFeilds() {
	//alert( $('#show_bill_addr').val()+ ' ' +$('#show_bill_addr2').val());
	$("#mailaddr").slideDown("normal");
	if (document.getElementById('show_bill_addr').checked == false) {
		hideDiffAddrFields();	
		deleteCookie( 'billfields', '/', '' );
		// change value of hidden field so that signup obj knows whether to set mandatory fields for billing address
		// See process() function in signup class
		$('#diffbill').val('0');
		$('.show_bill_addr').val('0');
	} else {
		setCookie('billfields',1,1);
		$('#diffbill').val('1');
		$('.show_bill_addr').val('1');
	}
}

/*
*@desc work out what the payment type is  and show the correct form
*
*@param int payment option id
*@return void
*/
function setPaymentMethod(payopt) {
	// set the payment option hidden field
	 $('#signupPaymentOption').val(payopt);
	// split the payment_methods string into array so we can match the payopt id to the payment method (pm)
	payment_methods_arr = new Array();
	payment_methods_arr = payment_methods.split(',');
	// match the payt opt id to the array of ( id => pm )
	$.each( payment_methods_arr, function(i, n){
		if (payopt == n.substr(0,3)) {
			pm = n.substr(4,5);
		}
	});
	
	if (pm == 'DD') {
		$('#paymentmethod').val('DD');
		$('#cc').hide();
		$('#dd').slideDown("normal");
		// Set cookie in case of errors refresh
		deleteCookie( 'CC', '/', '' );
		setCookie('DD',1,1);
	} else {
		$('#paymentmethod').val('CC');
		$('#cc').slideDown("normal");
		$('#dd').hide();
		// Set cookie in case of errors refresh
		deleteCookie( 'DD', '/', '' );
		setCookie('CC',1,1);
	}
}

//#####################################    GET SET AND DELETE JS C O O K I E S   #########################

function setCookie(c_name,value,expiredays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}
// ---------------------------------------------------------------------------

function getCookie(c_name) {
	if (document.cookie.length>0) {
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1) { 
		c_start=c_start + c_name.length+1; 
		c_end=document.cookie.indexOf(";",c_start);
		if (c_end==-1) c_end=document.cookie.length;
		return unescape(document.cookie.substring(c_start,c_end));
	    } 
	}
	return "";
}
// ---------------------------------------------------------------------------

function checkCookie() {
	username=getCookie('username');
	if (username!=null && username!="") {
		alert('Welcome again '+username+'!');
	} else {
		username=prompt('Please enter your name:',"");
		if (username!=null && username!="") {
			setCookie('username',username,365);
		} 
	}
}
// ---------------------------------------------------------------------------
function deleteCookie( name, path, domain ) {
if ( getCookie( name ) ) document.cookie = name + "=" +
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}
