// JavaScript Document
  var N = window.navigator.appName.substring(0,8); // Тип броузера
  var sUrlWord        = "../../word_act2.php";
  var sUrlWordAddComment = "../../wordcomments.php";
  var sUrlWordRel     = "../../relwords.php";
  var httpWord        = getHTTPObjectW(); // AJAX-объект
  var opera_obj       = getHTTPObjectW(); // AJAX-объект
  var AnnounceBgColor = 0, AnnounceColor = 0, AnnounceState = '';

//---------------------------------

  function UserBuy() {
    var comment_status = document.getElementById('comment_status').value;
    if(comment_status == 0)
    {
        var amount = document.user_action.amount_buy.value;
        if (amount <= 0){
          ShowAlert('Amount is incorrect!', 1);
        }else{

          if(lang == "en") ShowAlert("The transaction is being processed <br><br><img src=\"images/buy_sell_loader.gif\">", 0);
          else             ShowAlert("La transaction est en cours d\'&#234;tre enregistr&#233;e <br><br><img src=\"images/buy_sell_loader.gif\">", 0);
          document.getElementById('buy_button').style.disabled  = true;
          document.getElementById('sell_button').style.disabled = true;

          var price  = document.user_action.price.value;
          var sum = amount * price;

          var sVal = "?word=" + document.user_action.word.value +
    	           "&wordid=" + document.user_action.wordid.value +
				   "&nvrmnd=" + nvrmnd +
                   "&uid=" + document.user_action.uid.value +
    			   "&seckey=" + document.user_action.seckey.value +
                   "&pid=" + document.user_action.pid.value +
                   "&pnum=" + document.user_action.pnum.value +
                   "&count=" + amount +
                   "&price=" + document.user_action.price.value +
                   "&cid=" + document.user_action.cid.value +
                   "&action=buy" +
                   "&language=" + document.user_action.lang.value;

          httpWord.open("GET", sUrlWord + (sVal), true);

          httpWord.onreadystatechange = handleHttpResponseW; 	// handle what to do with the feedback
          httpWord.send(null);
          document.getElementById('comment_status').value =1;

          return true;
        }
    }
  }

  function UserSell() {
    var comment_status = document.getElementById('comment_status').value;
    if(comment_status == 0)
    {
        var  amount = document.user_action.amount_sell.value;
        if (amount <= 0){
          ShowAlert('Amount is incorrect!', 1);
        }else{

          if(lang == "en") ShowAlert("The transaction is being processed <br><br><img src=\"images/buy_sell_loader.gif\">", 0);
          else             ShowAlert("La transaction est en cours d\'&#234;tre enregistr&#233;e <br><br><img src=\"images/buy_sell_loader.gif\">", 0);

          document.getElementById('buy_button').style.disabled  = true;
          document.getElementById('sell_button').style.disabled = true;

          var price  = document.user_action.price.value;
          var sum = amount * price;


          var sVal = "?word=" + document.user_action.word.value +
    			   "&wordid=" + document.user_action.wordid.value +
				   "&nvrmnd=" + nvrmnd +
                   "&uid=" + document.user_action.uid.value +
    			   "&seckey=" + document.user_action.seckey.value +
                   "&pid=" + document.user_action.pid.value +
                   "&pnum=" + document.user_action.pnum.value +
                   "&count=" + amount +
                   "&price=" + document.user_action.price.value +
                   "&cid=" + document.user_action.cid.value +
                   "&action=sell" +
                   "&language=" + document.user_action.lang.value;

          httpWord.open("GET", sUrlWord + (sVal), true);

          httpWord.onreadystatechange = handleHttpResponseW; 	// handle what to do with the feedback
          httpWord.send(null);
          document.getElementById('comment_status').value =1;

          return true;
        }
    }
  }


  function getHTTPObjectW() {
    var xmlhttp;

     if (window.ActiveXObject)
	  {

	    return new ActiveXObject("Microsoft.XMLHTTP");

	  }
	  else if (window.XMLHttpRequest)
	  {
	  	return new XMLHttpRequest();
	  }
  }

  function handleHttpResponseW() {
    //if the process is completed, decide to do with the returned data
    if (httpWord.readyState == 4)
    {
         document.getElementById('comment_status').value =0;
        var S = httpWord.responseText;
        ArrS = S.split('&');

        if (ArrS[0].indexOf('Error') >= 0)
          // Error
          ShowAlert(ArrS[1], 1);
        else{
          // Success Text&newcount={$NewCount}&newcash={$NewCash}&pnum={$pnum}&pid={$pid}&p="


          PLink = '';
          if (ArrS.length == 7)
          {
            // Get Num
            PNumS = ArrS[3].split('=');
            if (PNumS.length == 2){
              NewNum = parseInt(PNumS[1]);

              UserName = ArrS[6].split('=');


              if (NewNum < PorID.length){

                // Get PID
                PIdS = ArrS[4].split('=');
                if (PIdS.length == 2){
                  NewPId = parseInt(PIdS[1]);
                  if (NewPId == PorID[NewNum])
                  {

                    // Get Count
                    CountS = ArrS[1].split('=');
                    if (CountS.length == 2)
                      PorCount[NewNum] = parseInt(CountS[1]);
                    // Get Cash
                    CashS = ArrS[2].split('=');
                    if (CashS.length == 2)
                      PorCash[NewNum] = parseInt(CashS[1]);

                    PortfolioChange();

                    PortfolioLink = ArrS[5].split('=');
                    if (PortfolioLink.length == 2)
                      PLink = PortfolioLink[1];


                  }
                }
              }
            }
          }
          if (PLink.length > 0)
          {
              ShowAlert(ArrS[0] + '<br><br><a href="' + lang + '/users/' + UserName[1] + '">' + PLink + '</a>', 0);
              var num_trans = parseFloat(document.getElementById('active_num').innerHTML);
              if(num_trans < 999)
              {
                document.getElementById('trans_today').innerHTML = num_trans;
                num_trans ++;
                if(num_trans == 11) document.getElementById('total_trans').innerHTML = document.getElementById('all_done').value;
                else                document.getElementById('active_num').innerHTML  = num_trans;
              }
          }
          else
            ShowAlert(ArrS[0], 0);
            document.getElementById('buy_button').style.disabled  = false;
            document.getElementById('sell_button').style.disabled = false;

        }
    }
  }

  function PortfolioChange() {
    var por  = document.user_action.portfolio.value;
    if (por > 0){
      document.user_action.pnum.value=por;
      document.user_action.pid.value=PorID[por];
      if (Price>0){

        LimitCount = (PorVal[por]*0.25/Price);
        if (LimitCount > PorCount[por])
          LimitCount -= PorCount[por];
        else
          LimitCount = 0;


  //    document.user_action.amount_buy.value=parseInt(PorCash[por]/Price);
  //    document.user_action.amount_sell.value=parseInt(PorCount[por] * 0.75);
        document.getElementById("user_have").innerHTML=parseInt(PorCount[por]);
        document.getElementById("user_can_buy").innerHTML=parseInt(
        Math.min(PorCash[por]/Price, LimitCount));
		document.user_action.amount_buy.value = parseInt(
        Math.min(PorCash[por]/Price, LimitCount));
		document.user_action.amount_sell.value = parseInt(PorCount[por]);
//alert(Math.min(PorCash[por]/Price, LimitCount));
        AmountBuyChange();
        AmountSellChange();
      }
    }
  }

  function AmountBuyChange() {
    var amount = document.user_action.amount_buy.value;
    var price  = document.user_action.price.value;
    document.getElementById("user_trans_buy").innerHTML = "<img src='images/icons/trsmall.gif'>" + parseInt(amount * price);
  }

  function AmountSellChange() {
    var amount = document.user_action.amount_sell.value;
    var price  = document.user_action.price.value;
    document.getElementById("user_trans_sell").innerHTML = "<img src='images/icons/trsmall.gif'>" + parseInt(amount * price);
  }


  function sell_position()
  {
     if(navigator.appName == "Opera") document.getElementById('sell').style.top = '-4px';
  }
//---------------------------------------------------------------------

function ChangeTab(objid, tabnum, wordid, lang){

	var TNum

	TNum = tabnum*1;
//	alert(TNum);
	if (TNum == 1){
		objid.src = "wordnews.php?wordid=" + wordid + "&language=" + lang;

		document.getElementById('wordnews').className="activeTab";
		document.getElementById('wordnews').style.top = "2px";
		document.getElementById('wordnews').style.height = "25px";

		document.getElementById('wordtransactions').className="inactiveTab";
		document.getElementById('wordtransactions').style.top = "10px";
		document.getElementById('wordtransactions').style.height = "20px";

		document.getElementById('wordusers').className="inactiveTab";
		document.getElementById('wordusers').style.top = "10px";
		document.getElementById('wordusers').style.height = "20px";
	}else if(TNum == 2){
		objid.src = "wordtrans.php?wordid=" + wordid + "&language=" + lang;

		document.getElementById('wordnews').className="inactiveTab";
		document.getElementById('wordnews').style.top = "10px";
		document.getElementById('wordnews').style.height = "20px";

		document.getElementById('wordtransactions').className="activeTab";
		document.getElementById('wordtransactions').style.top = "2px";
		document.getElementById('wordtransactions').style.height = "25px";

		document.getElementById('wordusers').className="inactiveTab";
		document.getElementById('wordusers').style.top = "10px";
		document.getElementById('wordusers').style.height = "20px";
	}else{
		objid.src = "wordusers.php?wordid=" + wordid + "&language=" + lang;

		document.getElementById('wordnews').className="inactiveTab";
		document.getElementById('wordnews').style.top = "10px";
		document.getElementById('wordnews').style.height = "20px";

		document.getElementById('wordtransactions').className="inactiveTab";
		document.getElementById('wordtransactions').style.top = "10px";
		document.getElementById('wordtransactions').style.height = "20px";

		document.getElementById('wordusers').className="activeTab";
		document.getElementById('wordusers').style.top = "2px";
		document.getElementById('wordusers').style.height = "25px";
	}

}

//---------------------------------------------------------------------
function ChangePeriod(period){
  //alert(sl.getValue(1) + ':' + sl.getValue(2));

  StopDate = new Date();
  var StopDate_ms = StopDate.getTime();

  if (period == 1){
        // Month
     var StartDate_ms = StopDate_ms - 7 * msInDay;
  }else if (period == 2){
        // 3 months
     var StartDate_ms = StopDate_ms - 30 * msInDay;
  }else {
        // All period
     var StartDate_ms = StopDate_ms - 12*30*10 * msInDay;
  }

  document.getElementById('ChartObject').innerHTML = '<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" '
 + 'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" '
 + 'WIDTH="650" '
 + 'HEIGHT="220" '
 + 'id="charts" '
 + 'ALIGN="" '
 + '>'
 + '<PARAM NAME=movie VALUE="charts.swf?license=C1U3JUCKXIB26UA0GFR-ZB4HZ5RKGK&library_path=charts_library&xml_source=generateXMLwv33.php?params='
 + WordDest + '=' + Lang + '=' + StartDate_ms + '=' + StopDate_ms + '">'
 + '<PARAM NAME=quality VALUE=high>'
 + '<PARAM NAME=bgcolor VALUE=#FFFFFF>'
 + '<param name="wmode" value="opaque" />'
 + '<EMBED  src="charts.swf?license=C1U3JUCKXIB26UA0GFR-ZB4HZ5RKGK&library_path=charts_library&xml_source=generateXMLwv33.php?params='
 + WordDest + '=' + Lang + '=' + StartDate_ms + '=' + StopDate_ms + '"'
 + '       quality=high '
 + '	   bgcolor=#FFFFFF 	   '
 + '       WIDTH="650" '
 + '       HEIGHT="220" '
 + '       NAME="charts" '
 + '       ALIGN="" '
 + '	   wmode="opaque" '
 + '       swLiveConnect="true" '
 + '       TYPE="application/x-shockwave-flash" '
 + '       PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer" >'
 + '</EMBED>'
 + '</OBJECT>  ';


};

//----------------------------------------------------------------------------------------
  function UserAddComment()
  {
    var comment_status = document.getElementById('comment_status').value;
    if(comment_status == 0)
    {


        var txt_comment = document.add_comment.text_comment.value;

        if (txt_comment.length > 0)
        {
            document.getElementById('loader_comment').style.visibility = "visible";
            var sVal = "?wordid=" + document.add_comment.wordid.value +
                       "&uid=" + document.add_comment.uid.value +
        			   "&seckey=" + document.add_comment.seckey.value +
                       "&text=" + escape(document.add_comment.text_comment.value) +
                       "&action=add" +
        			   "&cid=" + document.add_comment.cid.value +
                       "&language=" + document.user_action.lang.value;

            opera_obj.open("GET", sUrlWordAddComment + (sVal), true);

            opera_obj.onreadystatechange = handleHttpResponseWAddComment; 	// handle what to do with the feedback
            opera_obj.send(null);
            document.getElementById('comment_status').value =1;
        }
    }
  }

  function handleHttpResponseWAddComment()
  {
    //if the process is completed, decide to do with the returned data
    if (opera_obj.readyState == 4)
    {
        document.getElementById('comment_status').value = 0;
        var S = opera_obj.responseText;
        document.getElementById("RightComments").innerHTML = S;
        document.getElementById('loader_comment').style.visibility = "hidden";
    }


  }

//----------------------------------------------------------------------------------------
  function ChangeViewRelWords(view, wordid, lang) {
    var comment_status = document.getElementById('comment_status').value;
    if(comment_status == 0)
    {

        var sVal = "?wordid=" + wordid +
                   "&view=" + view +
                   "&language=" + lang;

        httpWord.open("GET", sUrlWordRel + (sVal), true);

        httpWord.onreadystatechange = handleHttpResponseWRelWords; 	// handle what to do with the feedback
        document.getElementById('comment_status').value =1;
        httpWord.send(null);

        return true;
    }

  }

  function handleHttpResponseWRelWords() {
    //if the process is completed, decide to do with the returned data
    if (httpWord.readyState == 4)
    {
        var S = httpWord.responseText;

        document.getElementById("rtab_content").innerHTML = S;
         document.getElementById('comment_status').value =0;
    }
  }
//----------------------------------------------------------------------------------------
  function NextComments(word, url_next){

     alert(word + '++' + url_next);
  }
//----------------------------------------------------------------------------------------
  function NextComments(word, start, lang){

    var comment_status = document.getElementById('comment_status').value;
   // alert("in" + comment_status);
    if(comment_status == 0)
    {

      document.getElementById('loader_comment').style.visibility= "visible";

        var sVal = "../../wordcomments.php?wordid=" + word + "&start=" + start + "&language=" + lang;
        opera_obj.open("GET", sVal, true);

        opera_obj.onreadystatechange = handleHttpResponseWAddComment;
        opera_obj.send(null);
        document.getElementById('comment_status').value =1;
        return true;
    }
  }

  function handleHttpResponseWNextComments() {
    //if the process is completed, decide to do with the returned data
    if (httpWord.readyState == 4)
    {
        var S = httpWord.responseText;
        document.getElementById('comment_status').value = 0;
        document.getElementById("rightTabContent").innerHTML = S;
    }
  }

