/**
*	Called in index.php of engine
*	Contains all javascript functions which will be used all over engine
**/

/* Pop up block checker */
function checkOpenWindow (openWin)
{
    var exists = false;
    if (openWin && !openWin.closed)
    {
        exists = true;
        try {
            openWin.focus();
        }
        catch (e) {
            //keepFocus = false;
            exists = false;
        }
        //if (!keepfocus)
        //{
         //   window.focus();
        //}
    }
    if (!exists)
    {
    	//Fire alert and print message on screen
        alert("Pop up blocker was detected, please disable this to continue.");
        document.write('<span style="color: #ff0000"><b>This website needs to open a window to continue with your image upload. Please disable your popup blocker.</b></span>');
    }
}

/*** USED IN MEDIA UPLOAD ***/

function getOptionValue(obj)
{
	var index0, index1;
	if (obj.name == "media_type")
	{
		index0 = document.img_upload.media_type.selectedIndex;
		value0 = document.img_upload.media_type[document.img_upload.media_type.selectedIndex].value;

 		if(value0 == "i") {
			document.img_upload.edit_option1.disabled=false;
			//document.img_upload.edit_option2.disabled=false;
			document.img_upload.edit_option3.disabled=false;
			
			//document.img_upload.fixed_sizes.disabled=false;
 		}
 		else {
			document.img_upload.edit_option1.disabled=true;
			//document.img_upload.edit_option2.disabled=true;
			document.img_upload.edit_option3.disabled=true;
			
			//document.img_upload.fixed_sizes.disabled=false;
 		}
	} 
}

function enableSize(){
	if (document.img_upload.edit_option1.checked){
		document.img_upload.fixed_sizes.disabled=false;
	}else{
		document.img_upload.fixed_sizes.disabled=true;
	}
}

function showLoading() {
	document.loading = eval('document.getElementById("processing")');
	document.loading.style.visibility = "visible";
}

/*** End functions used in MEDIA UPLOAD ***/

/*** USED IN BEN UPDATE ***/

//Tells IE browsers that attach image button has been pressed
function attachClickSubmit() {
document.getElementById('attach').value=1;
}
//Tells IE browsers that save button has been clicked
function formSave() {
document.getElementById('attach').value=0;
}

/*** USED in USERGROUP / USERS MANAGEMENT ***/

//Check ALL textboxes, uses name as identifier
function checkAll(checkAllState, field) {
	if(field.length == undefined) {
		field.checked = checkAllState.checked;
	}
	for (i = 0; i < field.length; i++) {
		field[i].checked = checkAllState.checked;
	}
}

//Check all checkboxes in a GROUP, uses ID as identifier
function checkAllGroup(checkAllState, cbGroup) {
	if(cbGroup.length == undefined) {
		cbGroup.checked = checkAllState.checked;
	}
	for (i = 0; i < cbGroup.length; i++)
	{
		cbGroup[i].checked = checkAllState.checked;
	}
}

//Set max tags to be checked
function limitCheckbox(checkObj, maxChecks) {
	var checkCount = 0;
	var itemNum = 1;

	//Determine the number of checked boxes
	while (document.getElementById('item'+itemNum)) {
		fieldObj = document.getElementById('item'+itemNum);
		if (fieldObj.checked) {
			checkCount++;
		}
		itemNum++;
	}

	var itemNum = 1;
	//Enable/disable unchecked boxes
	while (document.getElementById('item'+itemNum)) {
		fieldObj = document.getElementById('item'+itemNum);
		if (!fieldObj.checked)	{
			fieldObj.disabled = (checkCount==maxChecks);
		}
		itemNum++;
	}
}

/*Check file upload in dbimport*/
function checkFileUpload(){
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			if ((document.fileUpload.filename.value != 0) ||(document.fileUpload.filename.value != null)) {
				var ajaxDisplay = document.getElementById('formSubmit');
				ajaxDisplay.innerHTML = ajaxRequest.responseText;
			}
		}
	}
	ajaxRequest.open("GET", "js/check_upload.php", true);
	ajaxRequest.send(null); 
}