function validate_transfer(){
			//Check passwords match.
    if(document.funds.source_id.value == document.funds.target_id.value){
    	  alert ( "Please select two different accounts to transfer the funds." );
        return false;
    }
    
    if(document.funds.source_id.value == "-1" || document.funds.target_id.value == "-1"){
    		alert ( "Please select an account to transfer from and an account to transfer to." );
    		return false;
    }
    
    if(!notEmpty(document.getElementById('amount')))
				return false;
    
    if(!isAmount(document.getElementById('amount'), 'Amount must be numeric.'))
    	return false;
}

function changeAction(page) {
    document.forms[0].action = page;
}

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 showError(msg){
	alert(msg);
}

//launchGame('800','600','blackjack')

function launchGame(wwidth, hheight, game){
	
		var aurl = "/playgame.php?game="+game+"&width="+wwidth+"&height="+hheight;

		var game_name = 'game';
		
		var nocache = new Date().getTime();
		var ttop = (screen.height-hheight)/2;
		if (ttop < 0) ttop = 10;
		var lleft = (screen.width-wwidth)/2;
		if (lleft < 0) lleft = 10;
		
		wwidth = '990';
		hheight = '720';
		
		if(game == 'XGBINGO')
			game_name = 'bingo';
		else if(game == 'PARLAY90BINGO' || game == 'PARLAY75BINGO')
			game_name = 'instantbingo';
		else if(game.indexOf('CZBINGO') != -1)
			game_name = 'bpgbingo';
		else
		{
			wwidth = '900';
			hheight = '700';
		}
	
		window.open(aurl,game_name,'width='+wwidth+',height='+hheight+',top='+ttop+',left='+lleft+'');
}

function popUp(aurl, wwidth, hheight, popname){
    
        var nocache = new Date().getTime();
        var ttop = (screen.height-hheight)/2;
        if (ttop < 0) ttop = 10;
        var lleft = (screen.width-wwidth)/2;
        if (lleft < 0) lleft = 10;
    
        window.open(aurl,popname,'width='+wwidth+', scrollbars=1, height='+hheight+',top='+ttop+',left='+lleft+'');
}

function popupform(myform)
{
	if (! window.focus)return true;
	window.open('', 'formpopup', 'width=800,height=600,scrollbars=no');
	myform.target='formpopup';
	myform.submit();
}

function launchWalpay(aurl){
	
		var wwidth =  550;
		var hheight = 650;
	
		var nocache = new Date().getTime();
		var ttop = (screen.height-hheight)/2;
		if (ttop < 0) ttop = 10;
		var lleft = (screen.width-wwidth)/2;
		if (lleft < 0) lleft = 10;
	
		window.open(aurl,'walpay','width='+wwidth+', scrollbars=1, height='+hheight+',top='+ttop+',left='+lleft+'');
}

function launchCB(aurl){
    
        var wwidth =  740;
        var hheight = 650;
    
        var nocache = new Date().getTime();
        var ttop = (screen.height-hheight)/2;
        if (ttop < 0) ttop = 10;
        var lleft = (screen.width-wwidth)/2;
        if (lleft < 0) lleft = 10;
    
        window.open(aurl,'cb','width='+wwidth+', scrollbars=1, height='+hheight+',top='+ttop+',left='+lleft+'');
}

function launchRap(aurl){
		var wwidth =  220;
		var hheight = 60;
	
		var nocache = new Date().getTime();
		var ttop = (screen.height-hheight)/2;
		if (ttop < 0) ttop = 10;
		var lleft = (screen.width-wwidth)/2;
		if (lleft < 0) lleft = 10;
		
		window.open(aurl,'rap','width='+wwidth+', scrollbars=1, height='+hheight+',top='+ttop+',left='+lleft+'');
}

function notloggedin(){
	//alert('You must be logged in to play the games.');
	window.location = "/register.php?sender=notloggedin"
}

function closeAndRefreshPage()
{
	window.opener.location.reload( true );
	window.close();
}

function notEmpty(elem){
	
	if(elem.value.length == 0){
		alert("Please make sure you fill in the "+elem.name+" field");
		elem.focus(); // set the focus to this input
		return false;
	}
	return true;
}

function selectedValue(elem){
	
	if(elem.value == "0"){
		alert("Please select an option from the "+elem.name+" field");
		elem.focus();
		return false;
	}
	return true;
}

function emailValidator(elem){
	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	if(elem.value.match(emailExp)){
		return true;
	}else{
		alert('Please enter a valid email address');
		elem.focus();
		return false;
	}
}

function isNumeric(elem, helperMsg){
	var numericExpression = /^[0-9]+$/;
	if(elem.value.match(numericExpression)){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}


function isAmount(elem , helperMsg) {
  if (elem.value == null || !elem.value.toString().match(/^[-]?\d*\.?\d*$/))
  {
  	 alert(helperMsg);
		 elem.focus();
  	 return false;
  }
  else	 
  	return true;
}


function calcAge(d2)
{
	var bday=d2.getDate();
	var bmo=d2.getMonth();
	var byr=d2.getFullYear();
	
	var now = new Date();
	var byr;
	var age;
	
	tday=now.getDate();
	tmo=(now.getMonth()+1);
	tyr=(now.getFullYear());

	if((tmo > bmo)||(tmo==bmo & tday>=bday))
		age=byr
	else
		age=byr+1
	
	return(tyr-age);
}

function checkBirthday(elem) {
	
	if( !/^(\d{1,2})\/(\d{1,2})\/(\d{4})$/.test(elem.value) ) {
		alert( "Invalid date supplied - must be format DD/MM/YYYY" );
		elem.focus();
		return false;
	}
	
	var d = new Date();
	var d2 = new Date(RegExp.$3, RegExp.$2, RegExp.$1);
	
	age = calcAge(d2);

	if(age < 18 ) {
		alert("You must be 18+ to register.");
		return false;
	}
	
	return true;
}

function isUpperCase(param) {
  var val = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

  for (i=0; i<param.length; i++) {
    if (val.indexOf(param.charAt(i),0) == -1) return false;
  }
  return true;
}


function validate_registration(){
	
		//Check all required fields are filled in.
		var req_fields = new Array("nickname","pass","pass2","firstname","lastname","email","address1","town","zipcode","telephone","dob");
		
		var field = null;
		
		for (x in req_fields){
			if(!notEmpty(document.getElementById(req_fields[x])))
				return false;
		}
		
		
		//Check for invalid characters
		var chars = new Array("_","@");
		for (x in chars){
	    if(document.getElementById('nickname').value.indexOf(chars[x]) != -1){
	    	  alert ( "Your Username must not include the character '"+chars[x]+"'.");
	        return false;
	    }
	  }
    
    //check for Username all upercase.
   	if(isUpperCase(document.getElementById('nickname').value)){
				alert('Your Username must not be all UPPERCASE');
				return false;
		}

		//Check passwords match.
    if(document.getElementById('pass').value !=  document.getElementById('pass2').value){
    	  alert ( "Please make sure your passwords match" );
        return false;
    }
    
    //Check password is 6 chars or more.
    if(document.getElementById('pass').value.length < 6){
    	  alert ( "Please make sure your password is 6 characters or more." );
        return false;
    }
    
    //Check email address has been entered correctly.
 		if(!emailValidator(document.getElementById('email')))
 			return false;
        
    //Check telephone only contains numbers
    if(!isNumeric(document.getElementById('telephone'),'Telephone number must contain numbers only.'))
    	return false;
    	
    //Check dob field and make sure they are 18+
    if(!checkBirthday(document.getElementById('dob')))
    	return false;
    	
    if(document.getElementById('how').selectedIndex == 0)
    {
    	alert('Please tell us where you heard about us.');
    	return false;
    }
    	
    //Check user has accepted t's and c's
    if(!document.getElementById('terms').checked){
    	alert('You must accept the terms and conditions in order to register');
    	return false;
    }

    return true;
}

function validate_bingo_form(){
	
	if(document.forms[0].action.indexOf('register') != -1)
	{
		//User must be trying to register check fields have been entered correctly.
		
		//Check all required fields are filled in.
		var req_fields = new Array("nickname","pass","pass2","email");
		
		var field = null;
		
		for (x in req_fields){
			if(!notEmpty(document.getElementById(req_fields[x])))
				return false;
		}

		//Check passwords match.
    if(document.getElementById('pass').value !=  document.getElementById('pass2').value){
    	  alert ( "Please make sure your passwords match" );
        return false;
    }
    
    //Check password is 6 chars or more.
    if(document.getElementById('pass').value.length < 6){
    	  alert ( "Please make sure your password is 6 characters or more." );
        return false;
    }
    
    //Check email address has been entered correctly.
 		if(!emailValidator(document.getElementById('email')))
 			return false;
        
    //Check user has accepted t's and c's
    if(!document.getElementById('terms').checked){
    	alert('You must accept the terms and conditions in order to register');
    	return false;
    }

    return true;
  }
  else
  {
  	//Check all required fields are filled in.
		var req_fields = new Array("login-username","login-password");
		
		var field = null;
		
		for (x in req_fields){
			if(!notEmpty(document.getElementById(req_fields[x])))
				return false;
		}
		
		return true;
  }
   
}

function validate_details(){
		//Check all required fields are filled in.
		var req_fields = new Array("firstname","lastname","email","address1","town","zipcode","telephone","dob");
		
		for (x in req_fields){
			if(!notEmpty(document.getElementById(req_fields[x])))
				return false;
		}

    //Check email address has been entered correctly.
 		if(!emailValidator(document.getElementById('email')))
 			return false;
        
    //Check telephone only contains numbers
    if(!isNumeric(document.getElementById('telephone'),'Telephone number must contain numbers only.'))
    	return false;
    	
    //Check dob field and make sure they are 18+
    if(!checkBirthday(document.getElementById('dob')))
    	return false;
    
    return true;
}

function validate_password(){
			//Check passwords match.
    if(document.pass.pass.value !=  document.pass.pass2.value){
    	  alert ( "Please make sure your passwords match" );
        return false;
    }
    
    //Check password is 6 chars or more.
    if(document.pass.pass.value.length < 6){
    	  alert ( "Please make sure your password is 6 characters or more." );
        return false;
    }
}

function roundNumber(rnum, rlength) { // Arguments: number to round, number of decimal places
  var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
  return newnumber;
}

function calc_point_value(){
	document.redeem.pounds.value = roundNumber(document.redeem.points.value/200,2).toFixed(2);
}

function validate_points_2_pounds(){
	//Check amount only contains numbers
  if(!isAmount(document.getElementById('points'),'Amount entered must be numeric.'))
   	return false;
   	
  if(document.redeem.points.value < 1000)
  {
  	alert('You must enter an amount greater than 1000 points.');
  	return false;
  }
   	
  if(document.redeem.points.value > (document.redeem.balance.value*1))
  {
  	alert('Amount entered is greater than your Loyalty Points Balance please check and try again.');
  	return false;
  }	
  
  return true;
}

function validate_password(){
			//Check passwords match.
    if(document.pass.pass.value !=  document.pass.pass2.value){
    	  alert ( "Please make sure your passwords match" );
        return false;
    }
    
    //Check password is 6 chars or more.
    if(document.pass.pass.value.length < 6){
    	  alert ( "Please make sure your password is 6 characters or more." );
        return false;
    }
}

function validate_forgetten_password(){
    
    if(!notEmpty(document.getElementById('user'))){
        return false;
    }
    
    return true;
}

function validate_cashoo_reg(){
		//Check all required fields are filled in.
		var req_fields = new Array("firstname","lastname","email","address1","town","zipcode","county","telcode","telno");
		
		for (x in req_fields){
			if(!notEmpty(document.getElementById(req_fields[x])))
				return false;
		}

    //Check email address has been entered correctly.
 		if(!emailValidator(document.getElementById('email')))
 			return false;
        
    //Check telephone only contains numbers
    if(!isNumeric(document.getElementById('telno'),'Telephone number must contain numbers only.'))
    	return false;
    
    return true;
}

function validate_cashoo_pay(){
	
		//Check all required fields are filled in.
		var req_fields = new Array("email", "amount");
		
		for (x in req_fields){
			if(!notEmpty(document.getElementById(req_fields[x])))
				return false;
		}

    //Check email address has been entered correctly.
 		if(!emailValidator(document.getElementById('email')))
 			return false;
        
    //Check amount only contains numbers
    if(!isNumeric(document.getElementById('amount'),'Amount must contain numbers only.'))
    	return false;

		if(document.getElementById('amount').value < 10)
		{
				alert('The minimum deposit amount is 10');
				return false;
		}

    return true;
}

function validate_ukash(){
		
		//Check all required fields are filled in.
		var req_fields = new Array("vouchernumber", "vouchervalue");
		
		for (x in req_fields){
			if(!notEmpty(document.getElementById(req_fields[x])))
				return false;
		}
		
		if(document.getElementById('vouchervalue').value < 10)
		{
				alert('The minimum deposit amount is 10');
				return false;
		}

    return true;
}

function validate_cashout(){
	
		var req_fields = new Array("amount", "password", "name");
		var number_fields = new Array("amount");
	
		if(document.funds.method.value == "bank")
		{
			req_fields = new Array("amount", "password", "name", "bank", "number", "code1", "code2", "code3", "code4", "code5", "code6");
			number_fields = new Array("number", "code1", "code2", "code3", "code4", "code5", "code6");
		}	
		
		for (x in req_fields){
			if(!notEmpty(document.getElementById(req_fields[x])))
				return false;
		}

    for (x in number_fields){
			if(!isNumeric(document.getElementById(number_fields[x]), 'Field must contain numbers only.'))
				return false;
		}
		
    if(document.getElementById('number').value.length != 8){
    	  alert ( "Please make sure your account number is 8 digits." );
        return false;
    }
		
		if(document.getElementById('amount').value < 20){
				alert('The minimum cashout amount is 20');
				return false;
		}
    
    return true;
}

function validate_cb(){
	
		var req_fields = new Array("Amount");
		var number_fields = new Array("Amount");
	
		for (x in req_fields){
			if(!notEmpty(document.getElementById(req_fields[x])))
				return false;
		}

    for (x in number_fields){
			if(!isNumeric(document.getElementById(number_fields[x]), 'Amount must contain numbers only.'))
				return false;
		}
		
		if(document.getElementById('Amount').value < 10)
		{
				alert('The minimum deposit amount is 10');
				return false;
		}
    
    return true;
}


function validate_lps()
{
	//Check all required fields are filled in.
		var req_fields = new Array("amount","cardnumber","securitynumber");
		
		for (x in req_fields){
			if(!notEmpty(document.getElementById(req_fields[x])))
				return false;
		}

 		var num_fields = new Array("amount","cardnumber","securitynumber");
 			
 		for (x in num_fields){
			if(!isNumeric(document.getElementById(num_fields[x]),'Field must contain numbers only.'))
				return false;
		}
		
		if(document.getElementById('amount').value < 10)
		{
				alert('The minimum deposit amount is 10');
				return false;
		}
		    	
    return true;
}

function validate_yourshot(){

		//Check all required fields are filled in.
		var req_fields = new Array("firstname","lastname","dob","email","occupation","best-time","telephone","address1","town","zipcode","game-name","game-outline","competiting-games","game-advantage","game-detail","url");

		for (x in req_fields){
			if(!notEmpty(document.getElementById(req_fields[x])))
				return false;
		}

		//Check required dropdowns have been selected.
		var req_selections = new Array("game-platform","development-level","type-of-game","game-amount");
		
		for (x in req_selections){
			if(!selectedValue(document.getElementById(req_selections[x])))
				return false;
		}

		//Check email address has been entered correctly.
 		if(!emailValidator(document.getElementById('email')))
 			return false;
        
    //Check telephone only contains numbers
    if(!isNumeric(document.getElementById('telephone'),'Telephone number must contain numbers only.'))
    	return false;

    return true;
}

/**
 * Returns true if the string is a valid credit card number.
 *
 * @return bool
 */
String.prototype.is_valid_card_number = function()
{
	// replace any white space in the card number
	var number = this.replace(/\s+/g, '') ;

	//replace if contains non-numbers
	if(number.match(/\D/)){
		return false ;
	}

	// convert to array and reverse the number
	number = number.split('').reverse().join('') ;

	// loop through the number one digit at a time
	// double the value of every second digit starting
	// from the right, and concatenate the new values 
	// with the unaffected digits
	var digits = '';
	for(var i = 0; i < number.length; i++){
		digits += '' + ((i%2) ?
			number.charAt(i) * 2 :
			number.charAt(i)) ;
	}

	// add all of the single digits together
	var sum = 0 ;
	for (var i = 0; i < digits.length; i++){
		sum += (digits.charAt(i) * 1) ;
	}
	//alert(sum) ;

	// valid card numbers will be transformed into
	// a multiple of 10
	return (sum % 10) ? false : true ;
}