﻿if (typeof Brand2Media == "undefined") 
{
    /**
     * The Brand2Media global namespace object.  If Brand2Media is already defined, the
     * existing Brand2Media object will not be overwritten so that defined
     * namespaces are preserved.
     * @class Brand2Media
     * @static
     */
    var Brand2Media = {};
}

Brand2Media.Browser = function()
{
    this.Opera = true;
    this.IE = false;
    this.IE6 = false;
    this.Mozilla = false;
    this.Safari = false;
    
    this.IE = (document.all && document.getElementById) ? true : false;
    this.Mozilla = (!document.all && document.getElementById) ? true : false;
    if (!document.all && document.getElementById && navigator.appName == "Opera")
    {
        this.Opera = true;
        this.IE = false;
        this.Mozilla = false;
        this.Safari = false;
    }
    if (!document.all && document.getElementById && navigator.appName == "Safari")
    {
        this.Opera = false;
        this.IE = false;
        this.Mozilla = false;
        this.Safari = true;
    }
    this.major = parseInt(navigator.appVersion);
    this.minor = parseFloat(navigator.appVersion);
    this.userAgent = navigator.userAgent.toLowerCase();
    this.appVer = navigator.appVersion.toLowerCase();
    this.iePos = this.appVer.indexOf('msie');
    if (this.iePos !=-1) 
    {
        this.minor = parseFloat(this.appVer.substring(this.iePos + 5, this.appVer.indexOf(';', this.iePos)))
        this.major = parseInt(this.minor);
    }
    this.IE6 = (this.IE && this.major == 6);
}

var Brand2MediaBrowser = new Brand2Media.Browser();

//alert("Brand2MediaBrowser.IE6: " + Brand2MediaBrowser.IE6
//    + "\n\Brand2MediaBrowser.IE: " + Brand2MediaBrowser.IE);

function detailWindow(url, w, h, t, l) 
{
    if (t == null)
    {
	    var leftPoint = (top.screen.width - w) / 2;
	    var topPoint = (top.screen.height - h) / 2;
	}
	else
	{
	    leftPoint = l;
	    topPoint = t;
	}

	var winDetails = window.open(url, 'detail', 'toolbar=no,location=no,directories=no,status=no,menubar=0,scrollbars=yes,left=' + leftPoint + ',top=' + topPoint + ',resizable=yes,width=' + w + ',height=' + h + '')
	
	if (winDetails != null)
	{
		if (winDetails.opener == null)
			winDetails.opener = self;
		else
			winDetails.focus();
	}
}

function detailWindowR(url, w, h, t, l) 
{
    if (t == null)
    {
	    var leftPoint = (top.screen.width - w) / 2;
	    var topPoint = (top.screen.height - h) / 2;
	}
	else
	{
	    leftPoint = l;
	    topPoint = t;
	}

	var winDetails = window.open(url, 'detail', 'toolbar=no,location=no,directories=no,status=no,menubar=0,scrollbars=yes,left=' + leftPoint + ',top=' + topPoint + ',resizable=yes,width=' + w + ',height=' + h + '')
	
	if (winDetails != null)
	{
		if (winDetails.opener == null)
			winDetails.opener = self;
		else
			winDetails.focus();
	}
}
function detailWindowNS(url, w, h, t, l) 
{
    if (t == null)
    {
	    var leftPoint = (top.screen.width - w) / 2;
	    var topPoint = (top.screen.height - h) / 2;
	}
	else
	{
	    leftPoint = l;
	    topPoint = t;
	}

	var winDetails = window.open(url, 'detail', 'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,left=' + leftPoint + ',top=' + topPoint + ',resizable=0,width=' + w + ',height=' + h + '')
	
	if (winDetails != null)
	{
		if (winDetails.opener == null)
			winDetails.opener = self;
		else
			winDetails.focus();
	}
}
function detailWindowNR(url, w, h, t, l) 
{
    if (t == null)
    {
	    var leftPoint = (top.screen.width - w) / 2;
	    var topPoint = (top.screen.height - h) / 2;
	}
	else
	{
	    leftPoint = l;
	    topPoint = t;
	}

	var winDetails = window.open(url, 'detail', 'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,left=' + leftPoint + ',top=' + topPoint + ',resizable=0,width=' + w + ',height=' + h + '')
	
	if (winDetails != null)
	{
		if (winDetails.opener == null)
			winDetails.opener = self;
		else
			winDetails.focus();
	}
}

//trims the leading & ending white spaces of a text
function trim( val )
{   
    // return if string is empty
    if ( null == val || val.length < 1) return val;

    var value = val;
    var len = value.length;
    if (len == undefined) return;
    
    while (value.charAt(value.length-1) == " "){value = value.substring(0,value.length-1);}
    while(value.substring(0,1) ==" "){value = value.substring(1,value.length);}

    return value;
}


/*
good: 10
bad: 10.00, 10., 10.1
*/
function isNumber(val)
{
    if (trim(val) != "" && trim(val) != "undefined")
    {
        var regex = /^\d{1,}$/;
        if (regex.test(val))
        {
            return true;
        }
        else    
        {
            return false;
        }
    }
    else    
    {
        return false;
    }
}

/*
toggle the display/visibility of select boxes
*/
function toggleSelectBoxesDisplay(mode)
{
    var select_boxes = document.getElementsByTagName("SELECT");
    var itm_to_toggle;
    var display = (mode == "hide") ? "none" : "block";
    
    for (i = 0; i < select_boxes.length; i++)
    {
        itm_to_toggle = select_boxes[i];
        itm_to_toggle.style.display = display;
    }
}
function toggleSelectBoxesVisibility(mode)
{
    var select_boxes = document.getElementsByTagName("SELECT");
    var itm_to_toggle;
    var visibility = (mode == "hide") ? "hidden" : "visible";
    
    for (i = 0; i < select_boxes.length; i++)
    {
        itm_to_toggle = select_boxes[i];
        itm_to_toggle.style.visibility = visibility;
    }

}

/* check if a form element exist 
    @param object -- form
    @param string -- element name
    @return boolean
*/
function formElementExist(form, elem_name)
{
    var flag = false;
    for(i = 0; i < form.length; i++)
    {
        if (form.elements[i].name == elem_name)
        {
            flag = true;
            break;
        }
    }
    return flag;
}
       
/** 
* this function toggles a html element display
* @param String -- elementId
*/
function toggleElementDisplay(elementId)
{
    var element = document.getElementById(elementId);
    if (element.style.display == "") element.style.display = "none";
    element.style.display = (element.style.display == "none") ? "block" : "none";
} 

/**
* this function toggles a html element display
* @param String -- elementId
*/
function toggleElementDisplayInline(elementId)
{
    var element = document.getElementById(elementId);
    element.style.display = (element.style.display == "none") ? "inline" : "none";
}
       
/** 
* this function displays a hidden a html element
* @param String -- elementId
*/
function displayElement(elementId)
{
    if (document.getElementById(elementId))
    {
        document.getElementById(elementId).style.display = "block";
    }
} 
       
/** 
* this function hides a display a html element
* @param String -- elementId
*/
function hideElement(elementId)
{
    if (document.getElementById(elementId))
    {
        document.getElementById(elementId).style.display = "none";
    }
} 

//called by onmouseover/onmouseout event on table product list rows
//to highlight row
function highlightRow(rowId, mode)
{
    if ( document.getElementById(rowId) )
    {
        var row = document.getElementById(rowId);
        if (mode == "over")
        {            
            row.style.backgroundColor = "#cfffe8";
        }
        else
        {
            row.style.backgroundColor = "";
        }
    }
}//end of highlightRow

function highlightRowALL(rowId, mode)
{    
    if ( document.getElementById(rowId) )
    {
        var row = document.getElementById(rowId);
        var cells = row.getElementsByTagName("TD");
        var backgroundColor = "";
        if (mode == "over")
        {            
            backgroundColor = "#cfffe8";
        }
        for (i = 0; i < cells.length; i++)
        {
            cells[i].style.backgroundColor = backgroundColor;
        }
    }
}//end of highlightRow

function highlightRowALL(row, mode)
{    
    if ( row != null )
    {
        var cells = row.getElementsByTagName("TD");
        var backgroundColor = "";
        if (mode == "over")
        {            
            backgroundColor = "#cfffe8";
        }
        for (i = 0; i < cells.length; i++)
        {
            cells[i].style.backgroundColor = backgroundColor;
        }
    }
}//end of highlightRow

//this function changes an image src
//@param image
//@param string -- src
function changeImageSrc(image, src)
{
    image.src = src;
}


/**
* this function check if a check box is checked
* @param String -- elementId
* @return boolean
*/
function isCheckboxChecked(elem)
{
    var flag = false;
    if (elem && elem.type.toLowerCase() == "checkbox")
    {
        flag = elem.checked ? true : false;
    }
    return flag;
}


/**
* This function checks if a field is empty
* @param String -- field to check
* @return boolean
*/
function IsEmptyField(strField)
{
    if(strField == "")
        return true;
    else
        return false;
}

/**
* This function checks if a value is integer
* @param String -- val: value to check
* @return boolean
*/
function IsIntegerValue(val)
{
    var regex = /^\d+$/;
    
    if (regex.test(val))
        return true;
    else        
        return false;
}//end of IsIntegerValue

/**
* This function checks if a value is float
* @param String -- val: value to check
* @return boolean
*/
function IsFloatValue(val)
{
    var regex = /^((\d+(\.\d*)?)|((\d*\.)?\d+))$/;
    
    if (regex.test(val))
        return true;
    else        
        return false;
}//end of IsFloatValue


/**
* test if a variable is an array
*/
function isArray(obj) 
{
    if (typeof arguments[0] == 'object') 
    {  
        var criterion = arguments[0].constructor.toString().match(/array/i); 
        return (criterion != null);  
    }
    return false;
}

/*
* check if an email is valid
*/
function IsValidEmail(email)
{
    var regex = /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
    
    if (regex.test(email))
        return true;
    else        
        return false;    
}


/*
* to handle an exit from the page,
* by clicking the back button or closing the browser
*/
function handleOnClose(e)
{
    var msg = "By exiting this page the transaction may be interrupted or terminated";
    if (!confirm(msg))
    {
        return false;
    }
}


        
function findElementPosition(elem) 
{
	var left = 0;
	var top = 0;
	
	if (elem.offsetParent) 
	{
		left = elem.offsetLeft;
		top = elem.offsetTop;
//		alert("elem.offsetLeft: " + elem.offsetLeft + "\nelem.offsetTop: " + elem.offsetTop);
		while (elem = elem.offsetParent) 
		{
			left += elem.offsetLeft
			top += elem.offsetTop
		}
	}
	top = (document.all && document.getElementById) ? (top + 2) : top;
	left = (document.all && document.getElementById) ? (left + 2) : left;
	
	return [left, top];
}


/*
toggle the display/visibility of select boxes
*/
function toggleSelectBoxesDisplay(mode)
{
    var select_boxes = document.getElementsByTagName("SELECT");
    var itm_to_toggle;
    var display = (mode == "hide") ? "none" : "block";
    
    for (i = 0; i < select_boxes.length; i++)
    {
        itm_to_toggle = select_boxes[i];
        itm_to_toggle.style.display = display;
    }
}//end of toggleSelectBoxesDisplay
function toggleSelectBoxesVisibility(mode)
{
    var select_boxes = document.getElementsByTagName("SELECT");
    var itm_to_toggle;
    var visibility = (mode == "hide") ? "hidden" : "visible";
    
    for (i = 0; i < select_boxes.length; i++)
    {
        itm_to_toggle = select_boxes[i];
        itm_to_toggle.style.visibility = visibility;
    }
}//end of toggleSelectBoxesVisibility

/*
*
*/
function countCharUsage(e, textboxId, labelId, maxCount)
{
    var charCount = 0;
    var comments = document.getElementById(textboxId);
    var charactersUsed = document.getElementById(labelId);
    
    if (!IsIntegerValue(maxCount)) maxCount = 1;
    
    if (!comments || !charactersUsed) return;
    
    charCount = comments.value.length;
    if ( charCount == "" ) charCount = 0;
    //alert(charCount);
    var charCountBalance = parseInt(maxCount) - parseInt(charCount);
    if ( parseInt(charCount) > parseInt(maxCount))
    {
        comments.value = (comments.value).substr(0, maxCount);
    }
    else
    {
        charactersUsed.innerHTML = charCountBalance;
    }
} //end of orderCommentCountChar

/*
* submits a form on keypress
* for use with linkbuttons
*/
function submitOnEnterLB(event, linkButtonId)
{
    if (event.which || event.keyCode)
    {
        if ((event.which == 13) || (event.keyCode == 13))
        {
            if (document.getElementById(linkButtonId))
            {
                location = document.getElementById(linkButtonId).href;
                return false;
            }
        }
        return true;
    }
} //end of submitOnEnterLB