//-----------------------------------------------------------------------------
//	FILENAME:		common.js
//	DESCRIPTION:	Common functions for Ohio Efiling
//					[see OHFJavaScriptDesign.DOC]
//	AUTHOR:			James de Las Heras, Kimberly Martin, David Carroll
//	DATE:			04/14/1999
//-----------------------------------------------------------------------------

//--------------------------------------------------------------
// INTERNAL FUNCTIONS
//--------------------------------------------------------------
function initErrorArray () {
	this.length = initErrorArray.arguments.length;
	for (var i=0; i < this.length; i++) {
		this[i] = initErrorArray.arguments[i];
	}
}

//--------------------------------------------------------------
// GLOBAL VARIABLES
//--------------------------------------------------------------
var strErrorArray = new initErrorArray(
	"Error: Please enter your nine digit Taxpayer ID.",
	"Invalid Date: Please use mm/dd/yyyy format",
	"This field is protected and cannot be changed.",
	"Invalid first character. $, and other signs not valid.",
	"Invalid character(s), please use only digits, and decimals",
	"The entered amount contains invalid characters. The Business Service Center will remove them.",
	"Gross Sales must be greater than $0.00",
	"This field cannot contain a negative value.",
	"Exempt Sales cannot be greater than Gross Sales",
	"This value exceeds the maximum amount allowed 9999999999.99",
	"Please enter a valid date in the format: yyyy-mm-dd.",
	"This field is required and cannot be left blank.");
	
//No comma after last argument in the above function call	


//--------------------------------------------------------------
//	GLOBAL CONSTANTS
//--------------------------------------------------------------
// Constant used to index strErrorArray
var MSG_EIN_ENTRY_ERROR			= 0;
var MSG_INVALID_DATE_ERROR		= 1;
var MSG_PROTECTED_FIELD_ERROR	= 2;
var MSG_INVALID_FIRST_CUR_CHAR	= 3;
var MSG_INVALID_CUR_CHAR		= 4;
var MSG_NOT_CURRENCY			= 5;
var MSG_GROSS_SALES_0			= 6;
var MSG_NEG_VALUE				= 7;
var MSG_EXEMPT_GREAT_GROSS		= 8;
var MSG_TOO_GREAT				= 9;
var MSG_NOT_DATE				= 10;
var MSG_REQUIRED_FIELD			= 11;

//var MASK_EIN					= "##-#######";
var EIN_LENGTH					= 9;
var HELP_WIN_WIDTH				= 400;
var HELP_WIN_HEIGHT				= 200;
var HELP_WIN_NAME				= "Web_File_Help";
var ALL_DIGITS					= "1234567890";
var ALL_CHARS					= "abcdefghijklmnopqrstuvwxyz";
var MAX_VALUE					= "9999999999.99";
var NETSCAPE_BROWSER			= 0;
var INTERNET_EXPLORER_BROWSER	= 1;
var OTHER_BROWSER				= 2;
var HELP_FORM_URL				= "help.htm?";

var ZERO_DOLLARS_CENTS			= "$0.00"

var CD_ID_ACCT_FEIN				= "001";
var CD_ID_ACCT_SSN				= "002";


//--------------------------------------------------------------
//	GLOBAL FUNCTIONS
//--------------------------------------------------------------

function IsDigit (n) {
	if (ALL_DIGITS.indexOf (n) >= 0) {
		return true;
	}else
		return false;
}


function IsInt(n){
	for (i = 0; i < n.length; i++) {
		if (!IsDigit (n.charAt(i))) {
			return false;
		}
	}
	return true;
}

function IsDecimal(n){
	for (i = 0; i < n.length; i++) {
		if (!IsDigit(n.charAt(i)) && "." != n.charAt(i)) {
			return false;
		}
	}
	return true;
}


//------------------------------------------------------------------------------
//	Function Name:	IsDateUS
//	Parameters:		n - a string
//	Return Value:	Boolean
//	Description:	Returns true if n is a valid date in the format mm/dd/yyyy, 
//					else returns false. Checks leap years.
//------------------------------------------------------------------------------
function IsDateUS(a) {
	
	var ValidDate = true;
	var b = "";
	var d = "";
	var f = "";
    
	if (!IsInt(StripChar(a,"/"))){
		//alert("interr");
		return false;
	}		

	b = a.substring(0, a.indexOf("/"))// month
	a = a.substring(a.indexOf("/")+1)
	d = a.substring(0, a.indexOf("/"))// day
	f = a.substring(a.indexOf("/")+1)

	if (f.length != 4){
		return false;
	}
	if (b<1 || b>12){
		return false;
	} 
	if (d<1 || d>31){
		return false;
	}
	if (f<0 || f>2999){
		return false;
	}	
	if (b==4 || b==6 || b==9 || b==11){
		if (d==31){
			return false;
		}	
	}
	if (b==2){
		if (d>29){
			return false;
		}	
		if (d==29){
			if (f % 4 != 0){
				return false;
			}	
			if ((f % 4==0) && (f % 100==0) && (f % 400!=0)){
				return false;
			}	
		}
	}		
 return ValidDate;
}

function IsAmount(n){
	//-- Strip commas.
	n = StripChar(n,",");

	//-- Handle $ sign.
	if (n.charAt(0) == "$"){
		n = n.substring(1);
	}
	if (!IsInt(n)){
		return false;
	}else		
	return true;
}

function IsPhoneNum(n){
	n = StripChar(n,"-");
	n = StripChar(n,"(");
	n = StripChar(n,")");
	n = StripChar(n," ");
	if(n.length != 10 || !IsInt(n))
		return false;
	else
		return true;
}

function FormatDecimal(n){
	var r = "";
	for (var i=0; i < n.length; i++){	
		digit = n.charAt(i);
		if (IsDigit(digit) || digit == "." ){
			r = r + digit;
		}  
	}
	return r;
}	

function FormatInteger(n)  {
    var r = "";
	for (var i=0; i < n.length; i++){	
		digit = n.charAt(i);
		if (IsDigit(digit)){
			r = r + digit;
		}  
		//alert(r);
	}
	return r
}


function CheckDate(field){
	if(!IsDateUS(field.value)){
		alert("Please enter a valid date.  It must be in the MM/DD/YYYY format.");
		field.focus();
		field.select();
	}
}

function CheckAmount(field){
	if(!IsAmount(field.value) || field.value <= 0 && field.value != ""){
		alert("Please enter a valid whole dollar amount.");
		field.focus();
		field.select();
	}else{
	field.value = FormatInteger(field.value);
	}
}

function CheckInteger(field){
	if(!IsInt(field.value) || field.value <= 0 && field.value != ""){
		alert("Please enter a positive whole number.");
		field.focus();
		field.select();
	}
}

function CheckDecimal(field){
	if(!IsDecimal(field.value) || field.value <= 0 && field.value != ""){
		alert("Please enter a valid positive number.");
		field.focus();
		field.select();
	}
}

function CheckPhoneNum(field){
	if(!IsPhoneNum(field.value) && field.value != ""){
		alert("Please enter a valid 10-digit phone number");
		field.focus();
		field.select();
		return false;
	}else{
	//field.value = FormatInteger(field.value);
	}
}

//----------------------------------------------------------------------------------
//Function Name:	FnFormatNumeric
//	Parameters:		inputValue
//	Return Value:	Boolean
//	Description:	Takes input value and returns a plain numeric value that 
//                  can be used for computation.	
//
//	Dependencies:	FnIsDigit
//
//	Revision History:
//	Date     Revised By  Description
//	-------- ----------- -------------------------------------------------------
//	05/02/99 KAM		 Created.
//-----------------------------------------------------------------------------
function FnFormatNumeric(inputValue){
	var digit;
	var FinalNum = "";
	var chr;
	var minus = false;
	inputValue = "" + inputValue;
	var returnValue = "";
	
	if (inputValue == ""){
		return (inputValue);
	}
	
	chr = inputValue.charAt(0);
	//-- Strip all leading 0, $, other
	
	while (!parseFloat(chr) && chr != "." && inputValue != ""){
		if (chr == '-'){
			minus = true;
		}
		inputValue = inputValue.substring(1);
		chr = inputValue.charAt(0);
	}
		
	// strip everything but "." and digits 
	for (var i=0; i < inputValue.length; i++){	
		digit = inputValue.charAt(i);
		if (parseFloat(digit) || digit == "." || digit == "0"){
			returnValue = returnValue + digit;
		}  
	}
	
	//-- If there is a "." return no more than 2 decimal points.
	if (returnValue.indexOf(".") != -1){
		returnValue = returnValue.substring(0, returnValue.indexOf(".", 0)+3);
	}	
		
	if (returnValue.charAt(0) == "."){
		returnValue = "0" + returnValue;
	}
	if (minus){
		returnValue = "-" + returnValue;
	}				
	return(returnValue)
}

//------------------------------------------------------------------------------
//	Function Name:	FnFormatPercentage
//	Parameters:		s - a string representing a floating point decimal
//	Return Value:	a string
//	Description:	Takes a decimal value and returns a formatted percentage
//					ie:  "0.06" becomes "6%"
//	Dependencies:	Uses FnFormatNumeric
//
//	Revision History:
//	Date     Revised By  Description
//	-------- ----------- -------------------------------------------------------
//	07/11/99 DWC		 Created.
//------------------------------------------------------------------------------
function FnFormatPercentage(s){
		var num = 0;
		num = eval(s * 100);
		s = FnFormatNumeric(num) + "%";
		return s;		
}

//------------------------------------------------------------------------------
//	Function Name:	FnStripDashes
//	Parameters:		a - any string
//	Return Value:	a string
//	Description:	removes "-" characters from any string
//					ie:  "12-01-99" becomes "120199"
//
//	Revision History:
//	Date     Revised By  Description
//	-------- ----------- -------------------------------------------------------
//	07/05/99 DWC		 Created.
//------------------------------------------------------------------------------
function FnStripDashes(a){
		var b = "";
		var chr;
		for (var i=0;i < a.length;i++){
			chr = a.charAt(i);
			if (chr != '-'){
				b += chr;
			}
		}
		return b;
	}


//------------------------------------------------------------------------------
//	Function Name:	StripChar
//	Parameters:		a - any string, stripchr - Char to be stripped
//	Return Value:	a string
//	Description:	removes characters from a string
//					ie:  "12-01-99" becomes "120199"
//------------------------------------------------------------------------------
function StripChar(a,stripchr){
		var b = "";
		var chr;
		for (var i=0;i < a.length;i++){
			chr = a.charAt(i);
			if (chr != stripchr){
				b += chr;
			}
		}
		return b;
	}






//----------------------------------------------------------------------------
// Function :		FnGetClientDate
// Parameters :		none
// Return Value :	a string with the date on the client machine in
//					YYYY-MM-DD format
//
// Revision History:
// Date     Revised By  Description
// -------- ----------- -------------------------------------------------
// 07/20/99 DWC			Created.
//----------------------------------------------------------------------------
function FnGetClientDate(){
		var today = new Date(); 
		var str = "";
		var thisyear = today.getFullYear();
		var thismonth = today.getMonth() + 1;
		var thisday = today.getDate();
		if (thismonth < 10){
			thismonth = "" + "0" + thismonth;
		}
		if (thisday < 10){
			thisday = "" + "0" + thisday;
		}
		str = "" + thisyear + "-" + thismonth + "-" + thisday;
		return str;
	}

function ViewListingDetail(num){
	var url,name,features;
	url = "/listings/viewlistingdetail.asp?id=" + num;
	name = "viewlistingdetail";
	features = "status=no,height=450,width=600,directories=no,menubar=no";
	window.open(url,name,features)
}

function ViewContractDetail(num){
	var url,name,features;
	url = "/contracts/viewcontractdetail.asp?id=" + num;
	name = "viewcontractdetail";
	features = "status=no,height=450,width=600,directories=no,menubar=no,scrollbars=yes";
	window.open(url,name,features)
}


function CheckRequiredFields(form){
	for (var i=0; i < form.length; i++){
		oItem = form.elements[i];
		if (oItem.className == "required" && oItem.value == ""){
			alert("A required field, " + oItem.name + ", has been left blank.");
			oItem.focus();
			return false;
		}	
	}
	
	return true;
}


function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function NewSelectIt(newLoc){
	newPage = newLoc.options[newLoc.selectedIndex].value;
		if (newPage != "") {
			window.location.href = newPage;
		}
}