﻿/*************************************************
	Common JS tools for the site
/*************************************************/
var detect = navigator.userAgent.toLowerCase();
var OS,browser,version,total,thestring;

if (checkIt('konqueror'))
{
	browser = "Konqueror";
	OS = "Linux";
}
else if (checkIt('safari')) browser = "Safari";
else if (checkIt('chrome')) browser = "Chrome";
else if (checkIt('omniweb')) browser = "OmniWeb";
else if (checkIt('opera')) browser = "Opera";
else if (checkIt('webtv')) browser = "WebTV";
else if (checkIt('icab')) browser = "iCab";
else if (checkIt('msie')) browser = "Internet Explorer";
else if (!checkIt('compatible'))
{
	browser = "Netscape Navigator";
	version = detect.charAt(8);
}
else browser = "An unknown browser";

if (!version) version = detect.charAt(place + thestring.length);

if (!OS)
{
	if (checkIt('linux')) OS = "Linux";
	else if (checkIt('x11')) OS = "Unix";
	else if (checkIt('mac')) OS = "Mac";
	else if (checkIt('win')) OS = "Windows";
	else OS = "an unknown operating system";
}

function checkIt(string)
{
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}

//-----------------------------------------------------------------------------------------

function goToUrl(strURL) {
	window.location = strURL;
}

function openSmall(strFile, thewidth, theheight) {
	  window.open(strFile,'Add','width=' + thewidth + ',height=' + theheight + ',top=0,left=0,scrollbars=1,resizable=1,status=1');
}


function toggleNotified(cObj, tTable, sOpenImg, sClosedImg, link){
	if (document.getElementById(tTable).style.display == "none") {
		document.getElementById(tTable).style.display = "block";
		//cObj.i = "[ " + sOpenText + " ]";
		document.getElementById(link).setAttribute("src",sOpenImg);
		}
	else {
		document.getElementById(link).setAttribute("src",sClosedImg);
		document.getElementById(tTable).style.display = "none";
		//cObj.innerText = "[ " + sClosedText + " ]";		
		}
}

function checkHTML(obj)
{
	if (obj.value.indexOf('<') != -1)
		{ doHTMLStringStuff(obj,'bracket [<]',1);  return false; 	}
	if (obj.value.indexOf('>') != -1)
		{ doHTMLStringStuff(obj,'bracket [>]',1); return false; 	}													
	if (obj.value.toLowerCase().indexOf('bcc') != -1)
		{ doHTMLStringStuff(obj,'BCC',3); return false; 	}	
	return true;		
}

function doHTMLStringStuff(obj,err,removelength)
{
	obj.style.color='red'; 
	alert("Sorry, but you may not enter " + err + " here!"); 
	obj.style.color='#000000';
	newVal = RemoveHTML(obj.value);
	newVal = newVal.substr(0,newVal.length-removelength);
	obj.value = newVal;
}

function RemoveHTML( strText )
{
	var regEx = /<[^>]*>/g;
	return strText.replace(regEx, "");
}

function Trim(sInString) {
  sInString = sInString.replace( /^\s+/g, "" );// strip leading
  return sInString.replace( /\s+$/g, "" );// strip trailing
}

//confirm delete
//input - the name of what you are confirming
function confirmDelete(sItem) {
	if (confirm(sItem)==true) {
	return true;
	} else {
		return false;
	}	
}

//returns form select value
function getSelectVal(str)
{
	var index1 = document.getElementById(str).selectedIndex;
	var out = 0;
	if (index1 >= 0) 
	{	
		out = document.getElementById(str).options[index1].value;
	}
	return out;
}

//returns form element value
function getVal(str)
{
	var val="";
	if (document.getElementById(str) != null)
	{	
		var s = Trim(document.getElementById(str).value);
		val = (s.length > 0) ? s : "";
	}	
	return val;	
}

// returns 1 or more responses from checkboxes with the same name/id as csv
function getCollectionVal(str)
{
	var val="";
	var s="";
	if (document.getElementById(str) != null)
	{	
		for(var i=0; i < document.forms[0][str].length; i++)
		{
			if(document.forms[0][str][i].checked)
			{
				s+= (s.length != 0)? ", ":"";
				s+= document.forms[0][str][i].value
			}	
		}
		val += (s.length > 0) ? s : "";
	}	
	return val;	
}

//formats numbers as Currency
function formatCurrency(num, sym, symBefore) {
	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));
	if (symBefore) 
		return (((sign)?'':'-') + sym + num + '.' + cents);
	else
		return (((sign)?'':'-') + num + '.' + cents + ' ' + sym);
}


function ValidateEmail(src) {
    var emailReg = "^[\\w\\.]+@[a-zA-Z_-]+?\\.[a-zA-Z]{2,3}$" ;//"^[a-zA-Z][\\w\\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]\\.[a-zA-Z][a-zA-Z\\.]*[a-zA-Z]$";
     var regex = new RegExp(emailReg);
     return regex.test(src);
  }
	
// returns formatted number to form element
function FormatNumber(num, format, shortformat)
{
	if(format==null)
	{
		// Choose the default format you prefer for the number. 
		//format = "#-(###) ###-#### ";	// Telephone w/ LD Prefix and Area Code
		format = "(###) ###-#### ";	// Telephone w/ Area Code
		//format = "###-###-####";	// Telephone w/ Area Code (dash seperated)
		//format = "###-##-####";	//Social Security Number
	}					

	if(shortformat==null)
	{
		// Choose the short format (without area code) you prefer. 
		//If you do not want multiple formats, leave it as "".

		//var shortformat = "###-#### ";
		var shortformat = "";
	}
	

	var validchars = "0123456789";
	var tempstring = "";
	var returnstring = "";
	var extension = "";
	var tempstringpointer = 0;
	var returnstringpointer = 0;
	count = 0;

	var length = num.value.length;
		
	if (length > format.length)
	{
		length = format.length;
	}
	
	for (var x=0; x<length; x++)
	{
		if (validchars.indexOf(num.value.charAt(x))!=-1)
		{
			tempstring = tempstring + num.value.charAt(x);
		}
	}
	
	if (num.value.length > format.length)
	{
		length = format.length;
		extension = num.value.substr(format.length, (num.value.length-format.length));
	}
	
	
	for (x=0; x<shortformat.length;x++)
	{
		if (shortformat.substr(x, 1)=="#")
			{count++;}
	}
	
	if (tempstring.length <= count)
		{format = shortformat;}
	
	for (x=0; x<format.length;x++)
	{
		if (tempstringpointer <= tempstring.length)
		{
			if (format.substr(x, 1)=="#")
			{
				returnstring = returnstring + tempstring.substr(tempstringpointer, 1);
				tempstringpointer++;
			}else{
				returnstring = returnstring + format.substr(x, 1);
			}
		}
	}

	// We have gone through the entire format, let's add the extension back on.
	returnstring = returnstring + extension;
	
	num.value = returnstring;
}		

