var clipboard =-999;
var component;
var error = false;

function formatMeDecimalAddZero( changedField, addZeroToDecimalPlaces,decimalPlaces ){ 
   changedField.value = formatDecimal( changedField.value,addZeroToDecimalPlaces, decimalPlaces );
    
}

  //Gets the indexed form field with the given form name, index and fieldName.
  //The assumption is that the main html form is the first form on the page.
function getIndexedFormField(formName, index, fieldName){
  var prefix = "document.forms[0]";
  var eleName = formName + "[" + index + "]." + fieldName;
  var fullName = prefix+"['" + eleName + "']";
  //alert(fieldName + " at index " + index + " is: " + fullName);
  return eval(fullName);
}

/** Gets the indexed field's index from it's name */
function getIndex(lineField){
  var start = lineField.name.indexOf("[");
  var end = lineField.name.indexOf("]");
  var index = lineField.name.substring(start+1, end);
  //alert("Index of " + lineField.name + " is " + index);
  return index;
}

function formatMeDecimal( changedField, addzero, decimalPlaces )
{

   /* Set to 0 if empty, or return unchanged if not a number */
   if ( changedField.value.length == 0 ) {
   	  changedField.value = 0;
   } else if (isNaN(changedField.value) ) {
      return;
   }
   
   if(!addzero || decimalPlaces < 1 ) {
		changedField.value = formatDecimal( 
			changedField.value, 1,decimalPlaces );   
   } else {
   		changedField.value = formatDecimal( 
   			changedField.value, decimalPlaces,decimalPlaces );   
   }
}


function formatDecimal(argvalue, addZeroToDecimalPlaces, decimaln) {

 
  var numOfDecimal = (decimaln == null) ? 2 : decimaln;
  var number = 1;
  //alert("arg value before format:" + argvalue); 
   argvalue = roundToNPlaces(argvalue, numOfDecimal); 
   // alert("arg value after format:" + argvalue);
  // If you're using IE3.x, you will get error with the following line.
  // argvalue = argvalue.toString();
  // It works fine in IE4.
 
  argvalue = "" + argvalue;

  if (argvalue.indexOf(".") == 0)
    argvalue = "0" + argvalue;
   
  if (addZeroToDecimalPlaces > 0) {
    if (argvalue.indexOf(".") == -1)
      argvalue = argvalue + ".";

    while ((argvalue.indexOf(".") + 1) > (argvalue.length - addZeroToDecimalPlaces))
      argvalue = argvalue + "0";
  } 
  return argvalue;
}

String.prototype.trim = function()
{
  return( this.replace(/^\s*([\s\S]*\S+)\s*$|^\s*$/,'$1') );
}


// find the x position of an object
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;
}

// find the y position of an object
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 scrollToField(field, yMargin){
window.scrollTo(findPosX(field), findPosY(field) - yMargin);
}


function copyToClipboard(comp)
  {
  if(!isValid(comp))
      return;
  clipboard = comp.value;
  }

function getValue(v)
  {
  if(v.length == 0)
    return "0";
  else
    return v;
  }
  

function isValid(tag)
  {
  var valid = true;

 if( isNaN(tag.value) )
   {
   valid = false;
   }
 else if( tag.value < 0)
   {
   valid = false;
   }
 else if( tag.value.search(/\./) != -1)
   {
   valid = false;
   }

  return valid;
  }

function isFloatValid(tag)
  {
  var valid = true;

 if( isNaN(tag.value) )
   {
   valid = false;
   }
 else if( tag.value < 0)
   {
   valid = false;
   }
  return valid;
  }

   /**
    * Rounds the given number to the given decimal place.
    *
    * @return number a number rounded to the given place.
    */
  function roundToNPlaces(number, roundPlace)
   {   
    roundPlace = (!roundPlace ? 2 : roundPlace);
    //round 277.775 to 277.78 instead of 277.77
    number = 0.000000000001 + parseFloat(number) ;    
    return Math.round(number * Math.pow(10, roundPlace)) / Math.pow(10,roundPlace);
   }

   /**
    * Formats a money value.
    *
    * @return val a formatted String value
    */
   function formatCurrencyForInput( val )
   {
      val = roundToNPlaces( val, 2 );

      var textVal = val + "";

      dotIndex = textVal.indexOf(".");
      if( dotIndex == -1 )
      {
         textVal += ".00";
      }
      else if ( dotIndex - textVal.length == -2 )
      {
         textVal += "0";
      }
      else if ( ( dotIndex + 3 < textVal.length ) && dotIndex >= 0 )
      {
         textVal = textVal.substring( 0, dotIndex + 3 );
      }

      return textVal;
   }

   function formatCurrencyForDisplaySpecial( val, minDecimalPlaces, 
   		maxDecimalPlaces ) {
      /* Determine if signed positive or negative */
      val = parseFloat(val);
      positiveVal = Math.abs(val);
      isPositive = (val == positiveVal);     
      formattedValue = formatDecimal(positiveVal, minDecimalPlaces,maxDecimalPlaces); 
      if(isPositive){
        return "$" +  formattedValue;
      }else{
        return "-$" + formattedValue;
      }
   }

   function formatCurrencyForDisplay( val )
   {
     return formatCurrencyForDisplaySpecial(val, 2, 2);
   }
  
   /**
    *   Implements cross-browser support (IE3+, NS4, NS6+) for changing HTML
    *   text dynamically. The HTML should look like this:
    *
    *      <ilayer id="NS_id" name="NS_id">
    *        <layer id="NS_dummy" name="NS_dummy" left="0" top="0">
    *           <span id="IE_id">Your initial value.</span>
    *        </layer>
    *      </ilayer>
    *
    *   See the buyspeed:dynamicHtml tag for creating this HTML.
    *
    *   Credits: http://www.echoecho.com/ubb/viewthread.php?tid=30
    *
    *   @author Rob Marable
    *
    */
   function setDynamicHtmlValue( id, newValue )
   {
      IE=0; NS4=0; NS6=0;

      if (navigator.appName.indexOf('Netscape')!=-1
         && parseInt(navigator.appVersion)<5) {NS4=1;}
      if (navigator.appName.indexOf('Netscape')!=-1
         && parseInt(navigator.appVersion)>4.9) {NS6=1;}
      if (navigator.appName.indexOf('Microsoft')!=-1
         && parseInt(navigator.appVersion)>3) {IE=1;}

      if (IE==true)
      {
         document.all['IE_'+id].innerHTML=newValue;
      }

      if (NS4)
      {
         eval('var echoecho = document.layers.NS_'+id+'.layers[0].document;');
         echoecho.open();
         echoecho.write(newValue);
         echoecho.close();
      }

      if (NS6)
      {
         document.getElementById('NS_'+id).innerHTML = newValue;
      }
   }

function setWaitCursor()
{
 /*if (document.all)
   for (var i=0;i < document.all.length; i++)
     document.all(i).style.cursor = 'wait';  */
    var cursor = null;
    cursor = 
            document.layers ? document.cursor :
            document.all ? document.all.cursor :
            document.getElementById ? document.getElementById('cursor') : null;
    cursor = 'wait';
}

function clearForm(formEle)
{
    for(i=0; i < formEle.length;i++)
    {
        if ( formEle[i].type=="checkbox" )
        {
            formEle[i].checked = false;
        }
        else if ( formEle[i].type=="text")
        {
            formEle[i].value = "";
        }
        else if ( formEle[i].type=="select-one" )
        {
            formEle[i].selectedIndex = 0;
        }
        else if ( formEle[i].type=="select-multiple" )
        {
			for (j = 0; j < formEle[i].options.length; j++) {
			    formEle[i].options[j].selected = false;
			}
        }
    }
}

function openStandardPopup(title, url){
      window.status = title;
       strFeatures = "top=50,left=50,width=640,height=360," + "toolbar=no,menubar=no,location=no,directories=no,scrollbars=yes,resizable=yes,status=no";
       page = contextPath +url;
       objNewWindow = window.open(page, "anotherWindow", strFeatures);


}
/**This function opens up a separate browser window to show vendor
profile */
function viewVendorProfile(vid)
  {
      title = "Vendor Profile";
      url = "/vendorProfile.jsp?external=true&vid=" + vid;
      openStandardPopup(title, url);
  }
   
/**This function opens up a separate browser window to show 
   a document item's detail description.
*/
function viewItemDescription(docId, docType, releaseNbr, itemNbr, revisionNbr)
 {
       window.status = "Document Item Description";
       strFeatures = "top=50,left=50,width=640,height=360," + "toolbar=no,menubar=no,location=no,directories=no,scrollbars=yes,resizable=yes,status=no";

       page = contextPath +"/document/docItemDescriptionPopup.sdo?external=true&docId=" + escape(docId)
               +"&docType=" + docType +"&releaseNbr=" + releaseNbr
                +"&itemNbr=" + itemNbr;
       if(revisionNbr>0){
       	 page += "&revisionNbr=" + revisionNbr;
       }
       //alert("about to go to " + page);
       objNewWindow = window.open(page, "anotherWindow", strFeatures);
}
function openCrystalWindow(urlStr) {

   	crystalWinFeatures = "top=50,left=50,width=820,height=600," 
     + "toolbar=yes,menubar=yes,location=yes,directories=yes,"
     + "scrollbars=yes,resizable=yes,status=yes";
     
   	window.open(urlStr, "crystalWin", crystalWinFeatures);
}

function replaceAll(str, originalStr, newStr){
if(str.length < 1){
return;
}
index = str.indexOf(originalStr);;
while(index!=-1){
str = str.substring(0, index) +  str.substring(index, str.length).replace(originalStr, newStr);
index = str.indexOf(originalStr, index + (newStr.length - originalStr.length) + 1);
}
return str;

}
	
  /**
   * Programaticly set the select (single-select) dropdown's value to the given value
   */ 
  function setSelectValue(sel, val) {
     for (i=0;i<sel.options.length;i++) {
        if (sel.options[i].value == val) {
           sel.selectedIndex = i;
           break;
        }
     }
  }
  
function viewContractDetail(contractId)
{
    page = contextPath + "/purchaseorder/seller/poDetailView.sdo?docId=" + contractId+"&releaseNbr="+ 0;
    window.location.href = page;
}
   
function viewContractItemDetail(contractId,itemNbr)
{
	 page = contextPath + "/purchaseorder/seller/poItemDetailView.sdo?docId=" 
	        + contractId +"&itemNbr=" + itemNbr+"&releaseNbr=" + 0;
	 window.location.href = page;
}

function viewLaserFicheFile(docType, docId,releaseNbr, vendorNbr, vendorGroup, fileNbr){	
	 pageUrl = contextPath + "/document/laserfiche/viewLaserFicheFile.sdo?fileNbr=" + fileNbr
	            +"&docType=" + docType
	            +"&docId=" + docId + "&releaseNbr="+releaseNbr + "&vendorNbr=" + vendorNbr
	            +"&vendorGroup=" + vendorGroup+"&external=true";  
	 strFeatures = "top=50,left=80,width=800,height=600,toolbar=yes,menubar=yes,location=yes,scrollbars=yes,directories=yes,resizable=yes";   
	 window.open(pageUrl,'viewlaserfichefile',strFeatures);
}
   
