/* $Id: ajax.js 367 2007-10-06 08:20:45Z timw $ */
/**
 * APP JS -> ajax -> connect
 * 
 * Creates an ajax connection
 *
 * @package 		amoeba
 * @subpackage 	js
 * @version 		20070718
 *
 * @param		string		AJAX_URL
 * @param		string		POPUP_HEADING
 * @param		array			CALENDARS
 * @return	bool
 */
function APP_JS_ajaxconnect(isXML) {
  try {
    AJAXrequest = new XMLHttpRequest(); /* e.g. Firefox */
    if (isXML && AJAXrequest.overrideMimeType) {
      AJAXrequest.overrideMimeType('text/xml');
    }
  } 
  catch(e) {
    try {
      AJAXrequest = new ActiveXObject("Msxml2.XMLHTTP");  /* some versions IE */
    } 
    catch (e) {
      try {
        AJAXrequest = new ActiveXObject("Microsoft.XMLHTTP");  /* some versions IE */
      } 
      catch (E) {
        AJAXrequest = false;
      }
    }
  }
  return AJAXrequest;  
}

/**
 * APP JS -> popups -> Create Popup
 * 
 * Creates a ajax popup window with the uniqueID
 *
 * @package 		amoeba
 * @subpackage 	js
 * @version 		20071005
 *
 * @param		string		AJAX_URL
 * @param		string		POPUP_HEADING
 * @param		array			CALENDARS
 * @param		string		AFTERLOAD_FUNCTION
 * @return	bool
 */
function APP_JS_popup(theURL,theHeading,calendars,afterFunction) {
	/**
	 * Get the body object
	 */
	var objBody = document.getElementsByTagName('body').item(0);
	/**
	 * Create the unique ID
	 */
	var uniqueID = Date.parse(new Date());
	/**
	 * Create the opaque mask and loading image
	 */
	var loadingImage = document.createElement('img');
	loadingImage.id = 'appajaxbgloading'+ uniqueID;
	loadingImage.src = '_skins/default/images/ICONajaxloading_black.gif';
	loadingImage.style.margin = '20px';
	loadingImage.style.cssFloat = 'left';
	var popupMask = document.createElement('div');
	popupMask.id = 'appajaxbg'+ uniqueID;
	popupMask.className = 'appajaxbg';
	if ((document.all) && (navigator.userAgent.indexOf('Opera') == -1)) {
		popupMask.style.filter = 'alpha(0.8)';
	} else {
		popupMask.style.opacity = '0.8';
	}
	popupMask.appendChild(loadingImage);
	objBody.appendChild(popupMask);
	/**
	 * Create the app container - this is not the popup container
	 */
	var objContainer = document.createElement('div');
	objContainer.id = 'appajaxcontainer'+ uniqueID;
	objContainer.className = 'appajaxcontainer';
	objContainer.style.display = 'none';
	objBody.appendChild(objContainer);
	/**
	 * Create the popup elements
	 */
		//Popup Container
	var popupContainer = document.createElement('div');
	popupContainer.id = 'appajaxpopup'+ uniqueID;
	popupContainer.className = 'appajaxpopup';
		//Popup header - includes h3 and close button
	var popupHeader = document.createElement('h3');
	var popupHeaderText = document.createElement('span');
	popupHeaderText.id = 'appajaxpopupheader'+uniqueID;
	popupHeaderText.className = 'appajaxpopupheader';
	popupHeaderText.innerHTML = theHeading;
	var popupCloser = document.createElement('a');
	popupCloser.setAttribute('href','javascript:;');
	popupCloser.setAttribute('onclick','APP_JS_popupclose(\''+uniqueID+'\');');
	var popupCloserText = document.createTextNode('Close');
	popupCloser.appendChild(popupCloserText);
	var popupID = document.createElement('a');
	popupID.setAttribute('name',uniqueID);
		//Popup Content
	var popupContent = document.createElement('div');
	popupContent.id = 'appajaxpopupcontent'+uniqueID;
	popupContent.className = 'appajaxpopupcontent';
  /* Set up Ajax */
  AJAXrequest = APP_JS_ajaxconnect();
  /* Tell the user if Ajax is not going to work */
  if (!AJAXrequest) {
    alert('This method of using app is not supported by your browser - Please contact the administrator for more information');
    return false;
  }
  /* Get the details to the server */
  AJAXrequest.onreadystatechange = function () {
    if (AJAXrequest.readyState == 4) {
			loadingImage.style.display = 'none';
			objContainer.style.display = '';
      if (AJAXrequest.responseText && AJAXrequest.responseText != '0') {
				popupContent.innerHTML = AJAXrequest.responseText;
        if (typeof(calendars)=='object') {
        	for (i=0;i<calendars.length;i++) {
					  Calendar.setup({
					    inputField     :    calendars[i][0],     // id of the input field
					    ifFormat       :    (calendars[i][1]) ? "%Y-%m-%d %H:%M" : "%Y-%m-%d",     // format of the input field (even if hidden, this format will be honored)
					    displayArea    :    "display_"+calendars[i][0],       // ID of the span where the date is to be shown
					    daFormat       :    (calendars[i][1]) ? "%H:%M %d-%m-%Y" : "%d-%m-%Y",// format of the displayed date
					    button         :    "TRIGGER"+calendars[i][0],
					    align          :    "Tl",           // alignment (defaults to "Bl")
					    showsTime      :    calendars[i][1],           // show time
					    electric       :    false,           // update on close
					    singleClick    :    true
					  });
        	}
        }
	      /**
	       * Run after load function if it exists
	       */
	      if (afterFunction) {
	          eval(afterFunction);
	      }
      } else {
      	popupContent.innerHTML = 'An error occured';
      }
    }
  }
  AJAXrequest.open('GET',theURL,true);
  AJAXrequest.send(null); 

  /* Build the structure */
  popupHeader.appendChild(popupHeaderText);
  popupHeader.appendChild(popupCloser);
  popupHeader.appendChild(popupID);
  
  popupContainer.appendChild(popupHeader);
  popupContainer.appendChild(popupContent);
  
 	objContainer.appendChild(popupContainer);
 	
 	/* return true */
	return true;
}

/**
 * APP JS -> popups -> Close Popup
 * 
 * Destroys the popup with the uniqueID
 *
 * @package 		app
 * @subpackage 	js
 * @version			20071005
 *
 * @param		string			UNIQUE_ID
 * @return	bool
 */
function APP_JS_popupclose(uniqueID) {
  /* Init the vars */
  var theAppContainer = TWD_JS_findObj('appajaxcontainer'+uniqueID);
  var theAppBg = TWD_JS_findObj('appajaxbg'+uniqueID);

  /* Destroy the objects */
  theAppContainer.parentNode.removeChild(theAppContainer);
  theAppBg.parentNode.removeChild(theAppBg);
  
  /* return true */
  return true;
}

/**
 * APP JS -> popups -> Create Popup for Docmentation
 * 
 * Creates a ajax popup window with the uniqueID
 *
 * @package 		app
 * @subpackage 	js
 * @version 		20070811
 *
 * @param		string		AJAX_URL
 * @param		string		POPUP_HEADING
 * @param		bool			DEBUG_MODE
 * @return	bool
 */
function APP_JS_popup_documentation() {
	/**
	 * Get the body object
	 */
	var objBody = document.getElementsByTagName('body').item(0);
	/**
	 * Create the unique ID
	 */
	var uniqueID = '_documentation';
	/**
	 * Create the opaque mask and loading image
	 */
	var loadingImage = document.createElement('img');
	loadingImage.id = 'appajaxbgloading'+ uniqueID;
	loadingImage.src = '_skins/default/images/ICONajaxloading_black.gif';
	loadingImage.style.margin = '20px';
	loadingImage.style.cssFloat = 'left';
	var popupMask = document.createElement('div');
	popupMask.id = 'appajaxbg'+ uniqueID;
	popupMask.className = 'appajaxbg';
	if ((document.all) && (navigator.userAgent.indexOf('Opera') == -1)) {
		popupMask.style.filter = 'alpha(0.8)';
	} else {
		popupMask.style.opacity = '0.8';
	}
	popupMask.appendChild(loadingImage);
	objBody.appendChild(popupMask);
	/**
	 * Create the app container - this is not the popup container
	 */
	var objContainer = document.createElement('div');
	objContainer.id = 'appajaxcontainer'+ uniqueID;
	objContainer.className = 'appajaxcontainer';
	objContainer.style.display = 'none';
	objBody.appendChild(objContainer);
	/* Create the elements and attributes */
	var popupContainer = document.createElement('div');
	popupContainer.id = 'appajaxpopup'+ uniqueID;
	popupContainer.className = 'appajaxpopup';
	popupContainer.style.width = '80%';
	popupContainer.style.marginTop = '20px;';

	var popupHeader = document.createElement('h3');

	var popupHeaderText = document.createElement('span');
	popupHeaderText.id = 'appajaxpopupheader'+uniqueID;
	popupHeaderText.className = 'appajaxpopupheader';
	popupHeaderText.innerHTML = 'Documentation';

	var popupHeaderIcon = document.createElement('img');
	popupHeaderIcon.id = 'headericon';
	popupHeaderIcon.src = '_skins/default/images/ICONhelp.gif';
	popupHeaderIcon.alt = 'Help';
	popupHeaderIcon.align = 'absmiddle';
	popupHeaderIcon.border = 0;
	popupHeaderIcon.width = 16;
	popupHeaderIcon.height = 16;
	var popupID = document.createElement('a');
	popupID.setAttribute('name',uniqueID);

	var popupCloser = document.createElement('a');
	popupCloser.setAttribute('href','javascript:;');
	popupCloser.setAttribute('onclick','APP_JS_popupclose(\''+uniqueID+'\');');
	var popupCloserText = document.createTextNode('Close');
	popupCloser.appendChild(popupCloserText);

	var popupContent = document.createElement('div');
	popupContent.id = 'appajaxpopupcontent'+uniqueID;
	popupContent.className = 'appajaxpopupcontent';
  /* Get the menu */
  AJAXrequest = APP_JS_ajaxconnect();
  /* Tell the user if Ajax is not going to work */
  if (!AJAXrequest) {
    alert('This method of using app is not supported by your browser - Please contact the administrator for more information');
    return false;
  }
  AJAXrequest.onreadystatechange = function () {
    if (AJAXrequest.readyState == 4) {
      if (AJAXrequest.responseText) {
				popupContent.innerHTML = AJAXrequest.responseText;
				loadingImage.style.display = 'none';
				objContainer.style.display = '';
      }
    }
  }
  AJAXrequest.open('GET','_documentation/index.php',true);
  AJAXrequest.send(null);
   
   /* Build the structure */
  popupHeader.appendChild(popupHeaderIcon);
  popupHeader.appendChild(popupHeaderText);
  popupHeader.appendChild(popupCloser);
  popupHeader.appendChild(popupID);
  
  popupContainer.appendChild(popupHeader);
  popupContainer.appendChild(popupContent);
  
 	objContainer.appendChild(popupContainer);
	/* return true */
	return true;
}

/**
 * APP JS -> documentation -> Load Content
 * 
 * Loads the documentation into the documentation content pane
 *
 * @package 		app
 * @subpackage 	js
 * @version			20070811
 *
 * @param		string			UNIQUE_ID
 * @return	bool
 */
function APP_JS_documentation_loadcontent(theURL) {
  /* Init the vars */
  var thePane = TWD_JS_findObj('documentationcontent');
  /* Display the load image */
  thePane.innerHTML = '<img src="_skins/default/images/ICONajaxloading.gif" />';
  /* Set up Ajax */
  AJAXrequest = APP_JS_ajaxconnect();
  /* Tell the user if Ajax is not going to work */
  if (!AJAXrequest) {
    alert('This method of using app is not supported by your browser - Please contact the administrator for more information');
    return false;
  }
  /* Get the details to the server */
  AJAXrequest.onreadystatechange = function () {
    if (AJAXrequest.readyState == 4) {
      thePane.innerHTML = AJAXrequest.responseText;
    }
  }
  AJAXrequest.open('GET',theURL,true);
  AJAXrequest.send(null); 
   
 	/* return true */
	return true;
}

/**
 * APP JS -> ajax -> session set
 * 
 * Just touchs an url to set a session or cookie
 *
 * @package 		app
 * @subpackage 	js
 * @version			20070921
 *
 * @param		string			URL
 * @return	bool
 */
function APP_JS_sessionset(theURL) {
  /* Set up Ajax */
  AJAXrequest = APP_JS_ajaxconnect();
  /* Tell the user if Ajax is not going to work */
  if (!AJAXrequest) {
    alert('This method of using skink is not supported by your browser - Please contact the administrator for more information');
    return false;
  }
  /* Get the details to the server */
  AJAXrequest.onreadystatechange = function () {
    if (AJAXrequest.readyState == 4) {
    	return true;
    }
  }
  AJAXrequest.open('GET',theURL,true);
  AJAXrequest.send(null); 
  
 	/* return true */
	return true;
}




























































































/**
* SKINK Skin special Javascript Functions
*
* @package skink
* @author $Author: timw $
* @version $Rev: 367 $ $Date: 2007-10-06 21:20:45 +1300 (Sat, 06 Oct 2007) $
* @url $HeadURL: http://elibcsystems.dyndns.org/svn/skink/tags/1.5.0/scripts/ajax.js $
*/
function LIZARD_JS_ajaxconnect(isXML) {
  try {
    AJAXrequest = new XMLHttpRequest(); /* e.g. Firefox */
    if (isXML && AJAXrequest.overrideMimeType) {
      AJAXrequest.overrideMimeType('text/xml');
    }
  } 
  catch(e) {
    try {
      AJAXrequest = new ActiveXObject("Msxml2.XMLHTTP");  /* some versions IE */
    } 
    catch (e) {
      try {
        AJAXrequest = new ActiveXObject("Microsoft.XMLHTTP");  /* some versions IE */
      } 
      catch (E) {
        AJAXrequest = false;
      }
    }
  }
  return AJAXrequest;  
}


function LIZARD_JS_debug(theResponse) {
  var generator=window.open('','name','height=400,width=500,scrollbars=yes,resizable=yes');
  generator.document.write('<html><head><title>PANDA Debug Window</title>');
  generator.document.write('<script src="scripts/SCRIPTtwd.js"></script>');
  generator.document.write('<script src="_skins/default/SCRIPTpanda.js"></script>');
  generator.document.write('</head><body>');
  generator.document.write('<p><a style="float: right;" href="javascript:self.close()">Close</a><h2>DEBUGGED OUTPUT</h2></p>');
  generator.document.write(theResponse);
  generator.document.write('</body></html>');
  generator.document.close();
  return false;
}


function LIZARD_JS_resetmessages() {
  TWD_JS_showHideObjects('ajaxsaved','none');
  TWD_JS_showHideObjects('ajaxerror','none');        
  TWD_JS_showHideObjects('ajaxsaving','none');
 	/* return true */
	return true;
}


function LIZARD_JS_parseformvars(theForm){
  var str = "";
  var valueArr = null;
  var val = "";
  var cmd = "";
  for(var i = 0;i < theForm.elements.length;i++) {
    switch(theForm.elements[i].type) {
      case "textarea":
        str += theForm.elements[i].name + "=" + escape(theForm.elements[i].value) + "&";
      break;
      case "text":
        str += theForm.elements[i].name + "=" + escape(theForm.elements[i].value) + "&";
      break;
      case "password":
        str += theForm.elements[i].name + "=" + escape(theForm.elements[i].value) + "&";
      break;
      case "checkbox":
        if (theForm.elements[i].checked == true) str += theForm.elements[i].name + "=" + theForm.elements[i].value + "&";
      break;
      case "radio":
        if (theForm.elements[i].checked == true) str += theForm.elements[i].name + "=" + theForm.elements[i].value + "&";
      break;
      case "hidden":
        str += theForm.elements[i].name + "=" + escape(theForm.elements[i].value) + "&";
      break;
      case "select-one":
        str += theForm.elements[i].name + "=" + theForm.elements[i].options[theForm.elements[i].selectedIndex].value + "&";
      break;
    }
  }
  str = str.substr(0,(str.length - 1));
  return str;
}

function addZero(vNumber){ 
  return ((vNumber < 10) ? "0" : "") + vNumber 
} 
      
function LIZARD_JS_formatDate(vDate, vFormat){ 
  var vDay              = addZero(vDate.getDate()); 
  var vMonth            = addZero(vDate.getMonth()+1); 
  var vYearLong         = addZero(vDate.getFullYear()); 
  var vYearShort        = addZero(vDate.getFullYear().toString().substring(3,4)); 
  var vYear             = (vFormat.indexOf("yyyy")>-1?vYearLong:vYearShort) 
  var vHour             = addZero(vDate.getHours()); 
  var vMinute           = addZero(vDate.getMinutes()); 
  var vSecond           = addZero(vDate.getSeconds()); 
  var vDateString       = vFormat.replace(/dd/g, vDay).replace(/MM/g, vMonth).replace(/y{1,4}/g, vYear) 
  vDateString           = vDateString.replace(/hh/g, vHour).replace(/mm/g, vMinute).replace(/ss/g, vSecond) 
  return vDateString 
} 
