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 == false) { return };
	
	if (el.value.length > 1) {
		PU = 'Preview.aspx?PreviewURL=' + escape(f.PreviewURL.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=800,height=570,toolbar=no,location=no,directories=no,location=no;status=no,resizable=yes');
		PreviewWindow.focus()
		el.value = '';
	}

}

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;
	
   	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;
		
	document.all[LayerName].style.left = imgX; 
	document.all[LayerName].style.top = imgY; 
}

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 = imgX; 
	document.all[LayerName].style.top = imgY; 
}

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) {

    var e;

    if (confirm('This will remove this item from this list.  Click Ok to continue or Cancel.')) 
        {
            e = FindMyControl('hdFlag');
            e.value = Flag;
            e = FindMyControl('hdAction');
            e.value = Action;
            e.form.submit();
        }
    else
        { 
            return false;
          }
}

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 ValidateThisPhoneNumberField(maxLength, ID) {

    var el;
    var last;
    var nextElement = '';
    var baseID;
    var fullPhone = '';

    el = document.getElementById(ID);
    // make sure it's all numbers
    if (el.value.length >= 1) {
        last = el.value.substring(el.value.length - 1, el.value.length);
        if (last != '1' && last != '2' && last != '3' && last != '4' && last != '5' && last != '6' && last != '7' && last != '8' && last != '9' && last != '0') {
            el.value = el.value.substring(0, el.value.length - 1)
        }
    }
    // make sure our maxlength is not exceeded
    if (el.value.length > maxLength) {
        el.value = el.value.substring(0, maxLength)
    }

    // if at max length, then jump to next
    if (el.value.length == maxLength) {
        if (ID.indexOf('_P1') > 0) { nextElement = ID.replace('_P1', '_P2') }
        if (ID.indexOf('_P2') > 0) { nextElement = ID.replace('_P2', '_P3') }
        if (ID.indexOf('_P3') > 0) { nextElement = ''; }
        if (nextElement != '') { el = document.getElementById(nextElement); el.focus(); }
    }

    baseID = ID.substring(0, ID.length - 3);

    el = document.getElementById(baseID + '_P1');
    fullPhone = fullPhone + '(' + el.value + ') ';
    el = document.getElementById(baseID + '_P2');
    fullPhone = fullPhone + el.value + '-';
    el = document.getElementById(baseID + '_P3');
    fullPhone = fullPhone + el.value;

    // now find our hidden element that sums all the parts back together
    baseID = baseID.substring(baseID.indexOf('_EL') + 1, baseID.length);
        
    el = document.getElementById(baseID);
    el.value = fullPhone;



    //alert(maxLength + ' - ' + ID + ' - ' + last);
}
