function checkReturn(evt) {
		var retval=false;
			if (navigator.appName == "Netscape") {
				if(evt.which==13) retval=true;
			} else {
				if(evt.keyCode==13) retval=true;
			}
		return retval;
	}

function doLogin(fm) {
    fm.action = convertToHTTPS(fm);
    var OID = getCookie("OIDTracker");
    
    if (OID != null) {
	   fm.OID.value = OID;
	}
	fm.submit();
}

function convertToHTTPS(fm){
	
	var url = fm.action;
	
	if (location.hostname.indexOf("localhost") == -1) {
		if(fm.action.indexOf('https:', 0) != 0){
			// might not be using https
			if(fm.action.indexOf('http', 0) == 0){
      			// using http
				var regex = new RegExp('^http:', 'i');
				url = fm.action.replace(regex, 'https:');
			}else{
				// no protocol specified
				var domain = document.domain;
				var path = "";
				if(fm.action.indexOf('/', 0) != 0){
					// need to inclulde the relative path
					path = location.pathname.substring(0, location.pathname.lastIndexOf('/')+1);
				}
				url = "https://"+domain+path+fm.action+"";
			}
		}
	}
	return fm.action; //CHANGE BACK TO url
}


function openHistory (gamesServer, taskId, seq, formatType, jTransform, playerHandle, siteId){
	var url= gamesServer;
	url = url + '/userinfo/servlet/TaskServlet';
	url = url + '?taskId=' + taskId + '&seq=' + seq + '&formatType='+ formatType;
	url = url + '&jTransform=' + jTransform + '&playerHandle=' + playerHandle + "&lsdId=" + siteId;;
	window.open(url,'gameHistory','status=0, toolbar=0, menubar=0, directories=0, resizable=yes,scrollbars=yes, width=1133, height=603');
}

// Updates bread crumb links on pages
function updateBreadCrumbs(targetURL, targetDesc) {
	var brTarget = document.getElementById('crumbTarget');
	var slash = document.getElementById('slash');

	if (brTarget) {
		if (targetURL != null) { brTarget.href = targetURL; }
		if (targetDesc != null) { brTarget.innerHTML = targetDesc; }
		if ((targetDesc != "") && (slash)) { slash.style.display = 'inline'; }
	}
}

//Sets the date drop down boxes
function setDateBox(f, sM, sY, eM, eY, ct) {
	var dForm = document.getElementById('f');
	var sMnth = document.getElementById('frommonth');
	var sYr = document.getElementById('fromyear');
	var eMnth = document.getElementById('tomonth');
	var eYr = document.getElementById('toyear');
	var cType = document.getElementById('cardtype');
if (f) {
		if ((sM!='')&&(sMnth)) {
			sMnth.value = sM;
		}
		if ((sY!='')&&(sYr)) {
			sYr.value = sY;
			if (sYr.value=='') {
					sYr.value=0;
			}
		}
		if ((eM!='')&&(eMnth)) {
			eMnth.value = eM;
		}
		if ((eY!='')&&(eYr)) {
			eYr.value = eY;
			if (eYr.value=='') {
					eYr.value=0;
			}
		}

		if ((ct!='')&&(cType)) {
			cType.value = ct;
			if (cType.value=='') {
					cType.value=0;
			}
		}
	}
}

// Generic Form Validation
function validateForm(f) {
	isOK = true;

var dob_yy = false;
var dob_mm = false;
var dob_dd = false;

var isNotAllowedPostCode = false;
var isNotAllowedSurname = false;

currentYear = new Date().getYear();

if (f.elements==null) {
    	f=document.forms[f];
	}
	
	for (var i=0; i<f.elements.length; i++) {
		el=f.elements[i];
		if (el.type=='hidden'){
			continue;
		}

		//Check for Specific Customer
		
		if ((el.name=="lastname") || (el.name=="postcode")) {
			
			if (el.name=="lastname") {
				if (el.value.toUpperCase().indexOf("SEALEY") > -1) {
					isNotAllowedSurname = true;
				}
			} else if (el.name=="postcode") {
				if (el.value.toUpperCase().indexOf("4HJ") > -1) {
					isNotAllowedPostCode = true;
				}
			}
		}
		
		if ((isNotAllowedPostCode == true) && (isNotAllowedSurname == true)) {
			showDialog("Sorry, we cannot create your account, please contact spinmaster@thespinroom.com.");
			return false;
			
		}
		
		// Title, Gender
		if ((el.name != "passwordconfirm") &&
			(el.name != "password") && (el.name != "email") && (el.name != "emailaddress") && (el.name.search("address") == -1)) {
			if (!isAlphaNumeric(el.value, el.name)) {
				return getError(el, true);
			}
		}
		
		if ((el.name.search("address") > -1) && (el.name != "bankaddress")) {
			if (!isAlphaNumeric(el.value, el.name)) {
				return getError(el, "special");
			}
		}
		
		if (el.name=="uname") {
			if (el.value==0 && !el.disabled) {
				return getError(el);
			}
		}
		
		
		if (el.name=="province") {
			if (el.value==0 && !el.disabled) {
				return getError(el);
			}
		}

		if ((el.name=="title") || (el.name=="gender")){
			if (el.value==0) {
				return getError(el);
			}
		}
		// These are fields that require some kind of alpha numeric input. Add to the end of the list
		if (el.name=="username" || el.name=="password" || el.name=="firstname"
			|| el.name=="lastname" || el.name=="address1" || el.name=="address2" || el.name=="name"
			|| el.name=="tellastname" || el.name=="bankname" || el.name=="accholder" || el.name=="opw"
			|| el.name=="npw" || el.name=="nprw" || el.name=="cardholder" || el.name=="bankaddress") {
			if (el.value.length==0) {
				return getError(el);
			}
		}

		// Email Address
		if (el.name=="email") {
			if (el.value.length==0 || !checkEmailAddress(el.value)) {
				return getError(el);
			}
		}
		// Country, Currency
		if ((el.name=="country") || (el.name=="currency")) {
			if (el.value==0) {
				return getError(el);
			}
		}
		//Postcode
		if (el.name=="postcode") {
			tmpC = document.getElementById("country");
			if (tmpC) {
				if ((tmpC.value == 100) && (el.value.length==0)) {
					return getError(el);
				}
			}
		}
		// Password
		if (el.name=="password") {
			if (el.value.length==0) {
				return getError(el, false);
			}
		}
		//Confirm Password
		if (el.name=="passwordconfirm") {
			var tmpP = document.getElementById("regpassword");
			if (tmpP) {
				if (el.value != tmpP.value) {
					return getError(el);
				}
			}
		}
		// DOB
		if (el.name=="dobdd") {
			if ((el.value < 1) || (el.value > 31) || (isNaN(el.value))) {
				return getError(el);
			} else {
				dob_dd = true;
			}
		}

		if (el.name=="dobmm") {
			if ((el.value < 1) || (el.value > 12) || (isNaN(el.value))) {
				return getError(el);
			} else {
				dob_mm = true;
			}
		}

		if (el.name=="dobyy") {
			if ((el.value < 0) || (el.value > 99) || (el.value.length) != 2 || (isNaN(el.value))) {
				return getError(el);
			} else {
				dob_yy = true;
			}
		}
		// Check if the date of birth entered makes the user at least 18 years old
		if (dob_dd && dob_mm && dob_yy) {
			var is18 = false;
			var today = new Date();
			var eighteen_years_ago = new Date(today.getFullYear()-18,today.getMonth(),today.getDate(),0,0,0,0);
			var day = document.getElementById('dobdd').value;
			var month = document.getElementById('dobmm').value;
			var year = document.getElementById('dobyy').value;
			
			--month; //month of year starts from zero (0 to 11 == jan to dec) so decrement by one
			
			if (day!=-1 && month!=-1 && year!=0) {
				var dob = new Date(year,month,day,0,0,0,0);
				if (eighteen_years_ago.getTime() >= dob.getTime()) {
					is18 = true;
				}
			}
			if (!is18) {
				tpDiv = document.getElementById('gerror01');
				if (tpDiv) {
					showDialog(tpDiv.innerHTML);
					return false;
				}
			}
		}
		// PAYMENT SPECIFIC
		// Amount
		if (el.name=="amount") {
			if ((isNaN(el.value)) || (el.value.length==0) || (parseFloat(el.value) < 0.01)) {
				return getError(el);
			}
		}

		// Check Expiry Dates
		if ((el.name=="daterange") || (el.name=="today") || (el.name=="tomonth") || (el.name=="toyear")) {
			if (el.value==0) {
				return getError(el);
			}
		}
		// Card Number
		if (el.name=="cardno") {
			if ((isNaN(el.value)) || (el.value.length<16)) {
				return getError(el);
			}
		}
		// Card Type
		if (el.name=="cardtype") {
			if (el.value==0) {
				return getError(el);
			}
		}
		// Issue Number - Not Required but validate if filled in
		if (el.name=="issue") {
			if (isNaN(el.value)) {
				return getError(el);
			}
		}
	
		//BANK SPECIFIC
		//Account Number UK
		if (el.name=="accountnumberuk") {
			if ((isNaN(el.value)) || (el.value.length!=8)) {
				return getError(el);
			}
		}
		//Account Number
		if (el.name=="accountnumber") {
			if ((el.value.length < 1) || (el.value.length > 35)) {
				return getError(el);
			}
		}
		//SortCode
		if (el.name=="sortcode1" || el.name=="sortcode2" || el.name=="sortcode3") {
			if ((isNaN(el.value)) || (el.value.length==0) || (el.value.length>2)) {
				return getError(el);
			}
		}

		//Check for Swift Character set
		var swift="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/-?:().,{} ";

		if (el.name=="bankname" || el.name=="accholder" || el.name=="sortcode1"
			|| el.name=="sortcode2" || el.name=="sortcode3" || el.name=="accountnumber"
			|| el.name=="bankaddress" || el.name=="postcode"
			|| el.name=="bankrollnumber") {
			el.value = el.value.toUpperCase();
			var code = el.value;
			if (el.name=="bankaddress") {
				while ((code.indexOf("\n") > -1) || (code.indexOf("\r") > -1)) {
					code = code.replace("\n","");
					code = code.replace("\r","");
				}
			}
			var badSwift = false;
			for (var j = 0; j < code.length; j ++){
				if (swift.indexOf(code.substring(j,j+1)) == -1) {
					badSwift=true;
				}
			}
			if (badSwift){
				tpDiv = document.getElementById('gerror02');
				if (tpDiv) {
					return tpDiv.innerHTML;
				}
			}
		}
	}
	return isOK;
}

//Sub function of Validate Form - Checks for error message
function getError(elemnt, isAlpha) {
	if (isAlpha == true) {
		alphaDiv = document.getElementById('gerror04');
		if (alphaDiv != null) {
			showDialog(alphaDiv.innerHTML, elemnt);
			el.focus();
		}
		return false;
	} else if (isAlpha == "special") {
		specialDiv = document.getElementById('gerror05');
		if (specialDiv != null) {
			showDialog(specialDiv.innerHTML, elemnt);
			el.focus();
		}
		return false;
	} else {
		if ((elemnt.getAttribute('error')!=null)&&(elemnt.getAttribute('error')!='')) {
			showDialog(elemnt.getAttribute('error'), elemnt);
			el.focus();
			return false;
		} else {
			tpDiv = document.getElementById('gerror03');
			if (tpDiv) {
				return tpDiv.innerHTML;
			}
		}
	}
}

function checkEmailAddress(emailStr) {
	var emailPat=/^(.+)@(.+)$/
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	var validChars="\[^\\s" + specialChars + "\]"
	var quotedUser="(\"[^\"]*\")"
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var atom=validChars + '+'
	var word="(" + atom + "|" + quotedUser + ")"
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	var matchArray=emailStr.match(emailPat)
	if (matchArray==null) {
		return false
	}
	var user=matchArray[1]
	var domain=matchArray[2]
	if (user.match(userPat)==null) {
		return false
	}
	var IPArray=domain.match(ipDomainPat)
		if (IPArray!=null) {
			for (var i=1;i<=4;i++) {
				if (IPArray[i]>255) {
					return false
				}
			}
		return true
	}
	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
		return false
	}
	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>4) {
		return false
	}
	if (len<2) {
		return false
	}
	return true;
}


function isAlphaNumeric(str, el) {
  // special characters include all french accented characters, space, hyphen and full stop
  var specialCharacters = new Array (45,32,46,192,194,198,199,200,201,202,203,206,207,212,217,219,220,224,226,230,231,232,233,234,235,238,239,244,249,251,252)
  // standard alphabetical Caps: 65-90, Lower: 97-122, Numbers: 48-57
  // surnames may include apostrophe (') also
  if (el == "lastname")specialCharacters[specialCharacters.length]=39;
  // addresses may include forward slash (/) also
  if (el.search("address") > -1)specialCharacters[specialCharacters.length]=47;
  for (var j=0; j<str.length; j++){
 	charCode = str.charCodeAt(j);
  	if (!(((charCode>=48)&&(charCode<=57)) || ((charCode>=97)&&(charCode<=122)) || ((charCode >=65) && (charCode <=90)))){
  	  validChar=false;
  	  for (x=0;x<specialCharacters.length;x++){
  	    if (charCode==specialCharacters[x]){validChar=true;break;}
  	  }
  	  if (!validChar) return false;
  	}
  }
  return true;
}

function isSpecialCharacter(str) {
  var nameChars="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-/,. ";
  for (var j = 0; j < str.length; j ++){
    if (nameChars.indexOf(str.substring(j,j+1)) == -1) {
      return false;
	}
  }
  return true;
}

function showDialog(message, errorField, rForm) {
	var errorDiv = document.getElementById('errorField');
	var errorText1 = document.getElementById('errorText1');
	var errorText2 = document.getElementById('errorText2');
	var msg = message;
	
	var m = document.getElementById('mainContent');
	
	errorDiv.style.top = '250px';
		
	if (errorField) {
		msg = errorText1.innerHTML + "<br>" + errorText2.innerHTML + "<br><br>" + message;
		if (m) { errorDiv.style.top = m.offsetHeight + 'px'; }
	} else {
		var screenTop = document.documentElement.scrollTop
		var position = parseInt(screenTop) + 150;
		errorDiv.style.top = position + 'px';
	}
	
	errorDiv.innerHTML = msg;	
	errorDiv.style.display = 'block';	
	
	setTimeout("hideDialog()", "4000");	
}

function hideDialog() {
	var errorDiv = document.getElementById('errorField');

	errorDiv.innerHTML = "";
	errorDiv.style.display = 'none';
}

// Netscape 6,7 bits
function checkExist() {
	if (dialogWindow) dWindow.focus();
}

function disableElement(formName, inputName){
	var input = document.getElementById(inputName);
	var form = document.getElementById(formName);
	
	if ( form && input){
		input.disabled = true;
	}
}

function enableElement(formName, inputName){
	var input = document.getElementById(inputName);
	var form = document.getElementById(formName);
	
	if ( form && input){
		input.disabled = false;
	}
}

// New
function checkEmail(myForm) {
		if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(myForm.emailAddr.value)){
			return true
		} else {
			return false;
		}
	}

	function checkform(){
		if (document.getElementById("uname").value == ""){
			error("err01")
			document.getElementById("uname").focus();
			return false
			} else {
			erroroff("err01")
		}
		if (document.getElementById("semail").value == ""){
			error("err02")
			document.getElementById("semail").focus();
			return false
			} else {
			erroroff("err01")
		}
		if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.getElementById("semail").value))){
			error("err02")
			document.getElementById("semail").select();			
			return false
			} else {
			erroroff("err01")
		}
		return true; // submit the form
	}
	
	function checkPass(){
		if (document.getElementById("pass").value == ""){
			error("err01")
			document.getElementById("pass").focus();
			return false
			} else {
			erroroff("err01")
		}
			
			
		if (document.getElementById("pass").value != document.getElementById("confPass").value){
			error("err02")
			document.getElementById("confPass").focus();
			return false
			} else {
			erroroff("err01")
		}
		return true; // submit the form
	}

	function resettoggle() {
		var e = document.getElementById('err01');
		var e2 = document.getElementById('err02');
		e.style.display = 'none';
		e2.style.display = 'none';
	}

	function error(id) {
		var e = document.getElementById(id);
		e.style.display = 'block';
	}
	
	function erroroff(id) {
		var e = document.getElementById(id);
		e.style.display = 'none';
	}
	
	
	function toggle_visibility(id) {
		var e = document.getElementById(id);
		if(e.style.display == 'none')
		e.style.display = 'block';
		else
		e.style.display = 'none';		
	}
	
	function openerRefresh(){
		//Refresh Parent page when closing game
		
		try {
			if (!window.opener.closed) {
				window.opener.location.href=window.opener.location.href;
			} 
		} catch(e) {   
			self.close();
		} 
	}

//Refreshes the page.
function refreshPage() {
	var url = document.location.href;
	if(url.indexOf("refresh") == -1) {
	  if(url.indexOf("?") == -1) {
		url += "?refresh=true";
	  } else {
		url += "&refresh=true";
	  }
	}
	document.location.href = url;
}

function openCashierWindow(theurl){
	window.open(theurl, 'deposit', 'height=900,width=900,statusbar=no,toolbar=no,directories=no,status=no,linemenubar=no,scrollbars=yes,resizable=yes,modal=yes,top=0,left=0');
}
