﻿if (typeof Brand2Commerce == "undefined")
{
	/**
	* The Brand2Commerce global namespace object.  
	* If Brand2Commerce is already defined, the
	* existing Brand2Commerce object will not be overwritten 
	* so that defined namespaces are preserved.
	* @class Brand2Commerce
	* @static
	*/
	var Brand2Commerce = {};
}

Brand2Commerce.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 B2C_browser = new Brand2Commerce.Bowser();
var B2C_Form = document.forms[0];

var B2C_Debug = null;

Brand2Commerce.GetKeyCode = function(e)
{
	var keycode;

	if (window.event)
	{
		keycode = window.event.keyCode;
	}
	else if (e)
	{
		keycode = e.which;
	}

	return keycode;
}

Brand2Commerce.GetEvent = function(e)
{
	var evt = window.event ? window.event : e;
	return evt;
}

Brand2Commerce.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
//Brand2Commerce.screenSize = [top.screen.width, top.screen.height];
if (Brand2Commerce.Bowser.IE)
{
	Brand2Commerce.screenSize = [document.body.clientWidth, document.body.clientHeight];
}
else
{
	Brand2Commerce.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 B2C_Onload()
{
}
/**
* author: sherwin
* This function is called on page load.
* All unload functions should be called from here
*/
function B2C_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;
	}
}

/*
good: 10
bad: 10.00, 10., 10.1
*/
function isInteger(val)
{
	if (trim(val) != "" && trim(val) != "undefined")
	{
		var regex = /^\d{1,}$/;
		if (regex.test(val))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	else
	{
		return false;
	}
} //end of isInteger


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;
	}
}

/*
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";
}

//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;
	}
}


/**
* finds the absolute position of an element
* param: elem -- HTML element to be analysed
* return: array[leftPosition, topPosition]
*/
function B2C_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];
} //end of B2C_FindElementPosition


/*
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 B2C_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 B2C_CountCharUsage


/*
* submits a form on keypress
* for use with linkbuttons
*/
function B2C_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 B2C_SubmitOnEnterLB


//called by onmouseover/onmouseout event on table product list rows
//to highlight row
function B2C_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 B2C_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 B2C_CheckAll


/**
* stop event bubling
*/
function stopPropagation(e) 
{ 
    e = e || event;/* get IE event ( not passed ) */ 
    e.stopPropagation ? e.stopPropagation() : e.cancelBubble = true;
}



/**
*
*/
function B2C_RedirectCarAudio()
{
	if (top.location == self.location)
	{
		top.location.href = "http://www2.wiremagic.com/fitguide/vision/";
	}
}

/**
*
*/
function B2C_ReloadTopFrame(url)
{
//	    alert("url: " + url + "\nurl.indexOf(\"~/\"): " + url.indexOf("~/"));

	if (url.indexOf("~/") == 0)
	{
		url = url.replace("~/", B2C_Host_R);
	}
//	alert("url: " + url);

	if (top.location != self.location)
	{
		top.location.href = url;
	}
	else
	{
		top.location.href = url;
	}
} //end of B2C_ReloadTopFrame



function ValidateCustomerListBox(sender, args)
{
	if (trim(args.Value) == "" || trim(args.Value) == undefined)
	{
		args.IsValid = false;
		return;
	}
	args.IsValid = true;
} //end of ValidateCustomerListBox

function ValidateDropDownListBox(sender, args)
{
	if (trim(args.Value) == "" || trim(args.Value) == undefined)
	{
		args.IsValid = false;
		return;
	}
	args.IsValid = true;
} //end of ValidateDropDownListBox

/**
* Bookmark This Page Javascript
* 
* script by http://www.hypergurl.com 
*/
function B2C_AddToFavorites(title, url)
{
	if (title == null || title == "")
	{
		title = window.document.title;
	}

	if (url == null || url == "")
	{
		url = window.document.URL;
	}
	
	if (window.sidebar)
	{	// Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url, "");
	}
	else if (window.external)
	{	// IE Favorite
		window.external.AddFavorite(url, title);
	}
	else if (window.opera && window.print)
	{	// Opera Hotlist
		var elem = document.createElement('a');
		elem.setAttribute('href', url);
		elem.setAttribute('title', title);
		elem.setAttribute('rel', 'sidebar');
		elem.click();
	}
} //end of B2C_AddToFavorites

/**
*
*/
function detailGlossaryWindow(w, h)
{
	var leftPoint = (top.screen.width - w) / 2;
	var topPoint = (top.screen.height - h) / 2;

	window.open(B2C_Host_R + 'FeatureGlossary/FeatureGlossary.aspx', 'detail', 'toolbar=no,location=no,directories=no,status=no,menubar=0,scrollbars=yes,left=' + leftPoint + ',top=' + topPoint + ',resizable=yes,width=' + w + ',height=' + h + '')
} //end of detailGlossaryWindow



/**
*	
*	start of webbtoolkit code http://www.webtoolkit.info/
*   Javascript trim, ltrim, rtrim
*   http://www.webtoolkit.info/
*
**/

//function trim(str, chars)
//{
//	if (str == "") return str;
//	return ltrim(rtrim(str, chars), chars);
//}

function ltrim(str, chars)
{
	chars = chars || "\\s";
	return str.toString().replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars)
{
	chars = chars || "\\s";
	return str.toString().replace(new RegExp("[" + chars + "]+$", "g"), "");
}
/*
* end of http://www.webtoolkit.info/ trim()
*/

/**
*
*  Javascript string replace
*  http://www.webtoolkit.info/
*
**/
// standart string replace functionality
function str_replace(haystack, needle, replacement)
{
	var temp = haystack.split(needle);
	return temp.join(replacement);
}

// needle may be a regular expression
function str_replace_reg(haystack, needle, replacement)
{
	var r = new RegExp(needle, 'g');
	return haystack.replace(r, replacement);
}
/*
* end of http://www.webtoolkit.info/ str_replace()
*/


/**
*
*  URL encode / decode
*  http://www.webtoolkit.info/
*
**/
var webtoolkit_Url = {

	// public method for url encoding
	encode: function(string)
	{
		return escape(this._utf8_encode(string));
	},

	// public method for url decoding
	decode: function(string)
	{
		return this._utf8_decode(unescape(string));
	},

	// private method for UTF-8 encoding
	_utf8_encode: function(string)
	{
		string = string.replace(/\r\n/g, "\n");
		var utftext = "";

		for (var n = 0; n < string.length; n++)
		{

			var c = string.charCodeAt(n);

			if (c < 128)
			{
				utftext += String.fromCharCode(c);
			}
			else if ((c > 127) && (c < 2048))
			{
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else
			{
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}

		}

		return utftext;
	},

	// private method for UTF-8 decoding
	_utf8_decode: function(utftext)
	{
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;

		while (i < utftext.length)
		{

			c = utftext.charCodeAt(i);

			if (c < 128)
			{
				string += String.fromCharCode(c);
				i++;
			}
			else if ((c > 191) && (c < 224))
			{
				c2 = utftext.charCodeAt(i + 1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else
			{
				c2 = utftext.charCodeAt(i + 1);
				c3 = utftext.charCodeAt(i + 2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}

		}

		return string;
	}

}
/*
* end of http://www.webtoolkit.info/ webtoolkit_Url()
*/

/**
*
*  Javascript string pad
*  http://www.webtoolkit.info/
*
**/
var STR_PAD_LEFT = 1;
var STR_PAD_RIGHT = 2;
var STR_PAD_BOTH = 3;

function pad(str, len, pad, dir)
{

	if (typeof (len) == "undefined") { var len = 0; }
	if (typeof (pad) == "undefined") { var pad = ' '; }
	if (typeof (dir) == "undefined") { var dir = STR_PAD_RIGHT; }

	if (len + 1 >= str.length)
	{
		switch (dir)
		{

			case STR_PAD_LEFT:
				str = Array(len + 1 - str.length).join(pad) + str;
				break;

			case STR_PAD_BOTH:
				var right = Math.ceil((padlen = len - str.length) / 2);
				var left = padlen - right;
				str = Array(left + 1).join(pad) + str + Array(right + 1).join(pad);
				break;

			default:
				str = str + Array(len + 1 - str.length).join(pad);
				break;

		} // switch
	}

	return str;
}
/*
* end of http://www.webtoolkit.info/ pad()
*/

/**
	end of webbtoolkit code http://www.webtoolkit.info/
*/

function checkQty(textbox) {
    if (!isInteger(textbox.value) && !IsEmptyField(textbox.value)) {
        textbox.value = '1';
        alert('Invalid Quantity');
    }
}

function addToCart(productId) {
    var qty = 1;
    var elms = document.getElementsByTagName('input');
    for (var i = 0; i < elms.length; i++) {
        if (elms[i].type == 'text') {
            if (elms[i].className == 'class' + productId) {
                qty = elms[i].value;
                break;
            }
        }
    }

    document.getElementById("txtQty").value = qty;
    B2C_SubmitToCart('txtQty', productId);
}   
