/*
'<!--$$Revision: 31 $-->
'<!--$$Author: Mark $-->
'<!--$$Date: 19/10/09 3:32p $-->
'<!--$$Logfile: /4.0 Arts Hub/shared/js/ajax/ajaxCommon.js $-->
'<!--$$NoKeywords: $-->
*/

	var doc = null; 
	var http_request = false;
	var loadstatustext="<img id=loader src=/shared/images/icons/loading.gif width=16 height=16>";
	var tGlobalPathToCommonASP = "/shared/js/ajax/common.asp";
	
function getDoc(theString,targetDiv,workingDiv) {
	//Show status text for standard getDoc calls
	if ( workingDiv!='' && targetDiv != 'mailtolink' ){document.getElementById(workingDiv).innerHTML=loadstatustext};
	var aObj=new Array(); 
	var ajaxIndex = aObj.length;
	var tMailToLink = ""; //if we're using getDoc to return a mailto link
	
	aObj[ajaxIndex] = new sack();
	aObj[ajaxIndex].requestFile = theString;
	if(targetDiv!=''){
		//mailto links
		if (targetDiv == 'mailtolink') {
			aObj[ajaxIndex].onCompletion = function(){ 
				tMailToLink = "mailto:" + aObj[ajaxIndex].response + "";
				//Using workingDiv var for subject line
				if ( workingDiv != "" ) { tMailToLink += "?subject=" + workingDiv; }
				document.location.href = tMailToLink;
			};
		//standard getDoc calls
		} else {
			aObj[ajaxIndex].onCompletion = function(){ document.getElementById(targetDiv).innerHTML=aObj[ajaxIndex].response;if (workingDiv!=''){document.getElementById(workingDiv).innerHTML=""};};  
		}
	} else {
		aObj[ajaxIndex].onCompletion = function(){if (workingDiv!=''){document.getElementById(workingDiv).innerHTML=""} };  
	}
	aObj[ajaxIndex].runAJAX();		
}



/*function getDoc(theString,targetDiv,workingDiv){
	ajax = new sack();
	ajax.requestFile = theString;
	ajax.method = 'post'; //form.method.value;
	ajax.element = targetDiv;
	ajax.runAJAX();
}
*/	
function makePostString(obj) {
	var tmpStr=""
	for (x=0;x<obj.elements.length;x++)
	{
	tmpStr=tmpStr+"&"+obj.elements[x].name+"="+obj.elements[x].value
	}
	return tmpStr;
}

function makePOSTRequest(url, parameters) {
	http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		 http_request = new XMLHttpRequest();
		 if (http_request.overrideMimeType) {
				http_request.overrideMimeType('text/xml');
		 }
	} 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;
	}
	//	document.getElementById("info").innerHTML="parameters="+parameters;
	
	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);
}
/**
*
*  URL encode / decode
*  http://www.webtoolkit.info/
*
**/
var myURL = {

    // public method for url encoding
    encode : function (string) {
		//(^_^) pipes toooo
		if ( string.indexOf("|") > 0 ) {
			string = replace("|", "%7C");
		}
        return escape(this._utf8_encode(string));
    },

    // public method for url decoding
    decode : function (string) {
		//(^_^) pipes toooo
		if ( string.indexOf("%7C") > 0 ) {
			string = replace("%7C", "|");
		}
        return this._utf8_decode(unescape(string));
    },

    // private method for UTF-8 encoding
    _utf8_encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

}
/* ________________________________________________ */
//	Name 		- regionList --------------------------
//  Purpose		- 
//	Args 		- 
//	Returns		- 
/* _______________________________________________ */
	function regionList(countryCode,regionId,fldName,promptTxt,fldClass,strDiv,bIsShowOnlyParentRegions,bShowDefault) {
		var theString = tGlobalPathToCommonASP+"?action=regionlist&countryCode="+countryCode+"&regionId="+regionId+"&fldName="+fldName+"&promptTxt="+promptTxt+"&fldClass="+fldClass+"&tIsShowOnlyParentRegions="+bIsShowOnlyParentRegions+"&tShowDefault="+bShowDefault;
		getDoc(theString,strDiv,'');
	}
/* ________________________________________________ */
//	Name 		- regionListClassifieds --------------------------
//  Purpose		- 
//	Args 		- 
//	Returns		- 
/* _______________________________________________ */
	function regionListClassifieds(countryCode,regionId,fldName,promptTxt,fldClass,strDiv,bIsShowOnlyParentRegions,bShowDefault) {
		var theString = tGlobalPathToCommonASP+"?action=regionlistclassifieds&countryCode="+countryCode+"&regionId="+regionId+"&fldName="+fldName+"&promptTxt="+promptTxt+"&fldClass="+fldClass+"&tIsShowOnlyParentRegions="+bIsShowOnlyParentRegions+"&tShowDefault="+bShowDefault;
		getDoc(theString,strDiv,'');
	}

/* ________________________________________________ */
//	Name 		- fnGetGalleryMediumImageAndNumberedLinks --------------------------
//  Purpose		- 
//	Args 		- 
//	Returns		- 
/* _______________________________________________ */
	function fnGetGalleryMediumImageAndNumberedLinks(gid,giid,strDiv) {
		var theString = tGlobalPathToCommonASP+"?action=galimage&gId="+gid+"&gIId="+giid;
		getDoc(theString,strDiv,'');
	}
/* ________________________________________________ */
//	Name 		- fnGetGalleryComments --------------------------
//  Purpose		- 
//	Args 		- 
//	Returns		- 
/* _______________________________________________ */
	function fnGetGalleryComments(gid,giid,strDiv) {
		var theString = tGlobalPathToCommonASP+"?action=galimgcomments&gId="+gid+"&gIId="+giid;
		getDoc(theString,strDiv,'');
	}
/* ________________________________________________ */
//	Name 		- fnShowHideContactInfoCheckbox --------------------------
//  Purpose		- show checkbox in job ad form
//	Args 		- 
//	Returns		- 
/* _______________________________________________ */
	function fnShowHideContactInfoCheckbox(strDiv, cboObj) {
		var tShowHide = ""
		if (cboObj.checked) {
			tShowHide = "show";
		}
		var theString="/shared/js/ajax/common.asp?action=showhidecontactinfocheckbox&bShowHide="+tShowHide;
		getDoc(theString,strDiv,'');
	}
/* ________________________________________________ */
//	Name 		- fnGetApplyEmailUpdateMailToClickCount --------------------------
//  Purpose		- update click count and return mailtolink to document.location
//	Args 		- jid, subject line
//	Returns		- email
/* _______________________________________________ */
	function fnGetApplyEmailUpdateMailToClickCount(jid, tSubject) {
		var theString="/shared/js/ajax/common.asp?action=getapplyemailupdatemailtoclickcount&jid="+jid;
		getDoc(theString,'mailtolink',tSubject);
	}
/* ________________________________________________ */
//	Name 		- fnSwapClassifiedsDetailImage --------------------------
//  Purpose		- 
//	Args 		- 
//	Returns		- 
/* _______________________________________________ */
	function fnSwapClassifiedsDetailImage(imageId,strDiv,caption) {
		var theString="/shared/js/ajax/common.asp?action=swapClassImage&imageId="+imageId+"&caption="+escape(caption)
		getDoc(theString,strDiv,'')	
	}
	
	function fnHideMainProfileImage(imageId) {
		
		if (document.getElementById('thumb_' + imageId) !=null)
		{
			document.getElementById('thumb_' + imageId).style.visibility='hidden';
			document.getElementById('thumb_' + imageId).style.width='0px';
			document.getElementById('thumb_' + imageId).style.margin='0px 0px 0px 0px';
			document.getElementById('thumb_' + imageId).style.border='none';
		}
	}
	
	function fnActivateAllImages(imageList) {
		
		var ImageArray = imageList.split(",");
		
		for (var x=0;x<ImageArray.length;x++) {
			if (document.getElementById('thumb_' + ImageArray[x]) != null)
			{				
				document.getElementById('thumb_' + ImageArray[x]).style.visibility='visible';
				document.getElementById('thumb_' + ImageArray[x]).style.width='63px';
				document.getElementById('thumb_' + ImageArray[x]).style.margin='0px 5px 0px 0px';
				document.getElementById('thumb_' + ImageArray[x]).style.border='1px solid #E3DECD';
				//alert(ImageArray[x]);
			}
		}
		
	}
/* ________________________________________________ */
//	Name 		- fnOpenCat --------------------------
//  Purpose		- 
//	Args 		- 
//	Returns		- 
/* _______________________________________________ */
	function fnOpenCat(selectedId,catLevel,discludeCatIds,listSize,forWhat,showAll) {
		
		//Write the current cat
		var theString="/shared/js/ajax/common.asp?action=openCat&catLevel=" + catLevel + "&catId="+selectedId+"&discludeCatIds="+discludeCatIds+"&listSize="+listSize+"&forWhat="+forWhat+"&showAll="+showAll;
		getDoc(theString,'cat'+catLevel,'');	
		
		//Blank out redundant children
		for (i=catLevel+1;i<5;i++) {
				document.getElementById('cat'+i).innerHTML='';
		}
	}
/* ________________________________________________ */
//	Name 		- fnShowListingImages --------------------------
//  Purpose		- 
//	Args 		- 
//	Returns		- 
/* _______________________________________________ */
	function fnShowListingImages(listingId,mainImageId) {
		
		//Call the list images routine
		var theString="/shared/js/ajax/common.asp?action=showListingImages&mainImageId="+mainImageId+"&listingId="+listingId;
		getDoc(theString,'listingimages','');	
		
	}
/* ________________________________________________ */
//	Name 		- fnRemoveListingImage --------------------------
//  Purpose		- 
//	Args 		- 
//	Returns		- 
/* _______________________________________________ */
	function fnRemoveListingImage(imageId,listingId) {
		
		//Call the list images routine
		var theString="/shared/js/ajax/common.asp?action=removeListingImage&imageId="+imageId+"&listingId="+listingId;
		getDoc(theString,'listingimagesFloatB','');	
		
	}
/* ________________________________________________ */
//	Name 		- fnRemoveOrgProfileListingImage --------------------------
//  Purpose		- 
//	Args 		- 
//	Returns		- 
/* _______________________________________________ */
	function fnRemoveOrgProfileListingImage(imageId,listingId) {
		
		//Call the list images routine
		var theString="/shared/js/ajax/common.asp?action=removeOrgProfileListingImage&imageId="+imageId+"&listingId="+listingId;
		getDoc(theString,'listingimagesFloatB','');	
		
	}
/* ________________________________________________ */
//	Name 		- fnRemoveOrgProfileListingImageHeader --------------------------
//  Purpose		- 
//	Args 		- 
//	Returns		- 
/* _______________________________________________ */
	function fnRemoveOrgProfileListingImageHeader(imageId,listingId) {
		
		//Call the list images routine
		var theString="/shared/js/ajax/common.asp?action=removeOrgProfileListingImageHeader&imageId="+imageId+"&listingId="+listingId;
		getDoc(theString,'headerImage','');	
		
	}
/* ________________________________________________ */
//	Name 		- fnUpdateDateTo --------------------------
//  Purpose		- 
//	Args 		- 
//	Returns		- 
/* _______________________________________________ */
	function fnUpdateDateTo(day,month,year,duration) {
		
		//Call the update dateto field routine
		var theString="/shared/js/ajax/common.asp?action=updateDateTo&dday="+day+"&dmonth="+month+"&dyear="+year+"&dduration="+duration;
		getDoc(theString,'dateToDiv','');	
		
	}
	
/* ________________________________________________ */
//	Name 		- fnUpdateDateToOneField --------------------------
//  Purpose		- same as old one, but for Tigra date consolidated field
//	Args 		- 
//	Returns		- 
/* _______________________________________________ */
	function fnUpdateDateToOneField(dateFrom,duration) {
		
		//Call the update dateto field routine
		var theString="/shared/js/ajax/common.asp?action=updateDateToOneField&dateFrom="+dateFrom+"&dduration="+duration;
		getDoc(theString,'dateToDiv','');	
		
	}

/* ________________________________________________ */
//	Name 		- fnDrawSaveSearchForm --------------------------
//  Purpose		- 
//	Args 		- 
//	Returns		- 
/* _______________________________________________ */
	function fnDrawSaveSearchForm(memId) {	
		
		var theString="/shared/js/ajax/common.asp?action=drawSaveSearchForm&memId="+memId;
		getDoc(theString,'userTools','');	
		
	}
	
/* ________________________________________________ */
//	Name 		- fnSaveJobSearch --------------------------
//  Purpose		- 
//	Args 		- 
//	Returns		- 
/* _______________________________________________ */
	function fnSaveJobSearch(memId,searchName) {
		
		var theString="/shared/js/ajax/common.asp?action=saveJobSearch&memId="+memId+"&searchName="+searchName;
		getDoc(theString,'userTools2','');	
		
	}
	
	
	
/* ________________________________________________ */
//	Name 		- fnDrawSaveJobSearchForm --------------------------
//  Purpose		- 
//	Args 		- 
//	Returns		- 
/* _______________________________________________ */
	function fnDrawSaveJobSearchForm(memId) {	
		
		var theString="/shared/js/ajax/common.asp?action=drawSaveJobSearchForm&memId="+memId;
		getDoc(theString,'userTools2','');	
		
	}

/* ________________________________________________ */
//	Name 		- fnSaveSearch --------------------------
//  Purpose		- 
//	Args 		- 
//	Returns		- 
/* _______________________________________________ */
	function fnSaveSearch(memId,searchName) {
		
		var theString="/shared/js/ajax/common.asp?action=saveSearch&memId="+memId+"&searchName="+searchName;
		getDoc(theString,'userTools','');	
		
	}
	
	/* ________________________________________________ */
//	Name 		- fnShowCalendar --------------------------
//  Purpose		- 
//	Args 		- focusDate
//	Returns		- 
/* _______________________________________________ */
	function fnShowCalendar(focusDate,pubCode) {
		
		var theString="/shared/js/ajax/common.asp?action=showCalendar&focusDate="+focusDate+"&pubCode="+pubCode;
		getDoc(theString,'calendarDiv','');	
		
	}
	
/* ________________________________________________ */
//	Name 		- fnGetDashboardTab --------------------------
//  Purpose		- Gets the dynamic Org Dashboard tabs
//	Args 		- 
//	Returns		- 
/* _______________________________________________ */
	function fnGetDashboardTab(tab,args) {
		var theString = "", theString2 = "";
		var voidArgs = args;
		if (args == "divToVoid") {
			args = "";
		} else {
			voidArgs = "";	
		}
		theString="/shared/js/ajax/common.asp?action=getDashboardTab&tab="+tab+"&args="+args;
		getDoc(theString,'dashboardBody','');	
		if (voidArgs == "divToVoid") {
			theString2 = "/shared/js/ajax/common.asp?action=divToVoid";
			getDoc(theString2,'dashboardSubBody','');	
		}
		
	}

/* ________________________________________________ */
//	Name 		- fnGetDashboardSubBody --------------------------
//  Purpose		- Gets the dynamic div pertaining to jobs||classifieds||etc...
//	Args 		- 
//	Returns		- 
/* _______________________________________________ */
	function fnGetDashboardSubBody(section,args) {
		
		var theString="/shared/js/ajax/common.asp?action=getDashboardSubBody&section="+section+"&args="+args;
		getDoc(theString,'dashboardSubBody','loaderSub');	
		
	}

/* ________________________________________________ */
//	Name 		- fnGetAjaxNewsTab --------------------------
//  Purpose		- 
//	Args 		- 
//	Returns		- 
/* _______________________________________________ */
	function fnGetAjaxNewsTab(qs, nEncodeDecode, source, nSiteSection, strDiv) {
		var theString = "";
		var theWorkingDiv = '';
		
		if (nEncodeDecode == 1) {
			qs = myURL.encode(qs);
		} else if (nEncodeDecode == 2) {
			qs = myURL.decode(qs);
		} else { 
			//do nuffin mayte
		}

		//Front page lists
		if (qs.indexOf("newsTabHeadlinesOnly") >= 0) {
			theString = "/shared/js/ajax/common.asp?action=newsTabHeadlinesOnly&tabs=1&sc=" + source + "&section=" + nSiteSection;
			theWorkingDiv = 'ajaxImage';
			//window.prompt("Ajax call:",theString);
		//List pages || Library	
		} else {
			theWorkingDiv = 'ajaxImage';
			if (qs.indexOf("action=newsTab") >= 0) {
				theString = "/shared/js/ajax/common.asp?"+qs;
				//window.alert("1: "+theString);
			} else {
				theString = "/shared/js/ajax/common.asp?action=newsTab&tabs=1&"+qs;
				if (qs.indexOf("sc=") < 0) {
					theString += "&sc="+source;
				}
				//window.alert("2: "+theString);
			}
			//(@_@) Remove sc from qs and reformulate for tab correctly
			//window.alert("1:"+theString);
			if (theString.indexOf("&sc=") == (theString.length-4) || theString.indexOf("&sc=") == (theString.length-5)) {
				theString = theString.substring(0, theString.indexOf("&sc="));
			//window.alert("2:"+theString);
			}
			if (theString.indexOf("sc=") < 0) {
				theString += "&sc=" + source;
				//window.alert("3: "+theString);
			}
		}
		//window.prompt("",theString);
		getDoc(theString,strDiv,theWorkingDiv);
	}
/* ________________________________________________ */
//	Name 		- fnGetAjaxFormPost --------------------------
//  Purpose		- Doing a form post from an ajax box
//	Args 		- form name
//	Returns		- 
/* _______________________________________________ */
   function fnGetAjaxFormPost(obj, strDiv, theWorkingDiv, tAction) {
      var getstr = "/shared/js/ajax/common.asp?action=" + tAction + "&";
		var j;
	for (i=0; i<obj.childNodes.length; i++) {
	  //window.alert("tagName:"+obj.childNodes[i].tagName);
	  	 if (obj.childNodes[i].tagName == "FIELDSET") {
			 var n = obj.childNodes[i];
			for (j=0; j<n.childNodes.length; j++) {
	  			//window.alert("FIELDSET children:"+n.childNodes[j].tagName);
				 if (n.childNodes[j].tagName == "INPUT") {
					if (n.childNodes[j].type == "text" || n.childNodes[j].type == "hidden") {
					   getstr += n.childNodes[j].name + "=" + n.childNodes[j].value + "&";
					}
					if (n.childNodes[j].type == "checkbox") {
					   if (n.childNodes[j].checked) {
						  getstr += n.childNodes[j].name + "=" + n.childNodes[j].value + "&";
					   } else {
						  getstr += n.childNodes[j].name + "=&";
					   }
					}
					if (n.childNodes[j].type == "radio") {
					   if (n.childNodes[j].checked) {
						  getstr += n.childNodes[j].name + "=" + n.childNodes[j].value + "&";
					   }
					}
				 }   
				 if (n.childNodes[j].tagName == "TEXTAREA") {
					   getstr += n.childNodes[j].name + "=" + n.childNodes[j].value + "&";
				 }   
				 if (n.childNodes[j].tagName == "SELECT") {
					var sel = n.childNodes[j];
					getstr += sel.name + "=" + sel.options[sel.selectedIndex].value + "&";
				 }
			}
			 
		 } else {
	  
			 if (obj.childNodes[i].tagName == "INPUT") {
				if (obj.childNodes[i].type == "text" || obj.childNodes[i].type == "hidden") {
				   getstr += obj.childNodes[i].name + "=" + obj.childNodes[i].value + "&";
				}
				if (obj.childNodes[i].type == "checkbox") {
				   if (obj.childNodes[i].checked) {
					  getstr += obj.childNodes[i].name + "=" + obj.childNodes[i].value + "&";
				   } else {
					  getstr += obj.childNodes[i].name + "=&";
				   }
				}
				if (obj.childNodes[i].type == "radio") {
				   if (obj.childNodes[i].checked) {
					  getstr += obj.childNodes[i].name + "=" + obj.childNodes[i].value + "&";
				   }
				}
			 }   
			 if (obj.childNodes[i].tagName == "TEXTAREA") {
				   getstr += obj.childNodes[i].name + "=" + obj.childNodes[i].value + "&";
			 }   
			 if (obj.childNodes[i].tagName == "SELECT") {
				var sel = obj.childNodes[i];
				getstr += sel.name + "=" + sel.options[sel.selectedIndex].value + "&";
			 }
		 }
         
      }
	  //window.alert(getstr);
      getDoc(getstr,strDiv,theWorkingDiv);
   }
