var rightContentCached = {};
rightContentCached['content'] = "";
rightContentCached['time'] = "";

function getRightContentStats(){
	var url = '/php/AdIQStatistics.php';
	var rand = Math.random(9999);
	var pars = 'rand=' + rand;
	var myAjax = new Ajax.Request(url, {method: 'get', parameters: pars, onComplete: showStatistics} );
}

function showStatistics(request){
	var response = request.responseText;
	if(response != ""){
		$('right_content').innerHTML = response;
	}
	addEmailFormOnclick();
	loadContentImages();
}

function loadContentImages(){
	//Get images to rotate
	getImages('clients',4000,true,false);
	getImages('partners',3000,true,false);
}

function showCachedRightContent(){
	var cachedTime = rightContentCached['cachedTime'];
	var nowTime = new Date().getTime();
	//If more than an hour has passed, reload right content
	if((nowTime - cachedTime > 3600000)){
		getRightContentStats();
	}else{
		if(rightContentCached['content'] != ""){
			//console.log('loading right content from cache.');
			$('right_content').innerHTML = rightContentCached['content'];				
			loadContentImages();
		}
	}
	addEmailFormOnclick();
}

function getImages(folder,delay,isCached,isClickable){
	var url = '/php/AdIQLoadImages.php';
	var rand = Math.random(9999);
	var pars = 'folder=' + folder + '&rand=' + rand;
	var myAjax = new Ajax.Request(url, {method: 'get', parameters: pars, onComplete: function(request){loadImages( request, folder,delay,isCached,isClickable);}} );
}

function loadImages(request,folder,delay,isCached,isClickable){
	var response = request.responseText;
	var imgContainerId = folder + "Images";
	var imagesHtml = "";
	
	if(response != ""){
		var imgArray = response.split(',');
		imagesHtml = generateImagesHtml(imgArray);
	}
	$(imgContainerId).innerHTML = imagesHtml;
	
	//cache the content
	if(isCached){
		cacheRightContent($('right_content').innerHTML);
	}
	var images = '#' + imgContainerId + ' img';
	var imagesArray = $$(images);
	
	//if clickable, assign onclick event
	if(isClickable){
		assignOnClick(imagesArray);
	}
	startImageRotation(imagesArray,delay);
}

function generateImagesHtml(imgArray,folder){
	var imagesHtml = "";
	
	for(var i=0; i<imgArray.length; i++){
		if(imgArray[i] != "" && imgArray[i] != undefined){
			var imgId = folder + "Image_" + i;
			if(i == 0){
				imagesHtml += '<img id="' + imgId + '" class="fadeImage" src="/img/' + imgArray[i] + '" border="0">';
			}else{
				imagesHtml += '<img id="' + imgId + '" class="fadeImage" src="/img/' + imgArray[i] + '" border="0" style="display:none">';
			}
		}
	}
	return imagesHtml;
}

function assignOnClick(imagesArray){
	var href = "/uk/contact/find_out.html";
	for(var i=0; i<imagesArray.length; i++){
		var imageId = imagesArray[i].id;
		$(imageId).onclick  = function () {
			getPage(href);
			return false;
		};
	}
}
function cacheRightContent(rightContentHtml){
	var cachedTime = new Date().getTime();
	rightContentCached['content'] = rightContentHtml;
	rightContentCached['cachedTime'] = cachedTime;
}

function startImageRotation(imagesArray,delay){
	
	try{
		new iSlideShow({
			slides 	: imagesArray,
			wait	: delay
		});
	}catch(e){
		alert('error: ' + e);
	}
}

function addEmailFormOnclick(){
	if($('email_contact_form')){
		$('email_contact_submit').onclick = function(){
			submitEmailForm();
		}
	}
}

function submitEmailForm(){
	var email = $('email_contact').value;
	var sector = $('email_contact_sector').value;
	
	if(emailValid(email) && sector != ""){
		var url = '/php/AdIQSendEmail.php';
		var rand = Math.random(9999);
		var pars = 'emailTo=' + email + '&sector=' + sector + '&rand=' + rand;
		var myAjax = new Ajax.Request(url, {method: 'get', parameters: pars, onComplete: showEmailResult} );
	}else{
		highlightInputFields($('email_contact'),$('email_contact_sector'));
	}
}

function addFindOutFormOnclick(){
	if($('find_out_form')){
		$('email_find_out_submit').onclick = function(){
			submitFindOutForm();
		}
	}
}

function submitFindOutForm(){
	var name = $('find_out_name').value;
	var email = $('find_out_email').value;
	var organisation = $('find_out_company').value;
	var mobileno = $('find_out_phone_number').value;
	var callback = $('find_out_check_callback').checked;
	var emailinfo = $('find_out_check_email').checked;
	var callbackrequest= "";
	var emailrequest= "";
	
	if(callback){
		callbackrequest= "yes";
	}else{
		callbackrequest= "no";
	}
	if(emailinfo){
		emailrequest= "yes";
	}else{
		emailrequest= "no";
	}
	if(emailValid(email) && valid_mobile(mobileno) && name != "" && organisation != ""){
		var url = '/php/AdIQFindOut.php';
		var rand = Math.random(9999);
		var pars = 'name=' + name + '&organisation=' + organisation + '&find_out_email=' + email + '&mobileno=' + mobileno + '&callbackrequest=' + callbackrequest  + '&emailrequest=' + emailrequest + '&rand=' + rand;
		var myAjax = new Ajax.Request(url, {method: 'get', parameters: pars, onComplete: showFindOutResult} );
	}else{
		highlightFindoutInputFields($('find_out_email'),$('find_out_phone_number'),$('find_out_name'),$('find_out_company'));
	}
}

function showEmailResult(request){
	var response = request.responseText;
	//alert(response);
	if(response == "OK"){
		$('email_contact_form_fields').hide();
		$('email_contact_success').show();
	}else if(response == "NOK"){
		//$('email_contact_error').show();
	}
}

function showFindOutResult(request){
	var response = request.responseText;
	//alert(response);
	if(response == "OK"){
		$('find_out_form').hide();
		$('find_out_success').show();
	}else if(response == "NOK"){
		//$('email_contact_error').show();
	}
}

function highlightInputFields(emailField, sectorField){	
	if(emailField.value == ""){
		emailField.style.backgroundColor = '#FFCACA';
	}else{
		emailField.style.backgroundColor = '#fff';
	}
	if(sectorField.value == ""){
		sectorField.style.backgroundColor = '#FFCACA';
	}else{
		sectorField.style.backgroundColor = '#fff';
	}
}

function highlightFindoutInputFields(emailinfoField, nameField, companyField, phoneField){	
	if(emailinfoField.value == ""){
		emailinfoField.style.backgroundColor = '#FFCACA';
	}else{
		emailinfoField.style.backgroundColor = '#fff';
	}
	if(nameField.value == ""){
		nameField.style.backgroundColor = '#FFCACA';
	}else{
		nameField.style.backgroundColor = '#fff';
	}
	if(companyField.value == ""){
		companyField.style.backgroundColor = '#FFCACA';
	}else{
		companyField.style.backgroundColor = '#fff';
	}
	if(phoneField.value == ""){
		phoneField.style.backgroundColor = '#FFCACA';
	}else{
		phoneField.style.backgroundColor = '#fff';
	}
}

function emailValid(email){
	
	var regex = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	
	if(regex.test(email) == true) {
		return true;
	}
	return false;
}

function valid_mobile(mobileno){

	var regex = /^\+44[0-9]{10}$|^[0-9]{11}/;
	if (regex.test(mobileno) == true){
		return true;
	}
	return false;
}


