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(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 BrowserType = '';
	
	// determine our browser type for layer usage, since Netscape is 6 is the devil in disguise
	if(document.layers) { BrowserType='NN4' }
	if(document.all) { BrowserType='IE'  }
	if(!document.all && document.getElementById) { BrowserType='NN6' }
	if (BrowserType=='') { BrowserType='IE'  }	// default to IE, just because....
	
	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
	imgY = imgY + document.body.scrollTop;
	
	if (BrowserType=="IE") { document.all[LayerName].style.left = imgX; document.all[LayerName].style.top = imgY;  }
	if (BrowserType=="NN4") { document.layers[LayerName].left = imgX; document.layers[LayerName].top = imgY; }
	if (BrowserType=="NN6") { document.getElementById(LayerName).style.left = imgX; document.getElementById(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'); 
	PositionLayer('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;
        }
}

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 ChangeSort(SortCol) {

    var f = FindMyControl('hdSorting_Col'); 
    f.value = SortCol; 
    f.form.submit();
} 

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":
            
            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":
            
            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 OpenCustomerView(link) {

	var myDate = new Date()
	var DT
		
		PU = link
		// 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', '');
		PreviewWindow.focus()

}
