/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	Register.js - client-side validation for register forms
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	(c) Copyright 2003, Finsoft Ltd
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
sMsgRegCCAccNo = "";
sMsgRegEntFirstName = "";
sMsgRegEntLastName = "";
sMsgRegEntPhone = "";
sMsgRegEntAddress = "";
sMsgRegEntCity = "";
sMsgRegEntPostCode = "";
sMsgRegEntSeqAnswer = "";
sMsgRegPrivacyCheck = "";
sMsgAdvertSource = "";

//	this holds user messages to display, during form validation 
var aUserMsg = new Array();

/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	onSubmit function						
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
function FV_FormVal(oForm) {
	var sTmp, oTmp, bOk = true, x;

	//	prevent multiple clicks on submit
	if (oForm.submitted)
		return false;

	//	inform user about mistakes
	oForm.alertuser = true;

	var oElems = oForm.elements; 
	var nLen = oElems.length;

	for (var i=0; i<nLen; i++) {
		oTmp = oElems[i]; 
		if (typeof(oTmp.valme) != "function") continue;
		bOk = oTmp.valme();
		if (!bOk) break; 
	}

	if (bOk) {
		oForm.submitted = true;
	} 

	return bOk;
}

/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	set focus on error-field
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
function SetFocus() {
	if (document.registerform.elements[sErrorFieldSet])
		document.registerform.elements[sErrorFieldSet].focus();
}

/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	sets and resets correspondence options
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
function ToggleCorrespOpts(bChecked, sCorrespOptsId, oForm) {
	
	var oCorrespCheckboxes = oForm.elements['sCorrespondenceOptions'];
	
	if (oCorrespCheckboxes) {
		if (bChecked) {
			
			xOn(sCorrespOptsId);
			
			if (oCorrespCheckboxes.length) {
				for (var i=0; i < oCorrespCheckboxes.length; i++) {
					if (SG_sSuggCorrespOpts.indexOf(';' + oCorrespCheckboxes[i].value + ';') >= 0) oCorrespCheckboxes[i].checked = true;
				}
			} if (SG_sSuggCorrespOpts.indexOf(';' + oCorrespCheckboxes.value + ';') >= 0) oCorrespCheckboxes.checked = true;
			
		} else {
			
			xOff(sCorrespOptsId);	
			
			if (oCorrespCheckboxes.length) {
				for (var i=0; i < oCorrespCheckboxes.length; i++) oCorrespCheckboxes[i].checked = false;
			} oCorrespCheckboxes.checked = false;
			
		}
	}
}


/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	validation handler		
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
function FV_FieldVal(oField) {
	var bOk = true;

	var sFldName = oField.name;
	var oForm = oField.form;
	var sValue;

	var sType = sFldName.charAt(0).toLowerCase();
	switch(sType) {
		case "s":
			switch(sFldName) {
				case "sEmail":
					bOk = FV_IsEmail(oField.value);
					if (!bOk) {
						if (oForm.alertuser) {
							FV_ShowMessage(sMsgRegEntEmail);
							oForm.alertuser = false;
							oField.focus();
						}
						break;
					}
					break;

				case "sUsername":
					sValue = oField.value;
					bOk = (sValue.length >= nMinLenUsername && sValue.length <= nMaxLenUsername);
					if (!bOk) {
						if (oForm.alertuser) {
							FV_ShowMessage(SG_MsgLogEntUserLen);
							oForm.alertuser = false;
							oField.focus();
						}
						break;
					}
					bOk = ( FV_StripCharsNotInBag(sValue, SG_sInUserAllow) == sValue );
					if (!bOk) {
						if (oForm.alertuser) {
							FV_ShowMessage(SG_MsgLogEntUserChars + "\n" + SG_sInUserAllow);
							oForm.alertuser = false;
							oField.focus();
						}
					}
					break;
		
				case "sPassword":
					sValue = oField.value;
					bOk = (sValue.length >= nMinLenPassword && sValue.length <= nMaxLenPassword);
					if (!bOk) {
						if (oForm.alertuser) {
							FV_ShowMessage(SG_MsgLogEntPassLen);
							oForm.alertuser = false;
							oField.focus();
						}
						break;
					}
					bOk = ( FV_StripCharsNotInBag(sValue, SG_sInUserAllow) == sValue );
					if (!bOk) {
						if (oForm.alertuser) {
							FV_ShowMessage(SG_MsgLogEntPassChars + "\n" + SG_sInUserAllow);
							oForm.alertuser = false;
							oField.focus();
						}
					}
					break;

				case "sPassword2":
					sValue = oField.value;
					sValueOriginal = oField.form.elements["sPassword"].value;
					bOk = (sValue == sValueOriginal);
					if (!bOk) {
						if (oForm.alertuser) {
							FV_ShowMessage(sMsgRegEntRetypePass);
							oForm.alertuser = false;
							oField.focus();
						}
						break;
					}
					break;

				default:
					bOk = !FV_IsBlank(oField.value);
					if (!bOk) {
						if (oForm.alertuser) {
							if (aUserMsg[sFldName])
								FV_ShowMessage(aUserMsg[sFldName]);
							oForm.alertuser = false;
							oField.focus();
						}
						break;
					}
			}
			break;

		case "n":
			switch(sFldName) {
				case "nDobday": case "nDobmonth": case "nDobyear":
					var oDay = oForm.elements["nDobday"];
					var oMonth = oForm.elements["nDobmonth"];
					var oYear = oForm.elements["nDobyear"];
					var sDay = FV_Trim(oDay.value);
					var sMonth = FV_Trim(oMonth.value);
					var sYear = FV_Trim(oYear.value);
					//	check for all parts
					if (sDay == "" || sYear == "") {
						//	if this is form submit, then inform user about the missing field 
						if (oForm.alertuser) {
							bOk = false;
							FV_ShowMessage(sMsgRegEntDoB);
							oForm.alertuser = false;
							(sDay == "") ? oDay.focus() : oYear.focus();
							break;
						//	user is in process of entering data, so just return that all is Ok without validating date until all is entered  
						} else {
							bOk = false;
							break;
						}
					}
					bOk = FV_IsValidDate(sYear, sMonth, sDay, 1);
					if (!bOk) {
						if (oForm.alertuser) {
							FV_ShowMessage(sMsgRegEntDoB);
							oForm.alertuser = false;
							oField.focus();
						}
						break;
					}
					//	check if he is too young 
					var bToYoung = false;
					var dToday = new Date();
					dToday.setTime(dToday.getTime() - (clockLocalStartTime - clockServerStartTime));
					
					var dYearDiff = dToday.getFullYear() - parseInt(sYear);
					var dMonthDiff = (dToday.getMonth() + 1) - parseInt(sMonth);
					var dDayDiff = dToday.getDate() - parseInt(sDay);
			
					if (dYearDiff < 18) { 
						bToYoung = true; 
					} else {
						if (dYearDiff == 18) { 
							if (dMonthDiff < 0) { 
								bToYoung = true; 	
							} else {
								if (dMonthDiff == 0) { 
									if (dDayDiff < 0) bToYoung = true;
								}
							}
						}
					}
					bOk = !bToYoung;
					if (!bOk) {
						if (oForm.alertuser) {
							FV_ShowMessage(sMsgRegEntTooYoung);
							oForm.alertuser = false;
							oField.focus();
						}
						break;
					}
					break;

				case "nCCPIN":
					bOk = (FV_IsIntegerInRange(oField.value, 0, 9999) && oField.value.length == 4);
					if (!bOk) {
						if (oForm.alertuser) {
							FV_ShowMessage(sMsgRegCCPIN);
							oForm.alertuser = false;
							oField.focus();
						}
						break;
					}
					break;

				default:
					bOk = (FV_IsFloat(oField.value) || FV_IsInteger(oField.value));
					if (!bOk) {
						if (oForm.alertuser) {
							if (aUserMsg[sFldName])
								FV_ShowMessage(aUserMsg[sFldName]);
							oForm.alertuser = false;
							oField.focus();
						}
						break;
					}
			}
			break;

		case "b":
			if (aUserMsg[sFldName])
				FV_ShowMessage(aUserMsg[sFldName]);
			break;
	}

	return bOk;
}

/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	set field names and other country specific data, based on choosen country				
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
function SetCountrySpecifics(oSelect) {
	var oForm = oSelect.form;
	if (!oForm.firstChild) return;

	var sCountry = oSelect.options[oSelect.selectedIndex].value;

	//	change county/state/province
	var oCounty = oForm.elements["sCounty"].parentNode.getElementsByTagName("label")[0];
	if (oCounty) {
		switch (sCountry) {
			case "GB": case "EN": case "WA": case "SQ": case "ND": case "UK":
				oCounty.innerHTML = FS_I18N("County");
				break;
			case "US": case "UM":
				oCounty.innerHTML = FS_I18N("State");
				break;
			default:
				oCounty.innerHTML = FS_I18N("Province");
		}
	}

	//	change postcode labels for appropriate country
	var oPostCode = oForm.elements["sPostcode"].parentNode.getElementsByTagName("label")[0];
	if (oPostCode) {
		switch (sCountry) {
			case "US": case "UM":
				oPostCode.innerHTML = FS_I18N("ZIP code");
				break;
			default:
				oPostCode.innerHTML = FS_I18N("Post code");
		}
	}
	
	//set postcode mandatory if specific country
	var oPostCodeDiv = oForm.elements["sPostcode"].parentNode;
	var oPostCodeE = oForm.elements["sPostcode"];
	if (oPostCode) {	
		var bPCIsMandatory = false;
		if (sListOfCountriesWithMandatoryPostCodeAsCSVString.length > 0 ) {
			var sTmpList = "," + sListOfCountriesWithMandatoryPostCodeAsCSVString + ",";
			var sTmpCountry = "," + sCountry + ",";
			if ( sTmpList.indexOf(sTmpCountry) > -1 ) {
				bPCIsMandatory = true;	
			}
		}
		if (bPCIsMandatory) {
			oPostCode.className += " mandat";
			oPostCodeE.valme = function() {
					var oDiv = this.parentNode;
					if (this.type == "text")
						this.value = FV_Trim(this.value);
					bOk = FV_FieldVal(this);
					if (bOk)
						oDiv.className = oDiv.className.replace("badinput", "");
					else if (oDiv.className.indexOf("badinput") == -1) {
						oDiv.className += " badinput";
					}
					return bOk;
				};
			oPostCodeE.onblur = oPostCodeE.valme;
		} else {
			oPostCode.className = oPostCode.className.replace(/mandat/, "");
			oPostCodeE.valme = null;
			oPostCodeE.onblur = null;
			oPostCodeDiv.className = oPostCodeDiv.className.replace("badinput", "");
		}
	}
	
	//	change to appropriate currency
	var oCurrency = oForm.elements["sCurrency"];
	if (oCurrency) {
		switch (sCountry) {
			case "GB":
				for (i=0;i<oCurrency.options.length;i++)
					if (oCurrency.options[i].value == "GBP") {
						oCurrency.selectedIndex = i;
						break;
					}
				break;
			case "US": case "UM": case "AS": case "IO": case "TP": case "EC": case "SV": case "GU": case "HT": case "MH": case "FM": case "MP": case "PW": case "PA": case "PR": case "TC": case "VG": case "VI":
				for (i=0;i<oCurrency.options.length;i++)
					if (oCurrency.options[i].value == "USD") {
						oCurrency.selectedIndex = i;
						break;
					}
				break;
			case "CA":
				for (i=0;i<oCurrency.options.length;i++)
					if (oCurrency.options[i].value == "CAD") {
						oCurrency.selectedIndex = i;
						break;
					}
				break;
			case "AU": case "CX": case "CC": case "HM": case "KI": case "NR": case "NF": case "TV":
				for (i=0;i<oCurrency.options.length;i++)
					if (oCurrency.options[i].value == "AUD") {
						oCurrency.selectedIndex = i;
						break;
					}
				break;
			case "DK": case "FO": case "GL":
				for (i=0;i<oCurrency.options.length;i++)
					if (oCurrency.options[i].value == "DKK") {
						oCurrency.selectedIndex = i;
						break;
					}
				break;
			case "AT": case "AD": case "BE": case "FI": case "FR": case "GF": case "TF": case "DE": case "GR": case "GP": case "VA": case "IE": case "IT": case "LU": case "MQ": case "YT": case "MC": case "NL": case "PT": case "RE": case "PM": case "SM": case "ES":
				for (i=0;i<oCurrency.options.length;i++)
					if (oCurrency.options[i].value == "EUR") {
						oCurrency.selectedIndex = i;
						break;
					}
				break;
			case "HK":
				for (i=0;i<oCurrency.options.length;i++)
					if (oCurrency.options[i].value == "HKD") {
						oCurrency.selectedIndex = i;
						break;
					}
				break;
			case "JP":
				for (i=0;i<oCurrency.options.length;i++)
					if (oCurrency.options[i].value == "JPY") {
						oCurrency.selectedIndex = i;
						break;
					}
				break;
			case "BV": case "NO": case "SJ":
				for (i=0;i<oCurrency.options.length;i++)
					if (oCurrency.options[i].value == "NOK") {
						oCurrency.selectedIndex = i;
						break;
					}
				break;
			case "SG":
				for (i=0;i<oCurrency.options.length;i++)
					if (oCurrency.options[i].value == "SGD") {
						oCurrency.selectedIndex = i;
						break;
					}
				break;
			case "ZA":
				for (i=0;i<oCurrency.options.length;i++)
					if (oCurrency.options[i].value == "ZAR") {
						oCurrency.selectedIndex = i;
						break;
					}
				break;
			case "SE":
				for (i=0;i<oCurrency.options.length;i++)
					if (oCurrency.options[i].value == "SEK") {
						oCurrency.selectedIndex = i;
						break;
					}
				break;
			case "CH": case "LI":
				for (i=0;i<oCurrency.options.length;i++)
					if (oCurrency.options[i].value == "CHF") {
						oCurrency.selectedIndex = i;
						break;
					}
				break;
			case "TH":
				for (i=0;i<oCurrency.options.length;i++)
					if (oCurrency.options[i].value == "THB") {
						oCurrency.selectedIndex = i;
						break;
					}
				break;
		}

		if (typeof(OnCurrencyChange) != "undefined") OnCurrencyChange(oCurrency);
	}
}

/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	onload function		
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
function SetupOnLoad() {
	FV_SetupForm("registerform");

	var oForm = document.forms["registerform"]; 
	if (!oForm) return;

	oCountry = oForm.elements["sCountry"];
	if (oCountry) {
		oCountry.onchange = function() {
			SetCountrySpecifics(this);
		};
	}  

	oGoRegCard = oForm.elements["xGoregcard"];
	if (oGoRegCard) {
		oGoRegCard.onclick = function() {
			this.form.onsubmit = function() { return false; };
			location.href = "regcard.asp";
		};
	}  

	oGoDeposit = oForm.elements["xGodeposit"];
	if (oGoDeposit) {
		oGoDeposit.onclick = function() {
			this.form.onsubmit = function() { return false; };
			location.href = "Deposit.asp";
		};
	}  

	oPopupRules = document.getElementById( "rulespopup" );
	if (oPopupRules) {
		oPopupRules.onclick = function() {
			var sHref = this.href;
			SG_ShowWin(sHref + "?popup=true", "Rules", 640, 500);
			return false;
		};
	}  

	oPopupPrivacy = document.getElementById( "privacypopup" );
	if (oPopupPrivacy) {
		oPopupPrivacy.onclick = function() {
			var sHref = this.href;
			SG_ShowWin(sHref + "?popup=true", "Privacy", 600, 400);
			return false;
		};
	}  

	aUserMsg["sCCaccountno"] = sMsgRegCCAccNo;
	aUserMsg["sFirstname"] = sMsgRegEntFirstName;
	aUserMsg["sLastname"] = sMsgRegEntLastName;
	aUserMsg["sHomephone"] = sMsgRegEntPhone;
	aUserMsg["sAddress"] = sMsgRegEntAddress;
	aUserMsg["sCity"] = sMsgRegEntCity;
	aUserMsg["sPostcode"] = sMsgRegEntPostCode;
	aUserMsg["sSecurityanswer"] = sMsgRegEntSeqAnswer;
	aUserMsg["bPrivacy"] = sMsgRegPrivacyCheck;
	aUserMsg["sAdvertsource"] = sMsgAdvertSource;
}
AE_AttachEvent("onload", "SetupOnLoad");
