var scajax = 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;
}

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

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

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


/***********************************************************
* select image in rte_image pop-up window
***********************************************************/
function selectLibraryImage(id) {
	scajax.open('get','ajax.php?action=selectLibraryImage&id='+id);
	scajax.onreadystatechange = selectLibraryImageResponse;
	scajax.send(null);
}
function selectLibraryImageResponse() {
	if(scajax.readyState == 4){
		// this is the content of the called page
        var response = scajax.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() {
	scajax.open('get','ajax.php?action=selectLibraryParentCategory');
	scajax.onreadystatechange = selectLibraryParentCategoryResponse;
	scajax.send(null);
}
function selectLibraryParentCategoryResponse() {
	if(scajax.readyState == 4){
		// this is the content of the called page
        var response = scajax.responseText;

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


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

/***********************************************************
* Workorders - mark complete, basecamp, billed
***********************************************************/
function selectWorkorderAction(action, check_id) {
	if( document.getElementById(check_id).checked == '1' ){
		if( check_id == 'completed_check' )
			var con = confirm('Are you sure that you want to mark this workorder as completed?\n\nThe workorder will be moved to the archive of completed workorders.');
		else if( check_id == 'basecamp_check' )
			var con = confirm('Are you sure that you want to mark this workorder as setup in Basecamp?');
		else if( check_id == 'billed_check' )
			var con = confirm('Are you sure that you want to mark this workorder as having been billed?');
		else
			var con = true;
			
		if( con == false ){ document.getElementById(check_id).checked=''; return false; }
		var todo = 'set';
	}
	else{
		var todo = 'clear'
	}

	scajax.open('get','workorder_ajax.php?'+action+'&action='+todo);
	scajax.onreadystatechange = selectWorkorderActionResponse;
	scajax.send(null);
}
function selectWorkorderActionResponse() {
	if(scajax.readyState == 4){
		// this is the content of the called page
        var response = scajax.responseText;

       	//process the response
       	if( response ) {
			var res = response.split("|");
			/*
			r[0] = id
			r[1] = action
			r[2] = type
			r[3] = name
			r[4] = date
			*/
			//alert(res[0]+' '+res[1]+' '+res[2]+' '+res[3]+' '+res[4]);
			if( res[1] == 'clear' ){
				document.getElementById('info_'+res[2]).innerHTML = '';
				document.getElementById('info_'+res[2]).style.display = 'none';
			}
			else{
				document.getElementById('info_'+res[2]).style.display = 'block';
				document.getElementById('info_'+res[2]).innerHTML = res[3]+' - '+res[4];
			}
			
       	}
    }
}