// swaps image.gif
// for image2.gif
// and vice versa
function swapThisImage(oElement){
	imageSource = oElement.src;
	arrayParts = imageSource.split('.');
	var extension = arrayParts.pop();
	var path = arrayParts.join('.');
	if(imageSource.lastIndexOf('2.') > 1) {
		path = path.substring(0,path.length-1)
		oElement.src = path + '.' + extension;
	} else {
		oElement.src = path + '2.' + extension;
	}
}

function defaultFocus(oEle, sValue){
	if(oEle.value == sValue){
		oEle.value = '';
	}
}

function defaultBlur(oEle, sValue){
	if(oEle.value == ''){
		oEle.value = sValue;
	}
}

function printPage() {
	if (window.print)
		window.print()
	else
		alert("Sorry, your browser doesn't support this feature.");
}

var image_nav = new Array();
image_nav[0] = relPath +"pics/layout/nav_news2.gif";
image_nav[1] = relPath +"pics/layout/nav_case_studies2.gif";
image_nav[2] = relPath +"pics/layout/nav_optimisation2.gif";
image_nav[3] = relPath +"pics/layout/nav_marketing2.gif";

var image_other = new Array();
image_other[0] = relPath +"pics/layout/read_more2.gif";
image_other[0] = relPath +"pics/content/panel_green_btn.gif";
image_other[0] = relPath +"pics/content/panel_blue_btn2.gif";

function preload_image_array(image_array) {
	if (document.images) {
		preload_image_object = new Image();
		var i = 0;
		for(i=0; i<=image_array.length-1; i++) {
			preload_image_object.src = image_array[i];
		}
	}
}

function verifyHuman(formName) {
	document[formName].h_formValidated.value = 'true'
}

function init() {
	// preload navigation images
	preload_image_array(image_nav);
	// preload other images
	preload_image_array(image_other);
	// hide the extra content on the contact form
	// changeHeight('extraFormDetails', 0);
}
window.onload = init;







/* Expanding Div functions */

var height_movePx			= 6; // px
var height_moveDelay	= 5; // ms
var height_start			= false;
var height_end				= false;
var height_current		= false;
var height_direction	= false;
var height_element		= false;
var height_callback		= false;

function changeHeight(eleId, height_target, callback) {
	// if its the first call to this func
	// define some variables
  if(eleId) {
    height_element = document.getElementById(eleId);
		height_callback = callback;
    height_start = height_element.offsetHeight * 1;
		height_current = height_start;
		height_end = height_target;
		if(height_end > height_start) {
			height_direction = 'ASC';
		} else {
			height_direction = 'DESC';
		}
  }
  if (height_current != height_end) {
		if(height_direction == 'ASC'){
			height_current = height_current + height_movePx;
			if(height_current > height_end) {
				height_current = height_end;
			}
		} else {
			height_current = height_current - height_movePx;
			if(height_current < height_end) {
				height_current = height_end;
			}
		}
		height_element.style.height = height_current + "px";
    setTimeout("changeHeight()", height_moveDelay);
  } else {
		if(height_callback){
			height_callback();
		}
    // alert('done');
    return;
  }
}

var width_movePx			= 10; // px
var width_moveDelay		= 5; // ms
var width_start				= false;
var width_end					= false;
var width_current			= false;
var width_direction		= false;
var width_element			= false;
var width_callback		= false;

function changeWidth(eleId, width_target, callback) {
	// if its the first call to this func
	// define some variables
  if(eleId) {
    width_element = document.getElementById(eleId);
		width_callback = callback;
    width_start = width_element.offsetWidth * 1;
		width_current = width_start;
		width_end = width_target;
		if(width_end > width_start) {
			width_direction = 'ASC';
		} else {
			width_direction = 'DESC';
		}
  }
  if (width_current != width_end) {
		if(width_direction == 'ASC'){
			width_current = width_current + width_movePx;
			if(width_current > width_end) {
				width_current = width_end;
			}
		} else {
			width_current = width_current - width_movePx;
			if(width_current < width_end) {
				width_current = width_end;
			}
		}
		width_element.style.width = width_current + "px";
    setTimeout("changeWidth()", width_moveDelay);
  } else {
		if(width_callback){
			width_callback();
		}
    // alert('done');
    return;
  }
}

function expandEnquiryFormWidth() {
	// alert(height_element.style.overflow);
	height_element.style.overflow = "visible";
	changeWidth('extraFormDetails_width', 385);
}
function contractEnquiryFormHeight() {
	// alert(height_element.style.overflow);
	height_element.style.overflow = "hidden";
	changeHeight('extraFormDetails_height', 0);
}

// paass the callback function - (which is run once the first function has finished completely)
// without parenthesis, if you pass it as a function call it will be run straight away
function expandEnquiryForm() {
	changeHeight('extraFormDetails_height', 170, expandEnquiryFormWidth );
}
function contractEnquiryForm() {
	changeWidth('extraFormDetails_width', 165, contractEnquiryFormHeight );
}

/* Expanding Div functions */









/* Form verfiication functions */

function validateEmail(email_address) {
	if (/\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email_address)){
		return true;
	}
	return false;
}



function verifyEnquiryForm() {
	var onErrorAlert		= false;
	var onErrorMessage	= true;
	var errorMessages		= new Array();
	var focused					= false;
	var formObj					= document.enquiryForm;

	if (formObj.name.value == '' || formObj.name.value == 'your name') {
		errorMessages.push('Please enter your name');
		if(!focused){
			formObj.name.focus();
			focused = true;
		}
	}

	if (formObj.telephone.value == '' || formObj.telephone.value == 'telephone') {
		errorMessages.push('Please enter your telephone number');
		if(!focused){
			formObj.telephone.focus();
			focused = true;
		}
	}

	if (!validateEmail(formObj.h_fromemail.value)) {
		errorMessages.push('Please enter a valid email address');
		if(!focused){
			formObj.h_fromemail.focus();
			focused = true;
		}
	} else if (formObj.h_formValidated.value != 'true') {
		errorMessages.push('To Prevenet SPAM we require you to manually enter your email address');
		if(!focused){
			formObj.h_fromemail.focus();
			focused = true;
		}
	}

	if(errorMessages.length != 0){
		if(onErrorAlert){
				alert(errorMessages[0]);
		}
		if(onErrorMessage){
				oErrorDiv = document.getElementById("enquiry_errors");
				oErrorDiv.innerHTML = "<b>Error:</b><br />";
				for(var i=0; i<errorMessages.length; i++){
					oErrorDiv.innerHTML += "<li>" + errorMessages[i] + "</li>";
				}
				oErrorDiv.style.display = "block";
		}
		return false;

	} else {
		// copy the recipients email address to the hidden field
		// so that it can be seen in the body of the email
		// as well as in the senders field
		formObj.Email.value = formObj.h_fromemail.value;
		formObj.WebPage.value = location.href;
		if (formObj.company.value == 'company name') {
			formObj.company.value = '';
		}
		if (formObj.address.value == 'website address') {
			formObj.address.value = '';
		}
		return true;
	}

}


function verifyEnquiryFormLower() {
	var onErrorAlert		= false;
	var onErrorMessage	= true;
	var errorMessages		= new Array();
	var focused					= false;
	var formObj					= document.enquiryFormLower;

	if (formObj.name.value == '' || formObj.name.value == 'your name') {
		errorMessages.push('Please enter your name');
		if(!focused){
			formObj.name.focus();
			focused = true;
		}
	}

	if (formObj.telephone.value == '' || formObj.telephone.value == 'telephone') {
		errorMessages.push('Please enter your telephone number');
		if(!focused){
			formObj.telephone.focus();
			focused = true;
		}
	}

	if (!validateEmail(formObj.h_fromemail.value)) {
		errorMessages.push('Please enter a valid email address');
		if(!focused){
			formObj.h_fromemail.focus();
			focused = true;
		}
	} else if (formObj.h_formValidated.value != 'true') {
		errorMessages.push('To Prevenet SPAM we require you to manually enter your email address');
		if(!focused){
			formObj.h_fromemail.focus();
			focused = true;
		}
	}

	if(errorMessages.length != 0){
		if(onErrorAlert){
				alert(errorMessages[0]);
		}
		if(onErrorMessage){
				oErrorDiv = document.getElementById("enquiry_errors_lower");
				oErrorDiv.innerHTML = "<b>Error:</b><br />";
				for(var i=0; i<errorMessages.length; i++){
					oErrorDiv.innerHTML += "<li>" + errorMessages[i] + "</li>";
				}
				oErrorDiv.style.display = "block";
		}
		return false;

	} else {
		// copy the recipients email address to the hidden field
		// so that it can be seen in the body of the email
		// as well as in the senders field
		formObj.Email.value = formObj.h_fromemail.value;
		formObj.WebPage.value = location.href;
		if (formObj.company.value == 'company name') {
			formObj.company.value = '';
		}
		if (formObj.web_address.value == 'website address') {
			formObj.web_address.value = '';
		}
		if (formObj.enquiry.value == 'enquiry') {
			formObj.enquiry.value = '';
		}
		return true;
	}

}


function verifyContactForm() {
	var onErrorAlert		= false;
	var onErrorMessage	= true;
	var errorMessages		= new Array();
	var focused					= false;
	var formObj					= document.contactForm;

	if (formObj.name.value == '' || formObj.name.value == 'your name') {
		errorMessages.push('Please enter your name');
		if(!focused){
			formObj.name.focus();
			focused = true;
		}
	}

	if (formObj.telephone.value == '' || formObj.telephone.value == 'telephone') {
		errorMessages.push('Please enter your telephone number');
		if(!focused){
			formObj.telephone.focus();
			focused = true;
		}
	}

	if (!validateEmail(formObj.h_fromemail.value)) {
		errorMessages.push('Please enter a valid email address');
		if(!focused){
			formObj.h_fromemail.focus();
			focused = true;
		}
	} else if (formObj.h_formValidated.value != 'true') {
		errorMessages.push('To Prevenet SPAM we require you to manually enter your email address');
		if(!focused){
			formObj.h_fromemail.focus();
			focused = true;
		}
	}

	if(errorMessages.length != 0){
		if(onErrorAlert){
				alert(errorMessages[0]);
		}
		if(onErrorMessage){
				oErrorDiv = document.getElementById("enquiry_errors_contact");
				oErrorDiv.innerHTML = "<b>Error:</b><br />";
				for(var i=0; i<errorMessages.length; i++){
					oErrorDiv.innerHTML += "<li>" + errorMessages[i] + "</li>";
				}
				oErrorDiv.style.display = "block";
		}
		return false;

	} else {
		// copy the recipients email address to the hidden field
		// so that it can be seen in the body of the email
		// as well as in the senders field
		formObj.Email.value = formObj.h_fromemail.value;
		formObj.WebPage.value = location.href;
		if (formObj.company.value == 'company name') {
			formObj.company.value = '';
		}
		if (formObj.address.value == 'website address') {
			formObj.address.value = '';
		}
		return true;
	}

}

function verifyQuestionaireForm() {
	var onErrorAlert		= false;
	var onErrorMessage	= true;
	var errorMessages		= new Array();
	var focused					= false;
	var formObj					= document.questionaireForm;

	if (formObj.full_name.value == '') {
		errorMessages.push('Please enter your full name');
		if(!focused){
			formObj.full_name.focus();
			focused = true;
		}
	}
	
	if (!validateEmail(formObj.email_address.value)) {
		errorMessages.push('Please enter a valid email address');
		if(!focused){
			formObj.email_address.focus();
			focused = true;
		}
	} else if (formObj.h_formValidated.value != 'true') {
		errorMessages.push('To Prevenet SPAM we require you to manually enter your email address');
		if(!focused){
			formObj.email_address.focus();
			focused = true;
		}
	}

	if (formObj.telephone.value == '') {
		errorMessages.push('Please enter your telephone number');
		if(!focused){
			formObj.telephone.focus();
			focused = true;
		}
	}
	
	if (formObj.company_name.value == '') {
		errorMessages.push('Please enter your company name');
		if(!focused){
			formObj.company_name.focus();
			focused = true;
		}
	}
	
	if (formObj.primary_market.value == '') {
		errorMessages.push('Please enter your primary target market');
		if(!focused){
			formObj.primary_market.focus();
			focused = true;
		}
	}
	
		
	if (formObj.website_online_time.value == '') {
		errorMessages.push('Please enter how long your website has been online for');
		if(!focused){
			formObj.website_online_time.focus();
			focused = true;
		}
	}
	

	if(errorMessages.length != 0){
		if(onErrorAlert){
				alert(errorMessages[0]);
		}
		if(onErrorMessage){
				oErrorDiv = document.getElementById("enquiry_errors_contact");
				oErrorDiv.innerHTML = "<b>Error:</b><br />";
				for(var i=0; i<errorMessages.length; i++){
					oErrorDiv.innerHTML += "<li>" + errorMessages[i] + "</li>";
				}
				oErrorDiv.style.display = "block";
		}
		return false;

	} else {
		return true;
	}

}

function recordCompetitors(oTxtArea) {
	var strValue = oTxtArea.value;
	if (strValue) {
		var matches = strValue.match(/\w\s*(\n|$)/g);	
		document.getElementById('counterCompetitors').innerHTML = matches.length;
	}
}

function recordKeywords(oTxtArea) {
	var strValue = oTxtArea.value;
	if (strValue) {
		var matches = strValue.match(/\w\s*(\n|$)/g);
		document.getElementById('counterKeywords').innerHTML = matches.length;
	}
}

function primaryTarget(oSelect) {
	var strValue = oSelect.value;
	if (strValue == 'Other') {
		document.getElementById('primary_market_other').style.display = "block";
	} else {
		document.getElementById('primary_market_other').style.display = "none";
	}
}
