﻿var sme_idPrefix = "";

var sme_widthId = "";
var sme_heightId = "";

var sme_imgId = "";


var sme_imgWidth = 0.00;
var sme_imgHeight = 0.00;

var sme_imgWidthOld = 0;
var sme_imgHeightOld = 0;

var sme_imgRatio = 0;
var sme_imgWidthOriginal = 0;
var sme_imgHeightOriginal = 0;

function sme_loadWId(wId)
{
    sme_idPrefix = wId.substr(0, (wId.length - "txtWidth".length));
    sme_calculateRatio();
}

function sme_loadHId(hId)
{
    sme_idPrefix = hId.substr(0, (hId.length - "txtHeight".length));
    sme_calculateRatio();
}

function sme_calculateRatio()
{
    sme_widthId = sme_idPrefix + "txtWidth";
    sme_heightId = sme_idPrefix + "txtHeight";
    
  
    if (document.getElementById("hfRatio") != null)
    {
        sme_imgRatio = document.getElementById("hfRatio").value;
        //alert(sme_imgRatio);
    }
    if (document.getElementById("hfWidth") != null)
    {
        sme_imgWidthOriginal = document.getElementById("hfWidth").value;
    }
    if (document.getElementById("hfHeight") != null)
    {
        sme_imgHeightOriginal = document.getElementById("hfHeight").value;
    }
}

function sme_calculateWidth()
{
    if ((document.getElementById("ckKeepRatio")).checked)
    {
        //alert('calculate Height :'+sme_imgWidth +"," + sme_imgHeight + "  ratio="+sme_imgRatio);

        sme_imgHeight = (document.getElementById(sme_heightId)).value;
        
        //alert('new height='+sme_imgHeight);
        
        if (sme_imgHeight ==  sme_imgHeightOriginal)
        {
            sme_imgWidth = sme_imgWidthOriginal;
        }
        else
        {
        
            sme_imgWidth = sme_imgHeight * sme_imgRatio; 
            
            sme_imgWidth = parseInt(sme_imgWidth); 
            
            
        }        
        
        (document.getElementById(sme_widthId)).value = sme_imgWidth;
        
    }
}


function sme_calculateHeight()
{
    if ((document.getElementById("ckKeepRatio")).checked)
    {
        //alert('calculate Height :'+imgWidth +"," + sme_imgHeight + "  ratio="+sme_imgRatio);
        
        sme_imgWidth = (document.getElementById(sme_widthId)).value;
        
        //alert('new width='+sme_imgWidth);
        
        if (sme_imgWidth ==  sme_imgWidthOriginal)
        {
            sme_imgHeight = sme_imgHeightOriginal;
        }
        else
        {
            sme_imgHeight = sme_imgWidth / sme_imgRatio; 
            
            sme_imgHeight = parseInt(sme_imgHeight); 
            
        }

        (document.getElementById(sme_heightId)).value = sme_imgHeight;
        sme_resizeImage();


    }
}

function sme_resizeImage()
{
    //alert("resize");
    sme_imgId = sme_idPrefix + "image";
    
    //alert(sme_imgId);
    
    if (document.getElementById(sme_imgId) != null)
    {
        alert("sme_resizeImage():" + document.getElementById(sme_imgId));

        (document.getElementById(sme_imgId)).width = sme_imgWidth;
        (document.getElementById(sme_imgId)).height = sme_imgHeight;
        
    }
    
}

