var toSearch;
var isCat = false;
var iniLimit = 0;
var increment = 10;
var finLimit = increment;
var pageNumber = 0;
var totalPages = 0;
var clientId = 0;

var params = {
	firstday:0,
	dtype:'MM/dd/yyyy',
	width:275,
	windoww:300,
	windowh:200,
	border_width:0,
	border_color:'White',
	headerstyle: {
		type:"buttons",
		css:'clsDayName',
		imgnextm:'img/forward.jpg',
		imgprevm:'img/back.jpg'
	},
	monthnames:["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
	daynames:["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"]
};
t=false;


function showCalendarSmall(month, year){
  dojo.xhrPost({
    url: "calendar/calendar_small.php",  
    handleAs: "text",
	content:{curMonth:month, curYear:year},
	load: function(responseObject, ioArgs) {
	  dojo.byId("calendarSmall").innerHTML = responseObject;
      return responseObject;
    }
  });
}

function updateCalendarSmall(x){
        var nick = dojo.byId('month_now').value;
        var joel = dojo.byId('year_now').value;
        if (x > 0)
        {
          nick++;
        }
        else
        {
          nick--;
        }
        showCalendarSmall(nick, joel);
}

function showCalendar(month, year){
  dojo.xhrPost({
    url: "calendar/calendar.php",  
    handleAs: "text",
	content:{curMonth:month, curYear:year},
	load: function(responseObject, ioArgs) {
	  dojo.byId("calendar").innerHTML=responseObject;
      return responseObject;
    }
  });
}

function updateCalendar(x){
        var nick = dojo.byId('month_now').value;
        var joel = dojo.byId('year_now').value;
        if (x > 0)
        {
          nick++;
        }
        else
        {
          nick--;
        }
        showCalendarSmall(nick, joel);
}


function showCalendarAddForm(){
        window.open('calendar/calendar_add.php','Form', 'height=600, width=600, resizable=yes, scrollbars = yes');
}
function showCalendarDisplay(id){
        window.open('calendar/calendar_display.php?id='+id,'Calendar', 'height=580,width=650,resizable=yes,scrollbars=yes');
}
function showCalendarDayDisplay(id){
        window.open('calendar/calendar_daydisplay.php?hoy='+id,'Calendar', 'height=580, width=650, resizable=yes, scrollbars = yes');
}

function showGallery(announcementId){
	window.open('common/gallery.php?adId='+announcementId, 'Photos', 'fullscreen = yes, resizable = yes, scrollbars = yes');
}

function showRSSFeed(name,url){
  dojo.xhrPost({
    url: "actions/showRSSFeed.php",  
    handleAs: "text",
	content:{name:name, url:url},
	load: function(responseObject, ioArgs) {
	  dojo.byId("RSSfeeds").innerHTML=responseObject;
      return responseObject;
    }
  });
}

function getCategories(){
  dojo.xhrPost({
    url: "actions/getCategories.php",  
    handleAs: "text",
	content:{},
	load: function(responseObject, ioArgs) {
	  dojo.byId("categories").innerHTML=responseObject;
      return responseObject;
    }
  });
}

// functions for the calendar popup
function ie_ua(){
	t=ua;
}
function show(id){
	var c1 = new CodeThatCalendar(params);
	c1.popup(id);
}

// functions for the classified ads

function searchIniAds(){
    // fadeIn('beefheaderright');
	// fadeIn('beefheaderrightbot');
	isCat = false;
	pageNumber = 0;
	iniLimit = 0;
	finLimit = 5;
	toSearch = "";
	clientId = 0;
	searchAds();
}
function searchByKey(){
    // fadeIn('beefheaderright');
	// fadeIn('beefheaderrightbot');
	isCat=false;
	var key = dojo.byId('txtSearch').value;
	pageNumber = 0;
	iniLimit = 0;
	finLimit = increment;
	toSearch = key;
	clientId = 0;
	searchAds();
}
function searchByCatId(id){
    // fadeIn('beefheaderright');
	// fadeIn('beefheaderrightbot');
	isCat = true;
	toSearch = id;
	pageNumber = 0;
	iniLimit = 0;
	finLimit = increment;
	clientId = 0;
	searchAds();
}
function searchByClient(id){
	// fadeIn('beefheaderright');
	// fadeIn('beefheaderrightbot');
	isCat = false;
	toSearch = "";
	pageNumber = 0;
	iniLimit = 0;
	finLimit = increment;
	clientId = id;
	searchAds();
}
function searchAds(){
	// fadeIn('indicador');
	var ini = 0;
	var fin = 0;
	if (pageNumber == 0){
		ini = iniLimit;
		fin = finLimit;
	}
	if (pageNumber >= 1){
		ini = pageNumber * increment;
		fin = increment;
	}
	if (toSearch == "" || toSearch == null){
		toSearch = "";
	}
	// getTotalPages();
	// dojo.io.bind({url:'actions/searchAds.php', method:'post', content:{isCat:isCat, toSearch:toSearch, clientId:clientId, iniLimit:ini, finLimit:fin}, handler:searchFound});	
    dojo.xhrPost({
      url: "actions/searchAds.php",  
      handleAs: "text",
	  content:{isCat:isCat, toSearch:toSearch, clientId:clientId, iniLimit:ini, finLimit:fin},
	  load: function(responseObject, ioArgs) {
	    dojo.byId("classifieds").innerHTML=responseObject;
        return responseObject;
      }
    });
}

function showInsertAdForm(){
	window.open('actions/showInsertAdForm.php','Formulario', 'height=550, width=550, resizable=yes, scrollbars = yes');
}


// admin functions

function showStaff(){
  dojo.xhrPost({
    url: "../actions/showStaffTable.php",  
    handleAs: "text",
	content:{},
	load: function(responseObject, ioArgs) {
	  dojo.byId("staffTable").innerHTML=responseObject;
      return responseObject;
    }
  });
}

function showNews(){
  dojo.xhrPost({
    url: "../actions/showNewsTable.php",  
    handleAs: "text",
	content:{},
	load: function(responseObject, ioArgs) {
	  dojo.byId("newsTable").innerHTML=responseObject;
      return responseObject;
    }
  });
}

function showCalendarTable(){
  dojo.xhrPost({
    url: "../actions/showCalendarTable.php",  
    handleAs: "text",
	content:{},
	load: function(responseObject, ioArgs) {
	  dojo.byId("calendarTable").innerHTML=responseObject;
      return responseObject;
    }
  });
}


function showCalendarAdmin(month, year){
  dojo.xhrPost({
    url: "../calendar/calendar.php",  
    handleAs: "text",
	content:{curMonth:month, curYear:year},
	load: function(responseObject, ioArgs) {
	  dojo.byId("calendar").innerHTML=responseObject;
      return responseObject;
    }
  });
}

function updateCalendarAdmin(x){
        var nick = dojo.byId('month_now').value;
        var joel = dojo.byId('year_now').value;
        if (x > 0)
        {
          nick++;
        }
        else
        {
          nick--;
        }
        showCalendarSmall(nick, joel);
}

function showCalendarAddFormAdmin(){
        window.open('../calendar/calendar_add.php','Form', 'height=590, width=600, resizable=yes, scrollbars = yes');
}

function emailContact(){
	var errors = 0;
	var fName = "Contact Us Submission";

	var cName = dojo.byId("contactName").value;
        if (cName == "")
          errors++;
	var cEmail = dojo.byId("contactEmail").value;
        if (cEmail == "")
          errors++;
	var cPhone = dojo.byId("contactPhone").value;
        if (cPhone == "")
          errors++;
	var cSubject = dojo.byId("contactSubject").value;
	var cMessage = dojo.byId("contactMessage").value;
	
  if (errors > 0) {
    alert('One or more of the required fields is blank.');
    return;
  }

    dojo.xhrPost({
      url: "email.php",
      handleAs: "text",
  	  content:{formName:fName, contactName:cName, contactEmail:cEmail, contactPhone:cPhone, contactSubject:cSubject, contactMessage:cMessage},
	  load: function(responseObject, ioArgs) {
	    dojo.byId("mainContent").innerHTML=responseObject;
        return responseObject;
      }
    });
}
function emailInvolved(){
	var errors = 0;
	var fName = "Volunteer Submission";

	var cName = dojo.byId("volunteerName").value;
        if (cName == "")
          errors++;
	var cEmail = dojo.byId("volunteerEmail").value;
        if (cEmail == "")
          errors++;
	var cPhone = dojo.byId("volunteerPhone").value;
        if (cPhone == "")
          errors++;
	var cMessage = dojo.byId("volunteerMessage").value;
	
  if (errors > 0) {
    alert('One or more of the required fields is blank.');
    return;
  }

    dojo.xhrPost({
      url: "email.php",
      handleAs: "text",
  	  content:{formName:fName, Name:cName, Email:cEmail, Phone:cPhone, Message:cMessage},
	  load: function(responseObject, ioArgs) {
	    dojo.byId("mainContent").innerHTML=responseObject;
        return responseObject;
      }
    });
}

function showNewsComments(id) {
    dojo.xhrPost({
      url: "actions/showNewsComments.php",
      handleAs: "text",
  	  content:{newsId:id},
	  load: function(responseObject, ioArgs) {
	    dojo.byId("newsComments").innerHTML=responseObject;
        return responseObject;
      } 
    });
}
function addNewsComment(id) {
	var errors = 0;
	var cName = dojo.byId("commentName").value;
	if (cName == "")
 	  errors++;
	var cEmail = dojo.byId("commentEmail").value;
	if (cEmail == "")
	  errors++;
	var cComment = dojo.byId("commentComment").value;
	if (cComment == "")
	  errors++;

	if (errors > 0) {
	  alert('One or more of the required fields is blank.');
	  return;
	}

    dojo.xhrPost({
      url: "actions/addNewsComment.php",
      handleAs: "text",
  	  content:{newsId:id, commentName:cName, commentEmail:cEmail, commentComment:cComment},
	  load: function(responseObject, ioArgs) {
	    showNewsComments(id);
	    dojo.byId("commentName").value = "";
	    dojo.byId("commentEmail").value = "";
	    dojo.byId("commentComment").value = "";
	    // dojo.byId("newsComments").innerHTML=responseObject;
        return responseObject;
      }
    });
}
        
