var gBajax = createRequest();

function createRequest() {
	var obj;
	var browser = navigator.appName;
	if(browser == "Microsoft Internet Explorer"){
        obj = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        obj = new XMLHttpRequest();
    }
    return obj;
}

/***********************************************************
* select folder in rte_image pop-up window
***********************************************************/
function selectLibraryFolder(folder, fran) {
	gBajax.open('get','ajax.php?franchise=' + fran + '&action=selectLibraryFolder&folder='+folder);
	gBajax.onreadystatechange = selectLibraryFolderResponse;
	gBajax.send(null);
}
function selectLibraryFolderResponse() {
	if(gBajax.readyState == 4){
		// this is the content of the called page
        var response = gBajax.responseText;

       	//process the response
       	if( response ) {
       		document.getElementById('rte_library').innerHTML = response;
       	}
    }
}


/***********************************************************
* select image in rte_image pop-up window
***********************************************************/
function selectLibraryImage(id, fran) {
	gBajax.open('get','ajax.php?franchise=' + fran + '&action=selectLibraryImage&id='+id);
	gBajax.onreadystatechange = selectLibraryImageResponse;
	gBajax.send(null);
}
function selectLibraryImageResponse() {
	if(gBajax.readyState == 4){
		// this is the content of the called page
        var response = gBajax.responseText;

       	//process the response
       	if( response ) {
			var res = response.split("|");
			/*
			r[0] = id of image or "no"
			r[1] = url of image
			r[2] = alt of image
			*/
			if( res[0] == 'no' ) {
				alert('The selected image could not be found. Please try again.');
			}
			else {
				document.getElementById('selected_image_url').value = res[1];
				document.getElementById('selected_image_alt').value = res[2];
				var tables = document.getElementById('thumbnails').getElementsByTagName('table');
				for( var t=0; t < tables.length; t++ ) {
					tables[t].className = '';
				}
				document.getElementById('image_' + res[0]).className = 'selected_image';
			}
       	}
    }
}


/***********************************************************
* select folder in rte_image pop-up window
***********************************************************/
function selectLibraryParentCategory(fran) {
	gBajax.open('get','ajax.php?franchise=' + fran + '&action=selectLibraryParentCategory');
	gBajax.onreadystatechange = selectLibraryParentCategoryResponse;
	gBajax.send(null);
}
function selectLibraryParentCategoryResponse() {
	if(gBajax.readyState == 4){
		// this is the content of the called page
        var response = gBajax.responseText;

       	//process the response
       	if( response ) {
       		document.getElementById('rte_library').innerHTML = response;
       	}
    }
}


/***********************************************************
* cycle through pages of images
***********************************************************/
function cycleLibraryImages(start, fran) {
	gBajax.open('get','ajax.php?franchise=' + fran + '&action=getLibraryThumbnails'+start);
	gBajax.onreadystatechange = cycleLibraryImagesResponse;
	gBajax.send(null);
}
function cycleLibraryImagesResponse() {
	if(gBajax.readyState == 4){
		// this is the content of the called page
        var response = gBajax.responseText;
       	//process the response
       	if( response ) {
       		document.getElementById('rte_library').innerHTML = response;
       	}
    }
}

/***********************************************************
 * regenerate captcha image
 ***********************************************************/
function regenerateCaptcha() {
	gBajax.open('get','ajax.php?action=captcha');
	gBajax.onreadystatechange = regenerateCaptchaResponse;
	gBajax.send(null);
}
function regenerateCaptchaResponse() {
	if(gBajax.readyState == 4){
		// this is the content of the called page
		var response = gBajax.responseText;
		//process the response
		if( response ) {
			document.getElementById('captcha_img').src = response;
		}
	}
}