var browser = new detect_browser();

function ConfirmCancel(CancelURL)
{
	if(confirm('WARNING: Are you sure you want to cancel what you are doing? Click OK to proceed.')) {
		redirect(CancelURL);
	}
}

Array.prototype.inArray = function(value, pos)
{
	if(this.constructor.toString().indexOf("Array") == -1) {
		return false;
	}

	for(z = 0; z < this.length; z++) {
		if(this[z].toString() === value.toString()) {
			if(pos == true) {
				return z;
			}
			return true;
		}
	}
	return false;
}

var sa_count = 0;
var sa_current = 0;
var sa_form = '';

function select_all(frm_id)
{
	sa_form = frm_id;

	var frm = gId(frm_id);
	var sa  = gId('select_all');
	// ensure both the form and select_all object exist
	if(frm && sa) {
		// onclick functionality for select_all checkbox

 		sa.onclick = function()
 		{
			var chked = this.checked;
			// update the current amount
			if(chked) {
				sa_current = sa_count;
			} else {
				sa_current = 0;
			}
			// loop though each checkbox, and update its checked status
			var chkbx = gTagName('input', gId(sa_form));
			for(var i = 0; i < chkbx.length; i++) {
				var inp_chk = chkbx.item(i);
				if(inp_chk.type == 'checkbox' && inp_chk.disabled == false) {
					inp_chk.checked = chked;
				}
			}
 		}

 		// loop though the input elements
 		//debugger;
 		var chkbx = gTagName('input', gId(sa_form));
 		for(var i = 0; i < chkbx.length; i++) {
 			var inp_chk = chkbx.item(i);
 			if(inp_chk.type == 'checkbox' && inp_chk.id != 'select_all' && inp_chk.disabled == false) {
 				sa_count++;
 				inp_chk.onclick = function()
 				{
 					if(this.checked) {
 						sa_current++;
 					} else {
 						sa_current--;
 					}

 					if(sa_current == sa_count) {
 						gId('select_all').checked = true;
 					} else {
 						gId('select_all').checked = false;
 					}
 				}
 			}
 		}
 		// disable the check all if there is no data
 		if(sa_count == 0) {
 			sa.disabled = true;
 		}
	}
}

// gets the value of the selected checkbox from the select_all list
// only one checkbox may be selected
function get_selected_value()
{
	if(sa_current == 1) {
		var frm = gId(sa_form);
		var inp = gTagName('input', frm);
		for(i = 0; i < inp.length; i++) {
			if(inp[i].type == 'checkbox' && inp[i].id != 'select_all' && inp[i].disabled == false && inp[i].checked == true) {
				return inp[i].value;
			}
		}
	}
	return false;
}

// init the dws functionality and mouseovers
function do_with_selected()
{
	var dws = gId('dws');
	if(dws) {
		var buttons = gTagName('input', dws);
		// loop through the buttons
		for(var i = 0; i < buttons.length; i++) {
			var btn = buttons[i];
			btn.onmouseover = function()
			{
				this.className = 'hilite';
			}
			btn.onmouseout = function()
			{
				this.className = '';
			}
		}
	}
}

// init the choice list mechanism
function choice_list(sel)
{
	sel = sel || 0;

	var clist	= gId('choice_list');
	var cli		= gTagName('li', clist);
	for(var i = 0; i < cli.length; i++) {

		if(sel == i) {
			cli[i].className = 'selected';
			gTagName('input', cli[i])[0].checked = true;
		}

		cli[i].onmouseover = function()
		{
			if(this.className != 'selected') {
				this.className = 'hilite';
			}
		}
		cli[i].onmouseout = function()
		{
			if(this.className != 'selected') {
				this.className = '';
			}
		}
		cli[i].onclick = function()
		{
			var cli = gTagName('li', gId('choice_list'));
			for(var i = 0; i < cli.length; i++) {
				cli[i].className = '';
			}
			this.className = 'selected';
		}
	}
}

/**
 * Redirect the browser to the provided url
 */
function redirect(to)
{
	location.href = to;
}

function expand(newsletter_id)
{
	obj = gId('newsletter_'+newsletter_id);
	if(obj.style.display == 'none') {
		obj.style.display = 'inline';
		gId('news_img_'+newsletter_id).src = 'images/minus.gif';
	} else {
		obj.style.display = 'none';
		gId('news_img_'+newsletter_id).src = 'images/plus.gif';
	}
}

// Import JS
function import_custom_password(obj)
{
	frm = document.frmSubscriber;
	if(obj) {
		frm.password_custom.disabled = false;
		frm.password_custom.focus();
	} else {
		frm.password_custom.disabled = true;
	}
}

// Configuration
function mail_send(what)
{
	frm = document.frmConfig;
	if(what == 'mail') {
		frm.sending_batch.disabled = false;
		frm.sending_batch.focus();

		frm.smtp_server.disabled = true;
		frm.smtp_port.disabled = true;
	} else {
		frm.smtp_server.disabled = false;
		frm.smtp_port.disabled = false;
		frm.smtp_server.focus();

		frm.sending_batch.disabled = true;
	}
}

// View Subscribers
function vs_subto(val)
{
	if(val == '##new##') {
		window.open('includes/quick_newsletter.php', 'quick_newsletter', 'height=420,width=400');
	} else if(val != '') {
		alert('');
	}
}

function vs_dowidth(what)
{
	if(what == 'selected') {
		document.all.do_with.innerHTML = 'Do With Selected Subscribers&nbsp;';
	} else {
		document.all.do_with.innerHTML = 'Do With Entire List&nbsp;';
	}
}

function vs_change_what(what)
{
	document.frm_do_with.what.value = what;
	document.frm_do_with.submit();
}

// Issues
function show_preview(issue_id, sess_id)
{
	window.open('preview.php?id='+ issue_id+'&sid='+sess_id, 'preview', 'width=780, height= 550, resizable=1, scrollbars=1');
}



function set_schedule()
{
	var frm = document.frmSend;
	frm.action = 'sendissue.php';
	frm.target = '';
	frm.onsubmit = 'return true';
	frm.what.value = 'set_schedule';
	frm.submit();
}

function show_menu(id)
{
	link = gId('menu').getElementsByTagName('DD');

	// disable all popups
	if(link) {
		for(i = 0; i < link.length; i++) {
			link[i].style.display = 'none';
		}
	}
	obj = gId(id);
	if(obj) {
		obj.style.display = 'block';
	}
}

	window.onload = show_menu;

function add_form_field(frm_obj, field_type, field_name, field_value)
{
  if (document.getElementById) {
    var input = document.createElement('INPUT');
      if (document.all) {
        input.type = field_type;
        input.name = field_name;
        input.value = field_value;
      } else if (document.getElementById) { // so here is the
                                          // NN6 workaround
        input.setAttribute('type', field_type);
        input.setAttribute('name', field_name);
        input.setAttribute('value', field_value);
      }
    frm_obj.appendChild(input);
  }
}

// shortcut for document.getElementById();
function gId(obj_id)
{
	return document.getElementById(obj_id);
}

// shortcut for document.getElementsByTagName();
// the object is optional (defaults to document
function gTagName(tag_name, obj)
{
	obj = obj || document;
	return obj.getElementsByTagName(tag_name);
}

// fade the selected object, then remove it from display
/**
 *
 * @param {Div} obj
 */
function fadeandclose(obj)
{
	opacity(obj.id, 100, 0, 600);
	setTimeout("gId('"+obj.id+"').style.display = 'none';", 805);
	obj.onclick = null;
}

function opacity(id, opacStart, opacEnd, millisec) {
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) {
        for(i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++)
            {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
    var object = gId(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
}

// form functions
function get_selected_option(id)
{
	var obj = gId(id);
	if(obj.selectedIndex >= 0) {
		return obj.options[obj.selectedIndex];
	}
	return false;
}

// debug functions
function minimise_debug()
{
	var debugwin = gId('debug');
	if(debugwin.style.height == '17px') {
		debugwin.style.height = '';
		gId('debug_content').style.display = 'block';
	} else {
		debugwin.style.height = '17px';
		gId('debug_content').style.display = 'none';
	}
}

// figure out the browser this is running in
function detect_browser()
{
  var ua, s, i;

  this.isIE    = false;
  this.isNS    = false;
  this.version = null;

  ua = navigator.userAgent;

  s = "MSIE";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isIE = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  // Treat any other "Gecko" browser as NS 6.1.

  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = 6.1;
    return;
  }
}

function popup_window(uri, name, properties)
{
	return window.open(uri, name, 'width=800, height=600, resizable=yes, status=no, scrollbars=yes');
}