// JavaScript Document
$(window).load(function(){
		$("#findpersonalFieldSlide,#regworkFieldSlide,#regworkAreaSlide").animate({height:"toggle"});
		var m3allem_notification = $.cookies.get( 'm3allem_notification' );  
	if(m3allem_notification!="1"){
		$("#popper").lightbox_me({
		centered: true, 
		overlayCSS:{background: 'white',opacity: .6},
		closeSelector:".close",
		onLoad: function() {  
			
			$.ajax({
		   type: "POST",
		   url: "notification.php",
		 //  data:"tvc="+swf,
		   success: function(msg){//alert(msg)
				  if(msg==0){}
				  else{
					 $("#popper").html(msg);
					var h;
					//$("#response").wrap('<div class="pop_wrapper" />');
					 
						h=$("#response").height();
					 
					$(".pop-border").css({height:160,padding:'inherit'})
					$(".close").bind('click',function(){$(this).trigger('close'); return false})
				  }
		   		}
			})
			
		} 
		})	
	}
})
$(document).ready(function(){
 
					   
function verifyEmail(email){
  
var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
     if (email.search(emailRegEx) == -1) {
           
		  return false;
     }
     else {
          return true;
     }
}
function GetCurrentPageName() {
//method to get Current page name from url.
//return page name in small .
var sPath = window.location.pathname;
var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
return sPage.toLowerCase()
}
 if(GetCurrentPageName()=="" || GetCurrentPageName()=="index.php" || GetCurrentPageName()=="index"){
}//else $.scrollTo($('.bigBox'), {duration: 500});

function getPopHeight(){
	var height=$(window).height();
	return height-300;
}
////////COOKIES//////
var m3allem_ps = $.cookies.get( 'm3allem_ps' );  
if(m3allem_ps=="close"){$(".dialogueNotes").remove();}
else{	$(".dialogueNotes").load("note_onLoad.php")}
//$.cookies.del( 'm3allem_ps' ); 

////-------------CONTACT FORM -----------//////////
$("#contactus").click(function(){
	$("#popper").lightbox_me({
		centered: true, 
		overlayCSS:{background: 'white',opacity: .6},
		closeSelector:".close",
		onLoad: function() {  
			
			$.ajax({
		   type: "POST",
		   url: "contactus.php",
		 //  data:"tvc="+swf,
		   success: function(msg){//alert(msg)
				  if(msg==0){}
				  else{
					 $("#popper").html(msg);
					var h;
					//$("#response").wrap('<div class="pop_wrapper" />');
					 
						h=$("#response").height();
					 
					$(".pop-border").css({height:h+20,padding:'inherit'})
					$(".close").bind('click',function(){$(this).trigger('close'); return false})
				  }
		   		}
			})
			
		} 
	})	
	return false;
								   
})

$("#regworkFieldSlide ul li a").click(function(){
	$("#regworkFieldSlide ul li a.selected").removeClass("selected");
	$(this).addClass("selected");
	$("input#workfield_reg").val($(this).attr("rel"))
	return false;
						 
})
$("#findworkFieldSlide ul li a").click(function(){
	$("#findworkFieldSlide ul li a.selected").removeClass("selected");
	$(this).addClass("selected");
	$("input#workfield_find").val($(this).attr("rel"))
	return false;
						 
})
function dump(arr,level) {
	var dumped_text = "";
	if(!level) level = 0;
	
	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";
	
	if(typeof(arr) == 'object') { //Array/Hashes/Objects 
		for(var item in arr) {
			var value = arr[item];
			
			if(typeof(value) == 'object') { //If it is an array,
				dumped_text += level_padding + "'" + item + "' ...\n";
				dumped_text += dump(value,level+1);
			} else {
				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			}
		}
	} else { //Stings/Chars/Numbers etc.
		dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
}
/////------------SEARCH FORM----------------///////////
$("map[name=Map_find] area").click(function(){
	$('.active_area_find').data('maphilight',{alwaysOn:false}).trigger('alwaysOn.maphilight');
	$(this).addClass('active_area_find').data('maphilight',{alwaysOn:true}).trigger('alwaysOn.maphilight');	
 	$("input#region_find").val($(this).attr("rel"));
	return false;
})

$('#m3allem_find').bind('submit', function(e) {
			//var n = $(this).attr("rel");
			//$("#preview").html('<img src="loader.gif" alt="Updating...."/>');
			e.preventDefault(); // <-- important
			$(this).ajaxSubmit({
				//target: '#preview',
				beforeSubmit:  findRequest,
				success:findResponse,
				//clearForm:true,
				resetForm:true
			});
		});
function findRequest(formData, jqForm, options) { 

} 
function findResponse(responseText, statusText, xhr, $form)  { 
// alert(responseText);
 $("#searchResults").html(responseText);

 $.scrollTo($("#searchResults"), {duration: 500}) 
$("#findworkFieldSlide ul li a.selected").removeClass("selected");
data={};
 data.alwaysOn = false;

$("map[name=Map_find] area").trigger('alwaysOn.maphilight');
$('.active_area_find').data('maphilight',{alwaysOn:false}).trigger('alwaysOn.maphilight');
$("input#workfield_find").val("")
 $("input#region_find").val("")
} 
////---------REGISTRATION FORM -------------///////////

$("map[name=Map_reg] area").click(function(){
            
$('.active_area_reg').data('maphilight',{alwaysOn:false}).trigger('alwaysOn.maphilight');
$(this).addClass('active_area_reg').data('maphilight',{alwaysOn:true}).trigger('alwaysOn.maphilight');
 $("input#region_reg").val($(this).attr("rel"))
// $("input#country_reg").val("Lebanon")
return false; /// might conflict with inline script function

})

$('#m3allem_register').bind('submit', function(e) {
			//var n = $(this).attr("rel");
			//$("#preview").html('<img src="loader.gif" alt="Updating...."/>');
			e.preventDefault(); // <-- important
			$(this).ajaxSubmit({
				//target: '#preview',
				beforeSubmit:  regRequest,
				success:regResponse,
			//	clearForm:true,
				resetForm:true
			});
		});
	var form;
	function regRequest(formData, jqForm, options) { 
  //  var queryString = $.param(formData); 
  form= jqForm ;
 
 		var valid = true;
		form.find('span.warn').remove();
		form.find('span.error-message').hide();
		form.find('.req').each(function() {
			if ($(this).val() == "") {
				$(this).after('<span class="warn">Required!</span>');
				valid = false;
			} else if ($(this).hasClass('req-email') && !verifyEmail($(this).val())) {
				$(this).after('<span class="warn">Invalid E-mail!</span>');
				valid = false;
			} else if ($(this).hasClass('req-num') && !checknum($(this).val())) {
		    $(this).after('<span class="warn">Invalid! 10 Digit Phone Number Required.</span>');
		    valid = false;
		  }
		});
		var noreg=0;
		form.find('.twin').each(function() {
			var zid=$(this).attr('id')
			if(zid=='region_reg' && $(this).val()=="") {noreg++;}
			if(zid=='arabregion_reg' && $(this).val()=="") {noreg++;}
			 
		})
		if(noreg==2) $("#region_reg").after('<span class="warn">Required!</span>');
		
		if (!valid) {
			//$(this).find('span.error-message').text("Please correct the above errors.").show();
			$.scrollTo(form, {duration: 500});
			return false;
		}
		return true;
} 
function regResponse(responseText, statusText, xhr, $form)  { 

	if($(".thankyou_reg").length==0)
	{
		$("#popper").load("thankyouReg.php",function(){
				$("#m3allem_register").prepend($("#popper").html())
				$(".thankyou_reg").animate({height:80});
				
				$("#popper").html("")
			});
	}
	$.scrollTo(form, {duration: 500});
	$("#regworkFieldSlide ul li a.selected").removeClass("selected");
	data={};
	 data.alwaysOn = false;
 
	$("map[name=Map_reg] area").trigger('alwaysOn.maphilight');
	$('.active_area_reg').data('maphilight',{alwaysOn:false}).trigger('alwaysOn.maphilight');
	$("input#workfield_reg").val("")
	$("input#region_reg").val("")
	
//$("#listof").prepend(responseText)
} 

$(".getFieldList").click(function(){
	var sliderId=$(this).attr("rel");
	var h = $("#"+sliderId).children("ul").height();
	$("#"+sliderId).stop(false,true).animate({height:"toggle"})
	return false;
	})


try {
		oHandler = $(".countries").msDropDown().data("dd");
		//oHandler.visible(true);
		//alert($.msDropDown.version);
		//$.msDropDown.create("body select");
		$("#ver").html($.msDropDown.version);
		} catch(e) {
			alert("Error: "+e.message);
		}

});
