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 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 formatNumber(n)
{
     var i=n.indexOf(".");
     
     if (i==-1)
      return (n);
     else
      return (n.slice(0,i+3));
}

function formatNA(n)
{
     n=n.slice(0,3);
     var i=n.indexOf(".");
     
     if (n=="0. ")
      return ("N/A");
     else
     {
      if (i==-1)
       return (n);
      else
       return (n.slice(0,i));
     }
}

function formatZ(n)
{
     var p=n.slice(0,3);
     
     if (n==0)
      return (" ");
     else
     {
      var i=n.indexOf(".");
      
      if (i==-1)
       return (p);
      else
       return (n.slice(0,i));

     }
}

function formatP(n)
{
     var i=n.slice(0,3);
     
     if (i=="0.0")
      return (" ");
     else
      return (n);
}
   

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( 'pg-' + field_id ).src = 'images/spacer.gif';
	document.getElementById( field_id ).value = '';
	document.getElementById( field_id + '_filename' ).value = '';
}

function clear_link( field_id )
{
	document.getElementById( field_id ).value = '';
	document.getElementById( field_id + '_text' ).innerHTML = '';
	document.getElementById( field_id + '_text' ).style.backgroundColor = '';
}

//assigns a new function to string types
String.prototype.removespaces = removespaces;
String.prototype.trim_spaces = trim_spaces;

function toggleUsername(action) {
	un_field = document.getElementById('un_field');
	if( un_field ) {
		if( action == 'on' ) {
			if( un_field.value=='Username' ) {
				un_field.value='';
			}
		}
		else if( action == 'off' ) {
			if( un_field.value=='' ) {
				un_field.value='Username';
			}
		}
	}
}

function togglePassword(action) {
	var pw_field = document.getElementById('pw_field');
	var pw_area = document.getElementById('pw_area');
	if( pw_field ) {
		if( action == 'on' ) {
			if( pw_field.value=='Password' ) {
				pw_area.innerHTML = '<input type="password" id="pw_field" name="*LOGINPWD" onfocus="togglePassword(\'on\');" onblur="togglePassword(\'off\');" />';
				document.getElementById('pw_field').focus();
			}
		}
		else if( action == 'off' ) {
			if( pw_field.value=='' ) {
				pw_area.innerHTML = '<input type="text" id="pw_field" name="*LOGINPWD" value="Password" onfocus="togglePassword(\'on\');" onblur="togglePassword(\'off\');" />';
			}
		}
	}
}

function openMSDS() { 
	URL="http://catalog.wcpc.com/aa/usr/PDF/" + document.SEARCH.reqProductID.value + ".pdf?*ERROR_HTTP_404=nopdf.pdf";
	window.open(URL,'MSDS','toolbar=no,location=yes,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,screenX=50,top=50,screenY=50,left=50');
}

function alternateRows() {

el = document.getElementsByTagName("table");
for (i = 0; i < el.length; i++)
if (el.className == "altRows") {
rows = el.getElementsByTagName("tr");
for (j = 0; j < rows.length; j++)
rows[j].className = "row" + (j % 2);
}

}

function enlarge(img) {
    // Get the featured image    
	var featuredImg = document.getElementById("vndimg");        
	// Set image source    
	featuredImg.setAttribute("src", img);        
}    


// Order Request Scripts

// validate this form for specific field relations
function postValidateForm(formname) {
	document.returnValue = true;

	if (okToValidate) {
		// validate form fields
		if (! validateForm(formname)) {
			return false;
		}
		if ((formname.closed[1].checked) && (formname.closedate.value == '')) {
			alert("If your project is closed, \nyou must enter a close date.");
			return false;
		}
			
			
		if (formname.opendate.value != "" && formname.closedate.value != "") {
			var sOpen = new Date(formname.opendate.value);
			var sClose = new Date(formname.closedate.value);
			if (sOpen > sClose) {
				alert("The project close date must occur on or after the open date.");
				return false;
			}
		}
	}
}
	
// set the close date to the current date when the project is closed
function setCloseDate() {

	if (document.editproj.closedate.value == "") {
		var today = new Date();
		document.editproj.closedate.value = formatDateTimeStr(today, 0);
		fieldImg(document.editproj.closedate, 'nda');
	}
}
	
// clear the close date (except if ecd is checked) when the project is open
function clearCloseDate() {
	
	if (! document.editproj.ecd.checked) {
		document.editproj.closedate.value = "";
		fieldImg(document.editproj.closedate, 'nda');
	}
}

function setOptions(chosen) {
	var selbox = document.frmOrderRequest.CustType;

	if (chosen == "W") {
	  document.getElementById('cust#').style.display='none';
	  document.getElementById('oth').style.display='none';
	}
	if (chosen == "O") {
	  document.getElementById('cust#').style.display='none';
	  document.getElementById('oth').style.display='inline';
	}
	if (chosen != "W" && chosen != "O") {
	  document.getElementById('cust#').style.display='none';
	  document.getElementById('oth').style.display='none';
	}
}

// MSDS search JS

function formfield() {
    document.SEARCH.reqProductID.focus() 
}
function submitSearch() {
	var swrd=document.search.words.value;
	document.search.words.value=swrd.toUpperCase();
	document.search.SRCHWORD.value=swrd.toUpperCase();
	if (document.search.SEARCHTYPE.value == 'Q')
	{
		document.search.action="searchresult.hsm";
	}
	else
	{
		document.search.action="http://www.wcpc.com/search.php";
	}
	document.search.method="get";
	document.search.submit();
}
function submitSearchM() {
	var swrd=document.SEARCH.SRCHWORD.value;
	document.SEARCH.SRCHWORD.value=swrd.toUpperCase();
	document.SEARCH.action="MSDS.hsm";
	document.SEARCH.method="get";
	document.SEARCH.submit();
}
function submitSearch2() {
	var swrd=document.SEARCH2.SRCHWORD.value;
	document.SEARCH2.SRCHWORD.value=swrd.toUpperCase();
	document.SEARCH2.action="srcharea.hsm";
	document.SEARCH2.method="get";
	document.SEARCH2.submit();
}
function openMSDS() { 
	URL="http://catalog.wcpc.com/aa/usr/PDF/" + document.SEARCH.reqProductID.value + ".pdf?*ERROR_HTTP_404=nopdf.pdf";
	window.open(URL,'MSDS','toolbar=no,location=yes,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,screenX=50,top=50,screenY=50,left=50');
}

function errorLoading(imgid) {
	document.getElementById(imgid).src = "img/trans.gif";
}
//
// cookie support functions
//

function setCookie (name, value, expires) {
	if (!expires)
		expires = new Date();
	document.cookie = name + "=" + escape (value) +     // save cookie value
    "; expires=" + expires.toGMTString() +  "; path=/"; // set expiration date
    }

function getCookie(name) {
	var		dcookie = document.cookie; 
	var 	cname = name + "=";
	var 	clen = dcookie.length;
	var 	cbegin = 0;
    while (cbegin < clen) {
        var vbegin = cbegin + cname.length;
            if (dcookie.substring(cbegin, vbegin) == cname) { 
            var vend = dcookie.indexOf (";", vbegin);
                if (vend == -1) vend = clen;
            return unescape(dcookie.substring(vbegin, vend));
            }
        cbegin = dcookie.indexOf(" ", cbegin) + 1;
            if (cbegin == 0) break;
        }
    return null;
    }

function checkPrice() {
	var 	expdate = new Date ();
	var     prccode;
	var		name;
	
	prccode=document.GRP_CATS.WHCDE.value;
	expdate.setTime (expdate.getTime() + (1000 * 60 * 60 * 24 * 7));
	name='PRCCODE';

	// if PRCCODE==b, price code has been entered
	if (prccode!='') {
		// user logged in - check if cookie exists
		if (getCookie(name)) {
			delCookie(name);
			}
		else {
			}
		setCookie(name, prccode, expdate);
		}
	else
		{
		if (getCookie(name)) {
			// if cookie exists, set BSKNBR field to cookie value
			document.GRP_CATS.WHCDE.value=getCookie(name);
			}
		else {
			// if cookie doesn't exist, set cookie to BSKNBR field
			// returned from GETCOOKIEN server request
			setCookie(name, prccode, expdate);
			}
		}
}
		
function delCookie(name) {
	var 	expireNow = new Date();
	document.cookie = name + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT" +  "; path=/";
    }


function checkUser() {
	var 	expdate = new Date ();
	var     usrnme;
	var	name;
	
	name='LOGINUSR';

	// user logged in - check if cookie exists
	if (getCookie(name)) {
		usrnme=getCookie(name);
		link="ord_request2.htm?username=" + usrnme;
		window.open(link,"_self",""); 
	}
	else {
	}
}

