


var xmlData= null;
var refererUrl ;

function LoadData(p_url)
{

    document.write("<script src='" + p_url + "' type='text/javascript'/>");
}

function GetData(p_url)
{

// Create a new request object
bObj = new JSONscriptRequest(p_url); 

// Build the dynamic script tag
bObj.buildScriptTag(); 

// Add the script tag to the page
bObj.addScriptTag();

//alert("GetData");
}

function loadXMLDoc(fname)
{
var xmlDoc;
// code for IE
if (window.ActiveXObject)
  {
  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  }
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument)
  {
  xmlDoc=document.implementation.createDocument("","",null);
  }
else
  {
  alert('Your browser cannot handle this script');
  }
xmlDoc.async=false;
xmlDoc.load(fname);
return(xmlDoc);
}

function loadXMLString(txt) 
{
try //Internet Explorer
  {
  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  xmlDoc.async="false";
  xmlDoc.loadXML(txt);
  return(xmlDoc); 
  }
catch(e)
  {
  try //Firefox, Mozilla, Opera, etc.
    {
    parser=new DOMParser();
    xmlDoc=parser.parseFromString(txt,"text/xml");
    return(xmlDoc);
    }
  catch(e) {alert(e.message)}
  }
return(null);
}

function displayResultAgain()
{
//alert ("displayResultAgain")
    displayResult(ServiceScript_ElementId, ServiceScript_Xslt);
}

var ServiceScript_ElementId; 
var ServiceScript_Xslt; 
var ServiceScript_retry = 0  

function displayResult(elementId, xslt)
{
//alert ("xmlData2=" +xmlData)

ServiceScript_ElementId = elementId;
ServiceScript_Xslt = xslt;

    if (xmlData == null )
    {
        //alert ("No data for display");
        ServiceScript_retry ++;
        if (ServiceScript_retry < 10 )
            setTimeout("displayResultAgain()", 100)
        else
        {
            msg = "No Data available";
            document.getElementById(elementId).innerHTML= msg;
        }
        return;
    }

    if (xslt == null || xslt == "")
    {

        document.getElementById(elementId).innerHTML=xmlData;
    }
    else
    {
  
        //alert(xmlData);
        xml=loadXMLString(xmlData);
//alert ("xml=" + xml)
        xsl=loadXMLDoc(xslt);
//alert ("xsl=" + xsl)
        // code for IE
        if (window.ActiveXObject)
          {

              ex=xml.transformNode(xsl);
              document.getElementById(elementId).innerHTML=ex;
          }
        // code for Mozilla, Firefox, Opera, etc.
        else if (document.implementation && document.implementation.createDocument)
          {
        
              xsltProcessor=new XSLTProcessor();
              xsltProcessor.importStylesheet(xsl);
              resultDocument = xsltProcessor.transformToFragment(xml,document);
//alert ("   resultDocument=" + resultDocument);           
              document.getElementById(elementId).appendChild(resultDocument);
          }
    }
}

//function displayResult2(elementId, xslt)
//{
//    if (xmlData == null)
//    {
//        alert ("No data for display");
//        return;
//    }
//    document.getElementById(elementId).innerHTML=xmlData;
//}


function JSONscriptRequest(fullUrl) {
    // REST request path
    this.fullUrl = fullUrl; 
    // Keep IE from caching requests
    this.noCacheIE = '&noCacheIE=' + (new Date()).getTime();
    // Get the DOM location to put the script tag
    this.headLoc = document.getElementsByTagName("head").item(0);
    // Generate a unique script tag id
    this.scriptId = 'JscriptId' + JSONscriptRequest.scriptCounter++;
}

// Static script ID counter
JSONscriptRequest.scriptCounter = 1;

// buildScriptTag method
//
JSONscriptRequest.prototype.buildScriptTag = function () {

    // Create the script tag
    this.scriptObj = document.createElement("script");
    
    // Add script object attributes
    this.scriptObj.setAttribute("type", "text/javascript");
    this.scriptObj.setAttribute("charset", "utf-8");
    this.scriptObj.setAttribute("src", this.fullUrl + this.noCacheIE);
    this.scriptObj.setAttribute("id", this.scriptId);
    
   // alert (this.fullUrl);
}
 
// removeScriptTag method
// 
JSONscriptRequest.prototype.removeScriptTag = function () {
    // Destroy the script tag
    this.headLoc.removeChild(this.scriptObj);  
}

// addScriptTag method
//
JSONscriptRequest.prototype.addScriptTag = function () {
    // Create the script tag
    this.headLoc.appendChild(this.scriptObj);
}


//*****************************************************************************
// Script for cross domain communication for page in iframe
// Receiver "Receive.htm" script

function CallParent()
{
    var data = window.location.hash;
//alert ("Receiver data=" + data);
    
    window.parent.parent.SetIFrameHeight(data)
    
}

// Client IFrame script

var data = window.location.hash;
//alert ("data=" + data)
if (data != "")
{
    //alert ("call back data=" + data)
    var n = data.indexOf("!iframe_callback")
    if (n != -1)
    {
        try
        {
            //alert ("callback");
            CallParent();
            location.href ="blank.htm";
            
        }
        catch (e)
        {
            location.href ="blank.htm";
        }
     }
     
}

function SetIFrameHeight(p_data)
{

    var data = p_data.substring(1);
    var param = data.split("!");
    var divId = param[0];
    var w = param[1];
    var h = param[2];
    
    var height = 0;
    var width = 0;
    if (h.length > 0)
    {
        
        height = parseInt(h)
        //alert ("height1=" + height);
        var frameId = divId + "_iframe";
        var iframe1 = document.getElementById(frameId); 
        
        iframe1.style.height = height + "px" ;
        
    }
    if (w.length > 0)
    {
        
        width = parseInt(w)
     //   alert ("width=" + width);
        var frameId = divId + "_iframe";
        var iframe1 = document.getElementById(frameId); 

        iframe1.style.width = width + "px";
        
    }
    
    //Remove progress image
   
    var image = document.getElementById(divId + "_image");
//alert( image + divId);
    if (image != null)
    {
        image.style.display = "none";
    }
}

function CreateFrame2(p_frameId, p_url, p_width, p_height, p_progress , p_scroll, p_receiver)
{

    var m1 = p_url.indexOf("http://");
    var m2
    if (m1 != -1)
        m2 = p_url.indexOf("/", m1 + 7)
    else
        m2 = p_url.indexOf("/");
    
    var serverUrl = p_url.substring(0, m2 + 1)
     
    var domain = window.location.host;

    if (p_width == null)
        p_width = 0;
    
    if (p_height == null)
        p_height = 0;
    
    
    var refData = "";
    var refUrl = window.location.href;
//alert ("refUrl=" + refUrl);    
    var n1 = refUrl.indexOf("?");
    var n2 = refUrl.indexOf("#");
    var n = n1;
    if (n1 == -1 && n2 != -1)
        n = n2;
    if (n != -1)
        refUrl = refUrl.substring (0, n );
        
   // alert ( refUrl )
   
    //var divElement = document.getElementById(p_divId);
    //var url = p_url + "#" + p_frameId + "!" + domain
   if (p_receiver == null || p_receiver == '')
   {
    refData = refUrl + "*" + p_frameId ; // + "!"  + p_width + "!" + p_height
   }
   else
   {
    refData = "http://" + domain + "/" + p_receiver + "*" + p_frameId;
   }
   
   
   var jx = "&";
   if (p_url.indexOf('?') == -1)
        jx = "?";
   
        
   var url = p_url + jx + "referer=" + refData + "#" + p_frameId + "!" + refUrl + "!" + p_width + "!" + p_height
//alert ("url=" + url)    
    var width = "";
    
    if (p_width != null && p_width != 0)
        width = " width=" + p_width;
    else
        width = " width='100%' "
//     if (p_height != null && p_height != 0)
//        height = " height=" + p_height;

    var scroll = " scrolling='no' ";
    if (p_scroll == 1)
        scroll = " scrolling='auto' ";
        
    var height = " height=0 ";
    if (p_height != null & p_height !='0')
        scroll += " height='" +  p_height + "'";
 
 //url = "http://services.kinetum.com/webcontent.aspx?xc=6f5c5380-a767-4cfb-951f-537382a081ee"
 
    //var iframeScript = "<iframe id='" + p_frameId + "_iframe' src='" + url +"' "  + scroll + " frameborder='0' " + width + " ></iframe>";
    var iframeScript = "<iframe id='" + p_frameId + "_iframe' src='" + url +"' "  + scroll + " frameborder='0' " + width + " ></iframe>";
    
//alert (iframeScript);
    document.write(iframeScript);
    
    if (p_progress != null && p_progress ==1)
    {
        var imageScript = "<div id='" + p_frameId + "_image' style='margin:20px;' ><table bgcolor='white'><tr><td><img src='http://services.kinetum.com/images/progress/lite.gif'  ></td>";
        imageScript += "<td>Loading content...</td></tr></table></div>"
        document.write(imageScript);
    }

}

// Server IFrame script

function WebContentFrame()
{

    try
    {
 
        var divPageBottom = document.getElementById("divPageBottom");
        var divPageWidth = document.getElementById("divPageWidth");
        var posHeight = FindObjectPosition(divPageBottom )
        var posWidth = FindObjectPosition(divPageWidth )
        //var frmWidth = FindObjectPosition(w )
        
        var height = posHeight[1] - 20;
        var width = posWidth[0];
        
//        if (parent != null)
//        {
//            parent.SetIFrameHeight(pos[1], null);
//         }   
    
    }
    catch (e){}
    
//    var data1 = window.location.hash;
//    alert ("webcontent data=" + data1)

            var host = window.location.hash;
            host = host.substring(1);
            var param = host.split("!")
            host = param[1];
            var divId = param[0];
            var p_width = param[2];
            var p_height = param[3];
            
            try
            {
                if (p_width != "" && p_width != 0)
                    width = parseInt(p_width)
            }
            catch(e)
            {}
            try
            {
                if (p_height != "" && p_height !=0)
                    height = parseInt(p_height)
            }
            catch (e) {}
            
            url = "http://" + host + "/receiver.htm#" + divId + "!" + width+ "!" + height;
            //url = "http://" + host + "/test_videoframe.aspx?#" + divId + "!" + height + "!" + width + "!iframe_callback";
            
 

            newdivIFrame = document.createElement('iframe');
          
            newdivIFrame.src = url;
	        //newdivIFrame.setAttribute('id', spanName + 'IFrame');
	        newdivIFrame.style.position = 'absolute';
	        //newdivIFrame.style.top = document.all[spanName].style.top;
	        //newdivIFrame.style.left = document.all[spanName].style.left;
	        newdivIFrame.style.width = 100;
	        newdivIFrame.style.height = 100;
	        newdivIFrame.style.top =  10; // document.all[spanName].style.top;
	        newdivIFrame.style.left = - 300; //doc
	        
	        
            document.body.appendChild(newdivIFrame );
            
            
	            
}
// Moved to ContentScript.js
//function WebContentFrame2()
//{

//    try
//    {
// 
//        var divPageBottom = document.getElementById("divPageBottom");
//        var divPageWidth = document.getElementById("divPageWidth");
//        var posHeight = FindObjectPosition(divPageBottom )
//        var posWidth = FindObjectPosition(divPageWidth )
//        //var frmWidth = FindObjectPosition(w )
//        
//        var height = posHeight[1] - 20;
//        var width = posWidth[0] ;
//        
//    }
//    catch (e){}
//    
//            var host = window.location.hash;
//            host = host.substring(1);
//            var param = host.split("!")

//            host = param[1];
//            var divId = "";
//            var p_width = 0;
//            var p_height = 0;
//            
//            if (param.length > 1)
//            {
//                divId = param[0]
//                p_width = param[2];
//                p_height = param[3];
//            }

//            try
//            {
//                if (p_width != "" && p_width != 0)
//                    width = parseInt(p_width)
//            }
//            catch(e)
//            {}
//            try
//            {
//                if (p_height != "" && p_height !=0)
//                    height = parseInt(p_height)
//            }
//            catch (e) {}

//    try
//    {
//        if (refererUrl != null)
//              
//            //url = "http://" + host + "/receiver.htm#" + divId + "!" + height + "!" + width;
//            //url = host + "?#" + divId + "!" + width + "!" + height + "!iframe_callback";
//          
//            url = refererUrl.replace("*","?#") + "!" + width + "!" + height + "!iframe_callback";
//alert ("test3 url=" + url) 


//            newdivIFrame = document.createElement('iframe');
//          
//            newdivIFrame.src = url;
//	        //newdivIFrame.setAttribute('id', spanName + 'IFrame');
//	        newdivIFrame.style.position = 'absolute';
//	        //newdivIFrame.style.top = document.all[spanName].style.top;
//	        //newdivIFrame.style.left = document.all[spanName].style.left;
//	        newdivIFrame.style.width = 100;
//	        newdivIFrame.style.height = 100;
//	        newdivIFrame.style.top =  10; // document.all[spanName].style.top;
//	        newdivIFrame.style.left = - 300; //doc
//	        
//	        
//            document.body.appendChild(newdivIFrame );
//        }
//        catch(e)
//        {}    
//            
//}	            


function CreateServiceWindow( p_url, p_width, p_height, p_resizable, p_scrollbars )
{
    var attr = "";
    if (p_width != null)
        attr = "width=" + p_width ;
    if (p_height != null)
        attr += (attr == null ? "": " " ) + " height=" + p_height;
    if (p_resizable != null)
        attr += (attr == null ? "": " " ) +  " resizable=" + p_resizable;
    if (p_scrollbars != null)
        attr += (attr == null ? "": " " ) +  " scrollbars=" + p_scrollbars;
    
    
    window.open(p_url,"ServiceWindow", attr + " toolbar=no location=no" ) 

    
    
}    
