// JScript File
       /* function EditCell(cell)
        {
            var c = document.getElementById(cell);
            c.className="EditableCell";
            
        }


        function CleanTable(cell)
        {

            var c = document.getElementById(cell);
            c.style.backgroundColor='#FFFFFF';
            c.style.borderColor='white';
            BGProc('savepollutant.asp?id='+c.value;,true);
        }*/



var datachanged = false;




/**
 * DHTML phone number validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */

// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}
function trim(s)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not a whitespace, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (c != " ") returnString += c;
    }
    return returnString;
}
function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone){
var bracket=3
strPhone=trim(strPhone)
if(strPhone.indexOf("+")>1) return false
if(strPhone.indexOf("-")!=-1)bracket=bracket+1
if(strPhone.indexOf("(")!=-1 && strPhone.indexOf("(")>bracket)return false
var brchr=strPhone.indexOf("(")
if(strPhone.indexOf("(")!=-1 && strPhone.charAt(brchr+2)!=")")return false
if(strPhone.indexOf("(")==-1 && strPhone.indexOf(")")!=-1)return false
s=stripCharsInBag(strPhone,validWorldPhoneChars);
return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}




/**
 * DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */

function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail ID")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

 		 return true					
	}

/*function ValidateForm2()
	var emailID=document.frmSample.txtEmail
	
	if ((emailID.value==null)||(emailID.value=="")){
		alert("Please Enter your Email ID")
		emailID.focus()
		return false
	}
	if (echeck(emailID.value)==false){
		emailID.value=""
		emailID.focus()
		return false
	}
	return true
 }*/


function OpenWindow(url)
{
href = window.open(url,'','toolbar=0,location=0,directories=0,status=0,menubar=-1,scrollbars=-1,resizable=-1,top=200,left=200,width=200,height=200');
}


function ToggleMap()
{
nercmap = $('NercMap');
lnknercmap = $('lnkNercMap');
desc = $("NercDesc");
foot = $('footer');

if (nercmap.style.display=='block')
    {
        nercmap.style.display='none';
        lnknercmap.innerHTML = "Select from Map";
        //desc.innerHTML = "";
        foot.style.marginTop = '20px';
    }
else {    
        nercmap.style.display='block';
        lnknercmap.innerHTML = "Hide Map";
        foot.style.marginTop = '350px';
     }
}

function togglefaq(faq)
{
    faqid = $(faq);
    faqheader = $(faq+'header');
    faqimage = $(faq+'image');
    if (faqid.style.display=='block')
        {
            faqid.style.display='none';
            faqimage.src='images/plus-8.gif';
        }
    else
        {
            faqid.style.display='block';
            faqimage.src='images/minus-8.gif';
        }    
        

}

function ToggleMap2(txt,region)
{
writetext(txt,region);
nercmap.style.display='none';
lnknercmap = $('lnkNercMap').innerHTML = "Select from Map";
}


function writetext(txt,id)
{
desc = $("longname");
nerc = $("Nerc");
nerc.value = id;
desc.innerHTML = txt;
}

function ChangeScreen(url,loca)
{
//window.document.location.replace(url);
window.document.location.href = url;
}


    function ShowNercName()
    {
        datachanged = true;
        nercid = $("Nerc").value;
        if(nercid==0)
        {
            $("btnCalc").disabled=true;
            //$("Print").disabled=true;
        }
        else
        {    
            $("btnCalc").disabled=false;
            //$("Print").disabled=true;
        }
        
        
        GenericLoader("shownerc.asp?nercid="+nercid,"longname",true);
     }


function Right(str, n)
{
      if (n <= 0)
          return "";
      else if (n > String(str).length)
          return str;
      else
   {
          var iLen = String(str).length;
          return String(str).substring(iLen, iLen - n);
      }
}


// number formatting function
// copyright Stephen Chapman 24th March 2006, 22nd August 2008
// permission to use this function is granted provided
// that this copyright notice is retained intact



function formatNumber(num,dec,thou,pnt,curr1,curr2,n1,n2,el) 
{
num = num.replace(/,/g,'');

var x = Math.round(num * Math.pow(10,dec));
if (x >= 0) n1=n2='';
var y = (''+Math.abs(x)).split('');
var z = y.length - dec; 
if (z<0) z--; 
for(var i = z; i < 0; i++) 
y.unshift('0');
 if (z<0) z = 1; 
 y.splice(z, 0, pnt);
  if(y[0] == pnt)
   y.unshift('0');
    while (z > 3)
     {z-=3; y.splice(z,0,thou);}
     var r = curr1+n1+y.join('')+n2+curr2;
if (el!=='') 
    {
    $(el).value=r;
    }
else
    {return r};
}

function toggleVisibility(hide,show)
{
	hide.style.display='none';
	show.style.display='block';
}

function DoThePrint(url)
{
    if(datachanged)
    {
        alert("You've changed some data so you'll need to re-calculate before you print.");
        $('btnCalc').focus();
        return false;
    }
    
    newurl=AssembleURL(url);
    lipke = window.open(newurl);
    //lipke = window.open(newurl,'height=250,width=350,status=0,toolbar=0');
}

function CheckVars()
{
    nerc = $("Nerc");
    nercval = nerc.selectedIndex;
    
    kwh = $("kwhperbed");
    kwhval = kwh.value;
    
    Cef = $("cef");
    cefval = Cef.value;
    
    if(nercval==0)
    {   
        alert("You must select a Grid Region");
        nerc.focus();
        return false;
    }
    
    if(kwhval=='0')
    {   
        alert("You must enter you kWh per Year");
        kwh.focus();
        return false;
    }

    if (cefval > 100)
    {
        alert("Clean Energy Fraction cannot be > 100%");
        Cef.focus();
        return false;
    }
    
    return true;
}


function DoTheCalc(url,el,wait)
{
    // Create the URL and the Querystring from form data
    if(CheckVars())
    {
        datachanged = false;
        newurl=AssembleURL(url);
        //alert(newurl);
        LoadCalc(newurl,el,wait);
    }
}

function SaveCalc()
{
    nerc = $("Nerc").value;
    aku = $("kwhperbed").value;
    aku = replaceAll(aku,',','');
    cef = $("cef").value;
    url = 'savecalc.asp?nerc='+nerc+'&aku='+aku+'&cef='+cef //+'&so2q='+so2q;
    
    alert(url);
    //BGProc(url);

}

function replaceAll(OldString,FindString,ReplaceString) {
  	var SearchIndex = 0;
  	var NewString = ""; 
  	while (OldString.indexOf(FindString,SearchIndex) != -1)    {
    	NewString += OldString.substring(SearchIndex,OldString.indexOf(FindString,SearchIndex));
    	NewString += ReplaceString;
    	SearchIndex = (OldString.indexOf(FindString,SearchIndex) + FindString.length);         
 	}
  	NewString += OldString.substring(SearchIndex,OldString.length);
  	return NewString;
}


function GetCEF()
{
    return($('cef').value);
}

function GetKWH()
{
    kwh = $('kwhperbed').value;
    kwh = replaceAll(kwh,',','');
    return(kwh);
}

function AssembleURL(url)
{
    return(url+'?nerc='+GetCurrentNerc()+'&kwh='+GetKWH()+'&cef='+GetCEF());
}



function GetCurrentNerc()
{
    return ($('Nerc').value);
}

function LoadCalc(data, el, wait)
  {
  if (wait=='true')
  {
    /*var doc = $(el);
    doc.innerHTML = "<img src="+spinimage.src+">";*/
  }
    
  var xmlHttp;
  try
    {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    }
  catch (e)
    {
    // Internet Explorer
    try
      {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
    catch (e)
      {
      try
        {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
      catch (e)
        {
        alert("Your browser does not support AJAX!");
        return false;
        }
      }
    }
    xmlHttp.onreadystatechange=function()
      {
      if(xmlHttp.readyState==4)
        {
        //var elem = $(el)
        /*elem.innerHTML=xmlHttp.responseText;*/
        //Save for Pro Version
        //document.getElementById('MWLoad').innerHTML=xmlHttp.responseXML.getElementsByTagName('MWLoad')[0].firstChild.data;
        //
        
        $('UnintendedSocietalImpacts').innerHTML=xmlHttp.responseXML.getElementsByTagName('UnintendedSocietalImpacts')[0].firstChild.data;
        
        $('UnintendedHeatlhImpacts').innerHTML=xmlHttp.responseXML.getElementsByTagName('UnintendedHeatlhImpacts')[0].firstChild.data;


        $('SO2Quantity').innerHTML=xmlHttp.responseXML.getElementsByTagName('AnnualQuantity')[0].firstChild.data;
        $('SO2RightToEmit').innerHTML=xmlHttp.responseXML.getElementsByTagName('RightToEmit')[0].firstChild.data;
        $('NO2Quantity').innerHTML=xmlHttp.responseXML.getElementsByTagName('AnnualQuantity')[1].firstChild.data;
        $('NO2RightToEmit').innerHTML=xmlHttp.responseXML.getElementsByTagName('RightToEmit')[1].firstChild.data;
        $('CO2Quantity').innerHTML=xmlHttp.responseXML.getElementsByTagName('AnnualQuantity')[2].firstChild.data;
        $('CO2RightToEmit').innerHTML=xmlHttp.responseXML.getElementsByTagName('RightToEmit')[2].firstChild.data;
        /*document.getElementById('CarbonQuantity').innerHTML=xmlHttp.responseXML.getElementsByTagName('AnnualQuantity')[3].firstChild.data;*/
        /*document.getElementById('CarbonRightToEmit').innerHTML=xmlHttp.responseXML.getElementsByTagName('RightToEmit')[3].firstChild.data;*/
        $('MercuryQuantity').innerHTML=xmlHttp.responseXML.getElementsByTagName('AnnualQuantity')[3].firstChild.data;
        $('MercuryRightToEmit').innerHTML=xmlHttp.responseXML.getElementsByTagName('RightToEmit')[3].firstChild.data;
        
        $('PrematureMortalityIncidents').innerHTML=xmlHttp.responseXML.getElementsByTagName('Incidents')[0].firstChild.data;
        $('PrematureMortalitySocietalValue').innerHTML=xmlHttp.responseXML.getElementsByTagName('SocietalValue')[0].firstChild.data;
        $('PrematureMortalityCosts').innerHTML=xmlHttp.responseXML.getElementsByTagName('Cost')[0].firstChild.data;
        
        $('ChronicBronchitisIncidents').innerHTML=xmlHttp.responseXML.getElementsByTagName('Incidents')[1].firstChild.data;
        $('ChronicBronchitisSocietalValue').innerHTML=xmlHttp.responseXML.getElementsByTagName('SocietalValue')[1].firstChild.data;
        $('ChronicBronchitisCosts').innerHTML=xmlHttp.responseXML.getElementsByTagName('Cost')[1].firstChild.data;
        
        $('HospitalVisitIncidents').innerHTML=xmlHttp.responseXML.getElementsByTagName('Incidents')[2].firstChild.data;
        $('HospitalVisitSocietalValue').innerHTML=xmlHttp.responseXML.getElementsByTagName('SocietalValue')[2].firstChild.data;
        $('HospitalVisitCosts').innerHTML=xmlHttp.responseXML.getElementsByTagName('Cost')[2].firstChild.data;
        
        $('AsthmaAttacks').innerHTML=xmlHttp.responseXML.getElementsByTagName('Incidents')[3].firstChild.data;
        $('AsthmaAttacksSocietalValue').innerHTML=xmlHttp.responseXML.getElementsByTagName('SocietalValue')[3].firstChild.data;
        $('AsthmaAttacksCosts').innerHTML=xmlHttp.responseXML.getElementsByTagName('Cost')[3].firstChild.data;

        $('RespiratorySymptoms').innerHTML=xmlHttp.responseXML.getElementsByTagName('Incidents')[4].firstChild.data;
        $('RespiratorySymptomsSocietalValue').innerHTML=xmlHttp.responseXML.getElementsByTagName('SocietalValue')[4].firstChild.data;
        $('RespiratorySymptomsCosts').innerHTML=xmlHttp.responseXML.getElementsByTagName('Cost')[4].firstChild.data;
                
        $('WorkLossDays').innerHTML=xmlHttp.responseXML.getElementsByTagName('Incidents')[5].firstChild.data;
        $('WorkLossDaysSocietalValue').innerHTML=xmlHttp.responseXML.getElementsByTagName('SocietalValue')[5].firstChild.data;
        $('WorkLossDaysCosts').innerHTML=xmlHttp.responseXML.getElementsByTagName('Cost')[5].firstChild.data;

        $('MercuryRelated').innerHTML=xmlHttp.responseXML.getElementsByTagName('Incidents')[6].firstChild.data;
        $('MercuryRelatedSocietalValue').innerHTML=xmlHttp.responseXML.getElementsByTagName('SocietalValue')[6].firstChild.data;
        $('MercuryRelatedCosts').innerHTML=xmlHttp.responseXML.getElementsByTagName('Cost')[6].firstChild.data;
        
        $('TotalIncidents').innerHTML=xmlHttp.responseXML.getElementsByTagName('Incidents')[7].firstChild.data;
        $('TotalSocietalValue').innerHTML=xmlHttp.responseXML.getElementsByTagName('SocietalValue')[7].firstChild.data;
        $('TotalCosts').innerHTML=xmlHttp.responseXML.getElementsByTagName('Cost')[7].firstChild.data;
        
        }
      }
    
    xmlHttp.open("GET",data,true);
    xmlHttp.send(null);
  }  


function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function SwitchBanner()
{
v1b = document.getElementById('v1');
v2b = document.getElementById('v2');
v3b = document.getElementById('v3');
id = document.getElementById('bannerid');

if(v1b.style.display=='block')
    {
        v1b.style.display='none';
        v2b.style.display='block';
        v3b.style.display='none';
        id.innerHTML='&nbsp;Version 2';
        return true;
    }   
    
if(v2b.style.display=='block')
    {
        v1b.style.display='none';
        v2b.style.display='none';
        v3b.style.display='block';
        id.innerHTML='&nbsp;Version 3';
        return true;
    }

if(v3b.style.display=='block')
    {
        v1b.style.display='block';
        v2b.style.display='none';
        v3b.style.display='none';
        id.innerHTML='&nbsp;Version 1';
        return true;
    }
}


function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (fld.value == "") {
        error = "You didn't enter a phone number.\n";
        fld.style.background = 'Yellow';
    } else if (isNaN(parseInt(stripped))) {
        error = "The phone number contains illegal characters.\n";
        fld.style.background = 'Yellow';
    } else if (!(stripped.length == 10)) {
        error = "The phone number is the wrong length. Make sure you included an area code.\n";
        fld.style.background = 'Yellow';
    }
    if (error!='')
    {   
        alert(error);
        return false;
    }
    else{
        fld.style.background = 'White';
        $('phonehelp').innerHTML = '';
}

    //return true;
    //return error;
}


