/*******************************************************
 * 
 * Miscellaneous Functions/Prototypes
 * 
 ******************************************************/

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}

String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

function nl2br(input) {
	var regX = new RegExp("\\n", "g");
	var replaceString = "<br />\n";
	return input.replace(regX, replaceString);
}

function stripCarriageReturns(input) {
	var regX = new RegExp("\\r", "g");
	var replaceString = "";
	return input.replace(regX, replaceString);
}

function strReplaceAll(input, findWhat, replaceWith) {
	output = input;
	intIndexOfMatch = output.indexOf(findWhat);
	while (intIndexOfMatch != -1){
		output = output.replace(findWhat, replaceWith);
		intIndexOfMatch = output.indexOf(findWhat);
	}
	return output;
}

function checkEnter(event) {
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	if ((keyCode == 13)) {
		return true;
	}
	else {
		return false;
	}
}

function getHandle(what) {
	var handle;
	if (typeof(what) == 'string') {
		handle = document.getElementById(what);
	}
	else {
		handle = what;
	}
	return handle;
}

function show(what, displayMode) {
	if (typeof(displayMode) == 'undefined') displayMode = 'block';
	getHandle(what).style.display = displayMode;
}

function showBlock(what) {
	show(what);
}

function showInline(what) {
	show(what, 'inline');
}

function hide(what) {
	getHandle(what).style.display = 'none';
}

function toggle(what, displayMode) {
	if (typeof(displayMode) == 'undefined') displayMode = 'block';
	if (document.getElementById(what).style.display == 'none') {
		show(what, displayMode);
	}
	else {
		hide(what);
	}
}

function toggleBlock(what) {
	toggle(what, 'block');
}

function toggleInline(what) {
	toggle(what, 'inline');
}

function clearSelectList(id) {
	selectList = getHandle(id);
	while (selectList.length > 0) {
		selectList.options[selectList.length - 1] = null;
	}
}

function addToSelectList(id, text, value) {
	selectList = getHandle(id);
	selectList.options[selectList.length] = new Option(text, value);
}

function setOpacity(obj, opacity) {
	//window.status = 'obj = ' + obj + ', opacity = ' + opacity;
	opacity = (opacity == 100)?99.999:opacity;
	// IE/Win
	obj.style.filter = "alpha(opacity:"+opacity+")";
	// Safari<1.2, Konqueror
	obj.style.KHTMLOpacity = opacity/100;
	// Older Mozilla and Firefox
	obj.style.MozOpacity = opacity/100;
	// Safari 1.2, newer Firefox and Mozilla, CSS3
	obj.style.opacity = opacity/100;
}

function findPos(obj) {
	var left = !!obj.offsetLeft ? obj.offsetLeft : 0;
	var top = !!obj.offsetTop ? obj.offsetTop : 0;
	while(obj = obj.offsetParent) {
		left += !!obj.offsetLeft ? obj.offsetLeft : 0;
		top += !!obj.offsetTop ? obj.offsetTop : 0;
	}
	return{x:left, y:top};
}



/*******************************************************
 * 
 * AJAX Basic Functions
 * 
 ******************************************************/

var ajaxIsBusy = false;

function getAjax() {
	var xmlHttp = false;
	try {
		//Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
	}
	catch (e) {
		//Internet Explorer
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {
				
			}
		}
	}
	return xmlHttp;
}

function ajaxObject(url, callback) {
	if (typeof postOrGet == "undefined") {
		postOrGet = "GET";
	}
	var that = this;
		//workaround for some javascript idiosyncrocies
	var updating = false;
		//set to true if this object is already working on a request
	
	this.update = function(params) {
		//calling object.update(params) initiates the server call
		if (updating == true) { return false; }
			//abort if we're already processing a call
		updating = true;
			//set the updating flag
		var AJAX = getAjax();
			//get an xmlHttpRequest object
		if (!AJAX) {
			alert("Your browser does not support AJAX. Please upgrade your browser");
			return false;
		}
		else {
			AJAX.onreadystatechange = function() {
				//when the browser has the request info..
				if (AJAX.readyState == 4 || AJAX.readyState == "complete") {
					//if the complete flag is set...
					updating = false;
						//set the updating flag to false so we can do a new request
					that.callback(AJAX.responseText,AJAX.status,AJAX.responseXML);
						//that.callback();
						//call the post-processing function
					delete AJAX;
						//delete the AJAX object since it's done
				}
				//end Ajax readystate check
			}                                                           	
			//end create post-process fucntion block
			var timestamp = new Date();
				//get a new date (this will make the url unique)
			var uri = encodeURI(urlCall + '?nocache=' + (timestamp * 1));
				//append date to url (so the browser doesn't cache the call)
			if (postOrGet == "GET") {
				uri = uri + "&" + params;
				AJAX.open("GET", uri, true);
				AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      			AJAX.setRequestHeader("Content-length", uri.length);
      			AJAX.setRequestHeader("Connection", "close");
				AJAX.send(null);
			}
			else {
				AJAX.open("POST", uri, true);
				//open the url this object was set-up with
				AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      			AJAX.setRequestHeader("Content-length", uri.length);
      			AJAX.setRequestHeader("Connection", "close");
	      		//set some headers
				AJAX.send(params);
				//send the request
			}
			return true;
				//everything went well
		}
	}
		//do something with constructor parameters
	var urlCall = url;
	this.callback = callback || function() {}
		//post-processing call
}

function evalJavascript(scanThis) {
	var jsSpans = new Array();
	for (var i = 0; i < scanThis.length; i++) {
		if ((scanThis.substr(i, 7) == '<script') && (scanThis.indexOf('<' + '/script>', i) > -1)) {
			jsSpans[jsSpans.length] = scanThis.substring((scanThis.indexOf(">", i) + 1), (scanThis.indexOf("<" + "/script>", i)));
			i = scanThis.indexOf("<" + "/script>", i) + 8;
		}
	}
	if (jsSpans.length > 0) {
		for (var i = 0; i < jsSpans.length; i++) {
			//alert("evaluating: \n\n" + jsSpans[i]);
			eval(jsSpans[i]);
		}
	}
}

function isAjaxBusy() {
	if (ajaxIsBusy == false) {
		return false;
	}
	else {
		return true;
	}
}

/*******************************************************
 * 
 * Form Validation Functions
 * 
 ******************************************************/

/*
Portions of isRFC822ValidEmail() function are copyright (C) 2006  Ross Kendall - http://rosskendall.com
Portions of isRFC822ValidEmail() function are copyright (C) 1993-2005 Cal Henderson - http://iamcal.com
Licenced under Creative Commons _or_ GPL
Creative Commons Attribution-ShareAlike 2.5 License: http://creativecommons.org/licenses/by-sa/2.5/
GPL: http://www.gnu.org/copyleft/gpl.html
*/
function isRFC822ValidEmail(emailAddy) {
	var sQtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]';
	var sDtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';
	var sAtom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';
	var sQuotedPair = '\\x5c[\\x00-\\x7f]';
	var sDomainLiteral = '\\x5b(' + sDtext + '|' + sQuotedPair + ')*\\x5d';
	var sQuotedString = '\\x22(' + sQtext + '|' + sQuotedPair + ')*\\x22';
	var sDomain_ref = sAtom;
	var sSubDomain = '(' + sDomain_ref + '|' + sDomainLiteral + ')';
	var sWord = '(' + sAtom + '|' + sQuotedString + ')';
	var sDomain = sSubDomain + '(\\x2e' + sSubDomain + ')*';
	var sLocalPart = sWord + '(\\x2e' + sWord + ')*';
	var sAddrSpec = sLocalPart + '\\x40' + sDomain; // complete RFC822 email address spec
	var sValidEmail = '^' + sAddrSpec + '$'; // as whole string
	
	var reValidEmail = new RegExp(sValidEmail);
	if (reValidEmail.test(emailAddy)) {
		return true;
	}
	return false;
}

function checkInputSetToLength(idOrHandle, errorDiv, minimumLength) {
	if (typeof(minimumLength) == 'undefined') minimumLength = 1;
	if (getHandle(idOrHandle).value.length < minimumLength) {
		if (typeof(errorDiv) != 'undefined') show(errorDiv);
		return false;
	}
	else {
		if (typeof(errorDiv) != 'undefined') hide(errorDiv);
		return true;
	}
}

function checkEmailValid(idOrHandle, errorDiv) {
	if ((getHandle(idOrHandle).value.length < 1) || (!isRFC822ValidEmail(getHandle(idOrHandle).value))) {
		if (typeof(errorDiv) != 'undefined') show(errorDiv);
		return false;
	}
	else {
		if (typeof(errorDiv) != 'undefined') hide(errorDiv);
		return true;
	}
}

function checkSelectFieldChosen(idOrHandle, errorDiv) {
	if (getHandle(idOrHandle).selectedIndex == 0) {
		if (typeof(errorDiv) != 'undefined') show(errorDiv);
		return false; //https://mjswebdev6.mjsoffe.com/wcsstore/OutletStore/images/catalog/styles/M037_ZOOM.jpg
	}
	else {
		if (typeof(errorDiv) != 'undefined') hide(errorDiv);
		return true;
	}
}

function compareInputTextFields(idOrHandle1, idOrHandle2, errorDiv, caseSensitive) {
	if (typeof(caseSensitive) == 'undefined') caseSensitive = true;
	if (caseSensitive) {
		if (getHandle(idOrHandle1).value != getHandle(idOrHandle2).value) {
			if (typeof(errorDiv) != 'undefined') show(errorDiv);
			return false;
		}
		else {
			if (typeof(errorDiv) != 'undefined') hide(errorDiv);
			return true;
		}
	}
	else {
		if (getHandle(idOrHandle1).value.toLowerCase() != getHandle(idOrHandle2).value.toLowerCase()) {
			if (typeof(errorDiv) != 'undefined') show(errorDiv);
			return false;
		}
		else {
			if (typeof(errorDiv) != 'undefined') hide(errorDiv);
			return true;
		}
	}
}




/*******************************************************
 * 
 * Lightbox Related Functions
 * 
 ******************************************************/

function hideAllLightboxDivs() {
	try { document.getElementById('overlay').style.display = 'none'; } catch (err) { }
	try { document.getElementById('email_form').style.display = 'none'; } catch (err) { }
	try { document.getElementById('contactUsLightbox').style.display = 'none'; } catch (err) { }
	try { document.getElementById('shippingLightbox').style.display = 'none'; } catch (err) { }
	try { document.getElementById('investorRelationsLightbox').style.display = 'none'; } catch (err) { }
	try { document.getElementById('zoomLightbox').style.display = 'none'; } catch (err) { }
	try { document.getElementById('videoLightbox').style.display = 'none'; } catch (err) { }
	try { document.getElementById('sizeChartLightbox').style.display = 'none'; } catch (err) { }
	try { document.getElementById('notifyLightbox').style.display = 'none'; } catch (err) { }
}

function showLightbox(id) {
	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) {
		var ieversion = new Number(RegExp.$1);
		if ((ieversion >= 6) || (ieversion >= 5)) {
			window.scrollTo(0,0);
		}
	}
	showBlock('overlay');
	showBlock(id);
}

function hideLightbox(id) {
	hide('overlay');
	hide(id);
}