var xmlHttp

function sendme() 
{ 
    window.open("","myNewWin","width=550,height=300,toolbar=0"); 
    var a = window.setTimeout("document.form1.submit();",500); 
} 

function onSubmitForm() {
    var formDOMObj = document.frmSend;
    if (formDOMObj.attach1.value == "" || formDOMObj.caption1.value == "" || formDOMObj.description1.value == "")
		{
        alert("Please fill in all the fields so that the information about the picture you wish to upload is complete.  Thank you.");
		returnval = false;
		}
    else
		{
	    window.open("../includes/wait.asp","Upload","width=500,height=400,toolbar=0,resize=1"); 
        returnval = true;
		}
    return returnval;
	}

function trim(inputString) {
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; 
}

function IsEmpty(aTextField) {
	aTextField.value = trim(aTextField.value)
   if ((aTextField.value.length==0) ||
   (aTextField.value==null)) {
      return true;
   }
   else { return false; }
}

function ValidateForm(form){
   if(IsEmpty(form.location_id)) 
   { 
      alert('You have to select the village this GO was working at') 
      form.location_id.focus(); 
      return false; 
   } 
 
   if(IsEmpty(form.act_id)) 
   { 
      alert('Please tell us what the GO was doing') 
      form.act_id.focus(); 
      return false; 
   } 
   if(IsEmpty(form.addday))
   { 
      alert('Please choose a day.') 
      form.addday.focus(); 
      return false; 
   } 
   if(IsEmpty(form.addmonth))
   { 
      alert('Please choose a month.') 
      form.addmonth.focus(); 
      return false; 
   } 
   if(IsEmpty(form.addyear))
   { 
      alert('Please choose a year.') 
      form.addyear.focus(); 
      return false; 
   } 
return true;
} 

function people_activity_delete(go_id,act_id){
	if (confirm('Are you sure you want to delete this position ?'))
		{
		people_activity_list('go_detail_body','act=DELACT&act_id='+act_id+'&go_id='+go_id);
		}
	}

function people_add(fb_id){
	if (confirm('Are you sure you want to add this person to the G.O Database ?'))
		{
		people_activity_list('go_detail_body','act=ADDPERS&fb_id='+fb_id);
		}
	}

function people_delete(go_id){
	if (confirm('Are you sure you want to delete this person from the G.O Database ?'))
		{
		people_list('people','act=DELPERS&go_id='+go_id);
		}
	}

function people_activity_add(go_id,fun,location_id){
	if (confirm('Are you sure you want to add this ?'))
		{
		people_activity_list('go_detail_body','act=ADDACT&act_id='+act_id+'&go_id='+go_id+'&location_id='+location_id);
		}
	}

function people_activity_addform(go_id,fun,location_id){
	if (confirm('Are you sure you want to add this ?'))
		{
		people_activity_list('go_detail_body','act=ADDACT&act_id='+act_id+'&go_id='+go_id+'&location_id='+location_id);
		}
	}

function people_activity_list(tag,parms)
	{
		console.log("people activity list");
		if (tag.length==0)
			{ 
			document.getElementById(tag).innerHTML="";
			return;
			}
		xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null)
			{
			alert ("Your browser does not support AJAX!");
			return;
			} 
		var urlwait="../please_wait.asp";
		var url="../includes/people_activity_list.asp";
		url=url+"?"+parms+"&tag="+tag;
		xmlHttp.onreadystatechange = function(){ stateChanged(tag); };
		xmlHttp.open("GET",urlwait,true);
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	}


function people_list(tag,parms)
	{
		if (tag.length==0)
			{ 
			document.getElementById(tag).innerHTML="";
			return;
			}
		xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null)
			{
			alert ("Your browser does not support AJAX!");
			return;
			} 
		var url="../includes/people_list.asp";
		url=url+"?"+parms+"&tag="+tag;
		xmlHttp.onreadystatechange = function(){ stateChanged(tag); };
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	}

function addFavorite(PageName) {
	var url = document.location.href;
	if (navigator.appName != 'Microsoft Internet Explorer' ) {
		window.sidebar.addPanel(PageName,url,"");
	}
	else {
		window.external.AddFavorite(url,PageName);
	}
}



function ImgTogglePrivate(tag,img_id)
	{
	if (tag.length==0)
		{ 
		document.getElementById(tag).innerHTML="";
		return;
		}
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
		{
		alert ("Your browser does not support AJAX!");
		return;
		} 
	var url="../includes/img_toggle_private.asp";
	url=url+"?img_id="+img_id+"&tag="+tag;
	xmlHttp.onreadystatechange = function(){ stateChanged(tag); };

	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	} 


function stateChanged(str) 
	{ 
	if (xmlHttp.readyState==4)
	{document.getElementById(str).innerHTML=xmlHttp.responseText;}
	}

function GetXmlHttpObject()
	{
	var xmlHttp=null;
	try
	  // Firefox, Opera 8.0+, Safari
	  {xmlHttp=new XMLHttpRequest();}
	catch (e)
	  // Internet Explorer
	  {
	  try
		{xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");}
	  catch (e)
		{xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}
	  }
	return xmlHttp;
	}	


//##################################################################################
//## FORM SUBMIT WITH AJAX                                                        ##
//## @Author: Simone Rodriguez aka Pukos <http://www.SimoneRodriguez.com>         ##
//## @Version: 1.2                                                                ##
//## @Released: 28/08/2007                                                        ##
//## @License: GNU/GPL v. 2 <http://www.gnu.org/copyleft/gpl.html>                ##
//##################################################################################
function xmlhttpPost(strURL,formname,responsediv,responsemsg) {
    var xmlHttpReq = false;
    var self = this;
    // Xhr per Mozilla/Safari/Ie7
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // per tutte le altre versioni di IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
			// Quando pronta, visualizzo la risposta del form
            updatepage(self.xmlHttpReq.responseText,responsediv);
        }
		else{
			// In attesa della risposta del form visualizzo il msg di attesa
			updatepage(responsemsg,responsediv);
		}
    }
    self.xmlHttpReq.send(getquerystring(formname));
}



function getquerystring(formname) {
    var form = document.forms[formname];
	var qstr = "";

    function GetElemValue(name, value) {
        qstr += (qstr.length > 0 ? "&" : "")
            + escape(name).replace(/\+/g, "%2B") + "="
            + escape(value ? value : "").replace(/\+/g, "%2B");
			//+ escape(value ? value : "").replace(/\n/g, "%0D");
    }

	var elemArray = form.elements;
    for (var i = 0; i < elemArray.length; i++) {
        var element = elemArray[i];
        var elemType = element.type.toUpperCase();
        var elemName = element.name;
        if (elemName) {
            if (elemType == "TEXT"
                    || elemType == "TEXTAREA"
                    || elemType == "PASSWORD"
					|| elemType == "BUTTON"
					|| elemType == "RESET"
					|| elemType == "SUBMIT"
					|| elemType == "FILE"
					|| elemType == "IMAGE"
                    || elemType == "HIDDEN")
                GetElemValue(elemName, element.value);
            else if (elemType == "CHECKBOX" && element.checked)
                GetElemValue(elemName, 
                    element.value ? element.value : "On");
            else if (elemType == "RADIO" && element.checked)
                GetElemValue(elemName, element.value);
            else if (elemType.indexOf("SELECT") != -1)
                for (var j = 0; j < element.options.length; j++) {
                    var option = element.options[j];
                    if (option.selected)
                        GetElemValue(elemName,
                            option.value ? option.value : option.text);
                }
        }
    }
    return qstr;
}

function updatepage(str,responsediv){
    document.getElementById(responsediv).innerHTML = str;
}


function validate_and_submit(form)
	{
	if(ValidateForm(form))
		{
		xmlhttpPost('../includes/people_activity_list.asp', 'activity', 'go_detail_body', '<img src=\'../images/icons/48/refresh.png\'>');
	}
return false;
}


///////////////////////////////////////////////////////////////////////

function ajaxObject(layer, url) {                                    // This is the object constructor
   var that=this;                                                    // A workaround for some javascript idiosyncrocies
   var updating = false;                                             // Set to true if this object is already working on a request
   this.callback = function() {}                                     // A post-processing call -- a stub you overwrite.

   this.update = function(passData) {                                // Initiates the server call.
      if (updating==true) { return false; }                          // Abort if we're already processing a call.
      updating=true;                                                 // Set the updating flag.
      var AJAX = null;                                               // Initialize the AJAX variable.
      if (window.XMLHttpRequest) {                                   // Are we working with mozilla?
         AJAX=new XMLHttpRequest();                                  //  Yes -- this is mozilla.
      } else {                                                       // Not Mozilla, must be IE
         AJAX=new ActiveXObject("Microsoft.XMLHTTP");                //  Wheee, ActiveX, how do we format c: again?
      }                                                              // End setup Ajax.
      if (AJAX==null) {                                              // If we couldn't initialize Ajax...
         alert("Your browser doesn't support AJAX.");                // Sorry msg.                                              
         return false                                                // Return false (WARNING - SAME AS ALREADY PROCESSING!)
      } else {
         AJAX.onreadystatechange = function() {                      // When the browser has the request info..
            if (AJAX.readyState==4 || AJAX.readyState=="complete") { //   see if the complete flag is set.
               LayerID.innerHTML=AJAX.responseText;                  //   It is, so put the new data in the object's layer
               delete AJAX;                                          //   delete the AJAX object since it's done.
               updating=false;                                       //   Set the updating flag to false so we can do a new request
               that.callback();                                      //   Call the post-processing function.
            }                                                        // End Ajax readystate check.
         }                                                           // End create post-process fucntion block.
         var timestamp = new Date();                                 // Get a new date (this will make the url unique)
         var uri=urlCall+'?'+passData+'&timestamp='+(timestamp*1);   // Append date to url (so the browser doesn't cache the call)
         AJAX.open("GET", uri, true);                                // Open the url this object was set-up with.
         AJAX.send(null);                                            // Send the request.
         return true;                                                // Everything went a-ok.
      }                                                              // End Ajax setup aok if/else block                 
   }
      
   // This area set up on constructor calls.
   var LayerID = document.getElementById(layer);                     // Remember the layer associated with this object.
   var urlCall = url;                                                // Remember the url associated with this object.
}          

