﻿var dmm3fwsClassLib = new dmm3FWSclassLib();
//document.domain = "vdk.digizuite.dk";

function dmm3FWSclassLib()
{
    this.fileBasePathUNC 
    this.WSurl = "http://vdk.digizuite.dk/dmm3fws/default.asmx"    
    this.SOAP_DMM3FWSFRONTEND_NAMESPACE_URL = "http://www.digizuite.dk/"
    this.config ;
    this.emailTo = "cf@digieyez.dk";
    this.accessKey;
    this.guid;
    this.XMLDoc;
    this.FileArrayString = ""; // spillter på ';'
    this.FileBasepath = "";
    this.PreviewFormatId = 0;
    this.TranscodeDestinationId = 0;
    this.Extension = "";
}

dmm3FWSclassLib.prototype.generateGuid = function()
{
    var result, i, j;
    result = ''; 
    for(j=0; j<32; j++)
    {
        if( j == 8 || j == 12|| j == 16|| j == 20)
            result = result + '-';
        i = Math.floor(Math.random()*16).toString(16).toUpperCase();
        result = result + i;
    }
    this.guid = result
}

dmm3FWSclassLib.prototype.GetXMLHttpRequest = function()
{
    try {
        return new XMLHttpRequest();
        }
    catch(e){    
        var msxmls = new Array(
              'Msxml2.XMLHTTP.5.0',
              'Msxml2.XMLHTTP.4.0',
              'Msxml2.XMLHTTP.3.0',
              'Msxml2.XMLHTTP',
              'Microsoft.XMLHTTP');
            for (var i = 0; i < msxmls.length; i++) {
              try {
                return new ActiveXObject(msxmls[i]);
              } catch (e) {
              }
            }
        }
}

dmm3FWSclassLib.prototype.GetAccessKey = function()
{
    var url = "includes/wsaccess.aspx"
    var request = this.GetXMLHttpRequest();
    request.open("POST", url, false);
	request.setRequestHeader("Content-Type", "text/xml");
	request.send();
	this.accessKey = request.responseText
}

dmm3FWSclassLib.prototype.CreateZip=function()
{
    this.setXmlDoc("this.config",null)
    this.config.load("jobconfig.xml")
    
    var soapFunction = "StartJob";
	var soapAction = "http://www.digizuite.dk/StartJob";
	var soapRequest = "";
	var result = "";
	var paramArray = new Array();
	
	this.generateGuid();
	var guid = this.guid;
	
	var filename = this.GetnodeValue("//jobzip/combined_filename");
	var outFile = this.GetnodeValue("//jobzip/outputfilepath") + guid + "\\" + filename;
	
	var Job = "FrontendJobs.ZipEmail";
	this.GetAccessKey();
	var accessKey = this.accessKey;
	
	paramArray["InFile_Workfolder_Frontend"] = this.GetnodeValue("//jobzip/sourcebaseuncpath");
	paramArray["SMTPServer"] = this.GetnodeValue("//jobzip/smtp");
	paramArray["Html"] = "1";
	paramArray["PreservePaths"] = "0";
	paramArray["Subject"] = this.GetnodeValue("//jobzip/subject");
	paramArray["BodyText"] = this.GetnodeValue("//jobzip/mailbody").replace("###", "downloadFileUrl");
	paramArray["ToName"] = this.emailTo;
	paramArray["FromName"] = this.GetnodeValue("//jobzip/fromemail");
	paramArray["ZipFileCompleatPath"] = outFile;
	paramArray["OutFilePath"] = this.GetnodeValue("//jobzip/outputfilepath") + guid ;
	
	paramArray["FileSourceXML"] ="" +
    "<?xml version=\"1.0\" encoding=\"utf-8\"?> "+
    "<zipfilelist> "+this.CreateFileXML()+"</zipfilelist> ";
	
	soapRequest = "<StartJob xmlns=\"http://www.digizuite.dk/\">"+
	" <accesskey><![CDATA["+accessKey+"]]></accesskey> "+
    "   <progChainID><![CDATA["+Job+"]]></progChainID> "+
    "   <parametre> "+ this.CreateParamXML(paramArray)+"</parametre> "+
    " </StartJob> ";
    
    var url = "includes/ZipJob.aspx"
    var request = this.GetXMLHttpRequest();
    request.open("POST", url, true);
	//request.setRequestHeader("Content-Type", "text/xml");
	request.send("xmlToJob="+soapRequest+"&test=1");
	//var resul = request.responseText
	

/*
	result = SoapInvoke(this.WSurl, soapAction, soapRequest, soapFunction);
	//document.write("<textarea>"+ result +"</textarea>");
	
	ResultEval = eval("result."+soapFunction+"Result")
   
    if(ResultEval)
	{
	    if(ResultEval.xml)
	        result = new Array(ResultEval.xml);	        
	    else
	        result = ResultEval;	        	    
	}	 
	*/
}

dmm3FWSclassLib.prototype.GetnodeValue = function(XpathCommand)
{
    try{
        return this.config.selectSingleNode(XpathCommand).text
    }
    catch(e)
    {
        return ""
    }
}

dmm3FWSclassLib.prototype.CreateFileXML = function()
{
    var resultXML = "";
    this.FileBasepath = this.GetnodeValue("//jobzip/sourcebaseuncpath");
    
    var Filename = this.FileArrayString.split(";");
    var i;
    for(i in Filename)
    {
       resultXML +="<file mask='"+this.FileBasepath+this.BuildFilename(Filename[i], 6)+"' /> ";
    }
    //alert(resultXML);
    return resultXML
}

dmm3FWSclassLib.prototype.CreateParamXML = function(paramArray)
{
     
    var resultXML = "";
    for(var param in paramArray)
    {
       resultXML +="<ArrayOfString>"+
          "<string><![CDATA["+param+"]]></string>"+
          "<string><![CDATA["+paramArray[param]+"]]></string>"+
        "</ArrayOfString>";
    }
    return resultXML
}

dmm3FWSclassLib.prototype.setXmlDoc = function(obj,result)
{
    var parser
    var XMLResult
    
	try{
        parser = new DOMParser()
        this.config = parser.parseFromString(result,"text/xml")
        this.browser = "firefox";
    }
    catch(e){
        try{
           this.config = new ActiveXObject("Msxml2.DOMDocument.4.0");
           if(result!=null)
                this.config.loadXML(result)
        }
        catch(e)
        {
            try{
                this.config = new ActiveXObject("Msxml2.DOMDocument.3.0");
                if(result!=null)
                    this.config.loadXML(result)
            }
            catch(e){
                this.config = new ActiveXObject("Microsoft.XmlDOM");
                if(result!=null)
                    this.config.loadXML(result)
            }
        }
        if (document.implementation && document.implementation.createDocument) {
            this.config = document.implementation.createDocument("","doc",null);
            if(result!=null)
                this.config.load(result) 
        }
      //  alert(this.config)
       // alert("this.config: "+this.config)
        this.browser = "ie";
    }    
    //alert(this.browser)
}



//function BuildFilename(assetid, numbers)
dmm3FWSclassLib.prototype.BuildFilename = function(assetid, numbers)
{
    var res = "";
    if (assetid.length < numbers)
    {
        for (i = 0; i < numbers - assetid.length; i++)
        {
            res += "0";
        }
        res += assetid;
    }
    else
    {
        res = assetid;
    }
    return 'A' + res + '_' + this.PreviewFormatId + '_' + this.TranscodeDestinationId + '.' + this.Extension;
}

//testarea

//this.FileArrayString = "142;143"
//var dmm3fwsClassLib = new dmm3FWSclassLib();
//dmm3fwsClassLib.CreateZip()
//dmm3fwsClassLib.GetAccessKey();