/*
 * 
 * @param {Object} arrayObject
 * Functie om te bepalen of een object een array is
 * Input  : Obect
 * Output : true indien een array
 */
function isArray(arrayObject) {
	if (arrayObject==null || typeof(arrayObject)!="object" || typeof(arrayObject.length)!="number"){
		return false;
	} else {
		return true;
	}
}

function clearDefault(el) {
	if (el.defaultValue==el.value) el.value = ""
	}
	
var windowName="sabportal";

function launchPopUp(filename, windowParms, newWindow) {
	var parms=new Array();
	parms["resizable"]="yes";
	parms["toolbar"]=1;
	parms["directories"]=0;
	parms["location"]=1;
	parms["status"]=1; 
	parms["menuBar"]=1; 
	parms["scrollbars"]="yes";
	parms["top"]=10; 
	parms["left"]=10;
	parms["width"]=880;
	parms["height"]=600;
	if (windowParms){
		if (windowParms !== ""){
			windowParms=trim(windowParms);
			var parmsin=new Array();
			parmsin = windowParms.split(",");
			i=0;
			var tempArray = new Array();
			for (pair in parmsin) {
				tempArray = parmsin[pair].split("=");
				parms[tempArray[0]]=tempArray[1];
			}
		}
	}
	var winspecs = "";
	for (key in parms){
		winspecs = winspecs + key + "=" + parms[key] + ", ";
	}
	var winName = "windowname"; 
	if (newWindow){
		if (newWindow == 'true'){
			winName = "";
		}
	}
	windowName = window.open(filename, winName, winspecs);
	windowName.focus();
}

function trim(string) { 
	var tstring = "";
	string = '' + string;
	splitstring = string.split(" ");
	for(i = 0; i < splitstring.length; i++)	tstring += splitstring[i];
	return tstring;
} 
	
function closePopUpWindow(){
	if (windowName && windowName.open && !windowName.closed) windowName.close();
	}
	
function setZoomWindowWidth() {
	 var winW = 630;

	if (parseInt(navigator.appVersion)>3) {
	 if (navigator.appName=="Netscape") {
	  winW = window.innerWidth-25;
	 }
	 if (navigator.appName.indexOf("Microsoft")!=-1) {
	  winW = document.body.offsetWidth-20;
	 }
	}
	return winW;
}

function setZoomWindowHeight() {
	var winH = 460;
	
	if (typeof $ == 'function')
		return $(window).height()-20;
	
	if (parseInt(navigator.appVersion)>3) {
		if (navigator.appName=="Netscape") {
			winH = window.innerHeight-20;
		}
		if (navigator.appName.indexOf("Microsoft")!=-1) {
			winH = document.body.offsetHeight-20;
		}
	}
	return winH;
}

function SelectAll(id) {
    document.getElementById(id).focus();
    document.getElementById(id).select();
	}
		
function _EXT_String_trim() {
	 var s = this;
	 if (s.length > 0) while (s.indexOf(' ') == 0) s = s.substr(1);
	 if (s.length > 0) while(s.lastIndexOf(' ') == s.length - 1) s = s.substr(0, s.length - 1);
	 return s;
}

String.prototype.trim = _EXT_String_trim;

// Check of meegegeven datum voldoet aan format dd-mm-yyyy.
// Zoniet maak megegeven veld leeg en geef alert.
function checkDate(dateField){
	errorDescription = "de ingevoerde datum is niet geldig DD-MM-YYYY";
	myDate = dateField.value;
	myDate = myDate.replace(/ /g,'-');
	myDate = myDate.replace(/\//g,'-');

	intFirst = myDate.indexOf('-');
	intSec   = myDate.lastIndexOf('-');
	intLen   = myDate.length;

	day      = myDate.substr(0,intFirst);
	month    = myDate.substr(intFirst+1,intSec-(intFirst+1));
	year     = myDate.substr(intSec+1,intLen-intSec);
	
	myDateField = new Date();
	try {
		myDateField.setYear(year);
		myDateField.setMonth(month-1);
		myDateField.setDate(day);
	} catch(err) {
		alert(errorDescription);
		dateField.value="";
		return false;
	}

	if(!(day==myDateField.getDate() && month==myDateField.getMonth()+1)){
		alert(errorDescription);
		dateField.value="";
		return false;
	}
	return true;
}

// Submit EBSCO search query, opening a new window.
// Using this function makes the external EBSCO javascript file @ "http://support.ebscohost.com/eit/scripts/ebscohostsearch.js" unnecessary.
// Since EBSCO can update their search engine at any time, use the file mentioned above (could be updated through time) if something stops
// functioning.
function executeEBSCOSearch(form) {
	if (form.ebscohostsearchtext.value != '') {
		var ebscoHostUrl = form.ebscohosturl.value + '&bquery=(' + form.ebscohostsearchtext.value + ')';
		window.open(ebscoHostUrl, 'EBSCOhost');
	}
	return false;
}

// Function for updating the "toggle" images used for displaying detailed information through AJAX calls.
// Called from "dspAjaxCallInDiv" in "_ajax_lib.cfm".
function toggleImage(toggleImg) {
	if (!document.getElementById(toggleImg)) return;
	
	var file = document.getElementById(toggleImg);
	
	// Determine the current state by checking the filename of the image and set the newState accordingly.
	var newState = toggleState(toggleImg) == 'closed' ? 'open' : 'closed';
	
	// Update the image.
	file.src = file.src.replace(/(\.)(open|closed)(\.(png|jpg|gif))$/i, "$1" + newState + "$3");
	file.alt = newState == 'open' ? '[-]' : '[+]';
}
function toggleState(toggleImg) {
	if (!document.getElementById(toggleImg)) return false;
	return /\.open\.(png|jpg|gif)$/i.test(document.getElementById(toggleImg).src) ? 'open' : 'closed';
}

function wrapperIsOpen (id) {
	var o = document.getElementById(id+'Wrapper') ? document.getElementById(id+'Wrapper') : document.getElementById(id) ? document.getElementById(id) : false;
	if (typeof(o) == 'boolean') return false;
	return o.style.display == 'block';
}

function showOverlay(obj, id) {
	if (!obj || !document.getElementById('cover-'+id) || (obj.naturalHeight && obj.naturalHeight == 1)) 
		return;
	
	id = 'cover-'+id;
	
	var offsetOverlay = Ext.get(obj).getXY();
	var offsetContainer = Ext.get(obj).findParent('div', 5, true).getXY();
	
	var offsetY = offsetOverlay[1] - offsetContainer[1] - 4;
	var offsetX = 6;
	
	var yOffset = offsetContainer[1];
	
	// 550 is the maximum Y-offset where an enlarged cover fits inside the viewstack. If the LI is below that Y-offset, the enlargement might fall outside the accordion panel.
	if (offsetOverlay[1] > 550)
		offsetY -= 110;
	
	with (document.getElementById(id)) {
		style.display = 'block';
		style.left = offsetX.toString()+'px';
		style.top = offsetY.toString()+'px';
		onmouseout = function(event){
			hideOverlay(this);
		}
	}
	//Ext.get(obj).scrollIntoView(Ext.get(obj).findParent('div', 5, true), false);
}
function hideOverlay (obj) {
	obj.style.display = 'none';
}

function checkImage (obj) {
	// This means we have a blank image, not the actual cover.
	if (Ext.get(obj).getSize().width <= 1) {
		// Remove it from the DOM.
		obj.parentNode.removeChild(obj);
	} else {
		// Set a nice width and height if we DO have a cover.
		obj.style.width = '13px';
		obj.style.height = '18px';
	}
}

/*
 * This collection of function is used for storing the iProfile (BiebQuest) settings.
 * Geek note: this is the first function with advanced functionality to use jQuery instead of ExtJS!
 */
function updateIprofileNotification (setting, value, obj) {
	// Disable the object.
	$(obj).attr('disabled','disabled');
	
	// Call AJAX script to store this setting.
	$.post('?method=c_iprofile.act_set_iprofile_notifications', { setting:setting , value:value }, function(data){
		var bgColor = data.success ? '#a7d237' : '#d23737';
		
		$('#'+setting+'_feedback').css('background-color', bgColor)
			.html(data.message)
			.slideDown('slow')
			.delay(2000)
			.slideUp('slow', function(){
				// Enable the object.
				$(obj).removeAttr('disabled');
				if (!data.success && $(obj).is(':checkbox')) {
					$(obj).attr('checked', !$(obj).is(':checked'));
				}
			});
	}, 'json');
}


/*
 	handleAjaxForm:
		Handle a standard form that returns a json object "json.message". 
		Javascript does not support overloading so even if you don't use it, you still need to supply a false!
	
	Created by:
		Tristan P.
	
	Creation date:
		2011-12-21
	
	Last modified:
		2011-12-22
	
	Proper initiation:
		jQuery(document).ready(function(){
			$('form').submit(function(e){
				handleAjaxForm(e, this, true);
				OR
				handleAjaxForm(e, this, false);
				OR
				handleAjaxForm(e, this, "http://www.URL.nl/");
			});
		});
	
	Expects:
		json.message
*/
function handleAjaxForm(
	e, 							/* Event */ 
	formData, 					/* Form data */
	reloadOrGoToPage){ 			/* Reload page true or false or specify url to redirect to */
	var formContent = $(formData).serialize();
	var formAction = $(formData).attr('action');

	e.preventDefault();
		
	/*	Finds all form fields known to man and disables them (input, textarea, select, etc.). */
	$(" :input", formData).attr('disabled', true);

	$.post(formAction, formContent, function(data){
		$dialog = $('<div/>')
		.attr('id', 'dialog-modal')
		.css('display', 'none')
		.html( data.message )
		.dialog({
			autoOpen: false,
			modal: true,
			buttons: {
				Ok: function(){
					$(this).dialog('close');
				}
			},
			beforeClose: function() {
					switch(reloadOrGoToPage){
						case true:
							location.reload(true);
							break;
						case false:
							break;
						default:
							window.location.replace(reloadOrGoToPage);
							break;
					}
					
					$(formData)[0].reset();
					$(" :input", formData).attr('disabled', false);								
			},
			close: function() {
				$('#dialog-modal').dialog('destroy').remove();
			}
		})
		.dialog('open');
	}, 'json');
}

