﻿var mappedUrl = '';
//########################################################################
// Manage category
function jWDManageCategory(strUrl, intWidth, intHeight) 
{
    var strHandle = 'ManageItems';
    var strOptions;
    //check for optional parameters value
    if(intWidth == undefined)
        intWidth  = 678;
    if(intHeight == undefined)
        intHeight = 750;
    
    strOptions = 'dependent=yes,height=' + intHeight + ',width='+ intWidth +',location=no,menubar=no,resizable=yes,status=yes,toolbar=no,top=0,left=0,scrollbars=yes';
    // getting the newly open popup windows object
    var newwindow = window.open(strUrl, strHandle, strOptions);
    //set focus to new window and resize
    newwindow.focus();
    newwindow.resizeTo(intWidth, intHeight);
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// To check the search text box text
function jWDValidateSearchText(txtObjId, defaultText) {
    var strTextBoxValue;
    strTextBoxValue = document.getElementById(txtObjId).value;
    strTextBoxValue = trim(strTextBoxValue);
    if (( strTextBoxValue = "") || (strTextBoxValue == defaultText ))//if search textbox having default text 
        {
            return false;
        }
        else
        {
            return true;
        }
    }
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// To resize the window incase user forget to close the already open popup.
function jWDResizeWindow(intWidth, intHeight)
{
    // resize window to given width and height
    window.resizeTo(intWidth, intHeight); 
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function trim(str) 
{
	var temp
	if (typeof(str)=="object")
		temp=str.value;
	else
		temp=str;
	for(i=0; temp.charAt(i) ==" " || temp.charAt(i) == "\r" || temp.charAt(i) == "\n"; i++)
		{}
	temp = temp.substr(i, temp.length);
	for(i=temp.length-1; temp.charAt(i) == " " ||temp.charAt(i) == "\r" || temp.charAt(i) == "\n"; i--)
		{}
	temp=temp.substr(0, i+1);
	if (typeof(str)=="object")
		str.value=temp;
	else
		return temp;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function jWDDeleteImage(strUrl) 
{
    var chckAnswer
	chckAnswer = confirm('Do you really want to delete the image?');
	if(chckAnswer == false)
    {
		return;
	}
	else
	{
		jWDManageCategory(strUrl);
		return;
	}
}
//########################################################################
// Preview image
function jWDPreviewImage(strUrl, intWidth, intHeight) 
{
    var strHandle = 'ManageImage';
    var strOptions = 'dependent=yes,height=' + intHeight + ',width=' + intWidth + ',location=no,menubar=no,resizable=yes,status=no,toolbar=no,top=0,left=0,scrollbars=no';

    window.open(strUrl, strHandle, strOptions);
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// This function invokes on onkeydown event
//Checking whether the enter key is pressed
function jWDCheckKey()
{  
    if(event.which || event.keyCode)//check for fired event 
    {
        if ((event.which == 13) || (event.keyCode == 13)) //if enter key is pressed
        {
            return true;
        }
    } 
    else // if no enter key is pressed.
    {
        return false;
    }
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~        
// this function get invoked on click event of search text box
function jWDInitSearchBox(controlId,defaultText)//initializing search textbox's value
{  
    if(document.getElementById(controlId).value == defaultText)//if search textbox having default text 
    {
        document.getElementById(controlId).value = "";//clear text
    }
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~        
//This function invoke on blur event of search textbox
// To trim the text and set default text if text box is empty
function trimSearchText(str, defaultText) 
{
	var temp
	if (typeof(str)=="object")
		temp=str.value;
	else
		temp=str;
	for(i=0; temp.charAt(i) ==" " || temp.charAt(i) == "\r" || temp.charAt(i) == "\n"; i++)
		{}
	temp = temp.substr(i, temp.length);
	for(i=temp.length-1; temp.charAt(i) == " " ||temp.charAt(i) == "\r" || temp.charAt(i) == "\n"; i--)
		{}
	temp=temp.substr(0, i+1);
	
	if(temp == "")//if searchtext box is empty set back to default text
	    temp = defaultText;
	    
	if (typeof(str)=="object")
		str.value=temp;
	else
		return temp;
}
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
/* This function helps protect the email address from the evil spam-bots
that scan web pages for useful data such as (email addresses). 
Instead of using the data directly, the encoded value is stored in the
html and decoded when required. */
function decode(ServerEncoded)
{
    // The ServerEncoded parameter is a string that contains the encoded data.
    // Each character in the ServerEncoded parameter has been converted into 
    // a two digit number (hex / base16). This function converts the
    // series of numbers back into the normal form and returns the 
    // decoded string to the client

    // holds the decoded string
    var res = "";

    // go through and decode the full input server encoded string
    for (i=0; i < ServerEncoded.length;)
    {
	    // holds each letter (2 digits)
	    var letter = "";
	    letter = ServerEncoded.charAt(i) + ServerEncoded.charAt(i+1)

	    // build the real decoded value
	    res += String.fromCharCode(parseInt(letter,16));
	    i += 2;
    }
    //return the new decoded string to allow it to be rendered to screen
    location.href = "mailto:" + res;
}

//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
/* This function gets a reference to the server encrypted string and
then decrypts this using the decode() function and sets the
txtDecrypted value to the value return by the decode() function */
function jWDMailTo(email) 
{
	//get the table element
	return decode(email);
}


//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
/* This function sets the passed object to a a transparency value from 0-100,
where 0 is fully opaque and 100 being fully transparent. */
function jWDSetTransparency(element, percentage) {
    var count;
    var objStyle;
    var filterValue;
    var opacityValue;
    
    // Assign to local reference
    if(typeof(element) == "object" && element) {
        obj = element;
    }
    else if (document.getElementsByName(element) && document.getElementsByName(element)[0]) {
        obj = document.getElementsByName(element);
    }
    else if (document.getElementById(element)) {
        obj = document.getElementById(element);
    }
    else if (document.getElementsByTagName && document.getElementsByTagName(element) && document.getElementsByTagName(element)[0]) {
        obj = document.getElementsByTagName(element);
    }
    else {
        obj=false;
    }
    
    if(obj) {
        percentage = (typeof(percentage) == "undefined") ? 50 : 100 - percentage;
        
        filterValue = "Alpha(opacity=" + percentage + ")";
        opacityValue = "" + percentage / 100;
        count = (obj.length) ? obj.length : 1;
        
        for(var i = 0 ; i < count ; i++) {
            objStyle = (obj.length) ? obj[i].style : obj.style;
            objStyle.filter = filterValue;
            objStyle.MozOpacity = opacityValue;
            objStyle.KhtmlOpacity = opacityValue;
            objStyle.opacity = opacityValue;
        }
    }
} // jWDSetTransparency

var timeoutMap = [];    // Store timeout-IDs for context menu

//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
/* Fade content context menu in- and out. First parameter is the toggle control.
Second parameter is the div, holding the menu items. */
function jWDToggleContext(toggleId, menuId, pMode) {

    var toggleObj = document.getElementById(toggleId);
    var menuObj = document.getElementById(menuId);
    var arrImages = menuObj.getElementsByTagName("img");
    var startBlend;
    var destBlend;
    var step = 10;
    var delay = 30;
    var xOffset = 12;
    var displayTimeout = 5000;
                
    if(pMode == "relative") {
        // Position is relative to parent
        menuObj.style.left = xOffset;
        menuObj.style.top = 0;
    }
    else {
        // Position is absolute
        var pos = jWDFindStaticPosition(toggleObj);
        
        menuObj.style.left = pos[0] + xOffset;
        menuObj.style.top = pos[1];
    }
    
    if(menuObj.style.display == "" || menuObj.style.display == "none") {
        
        // Display
        menuObj.style.display = "inline";
        
        startBlend = 100;
        destBlend = 0;

        for(var i = 0 ; i < arrImages.length ; i++)
            jWDBlendTo(arrImages[i], startBlend, destBlend, step, delay);

        // Swap expand image
        toggleObj.src = "/img/icons/Collapse.png";
        
        timeoutMap[toggleId] = setTimeout(function() {jWDToggleContext(toggleId, menuId, pMode);}, displayTimeout);
    }
    else {
    
        startBlend = 0;
        destBlend = 100;

        for(var i = 0 ; i < arrImages.length ; i++)
            jWDBlendTo(arrImages[i], startBlend, destBlend, step, delay);
        
        // Delayed deactivation
        window.setTimeout(function() {menuObj.style.display = "none";}, (Math.abs(startBlend - destBlend) / step) * delay);
        
        // Swap expand image
        toggleObj.src = "/img/icons/Expand.png";
        
        // Clear running timeout
        if(timeoutMap[toggleId])
            clearTimeout(timeoutMap[toggleId]);
    }
} // jWDToggleContext

//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
/* This function implements a simple blending animation. Obj is the blend target,
 startBlend is the start blending value (0 - 100) destBlend is the destination
 blending value (0 - 100), step is the blending step size, delay is delay between
 blending steps, currBlend is the current blending factor (can be omitted) */
function jWDBlendTo(obj, startBlend, destBlend, step, delay, currBlend) {

    // Initialize
    if(typeof(currBlend) == "undefined") currBlend = startBlend;

    if(destBlend < startBlend) {
        if(currBlend <= destBlend) return;
        // 100 ==> 0
        currBlend -= step;
    }
    else if(startBlend < destBlend) {
        if(currBlend >= destBlend) return;
    
        // 0 ==> 100
        currBlend += step;
    }
    
    // Set blend value
    jWDSetTransparency(obj, currBlend);
    
    setTimeout(function() { jWDBlendTo(obj, startBlend, destBlend, step, delay, currBlend); }, delay);
} // jWDBlendTo

//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
/* This function calculates the pixel position of the passed in static element */
function jWDFindStaticPosition(obj) {
    var curleft = 0;
    var curtop = 0;
    
    if(obj.offsetParent) {
        do {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
        }
        while(obj = obj.offsetParent);
    }
    
    return [curleft, curtop];
} // jWDFindStaticPosition

//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
/* This function toggles the admin menu context panels */
function jWDToggleContextMenu(imageId, contextId, showMenu) {   
    var imageObj = document.getElementById(imageId);
    var contextObj = document.getElementById(contextId);
    /*var arrCells = contextObj.getElementsByTagName("td");
    var startBlend;
    var destBlend;
    var step = 10;
    var delay = 30;*/
    var yOffset = "39px";

    if(showMenu) {
        var pos = jWDFindStaticPosition(imageObj); 
        contextObj.style.left = pos[0] + "px"; 
        contextObj.style.top = yOffset;
        contextObj.style.display = "block"; 
        
        /*startBlend = 100;
        destBlend = 0;

        for(var i = 0 ; i < arrCells.length ; i++)
            jWDBlendTo(arrCells[i], startBlend, destBlend, step, delay);*/
    }
    else
    {
        /*startBlend = 100;
        destBlend = 0;

        for(var i = 0 ; i < arrCells.length ; i++)
            jWDBlendTo(arrCells[i], startBlend, destBlend, step, delay);
    
        // Delayed deactivation
        window.setTimeout(function() {contextObj.style.display = "none";}, (Math.abs(startBlend - destBlend) / step) * delay);*/
        
        contextObj.style.display = "none";
    }
} // jWDToggleContextMenu

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// This function will Refresh parent page only if its Present.
// Modified By  : Amol Udavant
// Modified On  : 2008/12/28
// Purpose      : To refersh parent page content properly
// Modified By  : Martin Sandrock
// Modified On  : 2009/05/29
// Purpose      : Added optional parameter for reload destination
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function jWDReloadSiteContent(reloadUrl) {

    if (reloadUrl != null) mappedUrl = reloadUrl;
    
    // User edit container and teaser details
    if (mappedUrl == '') mappedUrl = window.opener.location.href;
        
    if( opener && !opener.closed) // Parentpage/Opener is not closed.
    {
        window.opener.location.href = mappedUrl;
    }
    window.close();
}
/***********************************************************************************************/
// Created By : Amol Udavant
// Created On : 19.12.2008
// Purpose    : To map parent url if node name is changed.
// Modified by: Harshad Jadhav
// Modified On: 17.03.2009
// Purpose    : Entered the if condition to avoid error in live.   
function jWDParentUrlMapping(newNodeTitle)
{
    newNodeTitle = newNodeTitle.toLowerCase() + '.aspx'; //append extension to check for modified name
    
    var openerUrl = window.opener.location.href; // Get the opener url i.e. parent url
    var oldNodeTitle;//variable to hold oldNodeTitle  
    
    if (openerUrl.indexOf('?')> -1)// checking if '?' is aviliable or not. 
         oldNodeTitle = openerUrl.substring(openerUrl.lastIndexOf('/')+1, openerUrl.indexOf('?')); // Get the old title   
    else
         oldNodeTitle = openerUrl.substring(openerUrl.lastIndexOf('/')+1); // Get the old title
       
    if(oldNodeTitle != newNodeTitle) //if node name is changed
        mappedUrl = openerUrl.replace(oldNodeTitle,newNodeTitle); //New url for mapping opener window
    else // no node name is changed.
        mappedUrl = window.opener.location.href;
}

/***********************************************************************************************/
// Created By : Binjan Iyer
// Created On : 1.4.2009
// Purpose    : To calculate the diff based on interval
function jWDCalculateDateDifference(fromDateDD,fromDateMM,fromDateYY, fromDateHour, fromDateMin, toDateDD,toDateMM,toDateYY, toDateHour, toDateMin, interval)
{   
    //var interval = "d";
 
	var oneMinute=1000*60;
	
	//difference of date based on interval parameter
	var intervalObject=new Object();
	intervalObject["yyyy"]={units:1000*60*60*24*365,measure:"year"};
	intervalObject["m"]={units:1000*60*60*24*30,measure:"month"};
	intervalObject["d"]={units:1000*60*60*24,measure:"day"};
	intervalObject["Q"]={units:intervalObject["m"].units*3,measure:"quarter"};
	intervalObject["H"]={units:oneMinute*60,measure:"hour"};
	intervalObject["N"]={units:oneMinute,measure:"minute"};
	intervalObject["S"]={units:1000,measure:"second"};  
    
 //get date object for FROM DATE
    var date1temp = new Date(parseInt(fromDateYY, 10), parseInt(fromDateMM, 10) - 1, parseInt(fromDateDD, 10));
    date1temp.setHours(fromDateHour);
    date1temp.setMinutes(fromDateMin);
    date1temp.setSeconds(0);
    var date1 =date1temp.getTime();

    //get date object for TO DATE
    var date2temp = new Date(parseInt(toDateYY, 10), parseInt(toDateMM, 10) - 1, parseInt(toDateDD, 10));
    date2temp.setHours(toDateHour);
    date2temp.setMinutes(toDateMin);
    date2temp.setSeconds(0);
    var date2= date2temp.getTime();
    
    //check which date is greater
    if(date2>date1)
    {
		DSTAdjust=(date2temp.getTimezoneOffset() - date1temp.getTimezoneOffset()) * oneMinute;	
	}
	else
	{
		DSTAdjust=(date1temp.getTimezoneOffset() - date2temp.getTimezoneOffset()) * oneMinute;
	}
	
	//get the diff
	var diff=Math.abs(date2-date1) - DSTAdjust;
	//get the time diff based on interval
	var timeDiff=Math.floor(diff/intervalObject[interval].units);
	
	//Get the string for given interval
	//if(timeDiff>1)
	//{
	//	var rname=intervalObject[interval].measure + "s";
	//}
	//else
	//{
	//	var rname=intervalObject[interval].measure;
	//}
	
	var difference=parseInt(timeDiff);
    
    return difference;
}
