// JavaScript Document
//preload all images:
window.addEvent('domready', function(){
	
	$$('.promotionGal li').addEvent('click', function(){
		var index = $$('.promotionGal li').indexOf(this);
		var allContents = $$('.contents');
		
		for(i=0;i<allContents.length;i++){
			$(allContents[i]).setStyles({opacity: 0, display: 'none'});
		}
		$(allContents[index]).setStyle('display','block');
		$(allContents[index]).fade('in');
	});
	
	
	// Only for Front-page:
	if($('homeFlashArea') && imgArr.length >=3){ // requres at least 3 images in DB:
		var imgAll = $$('.img2');
		for(i=0; i<imgAll.length; i++){
			imgAll[i].setStyle('opacity','0');		
		}
		
		//after preload, call for display:
		setInterval('runImages()', 7000);
	}
});

//===========::HOME RANDOM FLASH IMAGES::================
var randIndex=new Array(-1,-1,-1);
function createRandomNumbers(max_range, num){
	for(i=0; i<num; i++){
		randIndex[i]=Math.floor(Math.random() * max_range);
		for(j=1; j<=i; j++) //Don't check for the 1st time:
			if(randIndex[j-1]==randIndex[i]){--i; break;}
	} //alert(randIndex);
}

function runImages(){
	var img1, img2;
	createRandomNumbers(imgArr.length,3);
	
	for(i=0; i<3; i++){
		img1 = $$('.homeFlashArea'+(i+1)+' .img1');
		img2 = $$('.homeFlashArea'+(i+1)+' .img2');
		
		exampleFx1 = new Fx.Tween(img1[0], {
			property: 'opacity',
			duration: 1800, 
			transition: Fx.Transitions.Quad.easeInOut
		});
		
		exampleFx2 = new Fx.Tween(img2[0], {
			property: 'opacity',
			duration: 1800, 
			transition: Fx.Transitions.Quad.easeInOut
		});
		
		if( img1[0].getStyle('opacity') == 0){
			img1[0].src=imgArr[randIndex[i]];
			
			img2[0].tween('opacity', 0); /*fade it out*/
			exampleFx1.start(0,1); /*fade it in*/
		}
		else if(img2[0].getStyle('opacity') == 0){
			img2[0].src=imgArr[randIndex[i]];
			
			img1[0].tween('opacity', 0); /*fade it out*/
			exampleFx2.start(0,1); /*fade it in*/
		}
	}	
}


//==============::FORM VALIDATIONs::====================

function chkReviewForm(){
	var formName = document.reviewForm;
	var reqFields = new Array(formName.r_name, formName.r_comments, formName.r_restaurant,formName.r_email, {'email': formName.r_email,'captcha': formName.r_captcha});
	//alert(1);
	return chkValidity(formName, reqFields);
}

function chkContestsForm(){
	var formName = document.contestsForm;
	var reqFields = new Array(formName.c_name, formName.c_email, formName.c_phone, formName.c_answer, {'email': formName.c_email,'captcha': formName.c_captcha});
	return chkValidity(formName, reqFields);
}

function chkTeaserForm(){
	var formName = document.teaserForm;
	var reqFields = new Array(formName.t_name, formName.t_email, formName.t_teaser_text, formName.t_answer_text, {'email': formName.t_email,'captcha': formName.t_captcha});
	return chkValidity(formName, reqFields);
}

function chkValidity(formName, reqFields){
	
	//check for empty fields:
	for(i=0; i < reqFields.length-1; i++){
		if(reqFields[i].value==''){
			alert('Please complete the required fields marked with (*).');
			return false;
		}
	}
	
	//check for only empty captcha field:
	if(reqFields[reqFields.length-1].captcha && reqFields[reqFields.length-1].captcha.value==''){
		alert('Please insert the image-code as text.');
		return false;
	}
	
	//check valid email:
	if(reqFields[reqFields.length-1].email){
		if(!checkEmail(reqFields[reqFields.length-1].email.value)){
			alert('Please enter a valid Email address.');
			return false;
		}
	}
	
	//check image-to-text validation via ajax:
	if(reqFields[reqFields.length-1].captcha){
		var req = new Request({
				method: 'post',
				url: temPath+'ajax.php',
				data: { 'text_captcha' : reqFields[reqFields.length-1].captcha.value },
				onComplete: function(response) {
						if(response==0){
							alert('Sorry, the code you entered was invalid. Try Again');
							
							//reset the field and focus on it:
							$$('div.captcha img').setProperty('src', temPath+'securimage/securimage_show.php?sid='+Math.random());
							reqFields[reqFields.length-1].captcha.value='';
							reqFields[reqFields.length-1].captcha.focus();
						}
						else{
							formName.submit();
							alert('Thank you for your submission');
						}
					}
			}).send();
		
		return false; //js submit;
	}
	else
		return true; //form submit;
}

function checkEmail(email){
	var str=email;
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if (filter.test(str))
		return true;
	else
		return false;
}