// $Id: js.js 8648 2010-04-06 10:18:25Z mark $
/*= Generic functions (not module specific) */

function enableOrDisableForwardingNumber() {
	// "Disable" forwarding number field if forward calls is unchecked
	// In reality we don't truly disable it, because this prevents the form value from
	// being submitted and causes problems with handling the form
	if (!$('#forward_calls').attr('checked')) {
		$('#forwarding_number')
			.attr('readonly', 'readonly')		// prevent user modification
			.addClass('disabled');	// also make it look disabled
	}
	
	$('#forward_calls').click(function() {
		if ($(this).attr('checked')) {	// we're unchecking
			$('#forwarding_number').removeAttr('readonly').removeClass('disabled');
		} else {	// we're checking
			$('#forwarding_number')
				.attr('readonly', 'readonly')		// prevent user modification
				.addClass('disabled');	// also make it look disabled
		}
	});
}

/**
 * Sets all checkboxes with a class of 'checkall' to the boolean state of
 * toggleState
 */
function toggleAllCheckboxes(toggleState) {

    $("input[class=checkall]").each(function() {
        this.checked = toggleState;
    });
}

function updateToggler(checkedState) {
    if ($("#toggle_all").attr('checked') && !checkedState) {
        $("#toggle_all").attr('checked', false);
    }
}

/**
 * jQuery plugin to clear out values from a form or form element
 */
$.fn.clear = function() {
	  return this.each(function() {
	    var type = this.type, tag = this.tagName.toLowerCase();
	    if (tag == 'form')
	      return $(':input',this).clear();
	    if (type == 'text' || type == 'password' || tag == 'textarea')
	      this.value = '';
	    else if (type == 'checkbox' || type == 'radio')
	      this.checked = false;
	    else if (tag == 'select')
	      this.selectedIndex = 0;
	  });
	};


/**
 * When this function is attached to an onclick of a link, it will make a div
 * appear with the contents of the supplied url. It will also toggle the text
 * of the toggle button. The targetDiv should have its display set to none on page
 * load if the target div should initially be hidden
 */
function ajaxToggler(toggleLink, targetDiv, toggleOnText, toggleOffText, Url){
	// Check if target div is hidden, if so show it
    if(targetDiv.css("display") == 'none') {

        toggleLink.html(toggleOffText);

        targetDiv.load(Url, function(output, status){

        	// These click handlers are defined here because they need to be assigned on all series messaging pages
        	// Avoids needing to define them on individual pages
            
         	// Click handler for 'next' wizard button
         	$('#wizard_navigation #next').click(function() {
         		$(this).closest('form')
         			// must avoid adding more and more of these...
         			.append('<input name="nextWizard" type="hidden" value="1"></input>') // tells the controller that 'next' was clicked
         			.ajaxSubmit({
         				success: seriesMessaging_formReceive
         			});
         		
         		return false;
         	});

         	// Click handler for 'finish' wizard button
         	$('#wizard_navigation #finish').click(function() {
         		$(this).closest('form')
         			.append('<input name="finishWizard" type="hidden" value="1"></input>') // tells the controller that 'finish' was clicked
         			.ajaxSubmit({
         				success: seriesMessaging_formReceive
         			});
         		
         		return false;
         	});
         	
         	// Click handler for 'finish' wizard button
         	$('#wizard_navigation #skip').click(function() {
         		$(this).closest('form')
	     			.append('<input name="skipWizard" type="hidden" value="1"></input>') // tells the controller that 'finish' was clicked
	     			.ajaxSubmit({
	     				success: seriesMessaging_formReceive
	     			});
         		
         		return false;
         	});
         	
         	// Handler for normal form submit (if not in wizard mode)
         	$('.series_form').submit(function() {
         	    $(this).ajaxSubmit({
         	        success: seriesMessaging_formReceive
         	    });
         	    return false;
         	});

            return false;        	
        	
        });
        targetDiv.show();
    } else {
        // Target Div is visible, so hide it
        toggleLink.html(toggleOnText);
        targetDiv.slideUp();
    }
    
}

/*= Generic JQuery based functions not module specific */
function alertBox(titleText, msg) {

	$('#dialogBoxPlaceHolder').dialog('destroy');
	
    $('#dialogBoxPlaceHolder').dialog({
            autoOpen: false,
            bgiframe: true,
            width: 300,
            resizable: false,
            show: 'drop',
            hide: 'drop',
            title: titleText,
            buttons: {
                "Ok": function() {
                        $(this).dialog("close");
                }
            }
    });
    $('#dialogBoxPlaceHolder').html(msg);
    $('#dialogBoxPlaceHolder').dialog('open');
    return true;

}

function loaderBox(titleText, msg) {
	$('#dialogBoxPlaceHolder').dialog('destroy');
	
    $('#dialogBoxPlaceHolder').dialog({
            autoOpen: false,
            bgiframe: true,
            width: 300,
            resizable: false,
            show: 'drop',
            hide: 'drop',
            title: titleText
    });
    $('#dialogBoxPlaceHolder').html(msg + getSpinnerImageHtml());
    $('#dialogBoxPlaceHolder').dialog('open');
    return true;
}

/**
 * @todo: need to implement a new confirm box
 */
function confirmBox(msg)
{
    return confirm(msg);
}

function getBodyHeight() {
    var height = 0;
    if (document.body && document.body.clientHeight) {
        height = document.body.clientHeight;
    }

    if(document.body && document.body.scrollHeight) {
        height = document.body.scrollHeight;
    }
    return height;
}

function getBodyWidth() {
    var width = 0;

    if (document.body && document.body.clientWidth) {
      width = document.body.clientWidth;
    }

    return width;
}

function fadeMainContent() {

    var isMSIE6 = /MSIE 6/i.test(navigator.userAgent);

    if(isMSIE6)
      return;
    var FadeBox = document.getElementById('FadeBox');

    FadeBox.style.height  = getBodyHeight() + "px";
    FadeBox.style.width   = getBodyWidth()  + "px";
    FadeBox.style.display = '';
}

function unfadeMainContent() {

    var isMSIE6 = /MSIE 6/i.test(navigator.userAgent);
    if(isMSIE6) return;
    var FadeBox = document.getElementById('FadeBox');
    FadeBox.style.display = 'none';
}

// This is needed for the fadeMainContent and unFadeMainContent
$(document).ready(function(){
        $(function() {

        var isMSIE6 = /MSIE 6/i.test(navigator.userAgent);
        window.onresize = function() {
        if(isMSIE6) return;
        var FadeBox = document.getElementById('FadeBox');

        FadeBox.style.height = getBodyHeight() + "px";
        FadeBox.style.width = getBodyWidth() + "px";
      }
    });
});

function openpopup(page, parameters) {
    
     var popurl=page;

     if (parameters) popurl += parameters;

     var winpops=window.open(popurl,"","width=500, height=450,scrollbars,resizable");

     if (!winpops.opener)
       winpops.opener = self;
}

function copyValueToForm(value, toElem) {
    window.opener.$("#"+toElem).val(value);
    window.close();
}

