function showTerms()
{
	var termsDiv = document.getElementById('termsofuse');
	if (termsDiv) {
		termsDiv.style.display = 'block';
	}
}

function hideTerms()
{
	var termsDiv = document.getElementById('termsofuse');
	if (termsDiv) {
		termsDiv.style.display = 'none';
	}
}

function submitForm()
{

	// check for agree to terms
	var terms = document.getElementById('terms');
	if (terms && terms.checked) {
	
		// validate form
		
		var allCompleted = true;
		var incompleteFields = '';
		
		if (!checkTextField('companyname')) {
			allCompleted = false;
			incompleteFields += '\n-Company Name';
		}
		
		if (!checkTextField('firstname')) {
			allCompleted = false;
			incompleteFields += '\n-First Name';
		}
		
		if (!checkTextField('surname')) {
			allCompleted = false;
			incompleteFields += '\n-Surname';
		}
		
		if (!checkTextField('telephone')) {
			allCompleted = false;
			incompleteFields += '\n-Telephone';
		}
		
		if (!checkTextField('email') || !isEmail('email')) {
			allCompleted = false;
			incompleteFields += '\n-Email';
		}
		
		if (!checkTextField('address')) {
			allCompleted = false;
			incompleteFields += '\n-Address';
		}
		
		if (!checkTextField('suburb')) {
			allCompleted = false;
			incompleteFields += '\n-Suburb';
		}
		
		if (!checkTextField('postcode')) {
			allCompleted = false;
			incompleteFields += '\n-Postcode';
		}
		
		if (isCreditCard()) {
		
			// is a credit card
			
			var ccnumber = document.getElementById('creditcardnumber');
			if (ccnumber) {
				if (!checkTextField('creditcardnumber')) {
					allCompleted = false;
					incompleteFields += '\n-Credit Card Number';
				}
			}
			
			var ccname = document.getElementById('ccNameOnCardTB');
			if (ccname) {
				if (!checkTextField('ccNameOnCardTB')) {
					allCompleted = false;
					incompleteFields += '\n-Name on Credit Card';
				}
			}
		
		} else {
			
			// is a direct debit
			
			var finame = document.getElementById('financialinstitutenametb');
			if (finame) {
				if (!checkTextField('financialinstitutenametb')) {
					allCompleted = false;
					incompleteFields += '\n-Financial Institute Name';
				}
			}
			
			var fiaddress = document.getElementById('financialinstituteaddresstb');
			if (fiaddress) {
				if (!checkTextField('financialinstituteaddresstb')) {
					allCompleted = false;
					incompleteFields += '\n-Financial Institute Address';
				}
			}
			
			var accHolders = document.getElementById('nameofaccountholderstb');
			if (accHolders) {
				if (!checkTextField('nameofaccountholderstb')) {
					allCompleted = false;
					incompleteFields += '\n-Account Holder Name(s)';
				}
			}
			
			var bsbNumber = document.getElementById('bsbnumbertb');
			if (bsbNumber) {
				if (!checkTextField('bsbnumbertb')) {
					allCompleted = false;
					incompleteFields += '\n-BSB Number';
				}
			}
			
			var accNumber = document.getElementById('accountnumbertb');
			if (accNumber) {
				if (!checkTextField('accountnumbertb')) {
					allCompleted = false;
					incompleteFields += '\n-Account Number';
				}
			}
		}
		
		if (allCompleted) {
		
			// show saving message
			showSavingMessage();
			
			// populate state hidden field
			var stateddl = document.getElementById('stateddl');
			var statehidden = document.getElementById('selectedState');
			if (stateddl && statehidden) {
				statehidden.value = stateddl.options[stateddl.selectedIndex].value;
            }

            // submit form
			return true;
			
		} else {
			alert('Please complete the following fields:\n' + incompleteFields);
			return false;
		}
	
	} else {
		alert('NOTE: You must agree to the Simple Salon\nTerms & Conditions to join.');
		return false;
	}
	
}

function checkTextField(fieldID) {
	var result = false;
	if (fieldID && fieldID.length > 0) {
		var field = document.getElementById(fieldID);
		if (field) {
			if (field.value && field.value.length > 0) {
				result = true;	
			}
		}
	}
	return result;
}

function isEmail(fieldID) {
	var result = false;
	if (fieldID && fieldID.length > 0) {
		var field = document.getElementById(fieldID);
		if (field) {
			if (field.value.indexOf('@') > -1) {
				result = true;
			}
		}
	}
	return result;
}

function displayDemo(filename,title) {
	alert('Coming Soon!');
	//window.open(filename,'_blank','width=828,height=606');
}

function loadStatesForCountry(ddl) {
	var countryID = ddl.options[ddl.selectedIndex].value;
	if (countryID) {
	    var stateDDL = document.getElementById('stateddl');
	    if (stateDDL) {
			stateDDL.options.length = 0;
			try {
			    var statesToShow = statesList[countryID];
			    for(i=0;i<statesToShow.length;i++) {
					stateDDL.options[i] = statesToShow[i];
				}
			} catch (e) {
				stateDDL.options.length = 0;
				stateDDL.options[0] = new Option('Not Required', '-1');
			}
        }
        if (countryID == '1') {
            hideDirectDebit(false);
        } else {
            hideDirectDebit(true);
        }
	}
}

function hideDirectDebit(mode)
{
    var paymentTypeDDL = document.getElementById('paymentmethodtypeddl');
    if (paymentTypeDDL) {
        paymentTypeDDL.options[1].disabled = mode;
        if (mode) {
            paymentTypeDDL.selectedIndex = 0;
            changePaymentType();
        }
    }
}

function loadPriceForCurrency(ddl) {
	var currency = ddl.options[ddl.selectedIndex].value;
	if (currency) {
		if (pricing) {
			for(i=0;i<pricing.length;i++) {
				if (pricing[i].currencyName == currency) {
					var priceSpan = document.getElementById('monthlyPrice');
					if (priceSpan) {
						priceSpan.innerHTML = pricing[i].currencySymbol + pricing[i].monthlyPrice;
					}
					priceSpan = document.getElementById('setupPrice');
					if (priceSpan) {
						priceSpan.innerHTML = pricing[i].currencySymbol + pricing[i].setupPrice;
					}
					priceSpan = document.getElementById('refundPrice');
					if (priceSpan) {
						priceSpan.innerHTML = pricing[i].currencySymbol + pricing[i].refundPrice;
					}
					break;
				}
			}
		}
	}
}

function loadStatesIfItExists() {
	try {
		loadStates();
	} catch (e) {
		//alert(e.toString());
	}
}

var pricing = new Array();

function setupPricing() {
	
	// create all prices
	pricing = new Array();
	
	var tempPrice = new Price();
	tempPrice.monthlyPrice = 49;
	tempPrice.setupPrice = 385;
	tempPrice.currencySymbol = '$';
	tempPrice.currencyName = 'Australian Dollars';
	tempPrice.refundPrice = 100;
	pricing.push(tempPrice);
	
	tempPrice = new Price();
	tempPrice.monthlyPrice = 29;
	tempPrice.setupPrice = 230;
	tempPrice.currencySymbol = '&#163;';
	tempPrice.currencyName = 'British Pounds';
	tempPrice.refundPrice = 50;
	pricing.push(tempPrice);
	
	tempPrice = new Price();
	tempPrice.monthlyPrice = 49;
	tempPrice.setupPrice = 385;
	tempPrice.currencySymbol = '$';
	tempPrice.currencyName = 'US Dollars';
	tempPrice.refundPrice = 100;
	pricing.push(tempPrice);
	
	tempPrice = new Price();
	tempPrice.monthlyPrice = 39;
	tempPrice.setupPrice = 310;
	tempPrice.currencySymbol = '&#128;';
	tempPrice.currencyName = 'Euros';
	tempPrice.refundPrice = 50;
	pricing.push(tempPrice);

	tempPrice = new Price();
	tempPrice.monthlyPrice = 49;
	tempPrice.setupPrice = 385;
	tempPrice.currencySymbol = '$';
	tempPrice.currencyName = 'Canadian Dollars';
	tempPrice.refundPrice = 100;
	pricing.push(tempPrice);

	
	var currencyDDL = document.getElementById('PricingDDL');
	if (currencyDDL) {
		currencyDDL.options.length = 0;
		for(i=0;i<pricing.length;i++) {
			currencyDDL.options[i] = new Option(pricing[i].currencyName,pricing[i].currencyName);
		}
	}
	
	// default to Australain Dollars
	loadPriceForCurrency(currencyDDL);
	
}

function changeFreeTrialVersion() {
	var versionddl = document.getElementById('ssversionddl');
	if (versionddl) {
		var selectedVers = versionddl.options[versionddl.selectedIndex].value;
		if (selectedVers == 'sales') {
			alert('Simple Salon Sales Free Trial Coming Soon!');
			versionddl.selectedIndex = 0;
		}
	}
}

function toggleDetails() {
	var referredDDL = document.getElementById('referredbyddl');
	var details = document.getElementById('referredbydetails');
	var detailsLabel = document.getElementById('detailsLabel');
	var detailsTB = document.getElementById('referredBy');
	if (referredDDL && details && detailsLabel && detailsTB) {
		detailsTB.value = '';
		var current = referredDDL.options[referredDDL.selectedIndex].value;
		if (current && current.length > 0) {
			if (current == 'Friend' || current == 'Other') {
				detailsLabel.innerText = 'Other';
				if (current == 'Friend') {
					detailsLabel.innerText = 'Friend\'s Name';
				}
				details.style.display = 'block';
			} else {
				details.style.display = 'none';
			}
		}
	}
}

/* Price Class */
function Price() {
	var monthlyPrice;
	var setupPrice;
	var currencySymbol;
	var currencyName;
	var refundPrice;
}

function isCreditCard() {
	var isCC = false;
	var pmtDDL = document.getElementById('paymentmethodtypeddl');
	if (pmtDDL) {
		var type = pmtDDL.options[pmtDDL.selectedIndex].value;
		if (type == 'cc') {
			isCC = true;
		}
	}
	return isCC;
}

function changePaymentType() {
	var pmtDDL = document.getElementById('paymentmethodtypeddl');
	if (pmtDDL) {
		var type = pmtDDL.options[pmtDDL.selectedIndex].value;
		var ccPanel = document.getElementById('ccDiv');
		var ddPanel = document.getElementById('ddDiv');
		if (ccPanel && ddPanel) {
			if (type == 'cc') {
				ccPanel.style.display = 'block';
				ddPanel.style.display = 'none';
			} else {
				ccPanel.style.display = 'none';
				ddPanel.style.display = 'block';
			}
		}
	}
}

function showSavingMessage() {
	var fCon = document.getElementById('theformcontents');
	var sMess = document.getElementById('savingMessage');
	if (fCon && sMess) {
		fCon.style.display = 'none';
		sMess.style.display = 'block';
	}
}

function loadStatesForCountryDDL() {
    var cDDL = document.getElementById('countryddl');
    if (cDDL) {
        loadStatesForCountry(cDDL);
    }
}

function validateHESignUpForm() {

    var phoneTB = document.getElementById('telephone');
    var mobilePhoneTB = document.getElementById('mobile');
    var termsCB = document.getElementById('terms');

    var heok = true;

    if (!termsCB.checked) {

        heok = false;
        alert('You must agree to the Terms & Conditions to continue');

    } else {

        if (phoneTB && mobilePhoneTB) {
            if (phoneTB.value.length > 0 && isNaN(parseInt(phoneTB.value))) {
                alert('Phone number must be a number');
                phoneTB.value = '';
                heok = false;
            }
            if (mobilePhoneTB.value.length > 0 && isNaN(parseInt(mobilePhoneTB.value))) {
                alert('Mobile number must be a number');
                mobilePhoneTB.value = '';
                heok = false;
            }
        }

        if (heok) {

            var stateddl = document.getElementById('stateddl');
            var statehidden = document.getElementById('selectedState');
            if (stateddl && statehidden) {
                statehidden.value = stateddl.options[stateddl.selectedIndex].value;
            }

        }

    }

    return heok;
    
}
