var select_poll = 0;
var searchString = "";

function sendPoll(url,id){	
	if (select_poll == 0)
	{
		alert('Please choose your option.');
	}
	else
	{
		$('#poll_cover').html('<img src=\'images/b.gif\' width=\'235\' height=\'87\' id=\'loading\' />');
		$.post('orangecounty/index.php/poll/', { "answer_id": select_poll}, function(data) { 
												  $("#poll_cover").html(data);												
											});
	}
}

function gup(name)
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

function changePage(obj, url)
{	
	if (isNaN(obj))
		var page = parseInt(obj.text)-1;
	else
		page = obj;
	
	/*$('#poll_cover').html('<img src=\'images/b.gif\' width=\'235\' height=\'87\' id=\'loading\' />');*/
	$.post(url, { "page": page, "act" : "page"}, function(data) { 
											if (searchString != "")
											{
											  $("#update_page").html(data);
												searchString = $("#txtSearch").val()
												arrWords = searchString.split(' ')
												for (var i=0; i<arrWords.length; i++)
												{
													if (trim(arrWords[i]) != '')
												  		$("#update_page").highlight(arrWords[i]);
												}
											}
											else											  
											  $("#update_page").html(data);
											  window.scrollTo(0,0);
										});	
}

function orderPage(url, type)
{	
	$.post(url, { "type": type, "act" : "order"}, function(data) { 
											  $("#update_page").html(data);
										});	
}

function changeState(state_id)
{	
	$.post('index.php/city/', { "state_id": state_id, "act" : "change_state"}, function(data){ 
											  $("#city").html(data);
										});	
}

function city_search()
{
	$.post('index.php/city/', { "city_id": $("#city").val(), "state_id": $("#state").val(), "category_id": $("#category").val(), "act" : "search"}, function(data) { 
											  $("#update_page").html(data);
										});	
}

function article_search(url)
{
	$.post(url, { "search": $("#txtSearch").val(), "act" : "search"}, function(data) { 
												$("#update_page").html(data);
												searchString = $("#txtSearch").val()
												arrWords = searchString.split(' ')
												for (var i=0; i<arrWords.length; i++)
												{
													if (trim(arrWords[i]) != '')
												  		$("#update_page").highlight(arrWords[i]);
												}
										});	
}

function interview_search()
{
	$.post('index.php/interviews/', { "category_id": $("#category").val(), "act" : "search"}, function(data) { 
											  $("#update_page").html(data);
										});	
}

function farmer_search()
{
	$.post('index.php/farmer-market/', { "name": $("#city").val(), "act" : "search"}, function(data) { 
											  $("#update_page").html(data);
										});	
}

function changePerson(id)
{	
	$.post('index.php/about-us/', { "emp_id": id, "act" : "person"}, function(data) { 
											  $("#update_page").html(data);
										});	
}

function submit_email()
{
	if (isEmail($("#email").val()))
	{
		/*$.post('newsletter/', { "email": $("#email").val()}, 
										function(data) { 
											 if (data.indexOf("fail") != -1)
											 {
												 $('.error1').html("The email already exists.");
												$('.error1').show();												
											 }
											 else
											 {												 
												   $('.error1').html("Thank you for registering.");
												$('.error1').show();												
												$("#email").val("");
											 }
										});*/
		document.inputForm.Mode.value='update'; document.inputForm.submit ();
	}
	else
	{
		$('.error1').html("Invalid email. Please input again.");
		$('.error1').show();
	}
}

function isEmail(s){

	if (s.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]{2,4}$/) != -1)
		return true ;
		
	return false ;

}

function trim(str) {
	return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

function checkLogin() 
{
	$.post('event-register/', { "email": $("#email").val(), "password1" : $("#password1").val(), "act" : "login"}, function(data) { 
											  if (data.indexOf('success') == -1)
											  {
											  	$("#err_notify").html('Email or password is incorrect.');
											  }
										});	
}

function select_category(cate_id, from)
{
	if (from != '')
	{
		$.post('job-board/', { "category_id": cate_id, "act" : "category", "from" : from}, function(data) { 
											  $("#update_category").html(data);
										});	
	}
	else
	{
		$.post('job-board/', { "category_id": cate_id, "act" : "category", "from" : from}, function(data) { 
											  $("#update_page").html(data);
										});	
	}
}

function job_search()
{	
	$.post('job-board/', { "city": $("#city").val(), "state": $("#state").val(), "search": $("#txtSearch").val(), "act" : "search"}, function(data) { 
											  $("#update_page").html(data);
										});	
}

function getSubCategory(category)
{
	$.post('index.php/send-your-business/', { "category": category, "act" : "change_state"}, function(data){ 
											 if (data.indexOf("none") == -1)
											 {
											  	$("#sub_category").html(data);
												$("#subcate_row").css({'display':'table-row'});
												$("#sub_category").addClass("required");
											 }
											 else
											 {												
											 	$("#subcate_row").css({'display':'none'});
												$("#sub_category").removeClass("required");												
											 }
										});	
}

function showGallery(id)
{		
	caobox.show('loading');
	$.post('gallery/', { "gallery_id": id, "act" : 'update_gallery'}, function(data) { 																			   											  $(".gallery_full").html(data);
											  $(".b_gallery_list li a").removeClass("active");	
											  $("#gallery"+id).addClass("active");	
											  caobox.hide();
										});	
}

function showGalleryDetail(id)
{		
	caobox.show('loading');
	$.post('gallery/', { "gallery_id": id,  "act" : 'update_content'}, function(data) { 																			   											 $(".overal").html(data);	
												if (bigBannerAuto) 	 clearTimeout(bigBannerAuto);
												if (smallBannerAuto) clearTimeout(smallBannerAuto);
												if (ctentBannerAuto) clearTimeout(ctentBannerAuto);																																			
												bigBannerTempI = 0;
												bigBannerTempY = 0;
												bigBannerCount = 0;											
												smallBannerTempI = 0;
												smallBannerCount = 0;												
												ctentBannerTempI = 0;
												ctentBannerCount = 0;
												
												
												if ($('.bigBanner_home').html() == null) isHome = false;
												else isHome = true;
												if ($('.rotateBanner').html() != null){
													bigBanner();
													bigBannerPaging();
													bigBannerBtns();
												}												
												smallBanner();
												ctentBanner();		
												caobox.hide();
										});	
}

function updateCalendar(day,month,year)
{		
	caobox.show('loading');
	$.post('calendar/', { "day": day, "month": month, "year": year, "act" : 'update_calendar'}, function(data) { 																			   											  $(".b_upcomingEvent").html(data);
												caobox.hide();																																													
																																																										
										});	
}

function showEvent(day, weekly)
{		
	caobox.show('loading');
	$.post('calendar/', { "dayOfMonth": day, "weekly" : weekly, "act" : 'show_event'}, function(data) { 																			   											  $(".b_upcomingEvent").html(data);
												caobox.hide();																																																									
										});	
}

function updatePage(page)
{
	caobox.show('loading');
	$.post(page+'/', { "act" : 'update_content'}, function(data) { 		
												$(".overal").html(data);
												
												if (bigBannerAuto) 	 clearTimeout(bigBannerAuto);
												if (smallBannerAuto) clearTimeout(smallBannerAuto);
												if (ctentBannerAuto) clearTimeout(ctentBannerAuto);
												if (pasBannerAuto) clearTimeout(pasBannerAuto);
												bigBannerTempI = 0;
												bigBannerTempY = 0;
												bigBannerCount = 0;											
												smallBannerTempI = 0;
												smallBannerCount = 0;												
												ctentBannerTempI = 0;
												ctentBannerCount = 0;
												
												
												if ($('.bigBanner_home').html() == null) isHome = false;
												else isHome = true;
												if ($('.rotateBanner').html() != null){
													bigBanner();
													bigBannerPaging();
													bigBannerBtns();
												}												
												smallBanner();
												ctentBanner();
												
												caobox.hide();
												
												if (page=="calendar") init_pasBanner();
										});	
}

function changeCalendar(day, year)
{
	caobox.show('loading');
	$.post('calendar/', { "act" : 'change_calendar', "link":"calendar", 'dayOfMonth': day}, function(data) { 		
												$(".calendar").html(data);																																				
												caobox.hide();
										});	
}

function linkEvent(day,weekly)
{
	caobox.show('loading');
	$.post('calendar/', { "act" : 'update_content', "link":"event", "dayOfMonth": day, "weekly" : weekly}, function(data) { 		
												$(".overal").html(data);
												
												if (bigBannerAuto) 	 clearTimeout(bigBannerAuto);
												if (smallBannerAuto) clearTimeout(smallBannerAuto);
												if (ctentBannerAuto) clearTimeout(ctentBannerAuto);																																			
												bigBannerTempI = 0;
												bigBannerTempY = 0;
												bigBannerCount = 0;											
												smallBannerTempI = 0;
												smallBannerCount = 0;												
												ctentBannerTempI = 0;
												ctentBannerCount = 0;
												
												
												if ($('.bigBanner_home').html() == null) isHome = false;
												else isHome = true;
												if ($('.rotateBanner').html() != null){
													bigBanner();
													bigBannerPaging();
													bigBannerBtns();
												}												
												smallBanner();
												ctentBanner();
												
												caobox.hide();
												window.scrollTo(0,500);
										});	
}

function sendReservation()
{
	var options = { 
		success:    function() { 
			$('#post_res').resetForm();
			caobox.show('sentSuccess');
		} 
	}; 
	$('#post_res').ajaxSubmit(options); 
}