

function clearField( field_name )
{
	document.getElementById( field_name ).value = '';
	document.getElementById( field_name ).style.backgroundColor = '';
}

function highlightFields( field_str )
{
	var fields = field_str.split(',');
	
	for( var i=0; i<fields.length; i++ )
	{
		highlightField( fields[i], '#F6F5CF' ); 
	}
	
}


function referenced_link(elementId, link)
{
    var objElement = window.opener.document.getElementById(elementId)
    
    if (objElement) 
    {
        objElement.value = link;
        
    }   //end if
    
	return;
	
}   //end function

function highlightField( field_name, field_color )
{
	document.getElementById( field_name ).style.backgroundColor = field_color;
}


function formatCurrency(num) 
{
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') +  num + '.' + cents);
}	//end function

function validate_required_fields(frmName, strTextBoxes)
{
	var oForm = document.getElementById(frmName);
	
	//turn the comma separated list into an array
	var arrBoxes = strTextBoxes.split(",");
	
	error = 0;
	
	//loop thru each box and determine if the text value is empty
	for( var intCount = 0; intCount < arrBoxes.length; intCount++ )
	{
		arrBoxes[intCount] = arrBoxes[intCount].trim_spaces();
		
		//alert( "inCount: " + intCount + " - text box: -->" + arrBoxes[intCount] + "<--" );
		
		var strText = document.getElementById(arrBoxes[intCount]).value;
		
		if( strText.length == 0 )
		{
			error++;
			
		}	//end if
		
	}	//end for loop
	
	//if any errors, then user did not enter all the required fields
	if( error > 0 )
	{
		alert( "Please enter all the required fields." );
		
		valid = false;
		
	}	//end if
	
	else
	{
		valid = true;
		
	}	//end else
	
	return valid;
	
}	//end function

function pop_templates()
{
	window.open('pop_templates.php','admin_pop','width=600,height=400,scrollbars=yes,resizable=yes');	
}

//removes all spaces in a string
function removespaces() 
{
	return this.replace(/.*\S/,'');
	
}	//end function

//trims leading and trailing spaces
function trim_spaces() 
{
	return this.replace(/^\s+/,'').replace(/\s+$/,'');
	
}	//end function

function clear_image( field_id )
{
    document.getElementById( field_id + "_src" ).src = '../images/spacer.gif';
	document.getElementById( field_id ).value = '';
	document.getElementById( field_id + '_filename' ).value = '';
}

function clear_link( field_id )
{
	document.getElementById( field_id + '_id' ).value = '';
	document.getElementById( field_id + '_text' ).value = '';
	document.getElementById( field_id + '_display' ).innerHTML = '&nbsp;';
	document.getElementById( field_id + '_nw' ).value = 0;
	
	if( document.getElementById( field_id + '_name' ) )
	    document.getElementById( field_id + '_name' ).value = '';
	    
}

function clear_page_reference( field_id )
{
	document.getElementById( field_id + '_name' ).value = '';
	document.getElementById( field_id + '_text' ).value = '';
	document.getElementById( field_id + '_display' ).innerHTML = '';
	document.getElementById( field_id + '_name_display' ).innerHTML = '';
}

function clear_document( field_id )
{
	document.getElementById( field_id ).value = '';
	document.getElementById( field_id + '_display' ).innerHTML = '';
	document.getElementById( field_id + '_h' ).value = '';
}

function create_external_link(element_id)
{
	var blnTargetNew = false;
	
	link = document.getElementById('address').value;
	
	short_link = link;
	
	var strURL = link;
	
	//check to see if the httP is missing
	if ( link.indexOf("http://") == -1 )
	{
	    strURL = "http://" + link;
	
	}   //end if
	
	if( document.getElementById('new_win_yes').checked )
	{	
		window.opener.document.getElementById(element_id+"_nw").value=1;
	}
	else
	{
		window.opener.document.getElementById(element_id+"_nw").value=0;
	}
		
	window.opener.document.getElementById(element_id+"_text").value=link;
	
	window.opener.document.getElementById(element_id+'_display').innerHTML=short_link;
	
	//if _name is present, then we are the page.aspx, use the strURL for _text
	if( window.opener.document.getElementById( element_id + '_name' ) )
	{
	    window.opener.document.getElementById( element_id + '_name' ).value = link;
	    
	    window.opener.document.getElementById(element_id+'_display').innerHTML=strURL;
	    
	    window.opener.document.getElementById(element_id+"_text").value = strURL;
	    
	}   //end if
	
}

function toggleCheckBoxesWithMatchingID( chkBoxAll, intItemID )
{
    //get all the input elements
    var arrElements = document.getElementsByTagName("input");
    
    var strType = "";
    
    var strElementId = "";
    
    var blnCheckValue = false;
    
    var intLastIndex = 0;
    
    var strSearch = "_id";
    
    var arrElementId;
    
    var intElementsItemId = 0;
    
    //use the value of the calling checkbox to determine the value of the other checkboxes
    if( chkBoxAll.checked )
    {
        blnCheckValue = true;
        
    }   //end if
    
    else
    {
        blnCheckValue = false;
        
    }   //end else
    
    //loop through all the input elements
    for( var intIndex = 0; intIndex < arrElements.length; intIndex++ )
    {
        //get the type attribute
        strType = arrElements[intIndex].getAttribute("type");     
               
        if( strType == "checkbox" )
        {
            strElementID = arrElements[intIndex].getAttribute("id"); 
            
            //split the element into an array
            arrElementId = strElementID.split(strSearch);
            
            //the last element will be the id number
            intLastIndex = arrElementId.length - 1;
            
            intElementsItemId = arrElementId[intLastIndex];
            
            //if the id number matches the id number in the element name, then toggle the checkbox
            if( intElementsItemId == intItemID )
            {
                arrElements[intIndex].checked = blnCheckValue;
             
            }   //end if
            
        }   //end if
        
    }   //end for loop
    
}   //end function

//----------- kevin's ----------------------------
// nav drop downs
startList = function() {
	if (document.all && document.getElementById("menu_main") ) {
		menuItems = document.getElementById("menu_main").getElementsByTagName("li");

		for (i=0; i<menuItems.length; i++) {
			menuItems[i].onmouseover = function() {
				this.className += " sfhover";
			}
			menuItems[i].onmouseout = function() {
				this.className = this.className.replace(new RegExp(" sfhover\\b"), "");
			}
		}
	}
}
window.onload = startList;


// HELPER FUNCTIONS

function $(element_id) {
	var thisElement = document.getElementById(element_id);
	if(thisElement)
		return thisElement;
}


//<SCRIPT LANGUAGE="JavaScript">
//<!-- Idea by:  Nic Wolfe (Nic@TimelapseProductions.com) -->
//<!-- Web URL:  http://fineline.xs.mw -->

//<!-- This script and many more are available free online at -->
//<!-- The JavaScript Source!! http://javascript.internet.com -->

function popUp(URL, intWidth, intHeight) 
{
    day = new Date();
    
    id = day.getTime();

    eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=" + intWidth + ",height=" + intHeight + "');");
}


//------------------------------------------

//assigns a new function to string types
String.prototype.removespaces = removespaces;
String.prototype.trim_spaces = trim_spaces;

function saveLanguagePreference( strLanguageCode )
{

    if( strLanguageCode == "en" )
    {
        //erase the cookie
        eraseCookie( "language" );
        
    }   //end if
    
    else
    {
        createCookie( "language", strLanguageCode, 90 );
        
    }   //end else

}   //end function

function redirectToTranslatedPage(strFileName)
{
    var strLanguageCode = "";
    
    if ( strLanguageCode = readCookie("language") )
    {
        var strRedirect = ""
        
        var arrFileName = strFileName.split(".")
    
        strRedirect = arrFileName[0] + "-" + strLanguageCode + "." + arrFileName[1] 
        
         window.location = strRedirect;
    
    }   //end if
    
}   //end function

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function selectDeselectAll(allSelected, allDeselected)
{
    if (document.getElementById('chbSelectAllBranches').checked)
    {
        document.getElementById('branches').innerHTML = allSelected
    }
    else
    {
        document.getElementById('branches').innerHTML = allDeselected
    }
}

function setRteImageFilename(filename, imageName)
{
 document.getElementById('hfRteImageFilename').value = filename;
 document.getElementById('rteImageFilename').innerHTML = "Image: " + imageName;
}
