﻿//Disables the button after it has fired the relevent postback events.  
//This prevents the user double-submitting a request
//Thanks to: http://www.codeproject.com/aspnet/OneTimeClickableButton.asp
function PreventDoubleSubmit(button,msg) {
    window.setTimeout("csr_disableButton('" + button.id + "','"+msg+"')", 0);
    return true;
}

function csr_disableButton(buttonID,msg) {
    if (typeof(Page_IsValid) == "undefined") Page_IsValid = true; //ensure it exists
    if (Page_IsValid) //if Page validation was successful
    {
        button = document.getElementById(buttonID);
        button.value = msg;
        button.disabled=true;
    }
}

//Crossbrowser modal popup window http://jguru.com/faq/view.jsp?EID=331270
function openModalWindow(modalDialogContainerUrl, modalDialogInnerUrl,width,height,callback)
{
	paramspassed = "";
	if(arguments.length>4)
		for(i=4;i<arguments.length;i++)
		paramspassed[paramspassed.length] = arguments[i];

	if(window.showModalDialog) //IE
	{
	    url = modalDialogContainerUrl + '?ModalDialogInnerUrl=' + escape(modalDialogInnerUrl) + '&height='+height+'px';
		var return_value = window.showModalDialog(url,paramspassed,"dialogWidth:"+width+"px;dialogHeight:"+height+"px;status:no;help:no; resizable:yes; scroll:no;");
		eval(callback);
	}
	else
	{
		url = modalDialogInnerUrl;
		var modalWin = window.open(url,"","resizable=yes, scrollbars=yes, width="+width+"px, height="+height+"px;");
		modalWin.moveTo(screen.availWidth/2-(width/2),screen.availHeight/2-(height/2));
		window.onfocus = function() {
			if(modalWin && !modalWin.closed)
			{
				modalWin.focus();
			}
			else
			{
			    eval(callback);
			}
		}
	}
}

function postBackHiddenField(hiddenFieldID) {
    window.onfocus = "";
    var hiddenField = document.getElementById(hiddenFieldID);
    if (hiddenField) {
        hiddenField.value = (new Date()).getTime();
        __doPostBack(hiddenFieldID,'');
    }
 }

function SetUniqueRadioButton(nameregex, current)
{
   re = new RegExp(nameregex);
   for(i = 0; i < document.forms[0].elements.length; i++)
   {
      elm = document.forms[0].elements[i]
      if (elm.type == 'radio')
      {
         if (re.test(elm.name))
         {
            elm.checked = false;
         }
      }
   }
   current.checked = true;
}

//Sets the selected index of a dropdownlist to the selectedValue
function selectDropDownListValue(dropdownlist,selectedValue)
{
    for (var i=0; i < dropdownlist.length; i++) {
        if (dropdownlist[i].value == selectedValue) {
            dropdownlist[i].selected = true;
        }
    }
}

//function ensureOneIsConsultedCheckboxChecked(checkbox1ID, checkbox2ID, checkbox3ID, commentsTextBoxID, errorMsg)
//{
//    var checkbox1 = document.getElementByID(checkbox1ID);
//    var checkbox2 = document.getElementByID(checkbox2ID);
//    var checkbox3 = document.getElementByID(checkbox3ID);
//    var commentsTextBox = document.getElementByID(commentsTextBoxID);
//    if (commentsTextBox.value!='' && !checkbox1.checked && !checkbox2.checked && !checkbox3.checked)
//    {
//        alert(errorMsg);
//    }
//}
