//Global variables...
var error_color = "pink"; // This is the background color of fields when an error occurs.
var disabled_color = "#cccccc"; // This is the background color of fields when they are disabled.
var reLoadFunctionName = "";//false; //It tells popup window if this page has a reLoadMe() function.

// Browser Detection:
var NS4 = (navigator.appName == "Netscape" && parseInt(navigator.appVersion) < 5);
var NSX = (navigator.appName == "Netscape");
var IE4 = (document.all) ? true : false;

String.prototype.trim = trim;

function movetocenter()
{
    var w = self.screen.width;
    var h = self.screen.height;
    if (document.all) { myw = document.documentElement.clientWidth; myh = document.documentElement.clientHeight; }
    else if (document.layers) { myw = window.innerWidth; myh = window.innerHeight; }
    w = w - myw;
    h = h - myh;
    if (window.moveTo) window.moveTo(w/2,h/2);
}

function redirectTo(theURL, sec){
    sec = (sec == null)? 5 : sec
    if(theURL != ""){
        setTimeout("window.location = '"+ theURL +"'", sec*1000)
    }
}

function exitWindow(ifReload)
{
    if(ifReload){
        var openerWindow = window.opener;
        if(openerWindow.reLoadFunctionName != ""){
            eval("openerWindow." + openerWindow.reLoadFunctionName + "()");
        }
    }
    window.close();
}

function resizeWithText(txtFld){
    if (txtFld.scrollHeight > txtFld.clientHeight){
        txtFld.style.height = txtFld.clientHeight + 15;
    }
    //How to use:  onkeyup="resizeWithText(this)"    
}

function showHideCalendar(cal){
    var objDiv = document.getElementById(cal);
    var filterscheck = objDiv.filters && objDiv.filters.length > 0;  
    var display = (objDiv.style.display == "none") ? "block" : "none";
    if(filterscheck){objDiv.filters[0].apply();}
    objDiv.style.display = display;
    if(filterscheck){objDiv.filters[0].play();}
}

function chkRadio(fld, n){
    document.aspnetForm[fld][n].checked = true;
}

function makeEmpty(fld){
    //fld.value = "";
    document.aspnetForm.ctl00$PageContent$txtOrderTitle.value = "";
}

function collapseExpandPanel(divName)
{
    var obj = document.getElementById("div" + divName);
    var img = document.getElementById("img" + divName);
    var display =(obj.style.display == "block") ? "none" : "block";
    var imgsrc = (obj.style.display == "block") ? "../../pub/imgs/icons/exp_right.gif" : "../../pub/imgs/icons/exp_down.gif";
    var filterscheck = obj.filters && obj.filters.length > 0;
    
    if(filterscheck){obj.filters[0].apply();}
    obj.style.display = display;
    if(filterscheck){obj.filters[0].play();}
    
    if(filterscheck){img.filters[0].apply();}
    img.src = imgsrc;
    if(filterscheck){img.filters[0].play();}
}

function ScrollToBottom() 
{ 
    //self.scrollBy(0,90000);
    if (document.body.scrollHeight) 
    { 
        self.scrollBy(0, document.body.scrollHeight); 
    } 
    else if (screen.height) 
    { 
        self.scrollBy(0, screen.height); 
    } 
}

//************************************************************************
//************************************************************************

function disableOnClick(obj){ 
        setTimeout("reallyDisableOnClick('"+obj.name+"')",200);
}

function reallyDisableOnClick(fld){
    var frm = document.forms[0].name;
    document[frm][fld].disabled = true;    
}



//************************************************************************
//********************** All Cookie Related Functions ********************
//************************************************************************
function getCookie(name){
    var cname = name + "=";               
    var dc = document.cookie;             
    if (dc.length > 0){
        begin = dc.indexOf(cname);       
	    if (begin != -1) {           
            begin += cname.length;       
            end = dc.indexOf(";", begin);
    	    if (end == -1){
    	        end = dc.length;
    	    }
    	    return unescape(dc.substring(begin, end));
        } 
    }
    return null;
}

function setCookie(name, value, expires, path, domain, secure) {
    document.cookie = name + "=" + escape(value) + 
    ((expires == null) ? "" : "; expires=" + expires.toGMTString()) +
    ((path == null) ? "" : "; path=" + path) +
    ((domain == null) ? "" : "; domain=" + domain) +
    ((secure == null) ? "" : "; secure");
}

//************************************************************************
//********************* Form Validation and Masking **********************
//************************************************************************

function trim()
{
	return this.replace(/^\s+/,'').replace(/\s+$/,'');
}

function isEmpty(field)
{
	if (field.trim() == "")
	{
	 return true;
	}
	return false;
}// isEmpty(field)


function formatZIP(strField){
	var oLen = strField.value.length;
	if(oLen == 5){strField.value += "-";}
}

function formatPhoneFax(strField,e)
{
	var oLen = strField.value.length; 
   	if (e.keyCode != 8 && e.keyCode != 9){
		if(oLen == 0){strField.value += "(";}
		if(oLen == 4){strField.value += ") ";}
		if(oLen == 9){strField.value += "-";}
	}
}

function checkphone(strField)
{
	if (strField.value.length == 1){
		strField.value = "" + strField.value;
	}
}

function ssnmask(strField)
{
    var patt = /(\d{3}).*(\d{2}).*(\d{4})/;
    var donepatt = /^(\d{3})-(\d{2})-(\d{4})$/;
    var str = strField.value;
    var result;
    if (!str.match(donepatt)){
        result = str.match(patt);
        if (result!= null){
            strField.value = strField.value.replace(/[^\d]/gi,'');
            str = result[1] + '-' + result[2] + '-' + result[3];
            strField.value = str;
        }
        else{
            if (strField.value.match(/[^\d]/gi)){
                strField.value = strField.value.replace(/[^\d]/gi,'');
            }
        }
    }
}

function orderIdMask(strField)
{
    var patt = /(\d{1,8})/;
    var donepatt = /^(\d{1,8})$/;
    var str = strField.value;
    var result;
    if (!str.match(donepatt)){
        result = str.match(patt);
        if (result!= null){
            strField.value = strField.value.replace(/[^\d]/gi,'');
            str = result[1];
            strField.value = str;
        }
        else{
            if (strField.value.match(/[^\d]/gi)){
                strField.value = strField.value.replace(/[^\d]/gi,'');
            }
        }
    }    
}

function isEmailAddr(email)
{
	var result = false;
	var theStr = new String(email);
	var index = theStr.indexOf("@");
	if (index > 0){
		var pindex = theStr.indexOf(".", index);
		if ((pindex > index + 1) && (theStr.length > pindex + 1)){
			result = true; 
		}
	}
	return result;
}

function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}

function isPhoneNumber(s)
{
	// Check for correct phone number
	rePhoneNumber = new RegExp(/^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/);
 	if (!rePhoneNumber.test(s)) {
		//alert("Phone Number Must Be Entered As: (555) 555-1234");
		return false;
	}
	return true;
}

function SSNValidation(ssn)
{
    var matchArr = ssn.match(/^(\d{3})-?\d{2}-?\d{4}$/);
    var numDashes = ssn.split('-').length - 1;
    if (matchArr == null || numDashes == 1) {
        alert('Invalid SSN. Must be 9 digits or in the form NNN-NN-NNNN.');
        msg = "does not appear to be valid";
        return false;
    }
    else if (parseInt(matchArr[1],10)==0) {
        alert("Invalid SSN: SSN's can't start with 000.");
        msg = "does not appear to be valid";
        return false;
    }
    else {
        msg = "appears to be valid";
        alert(ssn + "\r\n\r\n" + msg + " Social Security Number.");
        return false;
    }
    return true;
}

function isSSN(ssn)
{
    var matchArr = ssn.match(/^(\d{3})-?\d{2}-?\d{4}$/);
    var numDashes = ssn.split('-').length - 1;
    if (matchArr == null || numDashes == 1) {
        return "Invalid SSN. Must be 9 digits or in the form NNN-NN-NNNN.";
    }
    else if (parseInt(matchArr[1],10)==0) {
        return "Invalid SSN: SSN's can't start with 000.";
    }
    else {
        return "";
    }
    return "";
}

function hideMe(cb){
    cb.hide();
}

//************************************************************************
//************** Set of functions to show scrolling tooltip **************
//************************************************************************

function showtip2(current,e,text)
{
	if (document.all&&document.readyState=="complete"){
		document.all.tooltip2.innerHTML='<marquee style="border:1px solid black" scrolldelay="50" scrollamount="3">'+text+'</marquee>'
		document.all.tooltip2.style.pixelLeft=event.clientX+document.body.scrollLeft+10
		document.all.tooltip2.style.pixelTop=event.clientY+document.body.scrollTop+10
		document.all.tooltip2.style.visibility="visible"
	}
	else if (document.layers){
		document.tooltip2.document.nstip.document.write(''+text+'')
		document.tooltip2.document.nstip.document.close()
		document.tooltip2.document.nstip.left=0
		currentscroll=setInterval("scrolltip()",100)
		document.tooltip2.left=e.pageX+10
		document.tooltip2.top=e.pageY+10
		document.tooltip2.visibility="show"
	}
}

function hidetip2()
{
	if (document.all)
		document.all.tooltip2.style.visibility="hidden"
	else if (document.layers){
		clearInterval(currentscroll)
		document.tooltip2.visibility="hidden"
	}
}

function scrolltip()
{
	if (document.tooltip2.document.nstip.left>=-document.tooltip2.document.nstip.document.width)
	document.tooltip2.document.nstip.left-=5
	else
	document.tooltip2.document.nstip.left=150
}
//************************************************************************

//*******************************************************************
//*****************  BRS Printer Related Functions  *****************
//*******************************************************************

function installPrinter(divName){
    var askTxt = "";
    askTxt = askTxt + "The BRS-Document Printer is required to print multiple documents.\n";
    askTxt = askTxt + "The Document Printer installation will begin shortly.\n\n";
    askTxt = askTxt + "After successfully installing the application,\nclick the Print Selected Items again to print documents.\n\n";
    askTxt = askTxt + "To continue with installation click on OK\n-OR-\nClick CANCEL to quit installation.";
    if (confirm(askTxt)){
        document.getElementById(divName).innerHTML = getObjectTag();
    }
}

/*
function isPrinterInstalled(){
    try{
        var printer;
        if(window.ActiveXObject){
            printer = new ActiveXObject("AxFsPubPrinter.FsPubPrinter");
            return true;
        }
        else{
            return false;
        }
    }
    catch(err){
        return false;
    }
}
*/

function isPrinterInstalled(){
    var ret = false;
    var printer;
    try{        
        if(window.ActiveXObject){
            printer = new ActiveXObject("AxFsPubPrinter.FsPubPrinter");
            ret = true;
        }
        else{
            ret = false;
        }
    }
    catch(err){
        ret = false;
    }
    
    if(ret == false){
        printer = null;
        try{
            if(window.ActiveXObject){
                printer = new ActiveXObject("AxFsPubPrinter2.FsPubPrinter2");
                ret = true;
            }
            else{
                ret = false;
            }
        }
        catch(err){
            ret = false;
        }
    }
    return ret;    
}

function getObjectTag(){
    var objAX = "";
    objAX = objAX + "<OBJECT id='PubPrn' ";
    objAX = objAX + "name='PubPrn' classid='clsid:CD913BC9-4195-45b7-8A71-8E315836F13C' ";
	objAX = objAX + "codebase='http://portal.brsonline.com/PubPrinter/AxFsPubPrinter.CAB#version=2,0,0,0' ";
	objAX = objAX + "width='1' height='1'>";
	objAX = objAX + "</OBJECT>";
	return objAX;
}

function isIE(){
    return navigator.appName == "Microsoft Internet Explorer";
}

function FFproof(obj){
    if(!isIE()){
        document.getElementById(obj).style.display = "none";
    }
}

//*******************************************************************
//************************ POP-UP POPPERS ***************************
//*******************************************************************

function OpenImageViewer(theURL){
    window.open(theURL,"ImageViewer","fullscreen=no,resizable=1,status=1,scrollbars=0,toolbar=0,location=0,menubar=0,directories=0,width=900,height=600,toolbar=no,titlebar=no,menubar=no,location=no");
}

function OpenPDFViewer(theURL){
    window.open(theURL,"PDFViewer","fullscreen=no,resizable=1,status=1,scrollbars=0,toolbar=0,location=0,menubar=0,directories=0,width=350,height=300,toolbar=no,titlebar=no,menubar=no,location=no");
}

function OpenMultiplePDF(theURL){
    window.open(theURL,"multiPDFViewer","fullscreen=no,resizable=1,status=1,scrollbars=0,toolbar=0,location=0,menubar=0,directories=0,width=350,height=300,toolbar=no,titlebar=no,menubar=no,location=no");
}

function OpenImagePrinter(theURL){
    window.open(theURL,"ImagePrinter","fullscreen=no,resizable=1,status=1,scrollbars=0,toolbar=0,location=0,menubar=0,directories=0,width=350,height=300,toolbar=no,titlebar=no,menubar=no,location=no");
}

var currPrinted = 0;
function OpenPDFViewerDiv(theURL, divID){
    var objIframe = document.getElementById("iFrm_" + divID);
    var objDiv = document.getElementById("pdfDiv_" + divID);    
    objIframe.src = theURL;
    objDiv.style.display = "block";    
    
    if(currPrinted > 0 && currPrinted != divID){
        closeWhatsOpened();
    }
    currPrinted = divID;
}

function closeWhatsOpened(){
    var objIframePrntd = document.getElementById("iFrm_" + currPrinted);
    var objDivPrntd = document.getElementById("pdfDiv_" + currPrinted); 
    objIframePrntd.src = "about:blank";
    objDivPrntd.style.display = "none";
}

//*******************************************************************
//*************** ALL ORDER RELATED AND GENERAL FUNC ****************
//*******************************************************************


function openOrderToAdd(isFailed){
    if(isFailed){
        alert("Cannot add items to this order. We encountered a problem setting up this order.\nPlease contact our customer service department as soon as possible at 516-663-0600.");
        return false;
    }
    else{
        return true;
    }
}

//*******************************************************************
//*************** ALL PUBLIC PAGE CONTENTS SHOW/HIDE ****************
//*******************************************************************
var numOfNews = 7;
var numOfAboutus = 4;
var numOfResources = 8;
var numOfServices = 1;
    
function showORhideThisSection(divName, display)
{
    var obj = document.getElementById(divName);
    var filterscheck=obj.filters && obj.filters.length>0;  
    if(filterscheck){obj.filters[0].apply();}
    obj.style.display = display;
    if(filterscheck){obj.filters[0].play();}
}

function ShowNews(id)
{
    for(x=0;x<=numOfNews;x++){
        showORhideThisSection('news_'+x,'none');
    }
    showORhideThisSection('news_' + id,'block');
}

function ShowAboutus(id)
{
    for(x=0;x<=numOfAboutus;x++){
        showORhideThisSection('aboutus_'+x,'none');        
    }
    showORhideThisSection('aboutus_' + id,'block');
}

function ShowServices(id)
{
    for(x=0;x<=numOfServices;x++){
        showORhideThisSection('services_'+x,'none');
    }
    showORhideThisSection('services_' + id,'block');
}


function ShowResources(id)
{
    for(x=0;x<=numOfResources;x++){
        showORhideThisSection('resource_'+x,'none');
    }
    showORhideThisSection('resource_' + id,'block');
}


function ShowNewsFeed(URL, divID){

    var theText = "";
    URL = "../../pub/find/getNews.aspx?nUrl=" + escape(URL);
    
    try{
        if (NSX){
            var xmlhttpC = new XMLHttpRequest();
            xmlhttpC.open("GET", URL, false);
            xmlhttpC.send(null);
        }
        else if (IE4){
            var xmlhttpC = new ActiveXObject("Microsoft.XMLHTTP");
            xmlhttpC.open("GET", URL, false);
            xmlhttpC.send("");
        }
        theText = xmlhttpC.responseText;        
    }
    catch(err){
        theText = "   Failed to load news.   ";
    }

    
    //theText = theText.replace(/^\s*|\s*$/g,"");
    theText = theText.trim();

    if(theText != ""){
        document.getElementById(divID).innerHTML = theText;    
    }
    else{
        document.getElementById(divID).innerHTML = ""    
    }
}

//*******************************************************************
//*******************************************************************

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function pausecomp(Amount)
{ 
	d = new Date() //today's date 
	while (1){ 
		mill=new Date() // Date Now 
		diff = mill-d //difference in milliseconds 
		if(diff > Amount ) { 
			break;
		} 
	} 
}

//*******************************************************************
//*******************************************************************