formchanged = false;
function validate_email(p_sEmail) {
	var regEmail = /^[\w-]+(?:\.[\w-]+)*@(?:[\w-]+\.)+[a-zA-Z]{2,7}$/;
	return regEmail.test (p_sEmail);
}
function validate(obj) {
	var valid = true;
	var errorStr = "Please fix the following errors in the form\n\n";
	for(var i=0; i<jsvalidarray.length; i++) {
		var arr = jsvalidarray[i].split("|");
		var label = arr[0];
		var field = arr[1];		
		var type = arr[2];
		var min_length = arr[3];
		if (type=="text"||type=="textarea"||type=="select"||type=="file"||type=="password") {
			if (field=="Licensee") {
				if (obj[field].value==""||obj[field].value=="Other") {
					if (obj["LicenseeOther"].value=="") {
						valid = false;
						errorStr += "Licensee invalid – please advise your licensee group in the Other field\n";
					}
				}
			} else if (field=="_BrokerGroupName") {
				if (obj[field].value==""||obj[field].value=="Other") {
					if (obj["_BrokerGroupNameOther"].value=="") {
						valid = false;
						errorStr += "Broker Group Name invalid – please advise your broker group in the Other field\n";
					}
				}
			} else {
				if (obj[field].value.length<min_length) {
					valid = false;
					errorStr += label + " invalid\n";
				} else if (field=="Email") {
					var em_valid = validate_email(obj.Email.value);
					if (!em_valid) {
						valid = false;
						errorStr += "Email invalid\n";
					}
				}
			}
		} else if (type=="boolean"||type=="radio"||type=="session") {
			radchecked = false;
			if (obj[field].length>1) {
				for(b=0; b<obj[field].length; b++) {
					if (obj[field][b].checked) {
						radchecked = true;
						break;
					}
				}
			} else {
				radchecked = true;
			}
			if (!radchecked) {
				valid = false;
				errorStr += label+" invalid\n";
			}
		}
	}
	if (obj.Phone) {
		if (obj.Phone.value.length>0 && (obj.Phone_area.value.length!=2 || obj.Phone.value.length!=8)) {
			valid = false;
			errorStr += "Telephone Number invalid\n";
		}
	}
	if (obj.Mobile) {
		if (obj.Mobile.value.length>0 && (obj.Mobile.value.length!=6 || obj.Mobile_area.value.length!=4)) {
			valid = false;
			errorStr += "Mobile Number invalid\n";
		}
	}
	if (obj.accept_terms) {
		if(!obj.accept_terms.checked) {
			valid = false;
			errorStr += "You must accept the terms of the Privacy Statement to register\n";
		}
	}
	if (!valid) {
		alert(errorStr);
		return false;
	} else {
		if (formchanged) {
			if(confirm("ALERT!\n\nYOU ARE ATTEMPTING TO CHANGE AN EXISTING REGISTRATION FOR THIS EVENT.\n\nThe information that you have changed will DELETE the existing registration and REPLACE it with the new information.\n\nIf you want to continue with the changes you have made and DELETE the existing registration record with this new information, click OK.\n\nIf you would like to create a new registration and retain the existing registration record, please click cancel and click the link at the top of the page to clear the form.")) {
				return true;
			} else {
				return false;
			}
		} else {
			return true;	
		}
	}
}
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function eraseCookie(name) {
	createCookie(name,"",-1);
}
function show_id(myid) {
	$("#"+myid).show();
}
function hide_id(myid) {
	$("#"+myid).hide();
}
function toggle_id(myid) {
	$('#'+myid).toggle();
}
function slide_id(myid) {
	$('#'+myid).slideToggle("normal");
}
function slide_open(myid) {
	$('#'+myid).slideDown("normal");
}
function slide_closed(myid) {
	$('#'+myid).slideUp("normal");
}

function jvalidate(obj) {
	var valid = true;
	var errorStr = "Please fix the following errors in the form\n\n";
	$.each(jsvalidarray,function(i,n) {
		var arr = n.split("|");
		var label	= arr[0];
		var field	= arr[1];
		var type	= arr[2];
		var lenmin	= arr[3];
		var val = obj[field].value;
		if(type=="text"||type=="textarea"||type=="select"||type=="file"||type=="password") {
			if(field=="Licensee") {
				if (val==""||val=="Other") {
					if(alt_lic_field) {
						if (!$("#"+alt_lic_field).attr("value")) {
							valid = false;
							if(alt_lic_msg)
								errorStr += alt_lic_msg+"\n";
							else
								errorStr += "Licensee invalid – please advise your licensee group in the Other field\n";
						}
					}
				}
			} else if (field=="_BrokerGroupName") {
				if (val==""||val=="Other") {
					if ($("#_BrokerGroupNameOther").attr("value")=="") {
						valid = false;
						errorStr += "Broker Group Name invalid – please advise your broker group in the Other field\n";
					}
				}
			} else {
				if ((typeof val=="string"&&val.length<lenmin)||(typeof val=="number"&&val<1)) {
					valid = false;
					errorStr += label + " invalid\n";
				} else if (field=="Email") {
					var em_valid = validate_email(val);
					if (!em_valid) {
						valid = false;
						errorStr += "Email invalid\n";
					}
				}
			}
		} else if ((type=="radio"||type=="boolean")) {
			radchecked = false;
			if (obj[field].length>1) {
				for(b=0; b<obj[field].length; b++) {
					if (obj[field][b].checked) {
						radchecked = true;
						break;
					}
				}
			} else {
				radchecked = true;
			}
			if(field.indexOf('SessionChoice')!==-1&&!validate_sessions) {
				radchecked = true;
			}
			if (!radchecked) {
				valid = false;
				errorStr += label+" invalid\n";
			}
		}
	});
	if (obj.Phone && obj.Phone_area) {
		if (obj.Phone.value.length>0 && (obj.Phone_area.value.length!=2 || obj.Phone.value.length!=8)) {
			valid = false;
			errorStr += "Telephone Number invalid\n";
		}
	}
	if (obj.Mobile && obj.Mobile_area) {
		if (obj.Mobile.value.length>0 && (obj.Mobile_area.value.length!=4 || obj.Mobile.value.length!=6)) {
			valid = false;
			errorStr += "Mobile Number invalid\n";
		}
	}
	if (obj.accept_terms) {
		if(!obj.accept_terms.checked) {
			valid = false;
			errorStr += "You must accept the terms of the Privacy Statement to register\n";
		}
	}
	if (!valid) {
		alert(errorStr);
		return false;
	} else {
		if (formchanged) {
			if(confirm("ALERT!\n\nYOU ARE ATTEMPTING TO CHANGE AN EXISTING REGISTRATION FOR THIS EVENT.\n\nThe information that you have changed will DELETE the existing registration and REPLACE it with the new information.\n\nIf you want to continue with the changes you have made and DELETE the existing registration record with this new information, click OK.\n\nIf you would like to create a new registration and retain the existing registration record, please click cancel and click the link at the top of the page to clear the form.")) {
				return true;
			} else {
				return false;
			}
		} else {
			return true;	
		}
	}
}
function fromjson(data) {
	return jd(data);
}
function jd(data) {
	return eval('('+data+')');
}
