var lastAjaxResponse = '';
var divURLs = Array();


document.onkeydown = function(){
	if(document.all) // ie check
		if(event.ctrlKey && event.shiftKey && event.keyCode==67){
			var hWnd = window.open('?', 'ajaxConsole');
			hWnd.document.write(lastAjaxResponse);
		}
};

if(!tabIndexes)
	var tabIndexes = Array();

function loadDivURL(divName, url, onComplete){

	if(tabIndex = tabIndexes[divName]){
		divName = tabDivNames[tabIndex];
	}

	url += '&currentTab='+divName+'&currentDiv='+divName;

	divURLs[divName] = url;

	var ajax = new Ajax.Updater(
								divName,
								url+'&ajaxRequest=1',
								{
									onComplete: function(originalRequest){
										lastAjaxResponse = originalRequest.responseText;
										if(onComplete)
											onComplete();
									},
									onLoading: onLoading(divName),
									evalScripts: true
								}
								);
}

function loadDocument(url, onComplete){
	var ajax = new Ajax.Request(
								url,
								{
									method: 'get',
									onComplete: onComplete
								}
							);
}

function loadFrameURL(frameName, url){
	window.top.frames[frameName].location.href = url;
}

function reloadDiv(){
	for(i=0;i<arguments.length;i++){
		divName = arguments[i];
		if(url = divURLs[divName]){
			loadDivURL(divName, url);
		}
	}
}

function loadDivForm(form, divName){
	var url = form.action+'&'+Form.serialize(form);
	loadDivURL(divName, url);
}

function postForm(form, onComplete){
	updateFCKEditors(form);
	var postBody = Form.serialize(form);
	
	var ajax = new Ajax.Request(
								form.action+'&postForm=1&ajaxRequest=1',
								{
									method: 'post',
									postBody: postBody,
									onComplete: function(){
										if(onComplete)
											onComplete();
									}
								}
								);
}

function postData(action, postBody, onComplete){
	var ajax = new Ajax.Request(
								action+'&postForm=1&ajaxRequest=1',
								{
									method: 'post',
									postBody: postBody,
									onComplete: function(){
										if(onComplete)
											onComplete();
									}
								}
	);
}


function onLoading(divName){
	if(div = $(divName)){
		div.innerHTML = 'Loading ...';
	}
}

/* Update fckeditors in given form.
 * @author Mark Vermeltfoort
 */

function updateFCKEditors(form){
	if(!window.FCKeditorAPI) return;

	var inputs = $A(Form.getInputs(form, 'hidden'));

	inputs.each(function(input){
		var FCKe;
		if(FCKe = FCKeditorAPI.GetInstance(input.name))
			FCKe.UpdateLinkedField();
	}
	);

//	if(form.updateFCKeditor){
//		for(i=0;i<form.updateFCKeditor.length;i++){
//			form.updateFCKeditor[i]();
//		}
//	}
}



function copy_clip(text) {
  if (window.document.all) {
    window.clipboardData.setData("Text", text);
    return;
  } else {
    var flashcopier = 'flashcopier';
    if(!document.getElementById(flashcopier)) {
      var divholder = document.createElement('div');
      divholder.id = flashcopier;
      document.body.appendChild(divholder);
    }
    document.getElementById(flashcopier).innerHTML = '';
    var divinfo = '<embed src="http://www.travelportmarketing.com/central/data/flash/_clipboard.swf" FlashVars="clipboard='+encodeURIComponent(text)+'" width="50" height="50" type="application/x-shockwave-flash"></embed>';
    document.getElementById(flashcopier).innerHTML = divinfo;
  }
}



var totalTime = 0; // time in seconds
var interval = 60000*15; // 5 min
var maxTime = 60000*30; // 30 min max

function keepAlive(){
	//if(totalTime < maxTime){
		postData('index.php?keepalive=1', null, setTimeout('keepAlive()', interval));
		totalTime += interval;
	//}
}

keepAlive();

function printObject(object){
	var wnd = window.open('', 'print', '');
	wnd.document.write('<link rel="stylesheet" href="http://travelportmarketing.com/central/layout/css/stylesheet.css" type="text/css">');
	wnd.document.write('<link rel="stylesheet" href="http://travelportmarketing.com/central/layout/css/formhandler.css" type="text/css">');
	wnd.document.write('<div style="padding-left: 5%;width: 600px;">'+object.innerHTML+'</div>');
	wnd.document.close();
	wnd.focus();
	wnd.print();
	wnd.close();
}

function addToFavorites(urlAddress, pageName) {
	if (window.external) {
		window.external.AddFavorite(urlAddress,pageName)
	} else {
		alert("Sorry! Your browser doesn't support this function.");
	}
}

