//this function simply determines what kind of "loading.." image to display
function ftnReturnCogs(csize){
	
	var strCogsImage
	
	//fix this
	switch(csize){
		case 1:
		strCogsImage = 'loadingCogsLarge.gif';
		break;
		case 2:
		strCogsImage = 'loadingCogsSmall.gif';
		break;
		case 3:
		strCogsImage = 'loadingCogsWee.gif';
		break;
	};	
	
	return strCogsImage
}

function ftnDisplayLoadingCogs(divName, cogImage){
	var containerSt = YAHOO.util.Dom.get(divName);
	containerSt.innerHTML = '<img src="/images/global/' + ftnReturnCogs(cogImage) + '"/>';
}

// Calls the AJAX caller function
function ftnAjaxIt(strPage,strDivName,size, formName)
{
	
		
	loadCallback.argument[0]	=	strPage;
	loadCallback.argument[1]	=	strDivName;
	loadCallback.argument[2]	=	size;
	loadCallback.argument[3]	=	formName;
		
	
	//sniff for ie7
	var ie7 = (document.all && !window.opera && window.XMLHttpRequest) ? true : false;
	
	//only display the loading cogs at this early stage if the browser is not IE7
	if(ie7==false){
		ftnDisplayLoadingCogs(strDivName,size)	
	}
	
	loadHandler.call(loadCallback);
}

// Object that implements the Call, Handles the success & failure
var loadHandler = {

  handleSuccess:function(o){
  	
 	
     var container = YAHOO.util.Dom.get(o.argument[1]);
     if(o.responseText !== undefined){
		 if(o.responseText == 'error session expired'){
			window.location.href='/for-candidates/log-in.asp?intTypeId=3';
		 }else{

			container.innerHTML = o.responseText;
			if(o.argument[4] !== undefined){
				if(doneSecondAjax == 0){
					loadCallback.argument[0]	=	o.argument[4];
					loadCallback.argument[1]	=	o.argument[5];
					loadCallback.argument[2]	=	o.argument[6];
					loadCallback.argument[3]	=	o.argument[7];
					loadHandler.call(loadCallback);	
					doneSecondAjax +=1;
				}
				
			};
		 }
     }
  },

  handleFailure:function(o){
  
  var result = o.status + " " + o.statusText;
     alert("Transaction failed.  The error is: " + result);

  },
  
  call:function(oCallback){
	//  the default ajax method is 'GET' but 'POST' is fine too, as long as the name of the form is sent through
	
	
	
	var method = 'GET';
	var oForm = (oCallback.argument[3]);
	if(oForm!='GET' && oForm!=undefined){
	YAHOO.util.Connect.setForm(oForm);
	method = 'POST';
	}
    YAHOO.util.Connect.asyncRequest(method, oCallback.argument[0], oCallback);
		
		//display loading cots at this late stage, had to delay cogs for ie7 users
		ftnDisplayLoadingCogs(oCallback.argument[1], oCallback.argument[2]);
	}
	
	
};

// Callback function called by YUI when making a async request
var loadCallback =
{
  success: loadHandler.handleSuccess,
  failure: loadHandler.handleFailure,
  argument:[]
}

var doneSecondAjax = 0;

function ftnAjaxItTwice(strPage,strDivName,size,formName, strPage2,strDivName2,size2, formName2)
{
	
	doneSecondAjax = 0;
	
	
	loadCallback.argument[0]	=	strPage;
	loadCallback.argument[1]	=	strDivName;
	loadCallback.argument[2]	=	size;
	loadCallback.argument[3]	=	formName;
	loadCallback.argument[4]	=	strPage2;
	loadCallback.argument[5]	=	strDivName2;
	loadCallback.argument[6]	=	size2;
	loadCallback.argument[7]	=	formName2;
	
	//sniff for ie7
	var ie7 = (document.all && !window.opera && window.XMLHttpRequest) ? true : false;
	
	
	//only display the loading cogs at this early stage if the browser is not IE7
	if(ie7==false){
		ftnDisplayLoadingCogs(strDivName,size)	
	}
	
	loadHandler.call(loadCallback);
	
	
}
