
// Created on May 19, 2007 by nhorvath
// TWOPOINTO INC.
// All Rights Reserved - Unauthorized Use Prohibited
// For information contact licensing@twopointo.com
 

var g_callbackSpan;
var g_followUpFunction;
var g_urlHistory = new Array();


function makePOSTRequest(url, parameters, callbackSpan, followUpFunction) {
   g_callbackSpan = callbackSpan;
   g_followUpFunction = followUpFunction;
   http_request = false;
   if (window.XMLHttpRequest) { // Mozilla, Safari,...
      http_request = new XMLHttpRequest();
      if (http_request.overrideMimeType) {
      	// set type accordingly to anticipated content type
         //http_request.overrideMimeType('text/xml');
         http_request.overrideMimeType('text/html');
      }
   } else if (window.ActiveXObject) { // IE
      try {
         http_request = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
         try {
            http_request = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (e) {}
      }
   }
   if (!http_request) {
      alert('Cannot create XMLHTTP instance');
      return false;
   }
   
   http_request.onreadystatechange = alertContents;
   http_request.open('POST', url, true);
   http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   http_request.setRequestHeader("Content-length", parameters.length);
   http_request.setRequestHeader("Connection", "close");
   http_request.send(parameters);
}


function alertContents() {
  if (http_request.readyState == 4) {
      if (http_request.status == 200) {
		 if (document.getElementById(g_callbackSpan)) {
	         result = http_request.responseText;
	         document.getElementById(g_callbackSpan).innerHTML = result;
         }
         eval(g_followUpFunction);
      } else {
         throw ('TwoPointo Ajax Error: - could not perform post');
      }
   }
}


function twoPointOAjaxFormProcessor(theFormId, actionUrl, preProcessorFunction, followUpFunction, callbackSpan) {
	var parameterString = '';

	if (theFormId) {
	  if (verifyForm(document.getElementById(theFormId))) {
		  // get element array using the specified preProcessorFunction
		  argumentHash = eval(preProcessorFunction);
		  
		  for (var fieldName in argumentHash) {
		  	parameterString += fieldName+"="+encodeURIComponent(argumentHash[fieldName])+"&";
		  }
	  } else {
	  	  return false;
	  }
	}
	else {
		argumentHash = eval(preProcessorFunction);
		  
		for (var fieldName in argumentHash) {
		  	parameterString += fieldName+"="+encodeURIComponent(argumentHash[fieldName])+"&";
		}
	}
	
	// to avoid caching
	parameterString += "nxtilepost=true&decache=" + (Math.round((Math.random()*9)+1));
	
	 makePOSTRequest(actionUrl, parameterString, callbackSpan, followUpFunction);


}


function twoPointOTileLoader(targetAction, targetSpan, followUpFunction, title) {

	if (title) {
		var elementIndex = g_urlHistory.length;
	    g_urlHistory[elementIndex] = new Array();
	    
	    g_urlHistory[elementIndex]['targetAction'] = targetAction;
	    g_urlHistory[elementIndex]['targetSpan'] = targetSpan;
	    g_urlHistory[elementIndex]['followUpFunction'] = followUpFunction;
	    g_urlHistory[elementIndex]['title'] = title;
    	updateBreadCrumbs();
    }
   // var callAudit = 'twoPointOAjaxFormProcessor(\''+theFormId+, actionUrl, preProcessorFunction, followUpFunction, callbackSpan)';
	var parameterString = '';
	twoPointOAjaxFormProcessor('', targetAction, '', followUpFunction,  targetSpan);
	
}


function preProcessFormData () {
	var formFields = new Array();
	
	if (typeof tinyMCE !== "undefined") { 
		tinyMCE.triggerSave(); 
	}

	for(i=0; i<theForm.elements.length; i++) {
		if (theForm.elements[i].type == "radio") {
			if (theForm.elements[i].checked) {
         		formFields[theForm.elements[i].name] = theForm.elements[i].value;
         	}
		}
		else if (theForm.elements[i].type == "checkbox") {
			if (theForm.elements[i].checked) {
         		formFields[theForm.elements[i].name] = 1;
         	}
         	else {
				formFields[theForm.elements[i].name] = 0;
			}
        } 
        else {
         	formFields[theForm.elements[i].name] = theForm.elements[i].value;
        }
  	} 
	
	return formFields;
}


function showSaveProgress() {

}

function hideSaveProgress() {

}

function loadContent(elementSelector, sourceUrl) {
	$(""+elementSelector+"").load(""+sourceUrl+"");
}


/********** AUTO LOGOFF WITH TIMEOUT **********/


// adjust this value if necessary
var timer;

function resetLogoutInterval (logoutIntervalMinutes, url) {
	clearInterval(timer);

	timer = setInterval("autoLogout('"+url+"')", logoutIntervalMinutes*60*1000);
}


function autoLogout (url) {
	window.location=url;
}

 
