var ie4 = false;
var ie5 = false;
var ns4 = false;
var ns6 = false;
var mac = false;

agent = navigator.userAgent.toLowerCase();
mac   = (agent.indexOf("mac")!=-1);
ie4   = (document.all && !document.getElementById) ? true : false;
ie5   = (document.all && document.getElementById)  ? true : false;
ns4   = (document.layers)                          ? true : false;
ns6   = (document.getElementById && !document.all) ? true : false;

function SafeMail(name,domain,subject,display,css) {
  css       = (css)     ? ' class="' + css + '"' : '';
  subject   = (subject) ? '?subject=' + subject : '';
  displayed = (display) ? display : name + '@' + domain;
  mailto    = name + '@' + domain + subject;
  document.write('<a href="mailto:' + mailto + '"' + css + '>' + displayed + '</a>');
}

function Popup(title,filename,w,h,scroll,resizable){if (title == ''){ title = 'popup'; }if (scroll == 'scroll'){ scroll = 'yes'; }else{ scroll = 'no'; }if (resizable == 'lock'){ resizable = 'no'; }else{ resizable = 'yes'; }winOptions = eval("'width="+w+",height="+h+",toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars="+scroll+",resizable="+resizable+",copyhistory=no'");window.open(filename,title,winOptions);return;}
function SetFocus(fieldname,formname){if (formname == ''){ formname = 'main'; } eval("document."+formname+"."+fieldname+".focus();"); }
function DisableButton(obj) {
 obj.src = 'images/btn_processing.gif';
 obj.disabled = true;
 obj.style.border='none';
 obj.style.width = 72;
 obj.style.height = 13;
}

//phone format code
var areacodeLength  = 3;
var firstThree      = 3;
var previousLength  = null;
var thisInput       = null;
var originalLength  = null;
var thisAreacode    = null;
var thisFirstThree  = null;
var thisLastFour    = null;
var phone_test      = false;
var international   = false;
function autoFormat(input,type) {
  //lock out NS4
  if (!document.layers) {
    if (type == 'phone') {
      //var temp_selection = input.selectionStart;
      //alert("selectionStart=" + input.selectionStart);
      var addFirstParen   = false;
      var addSecondParen  = false;
      var addDash         = false;
      thisInput           = input.value.replace(/[. ()-\/]/gi,'');
      //if first input is a "+" assume its an international number and do not format
      if (input.value.length == 1 && input.value == '+') { international = true; return true;}
      else if (input.value.length == 1 && input.value == '(') { previousLength = 1; return true;}
      //dont do anything on backspace
      else if (input.value.length >= previousLength && international == false) {
        thisAreacode    = thisInput.substr(0,3);
        thisFirstThree  = thisInput.substr(3,3);
        thisLastFour    = thisInput.substr(6,4);
        
        //add '('
        if (thisInput.length > 0) { addFirstParen = true; }
        //add ') '
        if (thisAreacode.length == areacodeLength) { addSecondParen = true; }
        //add '-'
        if (thisFirstThree.length == firstThree) { addDash = true; }
        
        //add everything, assign to field
        if (addFirstParen) { thisAreacode = '(' + thisAreacode; }
        if (addSecondParen) { thisAreacode += ') '; }
        if (addDash) { thisFirstThree += '-'; }
        if (phone_test) { alert('writing'); }
        input.value = thisAreacode + thisFirstThree + thisLastFour;
      }
      previousLength = input.value.length
      //if (temp_selection && temp_selection != 'undefined') { input.selectionStart = temp_selection; input.selectionEnd = temp_selection; }
    }
  }
}

function PadDecimals(num, decimal_places) {
  // pad decimal_places?
  if (decimal_places == '0') {
    // return integer part
    return parseInt(num);
  } else {
    // cast as string
    num = num + '';
    
    // get decimal part
    var dec = num.substr((num.indexOf('.')+1));
    
    // if no decimal part, set to 0
    if (num.indexOf('.') == -1) {
      dec = '0';
    }
    
    // pad
    while (dec.length < decimal_places) {
      dec += '0';
    }
    
    // put it all together
    str = parseInt(num) + "." + dec.substr(0, decimal_places);
    
    // return padded number
    return str;
  }
}

function CleanNumber(num) {
  // strip off non digits
  var regex = /[^-.0-9]/g;
  val = new String(num);
  val = val.replace(regex, '');
  return val;
}

function AddCommas(num) {
  num = num + '';
  if (isNaN(num) || num == '') {
    num = 0;
  } else {
    var regex = new RegExp('([0-9])([0-9][0-9][0-9][,.])');
    arrNumber = num.split('.');
    arrNumber[0] += '.';
    do {
      arrNumber[0] = arrNumber[0].replace(regex, '$1,$2');
    } while (regex.test(arrNumber[0]));
    
    if (arrNumber.length > 1) {
      num = arrNumber.join('');
    } else {
      num = arrNumber[0].split('.')[0];
    }
  }
  
  return num;
}

function FormatNumber(num, decimal_places) {
  // only if decimal places
  if (decimal_places > 0) {
    // round number
    num = RoundNumber(num, decimal_places);
    
    // pad to decimal places
    num = PadDecimals(num, decimal_places);
  }
  
  return AddCommas(num);
}

function FormatCurrency(amount) {
	var i = parseFloat(amount);
  if (isNaN(i)) { i = 0.00; }
  
	var minus = '';
	if (i < 0) { minus = '-'; }
  
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
  
	return '$' + AddCommas(s);
}

function ShiftRight(theNumber, k) {
	if (k == 0) {
    return (theNumber);
	} else {
		var k2 = 1;
		var num = k;
		if (num < 0) {
      num = -num;
    }
		for (var i = 1; i <= num; i++) {
			k2 = k2*10;
		}
	}
  
	if (k>0) {
    return(k2*theNumber);
  } else {
    return(theNumber/k2);
  }
}

function RoundSigDig(theNumber, numDigits) {
	with (Math) {
    if (theNumber == 0) {
      return(0);
		} else if(abs(theNumber) < 0.000000000001) {
      return(0);
		} else { // warning: ignores numbers less than 10^(-12)
			var k  = floor(log(abs(theNumber))/log(10))-numDigits;
			var k2 = ShiftRight(round(ShiftRight(abs(theNumber),-k)),k);
			if (theNumber > 0) {
        return(k2);
			} else {
        return(-k2);
      }
	  }
  }
}

function RoundNumber(num, decimal_places) {
  // only if decimal places
  if (decimal_places > 0) {
    num = Math.round(num * Math.pow(10, decimal_places)) / Math.pow(10, decimal_places);
  } else {
    num = Math.round(num);
  }
  
  return (num);
}

