var othra = null;

function checkHover() {
	if (othra) {
		othra.find('ul').fadeOut('fast');	
	}
}
	
$(document).ready(function() {
	$(".calender:first").each(function(){
		$(this).find("ol > li:first-child").addClass("clickedday");
	})
	$(".globtag > ul").tabs();
	$("#gsinput").focus(function() {
	var kl = $("#gsinput").position()
			$("#scope_list").css({top:kl.top, left:kl.left})
		$("#scope_list").show("fast")
	})
	        $(document).bind('click', function(e) {
                var $clicked=$(e.target); 
                if($clicked.is('#scope_list') || $clicked.parents().is('#scope_list') || $clicked.is('#gsinput') ) { 
                        $("#gsinput").focus();
                } else { 
                        $("#scope_list").hide("slow")
                }
        });
	// MEAL DEALS MENU
  $("#dealfilter").find("span").each(function(i) {
	$(this).mousedown(
		function(){
		var z = $(this).position();
			if($(this).siblings("ul").css("display")==="none"){
			$("#dealfilter > ul").find("ul").css("display", "none");
			$("#dealfilter > ul").find("div").css("display", "none");
			$(this).siblings("ul").css({left:z.left-50, top:z.top+27, display:"block"})
			$(this).siblings("div").css({display:"block", left:z.left+($(this).width()/2)-7, top:z.top+20})
			}
			else {
			$("#dealfilter > ul").find("ul").css("display", "none");
			$("#dealfilter > ul").find("div").css("display", "none");
			}
		});
   });
   // END OF MEAL DEALS MENU
	$('.otherart > li').hover(function() {
		$(this).find('ul').css({left:($(this).position().left)+($(this).width())})
		if (othra) {
			othra.find('ul').fadeOut('fast');
			othra = null;
		}
		$(this).find('ul').fadeIn('fast');
	},
	function() {
		othra = $(this);
		setTimeout(
			"checkHover()",
			400);
	});	

 showCase = function() {
	var menu = $("#showcase > ul").find("li")
	var images = $("#showcase > ol").find("li:first-child")
	var sli = parseInt(images.css("height"))
	menu.each(function(i) {
		$(this).mouseover(function() {
			images.css("margin-top", -(sli*i))
			menu.each(function() {
				$(this).removeClass();
			});
			$(this).addClass("active");
		});
	});
};
   
 calender = function() {
 
var firstday = null;
firstday = $(".calender > ol > li:first-child").position();
if (firstday !== undefined){
var menu = $(".calender > ol").find("li")
var daychange = $(".calender > ol")
var daylist = $(".calender > ul")
var marker = $(".mark")

 /*calender marker first positioning*/

 marker.css({top:firstday.top, left:firstday.left, display:"block"})

 
 menu.each(function(i) {
		$(this).mousedown(function() {
		daychange.find(".clickedday").removeClass("clickedday");
		$(this).addClass("clickedday")
		var dayss = $(this).position()
		marker.css({top:dayss.top, left:dayss.left, display: "block"})
		daylist.find(".shown").slideUp("normal").removeClass();
		daylist.find("li:eq("+i+")").slideDown("normal").addClass("shown")
		});
 	});
	}
};
 showCase();
 });

	$(window).load(function () {
	$('.calender:first').each(function(){
		calender();
		$(window).resize(function(){
			moveto = $(". > ol").find(".clickedday").position();
			var markercorrection = $(".mark");
			markercorrection.css({left:moveto.left, display:"block"});
		});
	})
});


function showAll(title, show)
{
    var more = $('#'+title+'_morelink');
    var li_arr = $("li[id*='"+title+"']");
    $(li_arr).each(function(i){
        var item = $(this);
        if(show == 1)
           item.attr("style", "display:block;");
        else
           item.attr("style", "display:none;");
           
    });
    if(show == 1){
        more.attr("style", "display:none;");
        $('#'+title+'_hidelink').attr("style", "display:block;");
    }
    else
    {
        more.attr("style", "display:block;");
        $('#'+title+'_hidelink').attr("style", "display:none;");        
    }
}

function toggleText(obj, txt)
{
    if(obj.value == txt)
       obj.value = '';
    else
    if(obj.value == '')
       obj.value = txt;
}
function searchform_check(form)
{
    if($("#sf_query").length > 0 && $('#sf_query').attr('value') == 'Name') {
        //Setting it to blank seems to cause a routing problem
        $('#sf_query').attr('value', 'none');
    }
    if($("#start_date").length > 0){
    	if($('#start_date').attr('value') == 'Start date')
            $('#start_date').attr('value', 'none');
        else
            $('#start_date').attr('value', revDate($('#start_date').attr('value')));        
    }
    if($("#end_date").length > 0){
        if($('#end_date').attr('value') == 'End date')
            $('#end_date').attr('value', 'none');
        else
            $('#end_date').attr('value', revDate($('#end_date').attr('value')));
    }    
    
    document.forms['gigsearch'].submit();
}

function openPicWindow(wPage,width,height,resize) {
  window.open(wPage,'window','toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=' + resize + ',channelmode=no,directories=no,width=' + width + ',height=' + height + ',top=10,left=10');
}
function validateEmailfrm(){
  
  if($('#sender').val()=='' ||$('#from').val()==''||$('#to').val()=='')
  	{ 
  		alert('Please complete all the required fields before submitting');
  		return false;
  	} 
  else if(!validateEmail3($('#from')))
  {  		
  		$('#from').focus();
  		return false;
  }
  else if($('#to').val()!='')
  {		
  		var valExpression = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  		
  		str= $('#to').val().split(",");
		for(var i=0; i < str.length; i++)
		{
		if(!valExpression.test(str[i]))
			{
				alert('Invalid email address :'+str[i]);
				$('#to').focus();
	  			return false;
  			}	
		}
  		
  		
  }	 	
 		 	
  return true;
  	 
}
function validateEmail3(t)
{
  var valExpression = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
 
  if(!(valExpression.test(t.val())))
  {	
  		alert('Invalid email address :'+t.val());
  		return false;
  }		
    return true;
}
function validateCommentfrm(frm){
 
  if($('#sender').val()=='' ||$('#from').val()=='')
  	{ 
  		alert('Please complete all the required fields before submitting');
  		return false;
  	} 
  else if(!validateEmail3($('#from')))
  {  		
  		$('#from').focus();
  		return false;
  }
  els = document.comment.elements; 
  flag = 0;
  
  for(var i=0;i<els.length;i++)
	 { 
	   if(els[i].type=="radio"||els[i].type=="select-one"||els[i].type=="hidden")
	   { 
	    if((els[i].name).indexOf('rpt_')==0)
	     {
	     var x= document.getElementsByName(els[i].name);
	        for (var j=0;j<x.length;j++)
		     {
		     	if(els[i].type=="radio")
			      {  
			      	if(x[j].checked)
			            {
			             	flag=1;
			             	break;   
			            }
		          } 
		        else if(els[i].type=="hidden")
		        {
		        	if(x[j].value!='')
			            {
			             	flag=1;
			             	break;   
			            }
		        } 
		        else if(els[i].type=="select-one")
		        {
			        if(x[j].value!='-1')
				            {
				             	flag=1;
				             	break;   
				            } 
			    }        
		    }
		     i=i+(x.length)-1;
	     }//end index of
	    } 
	    	
	}
	
if(flag==0)
    {
    	if(jQuery.trim($('#msg').val())=='')
    		{    		
    		alert('Please add your comments /ratings ');
    		return false;
    		}
    }
 if($('#seccode').val()=='')
  	{ 
  		alert('Please enter the security code');
  		return false;
  	}		
  return true;  	 
}

function displayblock(obj,field){	
	if(obj.checked)
		$('#'+field+'').attr("style", "display:block;");	
	else
		$('#'+field+'').attr("style", "display:none;");
}


function validateEventfrm(){	
	obj = document.event.does_your_event_repeat;	
	if($('#event_name').val() == '' || $('#event_type').val() == -1 
	|| $('#date_of_the_event').val() == '' || $('#time_of_the_event').val() == ''
	|| (obj.checked && ($('#how_often_does_your_event_happen').val() == ''))
	|| $('#price').val() == '' || $('#event_description').val() == '' 
	|| $('#venue_name').val() == '' || $('#address').val() == ''
	|| $('#telephone_for_publication').val() == '' || $('#contact_name').val() == ''
	|| $('#contact_telephone').val() == '' || $('#contact_email').val() == ''){
		alert('There was a problem with your submission. Please ensure that you complete all required (*) fields.');
		return false;
	}
	if(!validateEmail3($('#contact_email')))
  	{  	  		
  		$('#contact_email').focus();
  		return false;
  	}	
	return true;
}

function check_poll(formobj, campaign)
{
	els = formobj.elements;
	err_flag = 0;
    for(var i=0;i<els.length;i++)
     { 
       if(els[i].type=="radio")
       { 

         var x= document.getElementsByName(els[i].name);
            for (var j=0;j<x.length;j++)
             {
                if(els[i].type=="radio")
                  {  
                    if(x[j].checked)
                        {
                            err_flag=1;
                            break;   
                        }//if
                  } //if
    
            }//for
             i=i+(x.length)-1;
         
        } //if
            
    }//for
    if(err_flag == 0)
    {
    	alert('Please select atleast one option.');
    	return false;
    }//if
    
    if(campaign == 0){
        formobj.action.value='vote';
        formobj.submit();
    }
    else{
        
        document.getElementById('camp_email_id').value = '';
	    /*$("#camp_email_div").attr("style", "display:block;");
	    $("#camp_content").attr("style", "display:block;");*/
	    $("#camp").attr("style", "display:block;");        
    }
}

function register_poll_user(frmobj)
{
	var email = frmobj.camp_email_id.value;
	if(email == "")
	{
		alert('Please enter a valid email address.');
		frmobj.camp_email_id.value = "";
		frmobj.camp_email_id.focus();
		return false;
	}//if
	
    if(validateEmail3($('#camp_email_id')) == false)
    {
        frmobj.camp_email_id.value = "";
        frmobj.camp_email_id.focus();
        return false;
    }//if	
    
    $.ajax({
   type: "POST",
   url: "/polls",
   data: "ajax_poll_id="+$('#poll_id').val()+"&ajax_email="+email,
   success: function(msg){
     if(msg == '1')
     {
     	alert('Sorry.You cannot vote more than once.');
     	close_poll_email();
     	return false;
     }//if
     else
     {
	    document.getElementById('camp_user_email').value = email;
	    check_poll(document.poll, 0);     	
     }
   }
 });
}

function close_poll_email()
{
	$("#camp").attr("style", "display:none;");
	/*$("#camp_email_div").attr("style", "display:none;");
	$("#camp_content").attr("style", "display:none;");*/
}

function revDate(str) { 
   var retStr = ""; 
   var dateBits = str.split('/');
   retStr = dateBits[2] + '/' + dateBits[1] + '/' + dateBits[0]
   return retStr; 
} 

function check_calendar(obj)
{
	var selindex = obj.selectedIndex;
	var selval = obj[selindex].value;
	if(selval == 'calendar')
	{
        $("#caldiv").attr("style", "display:block;");		  
	}
	else
	{
		$("#caldiv").attr("style", "display:none;"); 
	}
}

function refreshOrder(obj)
{
	var selval = obj[obj.selectedIndex].value;
	if(selval == -1)
	   selval = 'name';
	   
    var newQueryString = '';
    
    if (location.href.indexOf('&order') > -1)
    {
    		newQueryString =  location.href.replace(/(order=.*)/, "order="+selval);   
    }
    else if (location.href.indexOf('?order') > -1)
    {
         newQueryString =  location.href.replace(/(order=.*)/, "order="+selval);            	
    }
    else{
        newQueryString = location.href + '?order=' + selval;
    }
    location.href = newQueryString;
}

function validateNewsletterfrm(frm,field)
{   
	if(!validateEmail3($('#register_email')))
  	{  		
  		$('#register_email').focus();
  		return false;
  	}
  	
  	//els = document.nl_subscribe.elements;
  	els = field.elements;
  	err_flag = 0;
    for(var i=0;i<els.length;i++)
    { 
    	if(els[i].type=="checkbox")
    	{ 
       		var x= document.getElementsByName(els[i].name);
            for (var j=0;j<x.length;j++)
            {
            	if(els[i].type=="checkbox")
                {                  	
                	if(x[j].checked)
                    {
                    	err_flag=1;
                        break;   
                	}//if
                } //if    
            }//for
            i=i+(x.length)-1;         
        } //if           
    }//for
  	if(err_flag == 0){
  		alert('Select one of the newsletters before submitting!');
  		return false;
  	}
	return true;
}

function redirectSearch()
{
	var query = $('#gsinput').val();

	
	if(query == 'Search'){
		query = '';
	}	

	
	var str = location.href ;
	var re = new RegExp('^(?:f|ht)tp(?:s)?\://([^/]+)', 'im');
	var server = str.match(re)[1].toString();
	var qs = '';
	
	if(query != ''){
		qs = '?query='+query;
	}
			
	

		window.location = 'http://'+ server+ '/search'+qs;
		return true;
}

function checkSeoForm(module_id)
{
	var frm_obj = document.getElementById('seo_form_'+module_id);
	
	if(frm_obj.seo_metadata_id.value == '' || frm_obj.seo_module_id.value == ''){
	   alert('No metadata information set for this module');
	   return false;
	}
	
	if(frm_obj.seo_tags.length>0)
	{
		var chk_flag = false;
		for (var i = 0; i < frm_obj.seo_tags.length; i++){
			if(frm_obj.seo_tags[i].checked)
			 chk_flag = true;
		}//for
		
		if(chk_flag == false)
		{
			alert('Please select atleast one checkbox');
			return false;
		}//if
	}//if
	
	frm_obj.seo_action.value = 'save';
	return true;
}
	