﻿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.Bowser = function()
{
	this.IE = (document.all && document.getElementById) ? true : false;
	this.Mozilla = (!document.all && document.getElementById) ? true : false;
	this.Opera = (!document.all && document.getElementById && navigator.appName == "Opera") ? true : false;
	this.major = parseInt(navigator.appVersion);
	this.minor = parseFloat(navigator.appVersion);
	this.userAgent = navigator.userAgent.toLowerCase();
}

var B2M_browser = new Brand2Media.Bowser();
var B2M_Form = document.forms[0];

var B2M_Debug = null;

Brand2Media.GetKeyCode = function(e)
{
	var keycode;

	if (window.event)
	{
		keycode = window.event.keyCode;
	}
	else if (e)
	{
		keycode = e.which;
	}

	return keycode;
}

Brand2Media.GetEvent = function(e)
{
	var evt = window.event ? window.event : e;
	return evt;
}

Brand2Media.StopEvent = function(e)
{
	if (window.event)
	{
		window.event.cancelBubble = true;
	}
	else if (e && e.preventDefault && e.stopPropagation)
	{
		e.stopPropagation()
		e.preventDefault()
	}
}

//set gobal screen size
//Brand2Media.screenSize = [top.screen.width, top.screen.height];
if (Brand2Media.Bowser.IE)
{
	Brand2Media.screenSize = [document.body.clientWidth, document.body.clientHeight];
}
else
{
	Brand2Media.screenSize = [document.documentElement.clientWidth, document.documentElement.clientHeight];
}

/**
* author: sherwin
* This function is called on page load.
* All onload functions should be called from here
*/
function B2M_Onload()
{
}
/**
* author: sherwin
* This function is called on page load.
* All unload functions should be called from here
*/
function B2M_Unload()
{
}




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;
}

function isUrl(val)
{
	if (trim(val) != "" && trim(val) != "undefined")
	{
		var regex = /(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
		if (regex.test(val))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	else
	{
		return false;
	}
}

/*
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;
	}
}


function isDecimalNumber(val)
{
	if (trim(val) != "" && trim(val) != "undefined")
	{
		var regex = /^\d*[0-9](|.\d*[0-9])?$/;
		if (regex.test(val))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	else
	{
		return false;
	}
}


function isCurrency(val)
{
	if (trim(val) != "" && trim(val) != "undefined")
	{
		var regex = /^\d*\.?\d{1,2}$/;
		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 B2M_ToggleElementDisplay(elementId)
{
	var element = document.getElementById(elementId);
	if (element.style.display == "") element.style.display = "none";
	//	element.style.display = (element.style.display == "none") ? "block" : "none";
	if (element.style.display == "none")
	{
		$(element).slideDown('normal');
	}
	else
	{
		$(element).slideUp('normal');
	}
}

/**
* this function toggles a html element display
* @param String -- elementId
*/
function B2M_ToggleElementDisplayInline(elementId)
{
	var element = document.getElementById(elementId);
	element.style.display = (element.style.display == "none") ? "inline" : "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 highlightRowALL2(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 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;
}


/**
* 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

//isMatch
function isMatch(id, pattern)
{
	var regularExpresssion = new RegExp(pattern);

	if (id.match(regularExpresssion))
		return true;
	else
		return false;
} //isMatch

//isEmptyField
function isEmptyField(strField)
{
	if (trim(strField) == "")
		return true;
	else
		return false;
} //isEmptyField


/*
*
*/
function countCharUsage(e, textboxId, labelId, maxCount)
{
	var charCount = 0;
	var comments = document.getElementById(textboxId);
	var charactersUsed = document.getElementById(labelId);

	if (!isNumber(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

//called by onmouseover/onmouseout event on table product list rows
//to highlight row
function B2M_HighlightRow(row, mode)
{
	if (row)
	{
		var cells = row.getElementsByTagName("TD");
		var backgroundColor = "";
//		var borderBottom = "solid 1px #ccc"
		if (mode == "over")
		{
			backgroundColor = "#cfffe8";
//			borderBottom = "dashed 1px #f00";
		}
		for (i = 0; i < cells.length; i++)
		{
			cells[i].style.backgroundColor = backgroundColor;
//			cells[i].style.borderBottom = borderBottom;
		}
	}
} //end of highlightRow

/**
*
*/
function B2M_CheckAll(form)
{
	var form = document.forms[0];
	if (chkAll)
	{
		for (i = 0; i < form.elements.length - 1; i++)
		{
			if (form.elements[i].type == "checkbox")
			{
				form.elements[i].checked = chkAll.checked;
			}
		}
	}
} //end of B2M_CheckAll


/*
* switch language inputs
*/
function B2M_SwitchInputLanguage(elementID, lang)
{
	if ($("#" + elementID) == null)
	{
	return;
	}
		
	if (lang == "en")
	{
		$(".textboxEn").show();
		$(".textboxFr").hide();
		$("#" + elementID).attr("href", "javascript:B2M_SwitchInputLanguage('" + elementID + "', 'fr');")
		$("#" + elementID).html("<span>French</span>");
	}
	else
	{
		$(".textboxEn").hide();
		$(".textboxFr").show();
		$("#" + elementID).attr("href", "javascript:B2M_SwitchInputLanguage('" + elementID + "', 'en');")
		$("#" + elementID).html("<span>English</span>");
	}
}



/*
*
*/
function B2M_CountCharUsage(e, textboxId, labelId, maxCount)
{
	var charCount = 0;
	var textbox = document.getElementById(textboxId);
	var label = document.getElementById(labelId);

	if (!B2M_IsIntegerValue(maxCount)) maxCount = 1;

	if (!textbox || !label) return;

	charCount = textbox.value.length;
	if (charCount == "") charCount = 0;
	//alert(charCount);
	var charCountBalance = parseInt(maxCount) - parseInt(charCount);
	if (parseInt(charCount) > parseInt(maxCount))
	{
		textbox.value = (textbox.value).substr(0, maxCount);
	}
	else
	{
		label.innerHTML = charCountBalance;
	}
} //end of B2M_CountCharUsage



/*
	validation function
*/


/**
* this function check if a check box is checked
* @param String -- elementId
* @return boolean
*/
function B2M_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 B2M_IsEmptyField(strField)
{
	if (strField == "")
		return true;
	else
		return false;
}

/**
* This function checks if a value is digit
* @param String -- val: value to check
* @return boolean
*/
function B2M_IsDigitValue(val)
{
	var regex = /^\d{1}$/;

	if (regex.test(val))
		return true;
	else
		return false;
} //end of B2M_IsDigitValue

/**
* This function checks if a value is integer
* @param String -- val: value to check
* @return boolean
*/
function B2M_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 B2M_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 B2M_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 B2M_IsValidEmail(email)
{
	var regex = /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;

	if (regex.test(email))
		return true;
	else
		return false;
}

/*
	end of input validators
*/


/**
*	performs a mask edit on textbox 
*	mask (##########) -- digits only
*/
function B2M_DigitMaskEdit(textBox, e)
{
	if (textBox == null || textBox.value.length < 1)
	{
		return;
	}

	var flag = true;	
	var str = textBox.value;
	var len = str.length;
	var aChar = str.charAt(len - 1);
	var keyCode;

	if (document.all)
	{
		keyCode = event.keyCode;
	}
	else
	{
		keyCode = e.which;
	}

	if (keyCode == 8)
	{
		return flag;
	}

	if (!B2M_IsDigitValue(aChar))
	{
		textBox.value = str.substr(0, len - 1);
		flag = false;
	}
	textBox.focus();

	return flag;
} //end of B2M_DigitMaskEdit

/**
*	performs a mask edit on textbox 
*	mask (###-###-####)
*/
function B2M_PhoneNumberMaskEdit(textBox, e)
{
	if (textBox == null || textBox.value.length < 1)
	{
		return;
	}

	var str = textBox.value;
	var len = str.length;
	if (B2M_DigitMaskEdit(textBox, e))
	{
		if ((len == 3) || (len == 7))
		{
			textBox.value = str + "-";
		}
	}
	else
	{
		textBox.value = str.substr(0, len - 1);
	}
	textBox.focus();
} //end of B2M_PhoneNumberMaskEdit




function B2M_ConfirmSubmit()
{
	var msg = "If all inputs are correct \n\nclick \"OK\" to continue \n\nor \n\n click \"Cancel\" to verify your work.";
	if (confirm(msg))
	{
		return true;
	}

	return false;
} //end of B2M_ConfirmSubmit

function B2M_ConfirmSubmitPublish()
{
	var msg = "This action will publish the item to your production Web Site.\n\n"
	msg += "If all inputs are correct \n\nclick \"OK\" to continue \n\nor \n\n click \"Cancel\" to verify your work.";
	if (confirm(msg))
	{
		return true;
	}

	return false;
} //end of B2M_ConfirmSubmitPublish


/**
* This function requests confirmation before deleting an item
* from a grid (table)
* it set the text color to red while confirmatio box is displayed
* @param String -- Id: row or cell id
* @param String -- val: value to display
*/
function B2M_Grid_ConfirmDelete(Id, val)
{
	var elem = document.getElementById(Id);
	if (elem)
	{
		elem.style.color = "#ff0000";
	}

	if (confirm("Do you want to Delete item:\n\n" + val))
	{
		if (elem)
		{
			elem.style.color = "#000000";
		}
		return true;
	}
	else
	{
		if (elem)
		{
			elem.style.color = "#000000";
		}
		return false;
	}
} //end of B2M_Grid_ConfirmDelete
