YAHOO.namespace("sec.gui");

/* Shift the loading div code into here someday ... */

/* Popup Window Management */
YAHOO.sec.gui.managePopup = function(sUrl, sName, oConfig, event, ShowAlert) {
	
	var sConfig = '';
	var windowProps = {};
	
	windowProps['width'] = 620;
	windowProps['height'] = 400;
	windowProps['toolbar'] = 'no';
	windowProps['scrollbars'] = 'yes';
	windowProps['resizable'] = 'yes';
	windowProps['menubar'] = 'no';
	windowProps['status'] = 'no';
	windowProps['directories'] = 'no';
	windowProps['location'] = 'no';
	
	/* Configure the string Configuration for the given window */
	if (typeof oConfig == 'string') {
		sConfig = oConfig;
	}
	
	if (typeof oConfig == 'object') {
		for (var i in oConfig) {
			if (typeof windowProps[i] !== 'undefined' && typeof windowProps[i] === typeof(oConfig[i])) 
			{
				windowProps[i] = oConfig[i];			
			}
		}
	}
	
	/* Check to see if we need to dump out the new config ... */
	if (sConfig.length == 0) {
		var arrElems = [];
		for (var i in windowProps) {
			arrElems.push(i+"="+windowProps[i]);
		}
		
		sConfig = "'"+arrElems.join(",")+"'";
	}
	
	/* Open the window */
	YAHOO.sec.gui.oWin = window.open(sUrl, sName, sConfig);
	
	/* Feedback, if needed */
	if ( ShowAlert != 'no' )
	{
		var notice = 'Unable to launch popup window, please allow popups for senearthco.com and try again.';
		if (YAHOO.sec.gui.oWin == null || typeof(YAHOO.sec.gui.oWin) == 'undefined')
		{
			if (ShowAlert)
			{
				if (!YAHOO.sec.gui.noticeBox)
				{
					alert(notice);		
				}
				else
				{
					YAHOO.sec.gui.addNotice(notice);
				}		
			}
			
		}
		else
		{
			YAHOO.util.Event.preventDefault(event);
		}
		return YAHOO.sec.gui.oWin;
	}
}

/* Notice Box Management */
YAHOO.sec.gui.clearNotices = function() { 
	if (typeof(YAHOO.sec.gui.noticeBox) == 'undefined')
	{
		// Do nothing.
	}
	else
	{
		var listing = YAHOO.sec.gui.noticeBox.getElementsByTagName("ul")[0];
		while(listing.firstChild)
		{
			listing.removeChild(listing.firstChild);
		}
		
		YAHOO.sec.gui.noticeBox.style.display = 'none';	
	}
}

YAHOO.sec.gui.addNotice = function (str) { 
	if (typeof(YAHOO.sec.gui.noticeBox) == 'undefined')
	{
		alert(str);
	}
	else
	{
		var listing = YAHOO.sec.gui.noticeBox.getElementsByTagName("ul")[0];
		
		/* Make a new list item and add it to the notices */
		var li = document.createElement("li");
		li.innerHTML = str;
		listing.appendChild(li);
	
		/* Make sure the results are being displayed */
		YAHOO.sec.gui.noticeBox.style.display = 'block';
		
		var r = YAHOO.util.Dom.getRegion( YAHOO.sec.gui.noticeBox ); 
		
		if (r.top < document.body.scrollTop)
		{
			window.location = '#noticeBox';	
		}
	}
}

YAHOO.sec.gui.initNotice = function(e) {
	if (YAHOO.sec.gui.noticeBox == null && document.getElementById("sec_noticeBox") != null)
	{
		YAHOO.sec.gui.noticeBox = document.getElementById("sec_noticeBox");
	}
}

/* Checkbox_group Management */
YAHOO.sec.gui.checkUncheckAll_Click = function(e) {
	var tgt = YAHOO.util.Event.getTarget(e);
	
	/* check all inputs inside of checkAllGroup div */	
	var cbg_id = tgt.parentNode.parentNode.id;
	var flagged_cbg = YAHOO.util.Dom.getElementsByClassName('checkAllGroup', 'input', cbg_id);
	if (flagged_cbg && flagged_cbg.length > 0)
	{
		
		for (var i = 0; i < flagged_cbg.length; i++) {	
			if(tgt.value == 0) {
				flagged_cbg[i].setAttribute('checked', false);
				flagged_cbg[i].checked = false;
			} else {
				flagged_cbg[i].setAttribute('checked', true);
				flagged_cbg[i].checked = true;
			}
		}
	}
	
	/* Update the checkbox graphic */
	if(tgt.value == 0)  {
		tgt.value = 1;
		if (tgt.srcElement) {
			tgt.srcElement.src = '/images/icons/checkall.png';
		} else if (tgt.src) {
			tgt.src = '/images/icons/checkall.png';
		}
	} else {
		tgt.value = 0;
		if (tgt.srcElement) {
			tgt.srcElement.src = '/images/icons/uncheckall.png';
		} else if (tgt.src) {
			tgt.src = '/images/icons/uncheckall.png';
		}
	}
	YAHOO.util.Event.preventDefault(e);
}

YAHOO.sec.gui.revertAll_Click = function(e) {

	/* check all inputs inside of checkAllGroup div */	
	var cbg_id = this.parentNode.parentNode.parentNode.id;
	var flagged_cbg = YAHOO.util.Dom.getElementsByClassName('checkAllGroup', 'input', cbg_id);
	if (flagged_cbg && flagged_cbg.length > 0)
	{
		for (var i = 0; i < flagged_cbg.length; i++)
		{
			if(this.value.indexOf(flagged_cbg[i].value)) 
			{
				flagged_cbg[i].checked = false;
			} else {
				flagged_cbg[i].checked = true;
			}
		}
	}
	YAHOO.util.Event.preventDefault(e);
}

YAHOO.sec.gui.Register_Ad_Click = function(e){
	YAHOO.util.Event.stopEvent(e);
	var vendorAd_Id = this.className.split('_')[1];
	var oURL = '/siteadmin/vendorservices/xml_register_ad_click.cfm?id=' + vendorAd_Id;
	YAHOO.util.Connect.asyncRequest('GET', oURL, { cache:false }, null );
	YAHOO.sec.gui.managePopup(this.href,'ad' + vendorAd_Id, null, null, 'no');
}

YAHOO.sec.gui.disable_button_display = function(e){
	var oButton = YAHOO.util.Event.getTarget(e);
	YAHOO.util.Event.stopEvent(e);
	oButton.disabled = true;
	oButton.value = 'Processing...';
	oButton.form.submit();
	return true;
}

/* Page Loading */
YAHOO.sec.gui.Page_Load = function(e) {	
	
	/* added to handle any input buttons that we want to toggle */
	var submitButtons = YAHOO.util.Dom.getElementsByClassName('disabledOnClick');
	YAHOO.util.Event.addListener( submitButtons, 'click', YAHOO.sec.gui.disable_button_display );

	/* added to handle any checkbox groups check all/ uncheck all */
	var flagged_cb = YAHOO.util.Dom.getElementsByClassName('checkAll', 'input');
	YAHOO.util.Event.addListener( flagged_cb, 'click', YAHOO.sec.gui.checkUncheckAll_Click );
	
	/* added to handle any checkbox groups revert all */
	var flagged_cbr = YAHOO.util.Dom.getElementsByClassName('revertAll', 'input');
	if (flagged_cbr && flagged_cbr.length > 0)
	{
		for (var i = 0; i < flagged_cbr.length; i++)
		{
			YAHOO.util.Event.addListener( flagged_cbr[i], 'click', YAHOO.sec.gui.revertAll_Click );
		}
	}
	
	/* Tracking ad clickthrus if the ad is available */
	if ( document.getElementById('advertisement') ) YAHOO.util.Event.addListener( 'advertisement', 'click', YAHOO.sec.gui.Register_Ad_Click )
}

YAHOO.util.Event.onDOMReady( YAHOO.sec.gui.initNotice );
YAHOO.util.Event.onDOMReady( YAHOO.sec.gui.Page_Load );

