var IE = document.all?true:false;
function createListObjects()
{
    availableList = document.getElementById("availableOptions");
    selectedList = document.getElementById("selectedOptions");
}



function addAttribute()
{
	availableList = document.getElementById("availableOptions");
    selectedList = document.getElementById("selectedOptions");
	only10val=document.getElementById("only10").value;



	if(availableList.length<=0)
	{
		alert("Please search and select your stock");
		return false;
	}

	if(cnbc_matrix==1 && only10val=='yes' && selectedList.length>=25)
	{											
		alert("25 Stocks are already there in Your List. Please delete some before adding");
		return false;			
	}


	if(selectedList.length>=10 && only10val=='yes' &&cnbc_matrix!=1)
	{
		alert("10 Stocks are already there in Your List. Please delete some before adding");
		return false;
	}

	
	var index=availableList.selectedIndex;	
	if(index<0)
	{
	alert("Please select a stock from Search Results");
	return false;
	}


	
			var selectedoptionList = document.getElementById ("selectedOptions").options;
			var availableoptionList = document.getElementById ("availableOptions").options;
			var data = '';
			var len = selectedoptionList.length;
			var index=availableList.selectedIndex;
			var selectedvalue=availableoptionList.item(index).value;
				for(i=0; i<len; i++)
				{
					if(selectedvalue==selectedoptionList.item(i).value)
					{
					alert("This Stock already exists in Your list");
					return false;
					}
				}





   
   var addIndex = availableList.selectedIndex;
   if(addIndex < 0)
      return;
   selectedList.appendChild( 
      availableList.options.item(addIndex));
   selectNone(selectedList,availableList);
   anychange=1;
document.getElementById("save_button").disabled=false;
   
}

function setTop(top)
{
	document.getElementById
      ('someLayer').style.top = top;
}

function setLayerTop(lyr,top)
{
	lyr.style.top = top;
}



function selectNone(list1,list2)
{
    list1.selectedIndex = -1;
    list2.selectedIndex = -1;
    addIndex = -1;
    selIndex = -1;
}

function getSize(list){
    /* Mozilla ignores whitespace, 
       IE doesn't - count the elements 
       in the list */
    var len = list.childNodes.length;
    var nsLen = 0;
    //nodeType returns 1 for elements
    for(i=0; i<len; i++){
        if(list.childNodes.item(i).nodeType==1)
            nsLen++;
    }
    if(nsLen<2)
        return 2;
    else
        return nsLen;
}

function delAll(){
    var len = selectedList.length -1;
    for(i=len; i>=0; i--){
        availableList.appendChild(selectedList.item(i));
    }
    selectNone(selectedList,availableList);
    setSize(selectedList,availableList);
    
}

function addAll(){
    var len = availableList.length -1;
    for(i=len; i>=0; i--){
        selectedList.appendChild(availableList.item(i));
    }
    selectNone(selectedList,availableList);
    setSize(selectedList,availableList);
    
}

function post_data()
	{
	
    var optionList = document.getElementById("selectedOptions").options;
    var data = '';
	var matrixdata='';
    var len = optionList.length;

	if(len==0)
		{
			alert("Please add stocks to your list.");
			return false;
		}
    for(i=0; i<len; i++)
	{
            if(data=="")
			{
			data+=data;
			}
			else
			{
			data += '|';
			}
        data += optionList.item(i).value;
    }
   document.getElementById("inlist").value=data;
  

var main_section=document.getElementById("check_direct_save").value


    if(!IE)
		var objXml = new XMLHttpRequest();
	else
		var objXml = new ActiveXObject("Microsoft.XMLHTTP");
	  
	datafile = "/watchlist1/add_stocks.php?stocks_list="+data+"&section_main="+main_section;
	
	objXml.open("GET", datafile, true);
	objXml.setRequestHeader("Cache-Control","no-cache"); 

	objXml.onreadystatechange=function() 
		{
			if (objXml.readyState==4) 
			{
			if (objXml.status!=404)
				{	
				   if((objXml.responseText).length > 0)
					{
						if(main_section=='watchlist')
							{
							next_prev('0','watchlist');
							}
						else
						{
						var cookie_response=objXml.responseText;						
						var brokenscid=cookie_response.split("|");						
						for(var i=0; i < brokenscid.length-1; i++) 
							{
							add_to_cookie(brokenscid[i]);
							}

							if(cnbc_matrix==1)
							{
								document.getElementById("select_stk").innerHTML = "";
								var brokenscid_matrix=data.split("|");
								for(var i=0; i < brokenscid_matrix.length; i++) 
									{
										var scid_matrix=brokenscid_matrix[i].split("_");

											if(matrixdata=="")
											{
											matrixdata+=matrixdata;
											}
											else
											{
											matrixdata += '|';
											}
										matrixdata += scid_matrix[1];
									}								
								document.getElementById("stocks_saved").value=matrixdata;
								mailer_matrix();
							}
						next_prev('0', 'cookie');
						
						
						}
						
						
						
					}
				}
			
			}
		}
	objXml.send(null);
 return false;
}