var IsStatusThreadBusy = false;
var QueueId=0;
//var imgQS;
var lblQS;
var TimerId;
var DictIds="";
var MaxFreeCounter=3;

function init()
{
	
			var cookieEnabled=(navigator.cookieEnabled)? true : false

				//if not IE4+ nor NS6+
				if (typeof navigator.cookieEnabled=="undefined" && !cookieEnabled)
				{ 
					document.cookie="testcookie"
					cookieEnabled=(document.cookie.indexOf("testcookie")!=-1)? true : false
				}
				if ( !cookieEnabled )
				{
					alert ( "Cookies are disabled on your system. For translation to work, cookies should be enabled. Please enable cookies on your browser.");
					
				}
				
			if ( get_cookie ( "TransVisit" ) != null )
					{
						var VisitCounter = parseInt(get_cookie( "TransVisit" ) );
						if(VisitCounter==1)
							{
								if( document.getElementById("hdnUserLoginId").value == "0" )
								{
									var strMsg = "You must register to continue using this translation tool. Registration is free and only takes a moment.";
									alert ( strMsg );
									window.location.href="../registerfortrial.aspx"
									return;
									
								}
								else
								{	
									VisitCounter++;
								}
								
							}
						else if(VisitCounter>=2)
							{
								VisitCounter++;
							}
						set_cookie ( "TransVisit" , VisitCounter , 2025,12,12,null,null,null);
					}
			else
			{
				//do nothing user is visitng for the first time , cookie will be created when user tries to translate
			}
}

function UpdateStatus()
{
	try
	{
	IsStatusThreadBusy = true;
	CTCC.GetQueueStatus(QueueId,callback_Status);
	}
	catch(e)
	{
		IsStatusThreadBusy = false;
		SetQueueStatus("0","");
		EnableDisable(1);
	}
}

function callback_Status( res )
{
	var qs = res.value.split(';')[0];
	var ErrCode = res.value.split(';')[1];
	SetQueueStatus(qs,ErrCode);
	IsStatusThreadBusy = false;
	if ( qs=="3" )
	{
		GetTranslation();
		//alert("hello");
	}
	
}

function callback_GetTranslation(res)
{
	var strTranslation = res.value;
	if ( strTranslation == "ERROR" )
	{
		SetQueueStatus("0","");
	}
	else
	{
		var oEditor2 = FCKeditorAPI.GetInstance( "FCKeditor2" ) ;
		//alert( strTranslation);
		oEditor2.SetHTML ( strTranslation );
		//var doc2 = oEditor2.document;
		//doc2.designMode="Off";
		SetQueueStatus("6","");
		
	}
	EnableDisable(1);
}

function GetTranslation()
{
	CTCC.GetTranslation(QueueId,callback_GetTranslation);
	
}

function SetQueueStatus(pQueueStatus,pErrCode)
{
	// 0 - Error
	//1 - Queueing
	//2 - Being Translated
	//3 - Completed
	try{
	//imgQS.style.visibility='visible';
	switch ( pQueueStatus )
	{
		case '0':
			IsStatusThreadBusy = false;
			//imgQS.src= "images/Error.png";
			EnableDisable(1);
			if ( TimerId != null)
			{
				window.clearInterval(TimerId);
			}
			
			if ( pErrCode == "999" )
			{
				lblQS.innerHTML = "Your word limit has been reached. If you are a trial user, please buy Translution Pro. If you are Pro user, please buy additional word packs.";
			}
			else
			{
				lblQS.innerHTML = "Error in translation. Please try again.";
			}
			break;
		case '1':
			//imgQS.src= "images/Queuing.png";
			lblQS.innerHTML = "Queuing";
			break;
		case '2':
			//imgQS.src= "images/Being_Translated.png";
			lblQS.innerHTML = "Being Translated";
			break;
		case '3':
			//imgQS.src= "images/Being_Translated.png";
			lblQS.innerHTML= "Being Translated";
			//imgQS.src= "images/Completed.png";
			if ( TimerId != null)
			{
				window.clearInterval(TimerId);
			}
			break;
		case '6':
			//imgQS.src= "images/Completed.png";
			lblQS.innerHTML="Translation Completed";
			EnableDisable(1);
			break;
	}
	}
	catch (e)
	{
		//imgQS.src= "images/Completed.png";
		if ( TimerId != null)
		{
			window.clearInterval(TimerId);
		}
		EnableDisable(1);
	}
}

function EnableDisable(pEnable)
{
	if ( pEnable == 1)
		document.getElementById("btnTest").disabled=false;
	else
		document.getElementById("btnTest").disabled=true;
}

function TestMe()
{
	try
	{
	//CTCC.GetQueueStatus(10,callback_TestMe);
	var cookieEnabled=(navigator.cookieEnabled)? true : false

	//if not IE4+ nor NS6+
	if (typeof navigator.cookieEnabled=="undefined" && !cookieEnabled)
	{ 
		document.cookie="testcookie"
		cookieEnabled=(document.cookie.indexOf("testcookie")!=-1)? true : false
	}
	if ( !cookieEnabled )
	{
		alert ( "Cookies are disabled on your system. For translation to work, cookies should be enabled. Please enable cookies on your browser.");
		return;
	}


	
	if ( document.getElementById("hdnUserLoginId").value == "0" )
	{
		
		if ( get_cookie ( "TransVisit" ) == null )
		{
			set_cookie ( "TransVisit" , "1" , 2025,12,12,null,null,null);
		}
		
	}
	else
	{
		if ( get_cookie ( "TransVisit" ) == null )
		{
			set_cookie ( "TransVisit" , "2" , 2025,12,12,null,null,null);
		}
	}
	
	QueueId=0;
	
	var SL = document.getElementById("cmbSL").value;
	var TL = document.getElementById("cmbTL").value;
	var Formality = document.getElementById("cmbFormality").value;
	var oEditor = FCKeditorAPI.GetInstance( "FCKeditor1" ) ;
	var oEditorTarget = FCKeditorAPI.GetInstance( "FCKeditor2" ) ;
	oEditorTarget.SetHTML("");
	//oEditorTarget.SwitchEditMode();
	//alert(oEditor.EditorDocument.body.innerHTML);
	var SourceText = oEditor.EditorDocument.body.innerHTML;
	
	if ( SourceText == null )
	{
		
		alert("Please enter the source text");
		return;
	}
	else
	{
		if ( SourceText.length==0 )
		{
			alert("Please enter the source text");
			return;
		}
	}
	
	//imgQS = document.getElementById("imgQueueStatus");
	lblQS = document.getElementById ("lblQueueStatus");
	//imgQS.src="";
	lblQS.innerText="";
	
	if ( SourceText.length > 2500 )
	{
		if ( get_cookie ( "TransVisit" ) != null )
			{
				var VisitCounter = parseInt(get_cookie( "TransVisit" ) );
				if(VisitCounter==1)
					{
						if( document.getElementById("hdnUserLoginId").value == "0" )
						{
							//Users first Visit, not registered
							alert ( "Length of the source text cannot be geater than 2500 characters. Please register and download the trial version of product to be able to translate larger number of words");
							return;
						}
						else if(document.getElementById("hdnUserIsActivated").Value == "1")
						{
							//User registered and activated
							alert("Length of the source text cannot be geater than 2500 characters.Please use the downloaded product to be able to translate larger number of words"); 
							return;
						}
						else
						{
							//User registered but not downloaded or activated
							alert("Length of the source text cannot be geater than 2500 characters.As you are a registered user please  downloaded and activate the product to be able to translate larger number of words");
							return;
						}
					}
					//users subsequent visits
				else if(document.getElementById("hdnUserIsActivated").value == "1")
						{
							if(document.getElementById("hdnLicenseStatus").value == "4")
							{
								//User registered and activated but licence expired
								alert("Length of the source text cannot be geater than 2500 characters.Your license has expired ,Please purchase the product to be able to translate larger number of words"); 
								return;
							}
							else
							{
								//User registered and activated 
								alert("Length of the source text cannot be geater than 2500 characters.Please use the downloaded product to be able to translate larger number of words"); 
								return;
							}
						}
					else
						{
							//User registered but not downloaded or activated
							alert("Length of the source text cannot be geater than 2500 characters.As you are a registered user please download and activate the product to be able to translate larger number of words");
							return;
						}
			}
		else
			{
				//default message
				alert("Length of the source text cannot be geater than 2500 characters.");
				return;
			}
		
	}
	SetQueueStatus("1","");
	CTCC.CreateTranslationQueue(SL,TL,SourceText,Formality,document.getElementById("hdnDictIds").value,document.getElementById("hdnUserLoginId").value,callback_TestMe);
	EnableDisable(0);
	}
	catch ( e )
	{
		alert ( e.message);
		SetQueueStatus("0","");
		EnableDisable(1);
	} 
}

function ShowDictionaries()
{
	alert("hello");
}

function openDictionaries()
{
	var sFeatures;
	var RetVal;
	var hdnEle = document.getElementById("hdnDictIds");
	
	if ( window.showModalDialog )
	{
		sFeatures="dialogHeight:660px;dialogWidth:626px;center:yes;edge:raised;resizable:no;scroll:yes;";
		RetVal = window.showModalDialog("MyDictionaries.htm?b=1",hdnEle.value,sFeatures);
		if ( RetVal !=undefined )
		{
			hdnEle.value = RetVal;
		}
		//alert ( "ret val =" + hdnEle.value);
	}
	else
	{
		sFeatures="height=610px,width=650px,center=yes,edge=raised,resizable=no,scroll=yes,modal=yes";
		RetVal = window.open("MyDictionaries.htm?b=0&dicts=" + hdnEle.value,"Dictionaries",sFeatures);
	}
	
	
	
	
}

function callback_TestMe(res)
{
	//alert ( res.value );
	if ( res.value !="0" )
	{
		QueueId = res.value;
		UpdateStatus();
		TimerId = window.setInterval("GetTranslationStatus()",3000);
	}
	else
	{
		//SetQueueStatus("0");
		alert("Error in creating a translation request. Please contact support@translution.net");
		EnableDisable(1);
	}
}


//Timer
function GetTranslationStatus()
{
	if ( !IsStatusThreadBusy )
	{
		//alert ( " Calling Now" );
		UpdateStatus();
	}
}

//cookie functions
function set_cookie ( name, value, exp_y, exp_m, exp_d, path, domain, secure )
{
  var cookie_string = name + "=" + escape ( value );

  if ( exp_y )
  {
    var expires = new Date ( exp_y, exp_m, exp_d );
    cookie_string += "; expires=" + expires.toGMTString();
  }

  if ( path )
        cookie_string += "; path=" + escape ( path );

  if ( domain )
        cookie_string += "; domain=" + escape ( domain );
  
  if ( secure )
        cookie_string += "; secure";
  
  document.cookie = cookie_string;
}

function get_cookie ( cookie_name )
{
  var results = document.cookie.match ( cookie_name + '=(.*?)(;|$)' );

  if ( results )
    return ( unescape ( results[1] ) );
  else
    return null;
}



