﻿// File Selection Handler
// ----------------------
// Displays relevant conversion options based upon user selected 
// input file.

var DivList = document.getElementsByTagName("div");

function HideDivGroup(DivStr)
{   
    var DivId;
    
    for(var a = 0; a < DivList.length; a++)
    {
        DivId = DivList[a].getAttribute("id");
         
        if (DivId != null)
        {   
            if (DivId.indexOf(DivStr) != -1) 
                DivList[a].style.display = "none";
        }
        
    }
}

function ShowDivGroup(DivStr)
{   
    var DivId;
    
    for(var a = 0; a < DivList.length; a++)
    {
        DivId = DivList[a].getAttribute("id");
         
        if (DivId != null)
        {   
            if (DivId.indexOf(DivStr) != -1) 
                DivList[a].style.display = "block";
        }
        
    }
    
}
    
function StartUpload()
{
    // hide other boxes + show upload dialog
    document.getElementById('fileselect').style.display = "none";
    document.getElementById('converttype').style.display = "none";
    document.getElementById('UploadDlg').style.display = "block";
    setTimeout('document.images["progressimage"].src="Theme/progress.gif"', 200);

}

function StartConvSelect(ConvGroup)
{
    // hide file select + show conversion select
    document.getElementById('fileselect').style.display = "none";
    loadajax('Convert.aspx?group=' + ConvGroup, 'convoption');
    document.getElementById('converttype').style.display = "block";
}

function CancelConvSelect()
{
    // show file select + hide conversion select
    document.getElementById('fileselect').style.display = "block";
    document.getElementById('converttype').style.display = "none";
}

function SelectConvMode(ConvMode) 
{
    // show / hide File or URL conversion box
    if (ConvMode == "File") 
    {
        document.getElementById('fromfile').style.display = "block";
        document.getElementById('fromurl').style.display = "none";
    }
    else 
    {
        document.getElementById('fromfile').style.display = "none";
        document.getElementById('fromurl').style.display = "block";
    }

}

function GetQueryString() {
    // update URL input and launch conversion options if querystring provided


    var url = window.location.toString();
    var qs_loc = url.lastIndexOf("?URL=");
    var qs_str;

    if (qs_loc == -1) return;

    qs_str = url.substring(qs_loc + 5);

    SelectConvMode('URL');

    document.frmmain.URLSelector.value = qs_str;

    CheckFile('URL');

}

function isValidURL(UrlStr) 
{
    // simple URL regular expression checker
    var regexp = /http:\/\/[A-Za-z0-9\.-]/;
    return regexp.test(UrlStr);
}

function Trim(TrimStr)
{
    if (TrimStr.lastIndexOf('?') != -1) {
        TrimStr = TrimStr.substring(0, TrimStr.lastIndexOf('?'));
    }
    return TrimStr.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}
 

                 

// --------- AJAX Loader -----------
function getpagedata(url, target) {
  document.getElementById(target).innerHTML = ' Loading Conversion Options...';
  if (window.XMLHttpRequest) {
    req = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    req = new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (req != undefined) {
    req.onreadystatechange = function() {dataloaded(url, target);};
    req.open("GET", url, true);
    req.send("");
  }
}  

function dataloaded(url, target) {
  if (req.readyState == 4) { // only if req is "loaded"
    if (req.status == 200) { // only if "OK"
      document.getElementById(target).innerHTML = req.responseText;
    } else {
      document.getElementById(target).innerHTML=" Sorry - we ran into a problem. Please try later";
    }
  }
}

function loadajax(name, div) {
	getpagedata(name,div);
	return false;
}
