/*should call InitXrayInstance to init global vars*/
/*
*/
function InstantXray()
{
	 this.form=null;
	 this.InstantMode="D";
	 this.moreEntryUrl="";
	 this.resetUrl="";
	 this.changeModeUrl="";
	 this.helpUrl="";
	 this.entryNum=10;
	 this.maxHolding=100;
};
InstantXray.Instance=new InstantXray();

InstantXray.prototype.Init=function(pform,instantMode,moreEntryUrl,resetUrl,changeModeUrl,helpUrl,entryNum,maxHolding)
{
	this.form=pform;
	this.InstantMode=instantMode;
	this.moreEntryUrl=moreEntryUrl;
	this.resetUrl=resetUrl;
	this.changeModeUrl=changeModeUrl;
	this.helpUrl=helpUrl;
	this.entryNum=entryNum;
	this.maxHolding=maxHolding;
	countAll("");
}

InstantXray.Init=function(formId,instantMode,moreEntryUrl,resetUrl,changeModeUrl,helpUrl,entryNum,maxHolding)
{
	InstantXray.Instance.Init(formId,instantMode,moreEntryUrl,resetUrl,changeModeUrl,helpUrl,entryNum,maxHolding);
	addListener(document,"click",function(){document.getElementById('dropdown_1').style.display='none';});
}

function stripCommas(f)
{
  var strfloat=f;
	var strtemp="";
	var i=0;
	for (i=0; i < strfloat.length; i++)
	{
		if (strfloat.charAt(i)!=',')
		{
			strtemp+=strfloat.charAt(i);
		}		
	}	
	return strtemp;
}

function isEmpty(string)
{
   if (string == null)
      return true;
   for(var count=0;count<string.length;count++)
   {
      if(string.charAt(count)!=" ")
      return false;
   }
   return true;
}

function MoreEntry()
{
	  var pForm=InstantXray.Instance.form;
    pForm.action=InstantXray.Instance.moreEntryUrl;
    pForm.xmlfile.value=GetXmlFile(false);
    pForm.finalSubmit.value="";
    pForm.submit();
}

function equalAllocation()
{
	 var entryNum=InstantXray.Instance.entryNum;
   var numOfHolding = 0; 
   for (var i=0; i<entryNum; i++)
   {
      var T = document.getElementById("t" + i);
      if (!isEmpty(T.value))
         numOfHolding ++;
   }
   for(var i=0; i<entryNum; i++)
   {
      var T = document.getElementById("t" + i);
      if (!isEmpty(T.value))
      {
         var P = document.getElementById("p" + i);
         P.value = parseInt(100/numOfHolding*10000)/10000;
      }
   }
   countAll("");
}

function countAll(value)
{
	 var mode=InstantXray.Instance.InstantMode;
	 var entryNum=InstantXray.Instance.entryNum;
   var all=0.0;
   for (var i=0; i<entryNum; i++)
   {
      var T = document.getElementById("t" + i);
      if(isEmpty(T.value))
      	continue;
   	  var P;
   	  if(mode=="P")
   	    P = document.getElementById("p" + i);
   	  else
      	P = document.getElementById("d" + i);
       var num=stripCommas(P.value);
       if(num=="")
       	continue;
	     if (isNaN(num)||(num <0))
	     {
	     	  var j=i+1;
	        alert("Please input a positive number as weight of ticker in line "+j);
	        P.value="";
	     }
	     else
	     {
	     	  all= all+parseFloat(num);
	     }
   }
   document.getElementById("alll").value = all;
}

/*convert ti,di or pi input values to xmlfile format*/
function GetXmlFile(bDropZero)
{
	 var mode=InstantXray.Instance.InstantMode;
   var xmlFileValue="<Portfolio>";
   var entryNum=InstantXray.Instance.entryNum;
   for (var i=0; i<entryNum; i++)
   {
      var T = document.getElementById("t" + i);
      if (isEmpty(T.value))
        continue;
   	  var P;
   	  if(mode=="P")
   	    P = document.getElementById("p" + i);
   	  else
      	P = document.getElementById("d" + i);
      var num = stripCommas(P.value);
      if (isNaN(num)||(num <0))
      {
         num=0;
      }
      if(bDropZero&&num==0)
        continue;
      var xmlSecurity="<Security>";
      xmlSecurity+="<Ticker>"
      //xml encode tick,only replace five special chars &<>'" 
      var tickname=T.value.replace("&","&amp;").replace("<","&lt;").replace("\"","&gt;").replace("'","&apos;").replace(">","&quot;");
      xmlSecurity+=tickname;
      xmlSecurity+="</Ticker>"
      if(mode=="P")
      {
      	xmlSecurity+="<Percent>";
      	xmlSecurity+=num;
      	xmlSecurity+="</Percent>";
      }
      else
      {
	      xmlSecurity+="<MarketValue>"
	      xmlSecurity+=num;
	      xmlSecurity+="</MarketValue>"
	    }
      xmlSecurity+="</Security>";
      xmlFileValue+=xmlSecurity;
   }
   xmlFileValue+="</Portfolio>";
   return xmlFileValue;
}

function setSubmit()
{
	var mode=InstantXray.Instance.InstantMode;
	countAll("");
	if (isNaN (document.getElementById("alll").value))
	{
		alert ("The value must be a number, please check it.");
		return;
	}
	if(mode=="P")
	{
		if (document.getElementById("alll").value<99.99)
		{
			alert ("Your total allocation is less than 100%.\nPlease adjust your holding allocations so that they total 100%. ");
			return;
		}
		if (document.getElementById("alll").value>100.01)
		{
			alert ("Your total allocation is greater than 100%.\nPlease adjust your holding allocations so that they total 100%. ");
			return;
		}
	}
	else
	{
		if (document.getElementById("alll").value<=0.001)
		{
			alert ("Your total value must be a positive number.\nPlease adjust your holding allocations. ");
			return;
		}
	}
   var pForm=InstantXray.Instance.form;
   pForm.action=window.location.href;
   pForm.xmlfile.value=GetXmlFile(true);
   pForm.finalSubmit.value = "T";
   pForm.submit();
}

var smallwin;
function openTickerLook()
{
	smallwin = window.open('../Free/TickerLookup.aspx','TickerLookup','scrollbars=no,resizable=no,width=365,height=525')
}

function setTicker(tickervalue)
{
	var entryNum=InstantXray.Instance.entryNum;
	
	for (var count = 0; count <entryNum; count++)
	{
		var ticker = document.getElementById("t" + count);
		if (isEmpty(ticker.value))
		{
			ticker.value = tickervalue;
			return; 
		}
	}
	 var maxHoldings=InstantXray.Instance.maxHolding
    if (entryNum < maxHoldings)
    {
    	var msg="All entry fields are used. Do you want to add more entry fields?";
   	    if (confirm(msg)) 
   			MoreEntry();
   		smallwin.close();
	}
	else
	{
   		alert("You can not add more than " + maxHoldings +" holdings");
	}
}

function changeEntry()
{	
	 var pForm=InstantXray.Instance.form;
   pForm.action=InstantXray.Instance.changeModeUrl
   pForm.xmlfile.value=GetXmlFile(false);
   pForm.finalSubmit.value = "";
   pForm.submit();
}

function reset()
{
	window.location.href=InstantXray.Instance.resetUrl;
}

function switchObjShowHide(eve,obj)
{
	  if (obj.style.display == null || obj.style.display == "none" || obj.style.display == "") 
	  {
        obj.style.display = "block";
        
    }
    else 
        obj.style.display = "none";

   if(eve.stopPropagation)
     {
        eve.stopPropagation();
     }
     else
     {
        eve.cancelBubble=true;
     }
     if(eve.preventDefault)
     {
        eve.preventDefault();
     }
     else
     {
        eve.returnValue=false;
     }
}

function openDef()
{
	  var helpUrl=InstantXray.Instance.helpUrl;
    var winarg="toolbar=no,scrollbars=yes,status=no,resizable=yes,width=603,height=500";
    window.open(helpUrl,'DataDefs', winarg);
    return;
}

//-->
