/* Ajax and DHTML for the Viking Contact Form */

//Resuable variables
var contactMethod = '';
var contactDetail = '';
var tr;
var email;
var phone;
var fax;
var inputArray;
var textareaArray;
var params ='';

//Get the html objects on load
window.onload = function(){
tr = document.getElementById('contact-method-input');
email = document.getElementById('input-email');
phone = document.getElementById('input-phone');
fax = document.getElementById('input-fax');
inputArray = document.getElementsByTagName('input');
textareaArray = document.getElementsByTagName('textarea');
}

function showInput(radio){

var which = radio.value;

	switch(which){
	
		case 'email':
			tr.style.display = 'block';
			email.style.display = 'block';
			phone.style.display = 'none';
			fax.style.display = 'none';
			contactMethod = 'email';
		break;
		
		case 'phone':
			tr.style.display = 'block';
			email.style.display = 'none';
			phone.style.display = 'block';
			fax.style.display = 'none';
			contactMethod = 'phone';
		break;
		
		case 'fax':
			tr.style.display = 'block';
			email.style.display = 'none';
			phone.style.display = 'none';
			fax.style.display = 'block';
			contactMethod = 'fax';
		break;
	
	}

}

function sendForm(){

	//Validation
			
	//Validate the radio buttons
	for(i=0;i<inputArray.length;i++){
	
		if(inputArray[i].type == "radio"){
			
			if(inputArray[i].checked){
				contactMethod = inputArray[i].value;
				if(inputArray[8].value){
					contactDetail = inputArray[8].value;
				}
				else if(inputArray[7].value){
					contactDetail = inputArray[7].value;
				}
				else if(inputArray[6].value){
					contactDetail = inputArray[6].value;
				}
				
			}
			
		}
			
	}
	
	//Do some Ajax
	if(contactMethod != '' && contactDetail != ''){
	
		//Stick all form fields into a string for load()
		if(inputArray[0].value || inputArray[1].value){
			params += 'name='+inputArray[0].value+' '+inputArray[1].value;
		}
		if(inputArray[2].value){
			params += '&location='+inputArray[2].value;
		}
			params += '&contact_method='+contactMethod;
		if(contactDetail != ''){
			params += '&contact_detail='+contactDetail;
		}
		if(textareaArray[0].value){
			params += '&message='+textareaArray[0].value
		}
		
		
		load('contact.php','success','p', params)
		//Do some DHTML
		document.getElementById('form-body').style.display = 'none';
		document.getElementById('success').style.display = 'block';

	}
	else{
	alert("Please fill in either an email address, phone number, or fax number");
	}

}

function resetForm(){

	for(i=0;i<inputArray.length;i++){
	
		if(inputArray[i].type == "radio"){
			inputArray[i].checked = false;
		}
		else{
			inputArray[i].value = '';
		}
	
	}
	
	textareaArray[0].value = '';

	tr.style.display = 'none';
	email.style.display = 'none';
	phone.style.display = 'none';
	fax.style.display = 'none';
	
}