/*****************************************************
Function: copy data from one element to the other
		copy data from one select to another
		set text to an element
		enable/disable form element
******************************************************/

function copyData(from,to){
	var tempFrom = document.getElementById(from);
	var tempTo = document.getElementById(to);

	tempTo.value = tempFrom.value;
}

function copyDataSelect(form,from,to){
	document.forms[form][to].options.selectedIndex = document.forms[form][from].selectedIndex;
}

function setFormElementData(element,text){
	document.getElementById(element).value = text;
}

function enableDisableFormElement(form,element,type){ // type: true = disable ; false = enable
	var theForm = document.forms[form];
	var theElement = theForm[element];
	
	if(type){
		theElement.disabled = true;
	}else{
		theElement.disabled = false;
	}
}

function enableDisableForm(type){ // type: true = disable ; false = enable
	var inputs = document.getElementsByTagName('input');
	var selectbox = document.getElementsByTagName('select');
	var textbox = document.getElementsByTagName('textarea');
	var checkbox = document.getElementsByTagName('checkbox');
	var radio = document.getElementsByTagName('radio');
	
	if(inputs){
		for(i=0;i<inputs.length;i++){
			inputs[i].disabled = type;
		}
	}
	
	if(selectbox){
		for(i=0;i<selectbox.length;i++){
			selectbox[i].disabled = type;
		}
	}
	
	if(textbox){
		for(i=0;i<textbox.length;i++){
			textbox[i].disabled = type;
		}
	}
	
	if(checkbox){
		for(i=0;i<checkbox.length;i++){
			checkbox[i].disabled = type;
		}
	}
	
	if(radio){
		for(i=0;i<radio.length;i++){
			radio[i].disabled = type;
		}
	}
}

function setFieldFocus(form,sField){
	var theForm = document.forms[form][sField].focus();
}

function getSelectData(form,id){
	box = document.forms[form][id];
	alert(box.options[box.selectedIndex].value);	
}
/******************************************************
Function: check fields to make sure there is 
		something in it!  If fieldlist containts
		email, check to make sure the email address
		is valid!
******************************************************/
function CheckEmail(sEmail){
	str = document.getElementById(sEmail).value;
	var Errors = false;
	var Emailerrors = false;
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	if(str == ""){
		Emailerrors = true;	
	}
	if (str.indexOf(at)==-1){
	  Emailerrors = true;
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   Emailerrors = true;
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		Emailerrors = true;
	}

	 if (str.indexOf(at,(lat+1))!=-1){
		Emailerrors = true;
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		Emailerrors = true;
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
		Emailerrors = true;
	 }
	
	 if (str.indexOf(" ")!=-1){
		Emailerrors = true;
	 }
	 if(Emailerrors){
		alert("Sorry, your email address is invalid!");
		return false;
	 }else{
		return true;	 
	 }
}
function checkForm(){
	var firstName = $("FirstName");
	var lastName = $("Surname");
	var middleName = $("MiddleName");
	var over = $("Age");
	var employmentStatus = $("EmploymentStatus");
	var oftenPaid = $("OftenPaid");
	var paid = $("AmountPaid");
	var bankDeposit = $("BankDeposit");
	var borrow = $("Borrow");
	var contactPhone = $("ContactPhone");
	var contactPhone2 = $("ContactPhone2");
	var email = $("Email");
	var state = $("State");
	var suburb = $("Suburb");
	var agree = $("Agree");
	var advertising = $("advertising");
	var errors = false;
	var errorMsg = "The following fields are blank:\n\n";
	
	if(firstName.value.empty()){
		errors = true;
		errorMsg += "First Name\n";
	}
	
	if(lastName.value.empty()){
		errors = true;
		errorMsg += "Last Name\n";
	}

	if(!isChecked("Age")){
		errors = true;
		errorMsg += "Are you over 17 years old\n";
	}
	
	if(employmentStatus.value.empty()){
		errors = true;
		errorMsg += "What is your employment status\n";
	}
	
	if(paid.value.empty()){
		errors = true;
		errorMsg += "How often do you get paid\n";
	}
	
	if(!isChecked("BankDeposit")){
		errors = true;
		errorMsg += "Is your pay deposited into your bank\n";
	}
	
	if(borrow.value.empty()){
		errors = true;
		errorMsg += "How much do you wish to borrow\n";
	}
	
	if(contactPhone.value.empty()){
		errors = true;
		errorMsg += "What is your contact phone number\n";
	}
	
	if(contactPhone2.value.empty()){
		errors = true;
		errorMsg += "Please verify your contact phone number\n";
	}
	
	if(contactPhone.value != contactPhone2.value){
		errors = true;
		errorMsg += "Your phone numbers do not match\n";
	}
	
	if(email.value.empty()){
		errors = true;
		errorMsg += "What is your email address\n";
	}else{
		CheckEmail("Email");
	}
	
	if(state.value.empty()){
		errors = true;
		errorMsg += "Please select a state\n";
	}
	
	if(suburb.value.empty()){
		errors = true;
		errorMsg += "Please enter a suburb\n";
	} 
	if(advertising.value.empty()){
		errors = true;
		errorMsg += "How did you hear about us\n";
	} 
	if(!agree.checked){
		errors = true;
		errorMsg += "You must agree to our Terms and Conditions\n";
	}
	
	if(errors){
		alert(errorMsg);
		return false;
	}else{
		return true;
	}
}

function isChecked(field){
	var element = document.getElementsByName(field);
	for(i=0;i<element.length;i++){
		if(element[i].checked == true){
			return true;
		}
	}
	return false;
}

var itemSelected = null;

function checkPersonalLoans(){
	var age = $("Age");
	var sellOrLoan = $("SellOrLoan");
	var category = $("Category");
	var items = $("Item");
	var description = $("Description");
	var cash = $("Cash");
	var contactPhone = $("ContactPhone");
	var contactPhone2 = $("ContactPhone2");
	var email = $("Email");
	var state = $("State");
	var suburb = $("Suburb");
	var advertising = $("advertising");
	var agree = $("Agree");
	var errors = false;
	var errorMsg = "The following fields are blank:\n\n";
	
	if(!isChecked("Age")){
		errors = true;
		errorMsg += "Are you over 17 years old\n";
	}else{
		if(checkAge()) return false;
	}
	
	if(!isChecked("SellOrLoan")){
		errors = true;
		errorMsg += "Would you like to sell the goodz or get a loan secured against them\n";
	}
	
	if(category.value.empty()){
		errors = true;
		errorMsg += "Please select a category\n";
	}

	if(itemSelected == null){
		errors = true;
		errorMsg += "Please select an item\n";
	}else if($(itemSelected).value.empty()){
		errors = true;
		errorMsg += "Please select an item\n";
	}
	
	if(cash.value.empty()){
		errors = true;
		errorMsg += "How much do you need\n";
	}
	
	if(contactPhone.value.empty()){
		errors = true;
		errorMsg += "What is your contact phone number\n";
	}
	
	if(contactPhone2.value.empty()){
		errors = true;
		errorMsg += "Please verify your contact phone number\n";
	}
	
	if(contactPhone.value != contactPhone2.value){
		errors = true;
		errorMsg += "Your phone numbers do not match\n";
	}
	
	if(email.value.empty()){
		errors = true;
		errorMsg += "What is your email address\n";
	}else{
		CheckEmail("Email");
	}
	
	if(state.value.empty()){
		errors = true;
		errorMsg += "Please select a state\n";
	}
	
	if(suburb.value.empty()){
		errors = true;
		errorMsg += "Please enter a suburb\n";
	} 
	
	if(advertising.value.empty()){
		errors = true;
		errorMsg += "How did you hear about us\n";
	} 
	
	if(!agree.checked){
		errors = true;
		errorMsg += "You must agree to our Terms and Conditions\n";
	}
	
	if(errors){
		alert(errorMsg);
		return false;
	}else{
		return true;
	}
}

function checkAge(){
	/*
	var age = $("Age2");
	if(age.checked && age.value == "no"){
		
		return true;
	}
	*/
	var element = document.getElementsByName("Age");
	for(i=0;i<element.length;i++){
		if(element[i].checked == true){
			if(element[i].value == "no"){
				alert("I'm sorry. According to Qld legislatioy you must be 17 or over to take out this type of loan");
				return true;
			}
		}
	}
}

function showItem(name){
	switch(name){
		case "jewellery":
			id = "jewellery";
			break;
		case "big stuff":
			id = "big";
			break;
		case "industrial / agricultural":
			id = "industrial";
			break;
		case "sporting and leisure":
			id = "sporting";
			break;
		case "computer/office":
			id = "computer";
			break
		case "musical instruments":
			id = "music";
			break;
		case "game consoles":
			id = "gameconsole";
			break;
		case "dvd/cd/games":
			id = "game";
			break;
		case "home entertainment":
			id = "home";
			break;
		case "car audio/electronics":
			id = "car";
			break;
		case "tools/hardware":
			id = "tools";
			break;
		case "garden/garage":
			id = "garden";
			break;
		default:
			id = name;
			break;
	}
	
	var items = new Array();
	items[0] = "jewellery";
	items[1] = "sporting";
	items[2] = "electronic";
	items[3] = "computer";
	items[4] = "music";
	items[5] = "gameconsole";
	items[6] = "game";
	items[7] = "home";
	items[8] = "car";
	items[9] = "telecommunications";
	items[10] = "tools";
	items[11] = "garden";
	items[12] = "big";
	items[13] = "industrial";
	
	for(i=0;i<items.length;i++){
		$(items[i]).hide();	
	}
	
	itemSelected = id+"Select";
	$("selectedItem").value = itemSelected;
	$(id).show();
}

function CheckFields(sFields){
	var errors = false;
	var ErrorMsg = "Please enter a:\n\n";
	var temp = 0;
	ElementArr = sFields.split("|");
	for(i=0; i<ElementArr.length; i++){
		if(!document.getElementById(ElementArr[i]).value){
			//ErrorMsg += ElementArr[i] + "\n";
			temp = seperateText(ElementArr[i]);
			ErrorMsg += temp + "\n";
			alert(ErrorMsg);
			return false;
			errors = true;
		}
	}
	if(errors == true){
		alert(ErrorMsg)
		return false;
	}else{
		if(sFields.indexOf("Email") != -1){
			var validEmail = CheckEmail("Email");
			if(!validEmail){
				return false;	
			}else{
				return true;	
			}
		}else{	
			return true;
		}
	}
}

/*****************************************************
Function: get a field name, seperate by uppercase
******************************************************/
function seperateText(elementName){
	var temp = elementName.substr(0,1);
	
	for(i=1;i<elementName.length;i++){
		if( (elementName.charCodeAt(i) >= 65) && (elementName.charCodeAt(i)<=90)){
			temp += " " + elementName.substr(i,1);
		}else{
			temp += elementName.substr(i,1);
		}
	}
	return temp;
}

