var numSteps=3;
var actStep=1;
var intervalID;

var file_ext="docx";

function getObj(id){
	var obj=false;
	if(document.getElementById(id)) obj=document.getElementById(id);
		return obj
}

function clickBrs(){
//  if(actStep==1){
    return true;
//  }else {return false;}
}

function file_change(){
//	document.getElementById("txtPath").value = document.getElementById("uplfile").value;
	var str=document.getElementById("uplfile").value;
	var ext=getfileextension(str);
	ext=ext.toLowerCase();
  if(ext=="."+file_ext){
    document.getElementById("txtPath").value=str;
    getObj('divStep1').className = "step_1_inact";
    getObj('divFile').className = "divFile_inact";
    getObj('divStep2').className = "step_2_act";
    getObj('divConv').className = "divConv_act";
    getObj('divStep3').className = "step_3_inact";

    actStep=2;
  }
  else alert("You must select a file with extension '"+file_ext+"'!");
}

function checkFile(){
  var str=trim(document.getElementById("txtPath").value);
	var ext=getfileextension(str);
	ext=ext.toLowerCase();
  var regex = /^http:\/\//g;
  if((str.match(regex)!=null) && (ext=="."+file_ext)){
    getObj('divStep1').className = "step_1_inact";
    getObj('divFile').className = "divFile_inact";
    getObj('divStep2').className = "step_2_act";
    getObj('divConv').className = "divConv_act";
    getObj('divStep3').className = "step_3_inact";

    actStep=2;    
  }else{
    getObj('divStep2').className = "step_2_inact";
    getObj('divConv').className = "divConv_inact";
    getObj('divStep3').className = "step_3_inact";
    getObj('divStep1').className = "step_1_act";
    getObj('divFile').className = "divFile_act";
    actStep=1; 
  }
}

function getfileextension(str){
	var dot = str.lastIndexOf("."); 
	if( dot == -1 ) return "";
	var extension = str.substr(dot,str.length); 
	return extension; 	
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function brsOverOut(whichEvent){
	if(actStep==1) {
		if(whichEvent=='out')
			document.getElementById('divFile').className='divFile_act';
		else if(whichEvent=='over')
			document.getElementById('divFile').className='divFile_hover';
	}else{
 		if(whichEvent=='out')
			document.getElementById('divFile').className='divFile_inact';
		else if(whichEvent=='over')
			document.getElementById('divFile').className='divFile_inact_hover';   
  }
}

function file_conv(){
  if(actStep==2) {
 //http://people.opera.com/howcome/2005/ala/sample.pdf
 //http://www.rightbrain.com/download/books/ThinkingInPostScript.pdf

    var str=trim(document.getElementById("txtPath").value);
    var ext=getfileextension(str);
    ext=ext.toLowerCase();
    var regex = /^http:\/\//g;
    if((str.match(regex)!=null) && (ext=="."+file_ext)){
    }

    var fileName=document.getElementById("uplfile").value;
    if((fileName=="")&&((str.match(regex)==null) || (ext!="."+file_ext))){
		  alert("Choose a file first or enter a URL !!!");
		  return false;
    }
  	fileName=fileName.replace(/(.*)\\/gi,'');
  	fileName=fileName.replace(/(.*)\//gi,'');
  	fileName=fileName.replace(/ /gi,'_');
  	
    actStep=3;
    getObj('divStep2').className = "step_2_inact";
    getObj('divConv').className = "divConv_inact";
    document.getElementById("uplfile").disable=true;
    document.getElementById("txtPath").disable=true;
    document.getElementById("sbmBtn").disable=true;
    
    document.getElementById('pLoad').style.visibility = 'visible';

    document.getElementById("frmUpl").submit();
    //window.open('http://www.doc.investintech.com/download.php?name=MyName&arg=http://www.doc.investintech.com/x/v/559x4x9/_inputDocx71.doc','mywindow','width=400,height=200');
  } 
}

function stop_conv(success,isCap,isSuccess){
//  alert(success+" "+isCap+" "+isSuccess);
  document.getElementById("divLinkRes").innerHTML="";
  document.getElementById('pLoad').style.visibility = 'hidden';
	recFirefox();
  
  if(isSuccess!=""){
    document.getElementById('pConv').style.visibility = 'visible';
    document.getElementById("divTextRes").innerHTML="";
    document.getElementById("pDivConv").innerHTML=success;
//    document.getElementById("divLinkRes").innerHTML=isSuccess;
    setCheckStatus();
  }else{
    getObj('divStep3').className = "step_3_act";
    document.getElementById("divTextRes").innerHTML=success;   
  }

//  document.getElementById("divTextRes").innerHTML=success;

	if(isCap==1){
		document.getElementById("divCaptcha").innerHTML='<img src="createImage.php?'+Math.random()+'" />'+
			'<input type="text" id="txtCaptcha" name="txtCaptcha" />';
	}else{
		document.getElementById("divCaptcha").innerHTML='';
	}
  document.getElementById("uplfile").disable=false;
  document.getElementById("txtPath").disable=false;
	document.getElementById("sbmBtn").disable=false;

	return true;
}

function convOverOut(whichEvent){
	if(actStep==2) {
		if(whichEvent=='out')
			document.getElementById('divConv').className='divConv_act';
		else if(whichEvent=='over')
			document.getElementById('divConv').className='divConv_hover';
	}
}

function recFirefox(){
	if(navigator.userAgent.match("Firefox")!=null){
		getObj("uplfile").className="upl_file_ff";
	}
  str=document.getElementById("divTextRes").innerHTML;
  if(str!="&nbsp;"){
    var found = str.match("!");
    if(!found){
      setCheckStatus();
      document.getElementById('pConv').style.visibility = 'visible';
    }
  }
}

function cleanForm(){
	getObj('txtPath').value = "";
	getObj('uplfile').value = "";
}

var xmlHttp

function GetXmlHttpObject()
{
	var xmlHttp=null;
	try{
		xmlHttp=new XMLHttpRequest();
	}
	catch (e){
		try{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e){
    		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    	}
	}
	return xmlHttp;
}

function setCheckStatus(){
  intervalID = setInterval('checkStatus()',10000);
}

function unsetCheckStatus(){
  clearInterval(intervalID); 
}

function checkStatus()
{
 	xmlHttp=GetXmlHttpObject();
  if (xmlHttp==null)
  {
  	alert ("Your browser does not support AJAX!");
  	return;
  }
  var url="checkStatus.htm";
  xmlHttp.onreadystatechange=refStatus;
  xmlHttp.open("GET",url,true);
  xmlHttp.send(null);
}

function refStatus() 
{
	if (xmlHttp.readyState==4){
		document.getElementById("divStep3").innerHTML=xmlHttp.responseText;
		document.getElementById("pDivConv").innerHTML="";
		str=document.getElementById("divTextRes").innerHTML; 
		lnk=document.getElementById("divLinkRes").innerHTML;
		var found = str.match("!");
		if(found){
		  unsetCheckStatus();
		  document.getElementById('pConv').style.visibility = 'hidden';
		  getObj('divStep3').className = "step_3_act";
    }
	}
}
function popitup(url) {
	newwindow=window.open(url,'name','height=200,width=400');
	if (window.focus) {newwindow.focus()}
	return false;
}