// Tracks all opened pop-up windows
var popup_id = 0;

// Keeps track of locked input fields to make CSS play nicely
var fields = new Array();

// Opens the formatted help window
function showXHelp(title, message) {
	xWin = window.open("", "Xhelp" + popup_id, 'width=200,height=200,toolbar=no,menubar=no,directories=no,resizable=yes,status=no,scrollbars=yes');
	xWin.document.write("<link href='style.css' rel='stylesheet' type='text/css'>");
	xWin.document.write("<table border='0' align='center' width='100%' ><tr>" +
	"<td align='center' class='ad-btn'><b>"+unescape(title)+"</b></td></tr><tr><td align='left' bgcolor='#FFFFFF' style='padding:0.6em;text-align:center;'><br />");
	xWin.document.write(unescape(message));
	xWin.document.write("<br /><br /></td></tr><tr><td align='center'><a href='javascript:self.close()'>Close Help</a></td></tr></table>");
	xWin.document.bgColor="#ffffff";
	xWin.document.close();
	popup_id++;
}

// Wrapper for showXhelp
// Deprecated
function showHelp(title, message) {
	showXHelp(title, message);
}

// Displays the help icon as a link to launch showHelp
function showHelpIcon(title, message) {
	document.writeln("<a href=\"#\"><img valign=\"bottom\" src=\"images/help.gif\" title=\""+message+"\" onClick=\"showXHelp(\'"+escape(title)+"\', \'"+escape(message)+"\');\" border=\"0\"></a>");
}

// Uses URL encoding to make sure the icon will display within a javascript context

// Is this deprecated? I don't remember where I used this.
function showHelpIcon2(title, message) {
	return("<a style=cursor:pointer;cursor:hand;><img valign=\"middle\" src=\"images/help.gif\" title=\""+message+"\" onClick=\"showXHelp(\'"+escape(title)+"\', \'"+escape(message)+"\');\" border=\"0\"></a>");
}

// Advanced pop-up script that can detect if pop-up blocking is enabled.
// It is also known to work well on dual-head systems
function popup(uri, width, height, id) {
	x = (window.screen.availWidth - width)/4;
	y = (window.screen.availHeight - height)/2;
	popup_window = window.open(uri + "&noheader=1", 'eli'+id+popup_id, 'width='+width+',height='+height+',toolbar=no,menubar=no,directories=no,resizable=yes,status=no,scrollbars=yes,left='+x+',top='+y+',screenX='+x+',screenY='+y);
	if (!popup_window) {
		alert("Pop-up Windows must be allowed in order to perform this action. Please turn off pop-up blocking in your web-browser.");
	} else {
		popup_id += 1;
	}
}


// Advanced pop-up script that can detect if pop-up blocking is enabled.
// It is also known to work well on dual-head systems
// Shows menubar. Good for printing.
function popup_full(uri, width, height, id) {
	x = (window.screen.availWidth - width)/4;
	y = 10;
	popup_window = window.open(uri + "&noheader=1", 'eli'+id+popup_id, 'width='+width+',height='+height+',toolbar=no,menubar=yes,directories=no,resizable=yes,status=yes,scrollbars=yes,left='+x+',top='+y+',screenX='+x+',screenY='+y);
	if (!popup_window) {
		alert("Pop-up Windows must be allowed in order to perform this action. Please turn off pop-up blocking in your web-browser.");
	} else {
		popup_id += 1;
	}
}

var ie4 = false; if(document.all) { ie4 = true; }
function getObject(id) { if (ie4) { return document.all[id]; } else { return document.getElementById(id); } }


// Return true if the second parameter is part of the array in parameter 1
function in_array(array, val) {
	var i;
	for (i = 0; i < array.length; i++) {
		if (array[i] == val) {
			return true;
		}
	}
	return false;
}

// Deletes the array element in argument 1 that has the value passed in argument 2
function array_del(array, val) {
	var i;
	for (i = 0; i < array.length; i++) {
		if (array[i] == val) {
			array[i] = '';
		}
	}
}

// Pushes the value of argument 2 in array in argument 1
function array_push(array, val) {
	array[array.length] = val;
}

// Returns the human-readable representation of an array's content
function dump(array) {
	var temp = '';
	var i;
	
	for (i = 0; i < array.length; i++) {
		temp += i + " => " + array[i] + "\n";
	}
	
	return temp;
}

// Returns the last element of the array that has a value, then deletes it.
function array_pop(array) {
	var i;
	for (i = array.length; i > 0; i--) {
		if (array[i] != '') {
			var temp = array[i];
			array[i] = '';
			return temp;
		}
	}
}

// Expand/Collapse the DIV layer passed as argument
function toggle(divId) { 
	var d = getObject(divId);
	if (in_array(expanded, divId)) {
		// collapsing
		d.style.display = 'none';
		array_del(expanded, divId);
		
	} else {
		// expanding
		d.style.display = 'block';
		array_push(expanded, divId);	
	}
	//var tf = getObject('txt');
	//tf.value = dump(expanded);
}

// Returns True if a field is in locked state, false otherwise
function is_locked(field) {
	return in_array(fields, field);
}

// Sets a field in lock state.
// This prevents onMouseOut to override onFocus and onBlur
function lock_field(field) {
	array_push(fields, field);
}

// Unlocks a field (use this on onBlur)
function unlock_field(field) {
	array_del(fields, field);
}

// Checks if the email address passed as argument is valid or not
function isValidEmail(str) {
	var email=/^[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*@[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*\.([A-Za-z]){2,4}$/i;
	return(email.test(str));
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


// Opens links in new page IF the frmnewpage.newpage is checked.
function test(URL){
if (document.frmnewpage.newpage.checked==true)
   { 
     window.open(URL,"Cronos",
      "width=600,height=550,scrollbars=yes,toolbar=no,status=yes,resizable=yes,menubar=no,location=no");

   }
else
   {
     window.open(URL, "_parent");
   }
}

//popup(uri, width, height, id) {
function __invoices__schedule_instalments(num) {
	if (isNaN(parseInt(num))) {
		alert("Error: You must select a number of instalments to schedule");
	} else {
		popup("popup.php?module=invoice_management&action=display_instalments_schedule_dialog&num="+num, 640, 480, 'inst');
	}
}
