function CheckPreviewURL () {

    // this function is obsolete now, use CheckPreviewURL_v2(Form_Prefix) now - 11/10/2007 jbh
}

function CheckPreviewURL_v2(Form_Prefix) {

	var PU 
	var myDate = new Date()
	var DT
	var el = FindMyControl(Form_Prefix + '_hdPreviewURL');
	
		
	if (el.value.length > 1) {
		PU = 'Preview.aspx?PreviewURL=' + escape(el.value);
		el.value = '';
		// append a DateTime stamp to prevent caching
		DT = 'DATESTAMP' + myDate.getMonth();
		DT = DT + myDate.getDate();
		DT = DT + myDate.getYear();
		DT = DT + myDate.getHours();
		DT = DT + myDate.getMinutes();
		DT = DT + myDate.getSeconds();
		PU = PU + '&DT=' + DT;
		// pop-up preview URL
		PreviewWindow = window.open(PU, 'Preview', 'scrollbars=auto,width=1050,height=620,toolbar=no,location=no,directories=no,location=no;status=no,resizable=yes');
		PreviewWindow.focus()
	}

}

function TimeUnique() {
	var Val 
	var today = new Date();
	Val = 'M' + today.getMonth();
	Val = Val + 'D' + today.getDay()
	Val = Val + 'Y' + today.getYear();
	Val = Val + 'H' + today.getHours();
	Val = Val + 'M' + today.getMinutes();
	Val = Val + 'S' + today.getSeconds();
	Val = Val + 'ML' + today.getMilliseconds();
	
	return Val;
}

function PositionLayer(LayerName, OffSetX, OffSetY) {

	var imgX;
	var imgY;
	var MyScrollTop;
	var el;

	el = document.getElementById(LayerName);
	
   	if (OffSetX == 450 ) { OffSetX = 325 }		// correction
	if (navigator.appVersion.indexOf('MSIE 7') > -1) { OffSetY = OffSetY + 20 } // correct for IE 7
	
	imgX = (document.body.clientWidth / 2);	// find the center of the window
	imgX = imgX - OffSetX
	imgY = OffSetY;
		
	el.style.left = imgX + 'px';
	el.style.top = imgY + 'px';
	 
}

function PositionLayerNScroll(LayerName, OffSetX, OffSetY) {

	var imgX;
	var imgY;
	var MyScrollTop;
	
   	if (OffSetX == 450 ) { OffSetX = 325 }		// correction
	if (navigator.appVersion.indexOf('MSIE 7') > -1) { OffSetY = OffSetY + 20 } // correct for IE 7
	
	imgX = (document.body.clientWidth / 2);	// find the center of the window
	imgX = imgX - OffSetX
	imgY = OffSetY;
	// adjust for scroll position
	if (document.documentElement && !document.documentElement.scrollTop)   // IE6 +4.01 but no scrolling going on
        { MyScrollTop = 0 }    
    else if (document.documentElement && document.documentElement.scrollTop)  // IE6 +4.01 and user has scrolled
        { MyScrollTop = document.documentElement.scrollTop }   
    else if (document.body && document.body.scrollTop) // IE5 or DTD 3.2
        { MyScrollTop = document.body.scrollTop }   
	imgY = imgY + MyScrollTop;
		
	document.all[LayerName].style.left = String(imgX) + 'px'; 
	document.all[LayerName].style.top = String(imgY) + 'px';
}

function ShowLayer(LayerName) {		

	// determine our browser type for layer usage, since Netscape is 6 is the devil in disguise
	var BrowserType = "";
	if(document.layers) { BrowserType="NN4" }
	if(document.all) { BrowserType="IE"  }
	if(!document.all && document.getElementById) { BrowserType="NN6" } // what is Netscape smoking????
	if (BrowserType=="") { BrowserType="IE"  }	// default to IE, just because....

	if (BrowserType=="IE") { document.all[LayerName].style.visibility = 'visible' }
	if (BrowserType=="NN4") { document.layers[LayerName].visibility = 'visible' }
	if (BrowserType=="NN6") { document.getElementById(LayerName).style.visibility='visible' }
	
}

function CancelLayer(LayerName) {

	// determine our browser type for layer usage, since Netscape is 6 is the devil in disguise
	var BrowserType = "";
	if(document.layers) { BrowserType="NN4" }
	if(document.all) { BrowserType="IE"  }
	if(!document.all && document.getElementById) { BrowserType="NN6" } // what is Netscape smoking????
	if (BrowserType=="") { BrowserType="IE"  }	// default to IE, just because....

	if (BrowserType=="IE") { parent.document.all[LayerName].style.visibility = 'hidden' }
	if (BrowserType=="NN4") { parent.document.layers[LayerName].visibility = 'hidden' }
	if (BrowserType=="NN6") { parent.document.getElementById(LayerName).style.visibility='hidden' }
}		
       
function CleanHTMLv2(text){

	var text, s, e, idx, T, tag, fullTag, l, RepCnt;
	var KeeperList = "<P> </P> <BR> <UL> </UL> <LI> <OL> </OL> <IMG> <TABLE> </TABLE> <TR> </TR> <TD> </TD>";
	
	s = 0;
	RepCnt = 0;
		
	l = text.length;
	if (l == 0) { return }
	
	// get first occurance of '<'
	if (s == 0) { s = text.indexOf('<', 0); }
	while (s > -1 || s > l) {
		l = text.length;
		if ( text.charAt(s + 1) != '/') {	// replace the starting tags
			// find the end
			e = text.indexOf('>', s+1);
			T = text.substring(s, e+1);
			// get the absolute tag
			fullTag = T;
			tag = T.substring(1, T.indexOf(' '));
			if (tag == '<') { tag = T.substring(1, T.indexOf('>')) }
			tag = '<' + tag.toUpperCase() + '>';
			if (KeeperList.indexOf(tag) == -1) 
				{
				// get rid of tag
				text = text.substring(0, s) + text.substring(e+1, l);
				}
			else
				{
				// else tag is IN our keeper list
				// a few tags we want to save as is... for now
				if (tag != '<IMG>' && tag != '<A>') {
					// one last test for our jumpdown Font tag
					if (tag == '<FONT>')
						{
						// remove the font tag if not a Jumpdown, otherwise keep tag as is						
						if (fullTag.indexOf('class=Jumpdown') == -1) { text = text.substring(0, s) + text.substring(e+1, l); } 
						}				
					else
						{
						// replace with our good tag if
						text = text.substring(0,s) + tag + text.substring(e+1, l);
						}
				   }
					
				}			
		 }
		else
		{	// replace the ending tags not needed		
			// find the end
			e = text.indexOf('>', s+1);
			tag = text.substring(s, e+1);
			tag = tag.toUpperCase();
			if (KeeperList.indexOf(tag) == -1) 
				{
				// get rid of tag
				text = text.substring(0, s) + text.substring(e+1, l);
				}		
		}
		
		if (text.charAt(s) != '<' || RepCnt > 1) 
			{ s = text.indexOf('<', s+1); RepCnt = 0 }
		else
			{RepCnt = RepCnt + 1 }
			
	}	// end while
	
	// Replace all <P>&nbsp;</P>'s
	T = '<P>&nbsp;</P>'
	while (text.indexOf(T)>-1) {
	s= text.indexOf(T);
	text = "" + (text.substring(0, s) + text.substring((s + T.length), text.length));
	}
		
	return text;

}

function HideSelects() {
	var selects,i;
	selects=document.getElementsByTagName('select');
	for(i in selects)
	{
		if (selects[i].options) { selects[i].style.visibility = 'hidden'; }
	}
}			
	
function UnHideSelects() {
	var selects,i;
	selects=document.getElementsByTagName('select');
	for(i in selects)
	{
		if (selects[i].options) { selects[i].style.visibility = 'visible'; }
	}
}

function ShowTaskLayer(x, y, w, h, src) {

	ShowLayer('lyrTask'); 
	PositionLayerNScroll('lyrTask', x, y); 

	document.all.ifTask.style.width=w;
	document.all.ifTask.style.height=h; 
	document.frames('ifTask').location=src; 	

}

function Delete_List_Item(Flag, Action, Form_Prefix) {

    var el;

    if (confirm('This will remove this item from this list.  Click Ok to continue or Cancel.')) 
        {
            el = FindMyControl(Form_Prefix + '_hdFlag');
            el.value = Flag;
            el = FindMyControl(Form_Prefix + '_hdAction');
            el.value = Action;
            el.form.submit();
        }
    else
        { 
            return;
          }
}

function MoveItem(Flag) {
    var e;
    e = FindMyControl('hdFlag');
    e.value = Flag;
    e = FindMyControl('hdAction');
    e.value = 'MoveItem';
    e.form.submit();
}

function FindMyControl(MyID) {

	var frmElements,i,t,ctl;
	
	// Since the use of master pages mangles the names of child controls, we need a method 
	// of searching through the form elements and returning a reference to the control we are 
	// looking for that control by the name we know it as instead of the dumb-ass name ASP.Net creates.
	// First scan through all of the input elements and look for our control
    frmElements=document.getElementsByTagName('input');
    for(i in frmElements)
    {
        t = String(frmElements[i].id); 
        t = '_' + t;
        if (t.indexOf(MyID) > -1 )
            { return frmElements[i] }
    }
   // Still not found, scan through all of the select elements
    frmElements=document.getElementsByTagName('select');
    for(i in frmElements)
    {
        t = String(frmElements[i].id); 
        t = '_' + t;
        if (t.indexOf(MyID) > -1 )
            { return frmElements[i] }
    } 
   
   // Still not found, scan through all of the textarea elements
    frmElements=document.getElementsByTagName('textarea');
    for(i in frmElements)
    {
        t = String(frmElements[i].id); 
        t = '_' + t;
        if (t.indexOf(MyID) > -1 )
            { return frmElements[i] }
    }   
   
   // Still not found, scan through all of the iframe elements
    frmElements=document.getElementsByTagName('td');
    for(i in frmElements)
    {
        t = String(frmElements[i].id); 
        t = '_' + t;
        if (t.indexOf(MyID) > -1 )
            { return frmElements[i] }
    } 
            
   
   // Still not found, scan through all of the iframe elements
    frmElements=document.getElementsByTagName('iframe');
    for(i in frmElements)
    {
        t = String(frmElements[i].id); 
        t = '_' + t;
        if (t.indexOf(MyID) > -1 )
            { return frmElements[i] }
    }         
   
  return false;  

}

function  Select_Security_Group_User(SG_ID) {
    alert('This needs some work here...');
 }  
 
 function Validate_Form_Element(Label, FieldName, DataType, DisplayOption, MaxLength, Required) {

    var el;
    var el2;
    var ElementValid = true; 
   
   // make sure MaxLength is a good number    
   MaxLength= parseInt(MaxLength);
   if (MaxLength == 'NaN') { MaxLength = 0 } 
     
    switch (DisplayOption) {
        case "CL":
              // Required value test - currently test for at least one being checked
              var inputs = document.getElementsByTagName ('input');
              var SomethingChecked = false; 
              if (inputs) {
                for (var i = 0; i < inputs.length; ++i) {
                  if (inputs[i].type == 'checkbox' && inputs[i].name.indexOf(FieldName) > -1)
                        {
                            if (inputs[i].checked == true) { SomethingChecked = true }
                        } 
                  }
              }
             if ( Required == 'Yes' && SomethingChecked == false ) {
                    ElementValid = false;
                    alert(Label + ' is a required field.  Please select a value for this item.');
               } 
            break;
        case "DC":
            el = FindMyControl(FieldName);
            // blank test
            if (Required == 'Yes' && el.value.length < 2) {
                ElementValid = false;
                alert(Label + ' is a required field.  Please provide a date value for this item.');
                return false;
            }
            if (Required != 'Yes' && el.value.length < 2) {
                el.value = '1900-01-01';
            }
            // Date test 
            if (ElementValid) {
                var spT = el.value.split('-');
                if (spT.length != 3) {
                    ElementValid = false;
                    el.style.backgroundColor = '#fbcdcf';
                    alert('The value you provided for ' + Label + ' does not appear to be a valid date.' + FieldName);
                    return false;
                }
            }
            // if valid and required, make sure it isn't 1/1/1900
            if (Required == 'Yes' && ElementValid) {
                var t = spT[1] + '/' + spT[2] + '/' + spT[0];
                var myDate = new Date(t);
                var testDate = new Date('1/1/1901');
                if (myDate < testDate) {
                    ElementValid = false;
                    alert(Label + ' is a required field.  Please provide a date value for this item.');
                    return false;
                }

            }
            break;        

        case "DD":
            el = FindMyControl(FieldName);
            if (el == false) { return true }
            // Required value test
            if (Required == 'Yes' && el.selectedIndex <= 0) 
                {
                    ElementValid = false;
                    alert(Label + ' is a required field.  Please select a value for this item.');                    
                }
            break;
        case "HD":
            
            break;
        case "HF":
            
            break;
        case "HF2":
            
            break;
        case "ML":
            el = FindMyControl(FieldName);
            if (el == false) { return true }
            // Set background of element to white
            el.style.backgroundColor = '#FFFFFF';
            // Required value test
            if (Required == 'Yes' && el.value.length <= 0) {
                ElementValid = false;
                el.style.backgroundColor = '#fbcdcf';
                alert(Label + ' is a required field.  Please enter a value for this item.');
            }
            // Max length test
            if (ElementValid && MaxLength > 0 && el.value.length > MaxLength) {
                ElementValid = false;
                el.style.backgroundColor = '#fbcdcf';
                alert('The value you provided for ' + Label + ' is too long.  It should only be ' + String(MaxLength) + ' characters, but you have provided a value with ' + String(el.value.length) + ' characters.');
            }
            // Numeric test 
            if (ElementValid && (DataType == 'tinyint' || DataType == 'int' || DataType == 'money' || DataType == 'smallint' || DataType == 'int' || DataType == 'real') && IsNumeric(el.value) == false) {
                ElementValid = false;
                el.style.backgroundColor = '#fbcdcf';
                alert('The value you provided for ' + Label + ' does not appear to be a valid number.');
            }

            break;

        case "PW":
            
            break;
        case "RL":
              // Required value test
              var inputs = document.getElementsByTagName ('input');
              var SomethingChecked = false; 
              if (inputs) {
                for (var i = 0; i < inputs.length; ++i) {
                  if (inputs[i].type == 'radio' && inputs[i].name.indexOf(FieldName) > -1)
                        {
                            if (inputs[i].checked == true) { SomethingChecked = true }
                        } 
                  }
              }
             if ( Required == 'Yes' && SomethingChecked == false ) {
                    ElementValid = false;
                    alert(Label + ' is a required field.  Please select a value for this item.');
               } 
            break;
        case "SL":
            el = FindMyControl(FieldName);
            if (el == false) { return true }
            // Set background of element to white
            el.style.backgroundColor = '#FFFFFF';
            // Required value test
            if (Required == 'Yes' && el.value.length <= 0) 
                {
                    ElementValid = false;
                    el.style.backgroundColor = '#fbcdcf';
                    alert(Label + ' is a required field.  Please enter a value for this item.');
                }
            // Max length test
            if (ElementValid &&  MaxLength > 0 && el.value.length > MaxLength)
                {
                    ElementValid = false;
                    el.style.backgroundColor = '#fbcdcf';
                    alert('The value you provided for ' + Label + ' is too long.  It should only be ' + String(MaxLength) + ' characters, but you have provided a value with ' + String(el.value.length) + ' characters.');
                }  
            // Numeric test 
            if (ElementValid &&  ( DataType == 'tinyint' || DataType == 'int' || DataType == 'money' || DataType == 'smallint' || DataType == 'int' || DataType == 'real' ) && IsNumeric(el.value) == false)
                {
                    ElementValid = false;
                    el.style.backgroundColor = '#fbcdcf';
                    alert('The value you provided for ' + Label + ' does not appear to be a valid number.');
                }  
                
            break;
            
        case "AC":
            el = FindMyControl(FieldName);
            if (el == false) { return true }
            // ACE controls have 2 elements, get the visible one
            el2 =  FindMyControl('txt' + el.id);
            // Set background of element to white
            el2.style.backgroundColor = '#FFFFFF';
            // Required value test
            if (Required == 'Yes' && el.value.length <= 0) 
                {
                    ElementValid = false;
                    el2.style.backgroundColor = '#fbcdcf';
                    alert(Label + ' is a required field.  Please select a value for this item by typing a few letters to find the item.  Then specifically click on the item to select it.');
                }
           break;
      }
      
      return ElementValid
    
}


function IsNumeric(sText)
{
   var ValidChars = "0123456789.,$-";
   var IsNumber=true;
   var Char;
 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }
   
function CheckAll() {

      var inputs = document.getElementsByTagName ('input');
      if (inputs) {
        for (var i = 0; i < inputs.length; ++i) {
          if (inputs[i].type == 'checkbox')
                {
                    inputs[i].checked = true;
                } 
          }        
      }    

}

function UnCheckAll() {

      var inputs = document.getElementsByTagName ('input');
      if (inputs) {
        for (var i = 0; i < inputs.length; ++i) {
          if (inputs[i].type == 'checkbox')
                {
                    inputs[i].checked = false;
                } 
          }        
      }

  }

  function SaveCheckers() {

      document.forms[0].submit();

  }

  function AddSelectedCLs() {

      var inputs = document.getElementsByTagName('input');
      var MyChecked = '';
      var el;
      
      for (var i = 0; i < inputs.length; ++i) {
          if (inputs[i].type == 'checkbox' && inputs[i].checked) {
              MyChecked += inputs[i].value + ',';
          }
      }

      el = FindMyControl('hdParam');
      el.value = MyChecked;
      el = FindMyControl('hdAction');
      el.value = 'Add';
      el.form.submit();

  }

  function DelCLItem(CL_ID) {

      el = FindMyControl('hdParam');
      el.value = CL_ID;
      el = FindMyControl('hdAction');
      el.value = 'Del';
      el.form.submit();

  }

  function SendJIF() {

      ShowLayer('lyrJIFRecipients');

  }

  function AttachJIFFile(Type) {

      var el;

      el = FindMyControl('hdFlag');
      el.value = Type;

      ShowLayer('lyrAttachJIFFile');

  }

  function ToggleRows(RowIDNum) {

    var el;
    var IsIE;
    var UpOrDown;

    if (navigator.appVersion.indexOf('MSIE') > 0) { IsIE = true } else { IsIE = false }	      

      frmElements = document.getElementsByTagName('tr');
      for (i in frmElements) {
          t = String(frmElements[i].id);
          if (t.indexOf('trToggleRow_' + RowIDNum + '_') > -1) {
              // show or hide the row
              el = document.getElementById(t);
              if (el.style.display == '' || el.style.display == 'block' || el.style.display == 'table-row') {
                  el.style.display = 'none';
                  UpOrDown = 'Up';
              }
              else {
                  if (IsIE == true) { el.style.display = 'block'; } else { el.style.display = 'table-row' };
                  UpOrDown = 'Down';
              }
              
          }
      }

      // toggle image
      if (UpOrDown == 'Up') {
          el = document.getElementById('imgToggle' + RowIDNum);
          var img = new Image();
          img.src = '../images/Core/ToggleUp.gif';
          el.src = img.src;
      }
      else {
          el = document.getElementById('imgToggle' + RowIDNum);
          var img = new Image();
          img.src = '../images/Core/ToggleDown.gif';
          el.src = img.src;
      }

  }

  function Archive_List_Item(JIF_ID) {

      var el;

      if (confirm('Are you sure you want to archive this job posting?\n\nClick Ok to continue or Cancel.')) {

          el = FindMyControl('hdAction');
          el.value = 'Archive Job';
          el = FindMyControl('hdFlag');
          el.value = JIF_ID;

          el.form.submit();
      }

  }

  function AttachPBSFile(Type) {

      var el;

      el = FindMyControl('hdFlag');
      el.value = Type;

      ShowLayer('lyrAttachPBSFile');

  }

  function SendRB() {

      ShowLayer('lyrRBRecipients');

  }

  function AttachRBFile(Type) {

      var el;

      el = FindMyControl('hdFlag');
      el.value = Type;

      ShowLayer('lyrAttachRBFile');

  }


  var tmrJP;

  function ShowHideJobPostingsOther() {

    var el;
    var MyValue;
    var frmElements;
    var i;
    var t;
    var ShowStyle = 'table-row';
    var elTR;

    if (navigator.appVersion.indexOf('MSIE 7') > 0) { ShowStyle = 'inline'; }
    else { ShowStyle = 'table-row'; }

    // find this tr element to hide
    frmElements = document.getElementsByTagName('tr');
    for (i in frmElements) {
        t = String(frmElements[i].id);
        if (t.indexOf('trJob_Postings_Location_Other') > -1) {
            elTR = frmElements[i];
        }
    }      
        
    el = FindMyControl('Job_Postings_Office');
    MyValue = el[el.selectedIndex].text;
    if (MyValue == 'Other') {
        elTR.style.display = ShowStyle;
    } else {
        elTR.style.display = 'none';    
    }

    tmrJP = setTimeout('ShowHideJobPostingsOther()', 500);   
  }