﻿// JScript File
var objgIMG
var ZoomLevel, ZoomWidth, ZoomHeight;

function LoadDynaPopup(strURL)
{
    FetchContent(strURL, document.getElementById("idPopupCell"));
    document.getElementById("IDPopupDialog").style.display = "";
}

function LoadFilePick()
{
    document.getElementById("FrontPage").style.display = "none";
    document.getElementById("ChoosePhotoPage").style.display = "";
    IDSwitchInsidePageButton.style.display = "none";
    //FetchContent("ChooseYourPhoto.aspx", document.getElementById("idFilePopupCell"));
    //document.getElementById("IDFilePopupDialog").style.display = "";
}

function FetchContent(strURL, objContainer)
{
    xmlhttp = createXMLHttpRequest();
    if (typeof req == 'boolean') { 
        alert("You are using a non-compatible or a old version of the browser.");
        return;
    }
    if (strURL.indexOf("?") > -1) 
    {
        strURL = strURL + "&"
     }
     else
     {
        strURL = strURL + "?"
     }
    strURL = strURL + "rnd=" + Math.random()
    xmlhttp.open("GET", strURL, false);
    xmlhttp.send("");
    //alert(xmlhttp.responseText);
    strResponse = xmlhttp.responseText;
    strResponse = strResponse.replace("<form ","<forminactive ")
    strResponse = strResponse.replace("</form>","</forminactive>")
    objContainer.innerHTML = strResponse
}

function createXMLHttpRequest() {
  var types = [
    'MSXML2.XMLHTTP.5.0',
    'MSXML2.XMLHTTP.4.0',
    'MSXML2.XMLHTTP.3.0',
    'MSXML2.XMLHTTP',
    'Microsoft.XMLHTTP'
   ];

  for (var i = 0; i < types.length; i++) {
    try {
      return new ActiveXObject(types[i]);
    } catch(e) {} 
  }

  try {
    return new XMLHttpRequest();
  } catch(e) { }

  return false; // XMLHttpRequest not supported
}

function ClickDialogMaster(e)
{
    if (!e) e = window.event //fix IE
    e.cancelBubble = true;
}

function HidePopups()
{
    document.getElementById("IDPopupDialog").style.display = "none";
    //document.getElementById("IDFilePopupDialog").style.display = "none";
}

/* Dialog Functions */
function ChooseFrame(intFrameType, intPageNum)
{
    //alert(document.getElementById('IDChoosenFrame').style.filter);
    strURL = "chooseframe.aspx?FrameType=" + intFrameType + "&PageNum=" + intPageNum
    LoadDynaPopup(strURL)
}

function PreviewFrame(objPreviewID, strImageURL, lngFrameID, bolIsCaption)
{
    objPreviewID.innerHTML = "<A HREF='javascript:SelectFrame(" + "\"" + strImageURL + "\""  + "," + lngFrameID + "," + bolIsCaption + ")' title='Click to choose this frame'><img src='" + strImageURL + "'/></A>"
}

function SelectFrame(strImageURL, lngFrameID, bolIsCaption)
{
    
    document.getElementById('IDChoosenFrame').style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + strImageURL + "')"
    HidePopups();
    SetFrameID(lngFrameID, bolIsCaption);
}

function PreviewChosenPhoto(objIMG, strImageURL)
{   
    var intLoopVar;
    if (strImageURL != "")
    {
        objIMG.src = "";
        objIMG.src = strImageURL;
        objIMG.style.display = "";
        objgIMG = objIMG;
        window.setTimeout("ZoomIn()",500);
    }
    else
    {
        objIMG.style.display = "none";
    }
}
function dummy()
{}

function ZoomIn()
{
    var StandardWidth, lngWidth, lngHeight;
    StandardWidth = 472;    
    
    /*for(i=0;i<100;i++)
    {
        if (objgIMG.width > 100)
        {break;}
        window.setTimeout("dummy()",100);
    }*/

    if (objgIMG.width < 100)
    {
        window.setTimeout("ZoomIn()",100);
        return;
    }

    lngWidth = objgIMG.width
    lngHeight = objgIMG.height
    ZoomLevel  = Math.round((StandardWidth / lngWidth) * 100)
    //ZoomWidth  = Math.round((ZoomLevel / 100) * lngWidth)
    //ZoomHeight = Math.round((ZoomLevel / 100) * lngHeight)
    
    document.getElementById("FrontPage").style.display = "";
    document.getElementById("ChoosePhotoPage").style.display = "none";

    document.getElementById("IDBackPic").src        = objgIMG.src;
    document.getElementById("IDBackPic").style.zoom = ZoomLevel + "%";
    document.getElementById("IDBackPic").style.top  = 0;
    document.getElementById("IDBackPic").style.left = 0;
    //document.getElementById("IDBackPic").width  = ZoomWidth;
    //document.getElementById("IDBackPic").height = ZoomHeight;
    
    document.getElementById("IDInsertPhotoButton").src = "images/DesignSpace/ChangePhoto.gif"
    SetZoomLevel(ZoomLevel);
    IDSwitchInsidePageButton.style.display = "";
}

function EncodeText(strInput)
{
    strInput = strInput.replace("\n","^n")
    strInput = strInput.replace("\"","^q")
    strInput = strInput.replace("'","^s")
    strInput = strInput.replace("`","^a")
    
    return strInput 
}

function decimal2hexa(d) 
{
var hD="0123456789ABCDEF";
var h = hD.substr(d&15,1);
while(d>15) {d>>=4;h=hD.substr(d&15,1)+h;}
return h;
}

function QuotedString(str)
{
    var countUB;
    var charCode;
    var rstr;
    var IsQuoteChar;
    var strHex;
    rstr="";
    countUB = str.length -1;
    if (countUB < 0)
    {
    return "";
    }
        for (i = 0; i <= countUB; i++) 
    {
        IsQuoteChar = false;
        charCode = str.charCodeAt(i);
        if (charCode > 64 && charCode < 91) // Capital alphabets
        {}
        else if (charCode > 96 && charCode < 123) 
        {}
        else if (charCode > 47 && charCode < 58)
        {}
        else
        {IsQuoteChar=true;}
        
        if (IsQuoteChar == true)
        {
            strHex = decimal2hexa(charCode);
            if (strHex.length < 2) 
            {
                strHex = "0" + strHex;
            }
            rstr = rstr + "^" + strHex;
        }
        else
        {
            rstr = rstr + str.charAt(i);
        }
        
    }
    return rstr;
}

function ConstructFontStyle(strFontName, strFontColor, strIsBold)
{
    strRetVal = ""
    strRetVal = "<b>Font:</b> " + strFontName 
    strRetVal = strRetVal + "&nbsp;&nbsp;<b>Color:</b> <SPAN CLASS='FontChoose' style='background-color: " 
    strRetVal = strRetVal + strFontColor + "'></SPAN>"
    strRetVal = strRetVal + "&nbsp;&nbsp;<b>Style:</b> "
    if (strIsBold.toString() == "true") 
    {
        strRetVal = strRetVal + "Bold"
    }
    else
    {
        strRetVal = strRetVal + "Regular"
    }
    return strRetVal
}