// JavaScript Document


 function multiple(){
	//MULTIPLE SELECTS
			$('.add-multiple').click(function() {
				$(this).parent().parent().children('.mselect:first').addClass('current-mselect');
				
				$(this).parent().parent().children('.mselect_all:first').children('option:selected').remove().appendTo('.current-mselect');
				
				$(this).parent().parent().children('.mselect:first').removeClass('current-mselect');
				return false;
				 });  
			
				$('.remove-multiple').click(function() { 
				$(this).parent().parent().children('.mselect_all:first').addClass('current-mselect');
				$(this).parent().parent().children('.mselect:first').children('option:selected').remove().appendTo('.current-mselect'); 
				$(this).parent().parent().children('.mselect_all:first').removeClass('current-mselect');
				
				return false;
				});

	$('.add-all').click(function() {
		$(this).parent().parent().children('.mselect:first').addClass('current-mselect');
		$(this).parent().parent().children('.mselect_all:first').children('option').remove().appendTo('.current-mselect');
		$(this).parent().parent().children('.mselect:first').removeClass('current-mselect');
		//$(this).parent().parent().find('.add-multiple').click();
		return false;
	});
	
	$('.remove-all').click(function() {
		$(this).parent().parent().children('.mselect_all:first').addClass('current-mselect');
		$(this).parent().parent().find('.mselect option').remove().appendTo('.current-mselect'); 
		$(this).parent().parent().children('.mselect_all:first').removeClass('current-mselect');
		//$(this).parent().parent().find('.remove-multiple').click();
		return false;
	});
	//form submit
	$('form').submit(
				function(){
					
					$(this).find('.mselect option').each(function(i) {  
					$(this).attr("selected", "selected");  
					})
				}
	);
	
	$('.add-multiple').click();
}


  (function($){
			$.fn.input_fb = function(options) {  
    
 			 return this.each(function() {  
    			$obj=$(this);
				$obj.focus(function(){
					if($(this).attr('value')==$(this).attr('title'))
					$(this).attr('value','');
								 });
			
			$obj.blur(function(){
				  if($(this).attr('value')==''&&$(this).attr('id')!='key')
					$(this).attr('value',$(this).attr('title'));
								 });
    		 	});  
 			 };  
  })(jQuery);  

var $day,$month,$year;
function  changeCalendar(){
	
	$day=$('.jcalendar .jcalendar-select-day option:selected','#calendar').text();
	$month=$('.jcalendar .jcalendar-select-month option:selected','#calendar').attr('value');
	$year=$('.jcalendar .jcalendar-select-year option:selected','#calendar').attr('value');
	$('#calendar #day').text($day);
	$('#calendar #month').text($month);
	$('#calendar #year').text($year);
	
	
	
	
	
}


function busyDays(){
	
	$('#busy-dates','#calendar').find('.date').each(function(){
			
			date=$(this).text();
			date=new Date(date);
			date=date.toUTCString();
			
			$('.jcalendar td a','#calendar').each(function(){
				a=$(this).attr('rel');
				a=new Date(a);
				a=(a.toUTCString());
				
				filter =new RegExp('^'+date);
				
				if(filter.test(a)){
					$(this).parent().addClass('busy-day');
					
				}
				
						 						});
			});
}
function loadEvents(){
	$lang='ar';
	
$('#loaded-courses').empty().addClass('loader');
		$.get('_ajax_calendar_courses.php',{day:$day,month:$month,year:$year,lang:$lang},function(data){
				$('#loaded-courses').html(data).removeClass('loader');
											
				});	
	
}

var clickf=function(){changeCalendar();
						loadEvents();}
var changef=function(){busyDays(); }
function extendCalendarEvents(flag){
	

	$('.jcalendar td a','#calendar').click(clickf);
	
	$('.jcalendar td a','#calendar').livequery('click', clickf);
	
}
function calendar(){
	
			//extend
			extendCalendarEvents(true);
			busyDays();
			
			$('.jcalendar .jcalendar-selects select','#calendar').livequery('change',changef)
}
function newsletter(){
	$('#mailinglist_form').submit(function(e){
			
		if(!$('#submit-button').hasClass('disabled')){
		
		$phpfile=$(this).attr('action');
		$e=$('input[name=mailinglist_email]').attr('value');
		//lang=$('input[name=lang]').attr('value');
		lang='ar';
		if(validateEmail($e)){
		$('#submit-button').addClass('disabled');
		$('#newsletterarea').addClass('loader');
		$.post($phpfile,{'newsletteremail':$e,'lang':lang},function(data){
				
				$('#newsletterarea').removeClass('loader');
				$('#newsletterarea').find('p').text(data);							   
				$('#submit-button').removeClass('disabled');
				
				});
		}
		else
			alert('Enter a valid Email');
		}	
		//$(this).addClass('disabled');
		return false;
		});
	
}


function courseRegister(){
	
	
$('.course-register','.course-actions').click(function(){
						$('#course_register_form').slideToggle('normal');										   
						return false;
						});
			
			$('#course_register_form').submit(function(){
					form=$(this);								   
				if(!form.hasClass('loader')){
					
				
				$('.error',form).hide();
					//data
					$fullname=$('input[name=fullname]',form).attr('value');
					$title=$('input[name=title]',form).attr('value');
					$company=$('input[name=company]',form).attr('value');
					$email=$('input[name=email]',form).attr('value');
					$phone=$('input[name=phone]',form).attr('value');
					$mobile=$('input[name=mobile]',form).attr('value');
					$no_trainees=$('input[name=no_trainees]',form).attr('value');
					$country=$('input[name=country]',form).attr('value');
					$city=$('input[name=city]',form).attr('value');
					$comments=$('textarea[name=comments]',form).attr('value');
					$cid=$('input[name=cid]',form).attr('value');
					
					var validemail=validateEmail($email);
					var validfullname=($fullname==''?false:true);
					var validmobile=($mobile==''?false:true);
					var validphone=($phone==''?false:true);
					
					
					if(validemail&&validfullname&&validmobile&&validphone){
						form.addClass('loader');
						$phpfile=form.attr('action');
						
						$.post($phpfile,{cid:$cid,fullname:$fullname,title:$title,company:$company,email:$email,phone:$phone,mobile:$mobile,no_trainees:$no_trainees,country:$country,city:$city,comments:$comments},function(data){
						
						form.removeClass('loader');	
						$(form).html(data);
											
													}
						);//end post
					}//end validate
					
					else{
						if(!validemail)
						$('input[name=email]',form).after($errormessage).next().fadeIn('normal');
						if(!validfullname)
						$('input[name=fullname]',form).after($errormessage).next().fadeIn('normal');
						if(!validmobile)
						$('input[name=mobile]',form).after($errormessage).next().fadeIn('normal');
						if(!validphone)
						$('input[name=phone]',form).after($errormessage).next().fadeIn('normal');
					}
				}//end loader check
				
				$('#course-register').addClass('disabled');
			return false;
			});	
	
	
	
	
	
	
}

function memberRegister(){
	
	
	$('#register_form').submit(function(){
		
		form=$(this);
		form.find('span.error').remove();
		$fullname=$('input[name=fullname]',form).attr('value');
		$center=$('input[name=center]',form).attr('value');
		$email=$('input[name=email]',form).attr('value');
		$phone=$('input[name=phone]',form).attr('value');
		$mobile=$('input[name=mobile]',form).attr('value');
		$city=$('input[name=city]',form).attr('value');
		$country=$('select[name=country] option:selected',form).attr('value');
		$comments=$('textarea[name=comments]',form).attr('value');
		
		var validemail=validateEmail($email);
		var validcenter=($center==''?false:true);
		var validcomments=($comments==''?false:true);
		var validfullname=($fullname==''?false:true);
		
		var validphone=($phone==''?false:true);
		var validmobile=($mobile==''?false:true);
		var validcity=($city==''?false:true);
		var validcountry=($country==''?false:true);
		if(validemail&&validfullname&&validcenter&&validphone&&validmobile&&validcity&&validcountry){
			
			
			return true;
			
		}
		else{
			
			if(!validemail)
			$('input[name=email]',form).after($errormessage).next().fadeIn('normal');
			if(!validfullname)
			$('input[name=fullname]',form).after($errormessage).next().fadeIn('normal');
			if(!validcenter)
			$('input[name=center]',form).after($errormessage).next().fadeIn('normal');
			
			if(!validphone)
			$('input[name=phone]',form).after($errormessage).next().fadeIn('normal');
			if(!validmobile)
			$('input[name=mobile]',form).after($errormessage).next().fadeIn('normal');
			if(!validcity)
			$('input[name=city]',form).after($errormessage).next().fadeIn('normal');
			if(!validcountry)
			$('select[name=country]',form).after($errormessage).next().fadeIn('normal');
			
			alert($fillfieldsmessage);
			return false;
			}
		});
}


function trainerValidate(){
	
	
	$('#trainer_form').submit(function(){
		
		form=$(this);
		$fullname=$('input[name=name]',form).attr('value');
		//$gender=$('input[name=gender]:selected',form).attr('value');
		//$education=$('input[name=education]',form).attr('value');
		//$brief=$('textarea[name=brief]',form).attr('value');
		
		var validfullname=($fullname==''?false:true);
		
		if(validfullname){
			
			
			return true;
			
		}
		else{
			
			if(!validfullname)
			$('input[name=name]',form).after($errormessage).next().fadeIn('normal');
			
			alert($fillfieldsmessage);
			return false;
			}
		});
}

function courseValidate(){
	
	
	$('#course_form').submit(function(){
		
		form=$(this);
		$(this).find('.error').remove();
		$code=$('input[name=code]',form).attr('value');
		$fullname=$('input[name=name]',form).attr('value');
		$start=$('input[name=start_date]',form).attr('value');
		$end=$('input[name=end_date]',form).attr('value');
		
			
		$duration=$('input[name=duration]',form).attr('value');
		$timing=$('input[name=timing]',form).attr('value');
		$fees=$('input[name=fees]',form).attr('value');
		$location=$('input[name=location]',form).attr('value');
		$country=$('select[name=country] option:selected',form).attr('value');
		$city=$('select[name=city] option:selected',form).attr('value');
		/*$preface=  tinyMCE.getContent("preface");
		$objective=  tinyMCE.getContent("objective");
		$towhom=  tinyMCE.getContent("towhom");
		$content=  tinyMCE.getContent("content");
		$remarks=  tinyMCE.getContent("remarks");*/
		$preface=document.course_form.preface.value;
		$objective=document.course_form.objective.value;
		$towhom=document.course_form.towhom.value;
		$content=document.course_form.content.value;
		$remarks=document.course_form.remarks.value;
		
		var $trainers,$keywords;
		if($(".mselect[name^=trainers]").length ){
   				$trainers=$(".mselect[name^=trainers]").find("option").length;
		}
		if($(".mselect[name^=keywords]").length ){
   				$keywords=$(".mselect[name^=keywords]").find("option").length;
		}
	
		var validcode=($code==''?false:true);
		var validfullname=($fullname==''?false:true);
		var validstartdate=($start==''?false:true);
		var validenddate=($end==''?false:true);
		
		validdateformat=true;
		if(validstartdate&&validenddate){
		var sdate=new Date(($start.replace('-','/')).replace('-','/'));
		var edate=new Date(($end.replace('-','/')).replace('-','/'));
		var one_day=1000*60*60*24
		
		if(Math.ceil((edate.getTime()-sdate.getTime())/(one_day))>=0)
			validdateformat=true;
		else
			validdateformat=false;

		}
		
		var validduration=($duration==''?false:true);
		var validtiming=($timing==''?false:true);
		var validfees=($fees==''?false:true);
		var validlocation=($location==''?false:true);
		var validcity=($city==''?false:true);
		var validcountry=($country==''?false:true);
		var validpreface=($preface==''?false:true);
		var validobjective=($objective==''?false:true);
		var validtowhom=($towhom==''?false:true);
		var validcontent=($content==''?false:true);
		var validremarks=($remarks==''?false:true);
		var validtrainers=($trainers==0?false:true);
		var validkeywords=($keywords==0?false:true);
		validtrainers=true;
		if(validcode&&validfullname&&validduration&&validtiming&&validfees&&validpreface&&validobjective&&validtowhom&&validcontent&&validremarks&&validtrainers&&validkeywords&&validcity&&validcountry&&validlocation&&validdateformat){
			
			
			return true;
			
		}
		else{
			
			if(!validcode)
			$('input[name=code]',form).after($errormessage).next().fadeIn('normal');
			if(!validfullname)
			$('input[name=name]',form).after($errormessage).next().fadeIn('normal');
			if(!validstartdate)
			$('input[name=start_date]',form).after($errormessage).next().fadeIn('normal');
			if(!validenddate)
			$('input[name=end_date]',form).after($errormessage).next().fadeIn('normal');
			if(!validdateformat){
				alert(validdatemessage);
				
			}
			if(!validduration)
			$('input[name=duration]',form).after($errormessage).next().fadeIn('normal');
			if(!validtiming)
			$('input[name=timing]',form).after($errormessage).next().fadeIn('normal');
			if(!validfees)
			$('input[name=fees]',form).after($errormessage).next().fadeIn('normal');
			if(!validcity)
			$('select[name=city]',form).after($errormessage).next().fadeIn('normal');
			if(!validcountry)
			$('select[name=country]',form).after($errormessage).next().fadeIn('normal');
			if(!validlocation)
			$('input[name=location]',form).after($errormessage).next().fadeIn('normal');
			if(!validtrainers)
			$('select[name^=trainers[]',form).after($errormessage).next().fadeIn('normal');
			if(!validkeywords)
			$('select[name^=keywords[]',form).after($errormessage).next().fadeIn('normal');
			
			if(!validpreface)
			$('label[for=preface]',form).after($errormessage).next().fadeIn('normal');
			if(!validobjective)
			$('label[for=objective]',form).after($errormessage).next().fadeIn('normal');
			if(!validtowhom)
			$('label[for=towhom]',form).after($errormessage).next().fadeIn('normal');
			if(!validcontent)
			$('label[for=content]',form).after($errormessage).next().fadeIn('normal');
			if(!validremarks)
			$('label[for=remarks]',form).after($errormessage).next().fadeIn('normal');
			
			alert($fillfieldsmessage);
			return false;
			}
		});
}

function contact(){
	
			$('#contact_form').submit(function(){
					form=$(this);								   
				if(!form.hasClass('loader')){
					
				
				$('.error',form).hide();
					//data
					$fullname=$('input[name=fullname]',form).attr('value');
					$email=$('input[name=email]',form).attr('value');
					$phone=$('input[name=phone]',form).attr('value');
					$comments=$('textarea[name=comments]',form).attr('value');
					
					
					var validemail=validateEmail($email);
					var validfullname=($fullname==''?false:true);
					var validmessage=($comments==''?false:true);
					
					if(validemail&&validfullname&&validmessage){
						form.addClass('loader');
						$phpfile=form.attr('action');
						
			$.post($phpfile,{fullname:$fullname,email:$email,phone:$phone,comments:$comments},function(data){
						
						form.removeClass('loader');	
						$(form).html(data);
											
													}
						);//end post
					}//end validate
					
					else{
						if(!validemail)
						$('input[name=email]',form).after($errormessage).next().fadeIn('normal');
						if(!validfullname)
						$('input[name=fullname]',form).after($errormessage).next().fadeIn('normal');
						if(!validmessage)
						$('textarea[name=comments]',form).after($errormessage).next().fadeIn('normal');
					}
				}//end loader check
				
				
			return false;
			});	
	
	
	
	
	
	
}

function menu(){
	 //MENU
			$('#menu ul').hide();
			
			if(typeof on !='undefined'){
				
				$('#'+on).addClass('hover');
			}
			$('#menu').hover(function(){$('#slideshow').css('zIndex',-1);},function(){$('#slideshow').css('zIndex','0');});
			 $('#menu li').hover(
								
							  function() {
								 	
									
								   $(this).find('ul:first').show();
								   $(this).find('a:first').addClass('hover');
								 
							  },
							  
							function() {
								
								   $(this).find('ul:first').hide();
								   $(this).find('a:first').removeClass('hover');
								   if(typeof on !='undefined')
								   	$('#'+on).addClass('hover');
							  }
												
							
							).click(function() {
								   $(this).find('ul:first').toggle();
								  
			 });
	
}
function slideshow(){



 var $links= $('#slideshow a');
			if ( $links.length > 1 ){
			setInterval( "slideSwitch()", 4000 );
			}//animate if more than 1	


	
}

function statusButton(){
	
	$('#status-button').click(function(){
		
		$('input[name=enabled]').filter(':checked').siblings('input:eq(0)').click();
		
		$('#submit-button').click();
		
	}
	);
}
$(document).ready(
			function() {
			
			//ie fix
			$('#breadcrumb li:last').addClass('last-child');
			
			
			newsletter();
			multiple();
			$('input[@title]').input_fb();
			
			menu();
			/*calendar*/
			//$('#jcalendar').jcalendar();
			$('fieldset.jcalendar','#calendar').jcalendar();
			calendar();
			$('.jcalendar td.today a','#calendar').click();
			
			/*end calendar*/
			slideshow();
			//fix course blocks
			$('.course:odd ','#courses-latest').find('.oi').css('border-left','none');
			$('.course:odd ','#courses-result').find('.oi').css('border-left','none');
			
			//date pickers
			$('.date-picker').datepicker({yearRange:'-100:+100',dateFormat:'yy-mm-dd'});
			
			//Validation
			courseRegister();
			memberRegister();
			trainerValidate();
			courseValidate();
			statusButton();
			contact();
			
			$('a.lightbox').lightBox(
				{
					imageLoading: 'images-ar/loading_mid.gif',
					imageBtnClose: 'images-ar/close.gif',
					overlayOpacity: 0.4
				}
			);
			
		/*$('.editor').wysiwyg({
				controls : {
				separator04 : { visible : true },
				justifyLeft: { visible : true },
				justifyRight: { visible : true },
				 justifyCenter : { visible : true },
				justifyFull: { visible : true },
				insertOrderedList : { visible : true },
				insertUnorderedList : { visible : true },
				html : { visible : true },
				 colorSep : { separator : true },
					color : {
					visible   : true,
					exec      : function() { alert($(this.editor).documentSelection()); },
					className : 'color'
				}

					}
				}
		);
		$('#colorpicker').farbtastic(function callback(color){
					selected=$('#IFrame').documentSelection();
					
					});
		$('#pickcolor').click(function(){$('#colorpicker').show();});*/
		
		$('table.listing tr').not('thead').hover(function(){$(this).addClass('on')}
									,function(){$(this).removeClass('on')});

			}
		);




