// Show hide layers
function MM_showHideLayers() {
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) 
  with (document) if (getElementById && ((obj=getElementById(args[i]))!=null)) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'block':(v=='hide')?'none':v; }
    obj.display=v; }
}

// Display tips
function displayTip(tipID){
	var newTip = "";
	newTip = "<p><img src='../site_images/general/title_tips.gif' alt='Tips' width='58' height='32' /><br />"+ eval(tipID)+"</p>";
 	document.getElementById("tipData").innerHTML = newTip;
}

// Clear fields
function clearField(fieldId){
	document.getElementById(fieldId).value = '';
}

function paycheck(){
	var errormessage = '';
	
	if(WithoutCheck(document.form1.terms)) {
		errormessage = "Please confirm you have agreed to Terms and Conditions.\n";
	}
	
	if(errormessage.length > 2) {
		alert(errormessage);
		return false;
	}
	
	return true;
}

// open windows
function openWin(location,winId,optionArr) {
  window.open(location,winId,optionArr);
}

// Member signup page 1 validation
function memberSignupOne(){
	var errormessage = '';
		
	if(notAlphaNum(document.form1.firstname.value) || WithoutContent(document.form1.firstname.value)) {
		errormessage = "Invalid Firstname\n";
	}
	
	if(notAlphaNum(document.form1.surname.value) || WithoutContent(document.form1.surname.value)) {
		errormessage = "Invalid Surname\n";
	}
	
	if(!isValidEmail(document.form1.email.value) || WithoutContent(document.form1.email.value)) {
		errormessage = "Invalid Email\n";
	}
	
	if(errormessage.length > 2) {
		alert(errormessage);
		return false;
	}
	
	return true;
}

// Member signup page 1 validation
function passReset(){
	var errormessage = '';
	
	if(!isValidEmail(document.form1.emailaddress.value) || WithoutContent(document.form1.emailaddress.value)) {
		errormessage = "Invalid Email\n";
	}
	
	if(errormessage.length > 2) {
		alert(errormessage);
		return false;
	}
	
	return true;
}

// Member signup page 2 validation
function memberSignupTwo(){
	var errormessage = '';
		
	if(notAlphaNum(document.form1.screenName.value) || WithoutContent(document.form1.screenName.value)) {
		errormessage = "Screen names can only have letters and/or numbers without any spaces.\n";
	}
	
	if(WithoutContent(document.form1.password.value)) {
		errormessage = "No password entered.\n";
	}
	
	if(WithoutContent(document.form1.suburb.value)) {
		errormessage = "No Suburb entered.\n";
	}
	
	if(WithoutContent(document.form1.postcode.value)) {
		errormessage = "No Postcode entered.\n";
	}
	
	if(document.form1.state.selectedIndex == 0) {
		errormessage = "Please select your state\n";
	}
	
	if(NoneWithCheck(document.form1.radio)) {
		errormessage = "Please select gender.\n";
	}

	if(WithoutContent(document.form1.mobileNo.value)) {
		errormessage = "Please enter a mobile number.\n";
	}
	
	if(WithoutContent(document.form1.phoneNo.value)) {
		errormessage = "Please enter a phone number.\n";
	}
	
	if(document.form1.employerContact.selectedIndex == 0) {
		errormessage = "Please select a contact method.\n";
	}

	if(errormessage.length > 2) {
		alert(errormessage);
		return false;
	}
	return true;
}

// Employer signup page validation
function employerSignup(){
	var errormessage = '';
		
	if(WithoutContent(document.form1.firstname.value)) {
		errormessage = "Invalid Firstname\n";
	}
	
	if(WithoutContent(document.form1.surname.value)) {
		errormessage = "Invalid Surname\n";
	}
	
	if(WithoutContent(document.form1.busName.value)) {
		errormessage = "Please enter a business name\n";
	}
	
	if(WithoutContent(document.form1.busAddress.value)) {
		errormessage = "Please enter a business address\n";
	}
	
	if(WithoutContent(document.form1.suburb.value)) {
		errormessage = "Invalid Suburb\n";
	}
	
	if(WithoutContent(document.form1.postcode.value)) {
		errormessage = "Invalid Postcode\n";
	}
	
	if(document.form1.state.selectedIndex == 0) {
		errormessage = "Please select your state\n";
	}
	
	if(!isValidEmail(document.form1.emailaddress.value) || WithoutContent(document.form1.emailaddress.value)) {
		errormessage = "Invalid Email Address\n";
	}
	
	if(WithoutContent(document.form1.mobileNo.value) && WithoutContent(document.form1.phoneNo.value)) {
		errormessage = "Please enter either a phone number or mobile number\n";
	}
	if(WithoutCheck(document.form1.agreeTerms)) {
		errormessage = "You must agree to the Terms and Conditions to proceed.\n";
	}
	if(errormessage.length > 2) {
		alert(errormessage);
		return false;
	}
	
	return true;
}

// Check unique username
function checkSname(){
	var errormessage = '';
	
	if(notAlphaNum(document.form1.screenName.value) || WithoutContent(document.form1.screenName.value)) {
		errormessage = "Screen names can only have letters and/or numbers without any spaces.\n";
	}
	
	if(errormessage.length > 2) {
		alert(errormessage);
		return false;
	}
	var sname = '/web_inc/check_availability.php?s=' + document.form1.screenName.value;
 	window.open(sname,'checkavail','width=227,height=150');
	return true;
}

// Limit checkbox allowance to 3 selections
function select3Limit(formname){
	var total = 0;
	for(i = 0; i < document.form1.checkbox2.length; i++) {
		elm = document.form1.checkbox2[i];
		
		if(elm.checked){
			total++;
		}
	}
	if(total > 3){
		alert('Only 3 selections can be made.');
		return false;
	} else {
		storeAtt();
		document.getElementById(formname).modcheck.value = 1;
		return true;
	}
}

// Collect checkbox data and make a storable string
function storeAtt(){
	var outputStr = '';
	for(i = 0; i < document.form1.checkbox2.length; i++) {
		elm = document.form1.checkbox2[i];
		
		if(elm.checked){
			outputStr += "1-";
		} else {
			outputStr += "0-";
		}
	}
	
	outputStr = outputStr.slice(0,-1);
	document.form1.attarray.value = outputStr;
	document.getElementById('form1').modcheck.value = 1;
}

// Collect checkbox data and make a storable string
function storeAvail(){
	var outputStr = '';
	for(i = 0; i < document.form1.checkbox3.length; i++) {
		elm = document.form1.checkbox3[i];
		
		if(elm.checked){
			outputStr += "1-";
		} else {
			outputStr += "0-";
		}
	}
	
	outputStr = outputStr.slice(0,-1);
	document.form1.availarray.value = outputStr;
	document.getElementById('form1').modcheck.value = 1;
}

// Check if form has been modified and alert if no save has been initiated
function modCheck(formname){
	if(document.getElementById(formname).modcheck.value == 1){
		if(confirm('Do you wish to save your changes before continuing? Click "Okay" to save or "Cancel" to continue without saving...')){
			document.getElementById(formname).submit();
		} else {
			return true;
		}
	}
}

function makeMod(formname){
	document.getElementById(formname).modcheck.value = 1;
}

// update checkbox arrays
function updateCheckbox(attString){
	var temp = new Array();
	temp = attString.split('-');
	for(i = 0; i < temp.length; i++) {
		if(temp[i] == 1){
			elm = document.form1.checkbox2[i].checked = true;
		}
	}
}

function updateAvail(attString){
	var temp = new Array();
	temp = attString.split('-');
	for(i = 0; i < temp.length; i++) {
		if(temp[i] == 1){
			elm = document.form1.checkbox3[i].checked = true;
		}
	}
}

///////////////////////////////////////////////////
// Do not touch below this line
///////////////////////////////////////////////////

function WithoutContent(ss) {
	if(ss.length > 0) { return false; }
	return true;
}

function isValidEmail(str) {
	return (str.lastIndexOf(".") > 2) && (str.indexOf("@") > 0) && (str.lastIndexOf(".") > (str.indexOf("@")+1)) && (str.indexOf("@") == str.lastIndexOf("@"));
}

// Check if is a number
function isNumber(ss) {
	if (!isNaN(ss) && ss.length < 5) { return false; }
	return true;
}

// Check for alpha numeric characters
function notAlphaNum(ss){
	var testPattern = /[^a-zA-Z0-9]/ ;
	return testPattern.test(ss);
}

// Checking for content on an array of items
function NoneWithContent(ss) {
	for(var i = 0; i < ss.length; i++) {
		if(ss[i].value.length > 0) { return false; }
		}
	return true;
}

// Checking for a checkbox array to have no selections
function NoneWithCheck(ss) {
	for(var i = 0; i < ss.length; i++) {
		if(ss[i].checked) { return false; }
		}
	return true;
}

// No selection on checkbox check
function WithoutCheck(ss) {
	if(ss.checked) { return false; }
	return true;
}

// No selecetion value check
function WithoutSelectionValue(ss) {
	for(var i = 0; i < ss.length; i++) {
		if(ss[i].selected) {
			if(ss[i].value.length) { return false; }
			}
		}
	return true;
}
