
function ValidateSearch()
{
	if (document.forms[0].txtSearch.value == "" )
	{
		alert("Please type in what you want to search, then click GO.");
		
	}
	else
	{
		document.forms[0].action = "/Search.asp";
		document.forms[0].method="GET";
		document.forms[0].submit();
	}
}

function GoToLink()
{
	if (document.forms[0].cboLinks.options[document.forms[0].cboLinks.selectedIndex].value != "NoneSelected")
	{
		window.open(document.forms[0].cboLinks.options[document.forms[0].cboLinks.selectedIndex].value)
		//window.location = document.forms[0].cboLinks.options[document.forms[0].cboLinks.selectedIndex].value;
	}
}

function GoToFAQSelect()
{
	if (document.forms[0].cboFAQ.selectedIndex != 0)
	{
		window.location = document.forms[0].cboFAQ.options[document.forms[0].cboFAQ.selectedIndex].value;
	}
}

function GetColumn()
{
	window.location = document.forms[0].cboColumn.options[document.forms[0].cboColumn.selectedIndex].value;
}

function OpenNewWindow(url)
{
	//window.open(url, "MoreInformation","toolbar=yes,location=yes,directories=no,menubar=yes,scrollbars=yes,resizable=yes,top=20,left=20,width=680,height=480");
	window.open(url);
}

function ClickButton(sButton)
{
	document.forms[0].txtProcess.value = sButton
	var myArray = new Array();	
	
	document.forms[0].submit()
}




function SortBy(sColumnName)
{
	if(document.forms[0].txtSortBy.value == sColumnName){
		if(document.forms[0].txtSortByDirection.value == "ASC"){
			document.forms[0].txtSortByDirection.value = "DESC";		
		}
		else{
			document.forms[0].txtSortByDirection.value = "ASC";		
		}
	}
	
	if(document.forms[0].txtSortByDirection.value == ""){
		document.forms[0].txtSortByDirection.value = "DESC";
	}
	
	document.forms[0].txtSortBy.value = sColumnName;
	document.forms[0].txtCurrentPage.value = 1;
	document.forms[0].submit();
}

function format_phone(oElement)
{
	var sPhone = new String();
	var i
	for (i=0;i < oElement.value.length;i++)
	{
		if (!isNaN(oElement.value.substring(i,i+1)) && oElement.value.substring(i,i+1) != ' ') 
		{
			sPhone = sPhone + oElement.value.substring(i,i+1);
		};
	};
	
	if (sPhone.length == 7)
	{
		sPhone = '919' + sPhone;
	}
	
	if (sPhone.length == 10)
	{
		oElement.value = "(" + sPhone.substring(0,3) + ") " + sPhone.substring(3,6) + "-" + sPhone.substring(6,10);
	}
	else
	{
		return false;
	}
	
	return true;
 }
 
 function format_zipcode(oElement)
{
	var sZip = new String();
	var i
	for (i=0;i < oElement.value.length;i++)
	{
		if (!isNaN(oElement.value.substring(i,i+1))) 
		{
			sZip = sZip + oElement.value.substring(i,i+1);
		};
	};
	
	if (sZip.length == 5)
	{
		oElement.value = sZip;
	}
	else if (sZip.length == 9)
	{
		oElement.value = sZip.substring(0, 5) + "-" + sZip.substring(5, 9);
	}
	else
	{
		return false
	}
	
	return true
 }
 
function IsDate(oElement)
{
	var str = new String(oElement.value);
	var re = /^(\d{1,2})\/(\d{1,2})\/(\d{2,4})$/
	var arr = re.exec(str);
	var dMon;
	var dDay;
	var dYear;
	if (arr == null 
		|| isNaN(RegExp.$1)
		|| isNaN(RegExp.$2)
		|| isNaN(RegExp.$3)) 
	{
		return false;
	};

	dMon = parseInt(RegExp.$1,10);
	dDay = parseInt(RegExp.$2,10);
	dYear = parseInt(RegExp.$3,10);
	if (dMon < 1 || dMon > 12) return false;
	switch (dMon)
	{
		case 4,6,9,11:
			if (dDay < 1 || dDay > 30) return false;
			break;
		case 2: 
			if (dYear % 4 == 0)
			{
				if (dDay < 1 || dDay > 29) return false;
			}
			else
			{
				if (dDay < 1 || dDay > 28) return false;
			}
			break;
		default:
			if (dDay < 1 || dDay > 31) return false;
			break;
	};
	
	if (!((dYear >= 0 && dYear <= 99) || (dYear >= 1900 && dYear <= 3000))) return false
	return true;
}; 

function IsNumeric(oElement)
{
	var sValue
				
	//replace every $ sign with empty string
	var sValue = oElement.value.replace(/\$/g, "")	
				
	sValue = sValue.replace(/\s/g, "A")	//space is a number for function isNaN
	
	return ((isNaN(sValue) == true) ? false : true)
}

function IsNumericValue(sString)
{
	var sValue
				
	//replace every $ sign with empty string
	var sValue = sString.replace(/\$/g, "")
	
	//space is a number for function isNaN
	sValue = sValue.replace(/\s/g, "A")	
	
	return ((isNaN(sValue) == true) ? false : true)
}

function unformat_currency(oElement)
{
	var sValue; var sTValue;
	
	//replace every $ sign with empty string
	if (oElement == "[object]")
		sTValue = oElement.value
	else
		sTValue = oElement

	sValue = sTValue.replace(/\$/g, "") //remove dollar signs
	sValue = sValue.replace(/,/g, "")	//remove commas
	sValue = sValue.replace(/\s/g, "A")	//space is a number for function isNaN

	
	return ((isNaN(sValue) == true) ? sTValue : sValue)
}

function format_currency(sStr, bCommas, bDollarSign)
 {
	var sCur = new String();
	var sTmpStr = new String(sStr);
	var i;
	var iWhole;
	var sWhole;
		
	sTmpStr = sTmpStr.replace(/,/g,"");
	sTmpStr = sTmpStr.replace(/\$/g,"");
	sTmpStr = sTmpStr.replace(/ /g,"");
	
	if (sTmpStr.length == 0) return "";
		
	if (isNaN(sTmpStr)) return sStr;
		
	iWhole = parseInt(sTmpStr);
	
	if (isNaN(iWhole))
		iWhole = 0;
		
	sCur = format(sTmpStr,2);	

	sCur = sCur.replace(/-/g,"");
	
	if (iWhole < 0) sCur = "-" + sCur;

	if (bDollarSign) sCur = dollarize(sCur);

	return sCur;
 };
			
function format (expr, decplaces)
{
	var tmpStr 
	var i
	
	// raise incoming value by power of 10 times the
	// number of decimal places; round to an integer; convert to string
	var str = "" + Math.round (eval(expr) * Math.pow(10,decplaces))
 
	// pad small value strings with zeros to the left of rounded number
	while (str.length <= decplaces) 
	{
		str = "0" + str
	}
 
	// establish location of decimal point
	var decpoint = str.length - decplaces
	
	// assemble final result from: (a) the string up to the position of
	// the decimal point; (b) the decimal point; and (c) the balance
	// of the string. Return finished product.
	tmpStr = str.substring(0,decpoint) + "." + str.substring(decpoint,str.length);
	
	// split string into dollars and cents
	var dollars = tmpStr.substring(0,(tmpStr.length - 2)- (decplaces -1));
	var cents = tmpStr.substring((tmpStr.length - 1) - decplaces, tmpStr.length);

	// determine how many commas are needed
	var commas = parseInt(dollars.length / 3)

	var tmpdollars = dollars
	dollars = ""
	var tmplength = tmpdollars.length
	
	for (i = 1;i<= tmplength;i++)
	{
		dollars = tmpdollars.substr(tmplength - i, 1) + dollars;
		if (i % 3 == 0 && i != tmplength)
		{
			dollars = "," + dollars;
		} 
	}
	
	dollars = dollars + cents;
	return dollars;
 
}
			
function GETExprValue(sEXPR)
{
// Examines a given Factor and computes a value 
	var iIDX = 0
	var iPTR = 0
	var aArray = new Array()
	var vValue
	var sTString = ""
				
	//Build an array of figures and operations
	for (iIDX = 0; iIDX < sEXPR.length; iIDX++)
	{
		switch (sEXPR.substr(iIDX, 1))
		{
			case "+": case "-": case "*": case "/":
				iPTR = iPTR + 1
				aArray[iPTR] = sEXPR.substr(iIDX, 1)
				iPTR = iPTR + 1
				break;
			default:
				if (aArray.length == iPTR)
					aArray[iPTR] = ""
								
				sTString = aArray[iPTR]
				aArray[iPTR] = sTString.concat(sEXPR.substr(iIDX, 1))
				break;
		}
	}
				
	//Compute value by examining array elements
	vValue = aArray[0]
	for (iIDX = 1; iIDX < aArray.length; iIDX++)
	{
		switch (aArray[iIDX])
		{
			case "+": 
				vValue = parseFloat(vValue) + parseFloat(aArray[iIDX + 1])
				break;
			case "-": 
				vValue = parseFloat(vValue) - parseFloat(aArray[iIDX + 1])
				break;
			case "*": 
				vValue = parseFloat(vValue) * parseFloat(aArray[iIDX + 1])
				break;
			case "/":
				vValue = parseFloat(vValue) / parseFloat(aArray[iIDX + 1])
				break;
		}
		iIDX = iIDX + 1
	}
	return vValue
}

// turn incoming expression into a dollar value
function dollarize (expr) 
{
	return "$" + expr;
}

/*
 Email Syntax Validator v1.02
 (c) 2000 ~ Sascha.
 http://solair.eunet.yu/~aca2/ */

function emailwarning(address) {
	address.focus();
	address.select();
	alert("Syntax of your e-mail address is invalid. Please provide valid e-mail address.");
	}
	
function checkemail(address) {
	var x=address.value;
	var a=/([^A-Za-z0-9&_@\.\-\[\]])|(\.\.|@\.|\s)|(^@|^\.|@$|\.$)/;
	var b=/@/;
	var c=/\./;
	var d=/^(.+)@(.+)$/;
	var e=x.match(d);
	if ((a.test(x)) || (e==null)) {
		emailwarning(address);
		return false;}
	var f=e[1];
	var g=e[2];
	if ((!(c.test(g)))||(b.test(f))){
		emailwarning(address);
		return false;}
	var h=/[^0-9\.\[\]]/;
	var j=/^\[?(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]?$/;
	var k=g.match(j);
	if (!(h.test(g))&&(k!=null)) {
		if ((k[1]>255)||(k[2]>255)||(k[3]>255)||(k[4]>255)) {
			emailwarning(address);
			return false;}
		else {
			return true;}}
	else {
		var m=/\[|\]/;
		if (m.test(x)) {
			emailwarning(address);
			return false;}
		var n = /^(.+)\.(.+)$/;
		var p=g.match(n);
		var q=p[2];
		var r=/[^A-Za-z]/;
		var s=/info|name|coop|aero/i;
		var t=/museum/i;
		var v=/biz|pro|com|net|org|edu|gov|mil/i;
		if (((q.length==2) && (!(r.test(q))))||(q.length==3) && (v.test(q))||((q.length==4)&&(s.test(q)))||((q.length==6)&&(t.test(q)))) {
			return true;}
		emailwarning(address);
		return false;}}
		
		
		
function LTrim(str){
   var whitespace = new String(" \t\n\r");
   var s = new String(str);

   if (whitespace.indexOf(s.charAt(0)) != -1) {  
	var j=0, i = s.length;
    while (j > i && whitespace.indexOf(s.charAt(j)) != -1) j++;
    s = s.substring(j, i);
   }
   return s;
}


function RTrim(str){  
	var whitespace = new String(" \t\n\r");
	var s = new String(str);

   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {  
	var i = s.length - 1;       // Get length of string
    while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1) i--;
    s = s.substring(0, i+1);
   }
   return s;
}


function trim(str)
{ return RTrim(LTrim(str));
}		


function ViewCompanyDetails(id)
{
  //Used for the ComplaintRatio pages
  document.forms[0].action = "ViewCompanyDetails.asp";
  document.forms[0].CompanyId.value = id;
  document.forms[0].submit();					
}	 

function UpdateSelectedCheckboxes(id)
{				
  //Used for the ComplaintRatio pages
  var str = document.forms[0].CompareCheckboxHistory.value;
  //alert("UpdateSelectedCheckboxes("+id+")");
  if( str.match(id) != null)
  {
  	//Remove from the hidden field
  	//str = str.substring(1,(str.indexOf(id)-1) + str.substring(str.indexOf(id) + id.length, str.length));			 
  	var strFrontSide = trim(str.substring(0,str.indexOf(id)));
  	var strBackSide  = trim(str.substring(strFrontSide.length + id.length  + 1, str.length));			
  	//alert("strFrontSide = " + strFrontSide + "::length=" + strFrontSide.length );
  	if (strFrontSide.substring(strFrontSide.length -1,strFrontSide.length) == ",")
  	{	strFrontSide = strFrontSide.substring(0,strFrontSide.length-1);
  	}
  	if (strBackSide.substring(0,1) == ",")
  	{	strBackSide = strBackSide.substring(1,strBackSide.length);
  	}
  	//alert("strFrontSide = " + strFrontSide + "::length=" + strFrontSide.length );
  	//alert("strBackSide = " + strBackSide + "::length=" + strBackSide.length);
		
  	if(strBackSide.length == 0 )
  	{	str = strFrontSide;						
  	}
  	else if(strFrontSide.length == 0)
  	{	str = strBackSide;
  	}
  	else if(strFrontSide.length == 0 && strBackSide.length == 0)
  	{	str = "";
  	}
  	else
  	{	str = strFrontSide + "," + strBackSide;
  	}						
  	document.forms[0].CompareCheckboxHistory.value = str;
			
  }
  //alert(str);
} 
function DisplayComparedCompany()
{
  //Used for the ComplaintRatio pages
  document.forms[0].action = "ComplaintRatioCompareCompanies.asp";
  document.forms[0].txtCurrentPage.value = "1";
  document.forms[0].submit();		
}
