varRootURL = "http://metrics.socma.com/"
blOKToUnload = true

function submitGenForm(Form, Action, aryFieldValuePairs)
{
	document.forms[Form].target = ""
	document.forms[Form].action = Action;
	for(i=0; i < aryFieldValuePairs.length; i++)
	{
		document.forms[Form][aryFieldValuePairs[i][0]].value = aryFieldValuePairs[i][1];
	}	
	document.forms[Form].submit();
}
function openSubForm(Form, Action, Target, aryFieldValuePairs, Height, Width)
{
	x = ((window.screen.availWidth/2) - (parseInt(Width)/2))
	y = ((window.screen.availHeight/2) - (parseInt(Height)/2))
	if(x < 0){x="0"}
	if(y < 0){y="0";Height = window.screen.availHeight}
	
	document.forms[Form].action = Action;
	//Target = window.showModelessDialog('','','');
	xWin = window.open("", Target, "location=no,scrollbars=yes");
	xWin.resizeTo(Width, Height)
	xWin.moveTo(x,y)
	document.forms[Form].target = Target;
	
	for(i=0; i < aryFieldValuePairs.length; i++)
	{
		document.forms[Form][aryFieldValuePairs[i][0]].value = aryFieldValuePairs[i][1];
	}	
	document.forms[Form].submit();
}
function cleanUp(arySearchCharacters,searchString)
{
	for(var j = 0; j < arySearchCharacters.length; j++)
	{
		newSearchString = "";
		for(var i = 0; i < searchString.length; i++)
		{
			initialPosition = searchString.indexOf(arySearchCharacters[j][0],i);
			if(initialPosition == -1)
			{
				newSearchString += searchString.substring(i,searchString.length);
				break;
			}
			newSearchString += searchString.substring(i,initialPosition) + arySearchCharacters[j][1]
			i = initialPosition;
		}
		searchString = newSearchString
	}
	return searchString;
}
function formatCurrencyDisplay(formFields, formName)
{	
	for(i=0; i < formFields.length; i++)
	{
		if(document.forms(formName).item(formFields[i]).value != "")
		{
			document.forms(formName).item(formFields[i]).value = formatCurrency(cleanUp([["$", ""],[",", ""]], document.forms(formName).item(formFields[i]).value));
		}
	}
}
function formatDecimal(varValue, DecimalPlaces)
{
	varWhole = new String("")
	varFraction = new String("")
	varDecIndex = varValue.indexOf(".",0)
	blSwapZero = false;
	if(varDecIndex == -1)
	{
		varWhole = varValue
		for(i=0;i<parseInt(DecimalPlaces);i++)
		{
			if(i==0){varFraction = "."}
			varFraction += "0"
		}
		return varWhole + varFraction
	}
	else
	{
		varWhole = varValue.substring(0, varDecIndex)
		varFraction = varValue.substring(varDecIndex + 1, varValue.length)
		if(varFraction.length < parseInt(DecimalPlaces))
		{
			for(i=varFraction.length;i<parseInt(DecimalPlaces);i++){varFraction += "0"}
		}
		else
		{
			varRoundNum = varFraction.substring(parseInt(DecimalPlaces),parseInt(DecimalPlaces) + 1)
			varFraction = varFraction.substring(0,parseInt(DecimalPlaces))
			if(varFraction.charAt(0) == "0" && varFraction.charAt(1) != "9")
			{
				varFraction = "1" + varFraction.substring(1,varFraction.length)
				blSwapZero = true;
			}
			if(parseInt(varRoundNum) >= 5){varFraction = parseInt(varFraction) + 1}
			varFraction = varFraction.toString()
			if(blSwapZero){varFraction = "0" + varFraction.substring(1,varFraction.length)}
			if(varFraction.length > parseInt(DecimalPlaces))
			{
				varWhole = parseInt(varWhole) + 1
				varFraction = varFraction.substring(1,parseInt(DecimalPlaces) + 1)
			}
		}
		return varWhole + "." + varFraction
	}
}
function confirmNavigation(Section)
{
	doConfirm = true;
	varExcludedElements = ['btnSave','btnSaveAndClose','lnkNewClient','lnkRequestProposalNumber','lnkGNGDecision','lnkDebriefInformation','btnClose','lnkSetClient','btnSend','lnkForwardEmail','lnkPrintPage','btnNoSub','btnSelect','lnkSelectClient']
	
	for(i=0;i<varExcludedElements.length;i++)
	{
		if(document.activeElement.name == varExcludedElements[i])
		{
			doConfirm = false;
			break;
		}
	}
	if(doConfirm)
	{		
		window.event.returnValue = "Any unsaved changes to this " + Section + " will be lost.";
	}
}
function openErrorWin(varText)
{
	x = ((window.screen.availWidth/2) - (200))
	y = ((window.screen.availHeight/2) - (200))
	
	//<input type='button' name='OK' value='OK' onClick='window.close()'>
	
	varMessage = window.open('','ErrorWin','location=no,scrollbars=yes');
	varMessage.resizeTo(400,400);
	varMessage.moveTo(x,y)	
	varMessage.document.writeln("<html><head></head><body>" + varText + "<center><input type='image' name='btnOK' src='" + varRootURL + "common/images/btnOK_Off.gif' onClick='window.close()' onmouseover=\"this.src='" + varRootURL + "common/images/btnOK_On.gif'\" onmouseout=\"this.src='" + varRootURL + "common/images/btnOK_Off.gif'\" width='50' height='30' border='0'></center></body></html>");
	varMessage.document.close();
}
function openErrorForm(File, Height, Width)
{
	x = ((window.screen.availWidth/2) - (parseInt(Width)/2))
	y = ((window.screen.availHeight/2) - (parseInt(Height)/2))
	if(x < 0){x="0"}
	if(y < 0){y="0";Height = window.screen.availHeight}
	
	varMessage = window.open(File,'ErrorForm','height:" + Height + "px;width:" + Width + "px;center:yes;status:no;help:no;scrollbars:yes;resizable:no');
	varMessage.resizeTo(Width, Height)
	varMessage.moveTo(x,y)
}
function openModalFormX(File, Height, Width, Options)
{
	if(Options == ""){Options = ";resizable:no"}
	window.showModalDialog(File,window,"dialogHeight:" + Height + "px;dialogWidth:" + Width + "px;center:yes;status:no;help:no;scroll:yes" + Options)
}
function openModelessForm(File, Height, Width, Options)
{
	if(Options == ""){Options = ";resizable:no"}
	if(navigator.userAgent.toLowerCase().indexOf('msie') !=-1)
	{
		window.showModelessDialog(File,window,"dialogHeight:" + Height + "px;dialogWidth:" + Width + "px;center:yes;status:no;help:no;scroll:yes" + Options)
	}
	else
	{
		x = ((window.screen.availWidth/2) - (parseInt(Width)/2))
		y = ((window.screen.availHeight/2) - (parseInt(Height)/2))
		if(x < 0){x="0"}
		if(y < 0){y="0";Height = window.screen.availHeight}
		
		varMessage = window.open(File,'HelpWin','height:" + Height + "px;width:" + Width + "px;center:yes;status:no;help:no;scroll:yes' + Options);
		varMessage.resizeTo(Width, Height)
		varMessage.moveTo(x,y)
	}
}
function openHelpWin(File, Height, Width, Options)
{
	if(Options == ""){Options = ";resizable=no;"}

		x = ((window.screen.availWidth/2) - (parseInt(Width)/2))
		y = ((window.screen.availHeight/2) - (parseInt(Height)/2))
		if(x < 0){x="0"}
		if(y < 0){y="0";Height = window.screen.availHeight}
		
		//varMessage = window.open(File,'HelpWin','scrollbars=yes' + Options);
		varMessage = window.open(File,'HelpWin','scrollbars=yes');
		varMessage.resizeTo(Width, Height)
		varMessage.moveTo(x,y)

}
function openCmntsWin(File, Height, Width, Options)
{
	if(Options == ""){Options = ";resizable=no;"}

		x = ((window.screen.availWidth/2) - (parseInt(Width)/2))
		y = ((window.screen.availHeight/2) - (parseInt(Height)/2))
		if(x < 0){x="0"}
		if(y < 0){y="0";Height = window.screen.availHeight}
		
		//varMessage = window.open(File,'HelpWin','scrollbars=yes' + Options);
		varMessage = window.open(File,'CmntsWin','scrollbars=yes,resizable=yes,menubar=yes');
		varMessage.resizeTo(Width, Height)
		varMessage.moveTo(x,y)

}
function openPrintWin(Form, Action, Target, aryFieldValuePairs, Height, Width)
{
	x = ((window.screen.availWidth/2) - (parseInt(Width)/2))
	y = ((window.screen.availHeight/2) - (parseInt(Height)/2))
	if(x < 0){x="0"}
	if(y < 0){y="0";Height = window.screen.availHeight}
	
	document.forms[Form].action = Action;
	xWin = window.open("", Target, "location=no,scrollbars=yes,resizable=yes,menubar=yes");
	xWin.resizeTo(Width, Height)
	xWin.moveTo(x,y)
	document.forms[Form].target = Target;
	
	for(i=0; i < aryFieldValuePairs.length; i++)
	{
		document.forms[Form][aryFieldValuePairs[i][0]].value = aryFieldValuePairs[i][1];
	}	
	document.forms[Form].submit();
}
function showHelp(varTyp,varID)
{
	if(varTyp == 'T'){openHelpWin(varRootURL + "content/help/help_popup.aspx?ID=" + varID,"200","400","")}
	else{openHelpWin(varRootURL + "content/help/RCMDMS_Users_Manual_" + varTyp + ".pdf#page=" + varID,"600","700","")}
}
function FormatNumber(num,decimalNum,bolLeadingZero,bolParens,bolCommas)
/**********************************************************************
	IN:
		NUM - the number to format
		decimalNum - the number of decimal places to format the number to
		bolLeadingZero - true / false - display a leading zero for
										numbers between -1 and 1
		bolParens - true / false - use parenthesis around negative numbers
		bolCommas - put commas as number separators.
 
	RETVAL:
		The formatted number!
 **********************************************************************/
{ 
        if (isNaN(parseInt(num))) return "NaN";

	var tmpNum = num;
	var iSign = num < 0 ? -1 : 1;		// Get sign of number
	
	// Adjust number so only the specified number of numbers after
	// the decimal point are shown.
	tmpNum *= Math.pow(10,decimalNum);
	tmpNum = Math.round(Math.abs(tmpNum))
	tmpNum /= Math.pow(10,decimalNum);
	tmpNum *= iSign;					// Readjust for sign
	tmpNum = tmpNum.toFixed(decimalNum);
	
	// Create a string object to do our formatting on
	var tmpNumStr = new String(tmpNum);

	// See if we need to strip out the leading zero or not.
	if (!bolLeadingZero && num < 1 && num > -1 && num != 0)
		if (num > 0)
			tmpNumStr = tmpNumStr.substring(1,tmpNumStr.length);
		else
			tmpNumStr = "-" + tmpNumStr.substring(2,tmpNumStr.length);
		
	// See if we need to put in the commas
	if (bolCommas && (num >= 1000 || num <= -1000)) {
		var iStart = tmpNumStr.indexOf(".");
		if (iStart < 0)
			iStart = tmpNumStr.length;

		iStart -= 3;
		while (iStart >= 1) {
			tmpNumStr = tmpNumStr.substring(0,iStart) + "," + tmpNumStr.substring(iStart,tmpNumStr.length)
			iStart -= 3;
		}		
	}

	// See if we need to use parenthesis
	if (bolParens && num < 0)
		tmpNumStr = "(" + tmpNumStr.substring(1,tmpNumStr.length) + ")";

	return tmpNumStr;		// Return our formatted string!
}

/************QC - JAvascript************/
//Tariku Tessema
//Dec 16 - 2010

var blnClear = true;
function clearQC()
{
 //Clear QC
    if (blnClear == true)
    {
        var rtnClear = SOCMA.AjaxMethods.MTRC_QC_Clear(document.getElementsByName('hidCmpnyID')[0].value,document.getElementsByName('hidMtrcID')[0].value,document.getElementsByName('hidRptngYr')[0].value);
    }
}
function doQC(usrID, pgAction, txtBoxToQc)
{

    var allQC = '';
    var blnPass= true;
    var initQc = true;
    var allFcltySel = '';
    var allFcltyDesc = '';
    
    //Update QC Comments
     for (i=0; i < document.getElementsByName('hidFcltyID').length; i ++)
    {
        if (document.getElementsByName('txtVarRsn')[i].value != '')
        { 
            clearQC();
            blnClear = false;
            var rtnUpdate = SOCMA.AjaxMethods.MTRC_QC_Update(document.getElementsByName('hidCmpnyID')[0].value,document.getElementsByName('hidFcltyID')[i].value,document.getElementsByName('hidMtrcID')[0].value,document.getElementsByName('hidRptngYr')[0].value, document.getElementsByName('hidQsnDesc')[i].value,document.getElementsByName('txtVarRsn')[i].value, usrID);
        }
    }
   
   //Run QC
   
   
    for (i=0; i < document.getElementsByName('hidFcltyID').length; i ++)
    {
         initQc = true;
         var rtn = SOCMA.AjaxMethods.MTRC_QC(document.getElementsByName('hidCmpnyID')[0].value,document.getElementsByName('hidFcltyID')[i].value,document.getElementsByName('hidMtrcID')[0].value,document.getElementsByName('hidRptngYr')[0].value, document.getElementsByName(txtBoxToQc)[i].value, pgAction,bgColor);
         var rtnVal = rtn.value.Rows[0]['QcTBl'];
         var rtnPassFail = rtn.value.Rows[0]['blnPassFail'];
         if(rtnVal != null || typeof(rtnVal) != 'undefined')
         {
         allQC = allQC + rtnVal;
         }
         
         if (rtnPassFail == '0' && document.getElementsByName('txtVarRsn')[i].value == '')
         {
            blnPass = false;
         }
         
         if (document.getElementsByName('cboCmpnyRvwd')[i].value == 1)
         {
            initQc = false;
         }
    }
    
   if (blnPass == false)
   {
       
        
       if (initQc == true)
       {
            document.getElementById('dvQC').innerHTML = allQC;
       }
      
       var divErr = document.createElement("div")
       divErr.innerHTML =  allQC;
         
        for (i=0; i < divErr.getElementsByTagName('div').length; i ++)
        {
            if (divErr.getElementsByTagName('input')[i].value != '')
             {
                 if (divErr.getElementsByTagName('select')[i].value == '')
                 {
                     allFcltySel += '<li>' + divErr.getElementsByTagName('div')[i].innerHTML + '</li>';
                 }
              }
        }
       
         formatValErr(allFcltySel);
         formatValErr(allFcltyDesc);
              
       if (allFcltySel != '')
       {
            allFcltySel = 'At least one QC Metric Validation check has been identified for the following:<br /><br />' + allFcltySel;
       }
       
       var allErrMsg = allFcltySel + allFcltyDesc + '<br /><br />If the entered metric values are correct, please confirm and supply a reason for the variance.';
       if (allErrMsg != '<br /><br />')
         {
            showError(allErrMsg);
         }
        higlightQC();
       // formatQC();
   } 
   if (blnPass == true)
   {
    doQcSave();
   }
 
   return blnPass;
}
  function formatValErr(errList)
  {
    if(errList != '')
    {
        errList = '<ul>' + errList + '</ul>';
    }
  }
  function higlightQC()
  {
  //Highlight QC Drop down
      for (h=0; h < document.getElementsByName('cboCmpnyRvwd').length;h++)
          {
            if (document.getElementsByName('cboCmpnyRvwd')[h].value == '')
                {
                    document.getElementsByName('cboCmpnyRvwd')[h].style.backgroundColor = '#ff4646';
                    
                }
             else if (document.getElementsByName('cboCmpnyRvwd')[h].value == 1) // Yes
                {
                     document.getElementsByName('cboCmpnyRvwd')[h].style.backgroundColor = '#FFF';
                     if (document.getElementsByName('txtVarRsn')[h].value == '')
                    {
                        document.getElementsByName('txtVarRsn')[h].style.backgroundColor = '#ff4646';
                    }
                 else
                    {
                         document.getElementsByName('txtVarRsn')[h].style.backgroundColor = '#FFF';
                    }
                }
             else if (document.getElementsByName('cboCmpnyRvwd')[h].value == 2) //No
                {
                   document.getElementsByName('cboCmpnyRvwd')[h].style.backgroundColor = '#FFF';
                   document.getElementsByName('txtVarRsn')[h].style.backgroundColor = '#FFF';
                }
          }
  }
  function toggleComment(e)
  {
      var tblRw = e.parentNode.parentNode.parentNode.rows[e.parentNode.parentNode.rowIndex + 1];
      if(e.value == 1) //Yes Selected
          {
          tblRw.style.display = '';
              for(h =0; h <  tblRw.getElementsByTagName("span").length; h++)
               {
                    if (tblRw.getElementsByTagName("span")[h].style.color == "red")
                    {
                        tblRw.getElementsByTagName("span")[h].style.display = 'none';
                    }
                    else
                    {
                        tblRw.getElementsByTagName("span")[h].style.display = '';
                    }
               }
                for(h =0; h <  tblRw.parentNode.rows[tblRw.rowIndex + 1].getElementsByTagName("textarea").length; h++)
               {
                        tblRw.parentNode.rows[tblRw.rowIndex + 1].getElementsByTagName("textarea")[h].style.display = '';
                        tblRw.parentNode.rows[tblRw.rowIndex + 1].style.display = '';
               }
          }
      else if (e.value == 0) // No Selected
      {
            tblRw.style.display = '';
               for(h =0; h <  tblRw.getElementsByTagName("span").length; h++)
               {
                    if (tblRw.getElementsByTagName("span")[h].style.color == "red")
                    {
                        tblRw.getElementsByTagName("span")[h].style.display = '';
                    }
                    else
                    {
                        tblRw.getElementsByTagName("span")[h].style.display = 'none';
                    }
               }
                for(h =0; h <  tblRw.nextSibling.getElementsByTagName("textarea").length; h++)
               {
                        tblRw.parentNode.rows[tblRw.rowIndex + 1].getElementsByTagName("textarea")[h].style.display = 'none';
                        tblRw.parentNode.rows[tblRw.rowIndex + 1].getElementsByTagName("textarea")[h].value = '';  
                       tblRw.parentNode.rows[tblRw.rowIndex + 1].style.display = 'none';
               }
      }
      else //No Selection
          {
          tblRw.parentNode.rows[tblRw.rowIndex + 1].style.display = 'none';
          }
  }
  
  var bgColor = "#FFF";
  
  function formatQC()
  {
    for(i=0; i < document.getElementsByTagName('table').length; i ++)
    {
      if (document.getElementsByTagName('table')[i].name == 'tblQCItem' && document.getElementsByTagName('table')[i].style.display != 'none')
      {
        if (bgColor == "#FFF"){bgColor = "#D5D5D5";} else {bgColor = "#FFF";}
        document.getElementsByTagName('table')[i].style.backgroundColor = bgColor;
      }  
    }
  }
  
  /************QC - JAvascript************/
