// Print function

function printit(){
    if (window.print) {
        window.print() ;
    } else {
        alert('Please use the print feature of your browser.');
    }
} 


// popups
function popUp(objLink, h, w) {
	window.open(objLink.href, "popup", "width="+w+",height="+h+",scrollbars=no,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no");
}


//Empty form fields

function clearText(objField, defaultButton){
    if (objField.defaultValue==objField.value)
    objField.value = ""
}
function resetText(objField){
    if (objField.value=="")
    objField.value = objField.defaultValue
}

function KeyDownHandler(event, btn)
{
    // process only the Enter key
    if (event.keyCode == 13)
    {
        // cancel the default submit
        event.returnValue=false;
        event.cancel = true;
        // submit the form by programmatically clicking the specified button
        document.getElementById(btn).click();
    }
}

//JS for Suckerfish dropdown nav

sfHover = function() {
var sfEls = document.getElementById("navigationmain").getElementsByTagName("LI");
for (var i=0; i<sfEls.length; i++) {
	sfEls[i].onmouseover=function() {
		this.className+=" sfhover";
	}
	sfEls[i].onmouseout=function() {
		this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
	}
}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

/// ACCESSIBLE POPUP SCRIPT ///////////////////////////////////////////////////////////
// ARRAY EXTENSIONS

if (!Array.prototype.push) Array.prototype.push = function() {
    for (var i=0; i<arguments.length; i++) this[this.length] = arguments[i];
    return this.length;
}

Array.prototype.find = function(value, start) {
    start = start || 0;
    for (var i=start; i<this.length; i++)
        if (this[i]==value)
            return i;
    return -1;
}

Array.prototype.has = function(value) {
    return this.find(value)!==-1;
}

// FUNCTIONAL

function map(list, func) {
    var result = [];
    func = func || function(v) {return v};
    for (var i=0; i < list.length; i++) result.push(func(list[i], i, list));
    return result;
}

function filter(list, func) {
    var result = [];
    func = func || function(v) {return v};
    map(list, function(v) { if (func(v)) result.push(v) } );
    return result;
}

function valQuantity(source, arguments)
{	
	arguments.IsValid = false;

	objectNamePosition = 0;
	
	for (i = 0; i >= 0; i = source.id.indexOf("_", i+1)) {objectNamePosition = i;}
	
	var clientId = source.id.substring(0, objectNamePosition + 1);
	var itemIndex = parseInt(clientId.slice(objectNamePosition -2, objectNamePosition));
	
	for (i = 1; i <= 20; i++)
	{
	    var txtQty
	    
	    if (i < 10) {
	        txtQty = document.getElementById(clientId + 'repProducts_ctl0' + i + '_txtQuantity');
	    } else {
	        txtQty = document.getElementById(clientId + 'repProducts_ctl' + i + '_txtQuantity');
	    }
	    
	    if (txtQty != null)  {
	        if (txtQty.value.length > 0 && !isNaN(txtQty.value) && !txtQty.disabled) {
	            if (txtQty.value > 0) {arguments.IsValid = true;}
	        }
	    } else {
            break;
        }
	}
}


// DOM

function getElem(elem) {
    if (document.getElementById) {
        if (typeof elem == "string") {
            elem = document.getElementById(elem);
            if (elem===null) throw 'cannot get element: element does not exist';
        } else if (typeof elem != "object") {
            throw 'cannot get element: invalid datatype';
        }
    } else throw 'cannot get element: unsupported DOM';
    return elem;
}

function hasClass(elem, className) {
    return getElem(elem).className.split(' ').has(className);
}

function getElementsByClass(className, tagName, parentNode) {
    parentNode = !isUndefined(parentNode)? getElem(parentNode) : document;
    if (isUndefined(tagName)) tagName = '*';
    return filter(parentNode.getElementsByTagName(tagName),
        function(elem) { return hasClass(elem, className) });
}

// MISC CLEANING-AFTER-MICROSOFT STUFF

function isUndefined(v) {
    var undef;
    return v===undef;
}

// These defaults should be changed the way it best fits your site
var _POPUP_FEATURES = '';

function raw_popup(url, target, features) {
    // pops up a window containing url optionally named target, optionally having features
    if (isUndefined(features)) features = _POPUP_FEATURES;
    if (isUndefined(target  )) target   = '_blank';
    var theWindow = window.open(url, target, features);
    theWindow.focus();
    return theWindow;
}

function link_popup(src, features) {
    // to be used in an html event handler as in: <a href="..." onclick="link_popup(this,...)" ...
    // pops up a window grabbing the url from the event source's href
    return raw_popup(src.getAttribute('href'), src.getAttribute('target') || '_blank', features);
}

/// END ACCESSIBLE POPUP SCRIPT ////////////////////////////////////////////////////////


function getElementsByClassName(className, tag, elm){
	var testClass = new RegExp("(^|\\\\s)" + className + "(\\\\s|$)");
	var tag = tag || "*";
	var elm = elm || document;
	var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
	var returnElements = [];
	var current;
	var length = elements.length;
	for(var i=0; i<length; i++){
		current = elements[i];
		if(testClass.test(current.className)){
			returnElements.push(current);
		}
	}
	return returnElements;
}


//Ajax
function ajaxObj(url, request_params) {
	this.loading = document.createElement("div");
	this.load = function() {

	};
	this.done = function() {
		
	};
	var ajax = this;
	this.request_timeout = 5000;
	this.maxRetry = 3;
	this.trys = 0;
	this.timeout = null;
	this.request_onTimeout = function()
	{ 	                            
		if(this.http_request != null && this.http_request.readyState < 4)
		{
			this.done();
			this.http_request.abort();
			clearTimeout(this.timeout);
			if(getQueryVariable("debug") != null)
			{
				if(confirm("A server timeout has occured, would you like to retry?"));
					this.init();
			}
			else
			{
				if(this.maxRetry > this.trys)
					this.init();
				else
					alert("A timeout error has occured, please check your internet connection.");
			}
			
		}
	};

	this.handler_params = new Array();
	this.handler_method = function() { return null; };
	
	this.http_request = null;
	this.xmlDoc = null;
	this.text = null;

	this.init = function()
	{
		this.trys += 1;
		this.load();
		
		if (window.XMLHttpRequest) { // Mozilla, Safari,...
			this.http_request = new XMLHttpRequest();

			if(typeof this.http_request.async != "undefined")
				this.http_request.async = true;            

		} else if (window.ActiveXObject) { // IE
		
			try
			{
				this.http_request = new ActiveXObject("MSXML2.XMLHTTP.3.0");
			}
			catch (e)
			{
				try
				{
					this.http_request = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch (e)
				{
				}
			}
		} else {
			alert("Your browser does not support AJAX");
		}
		
		if(this.http_request != null) {
			this.http_request.onreadystatechange = function() // 
			{
				if (ajax.http_request.readyState == 4) {
					ajax.done();
					clearTimeout(ajax.timeout);
					if (ajax.http_request.status == 200) {
						
						if(ajax.http_request.getResponseHeader("Content-Type") == "text/xml" && ajax.http_request.responseXML.documentElement != null && ajax.http_request.responseXML.documentElement.nodeName != "parsererror")
							ajax.xmlDoc = ajax.http_request.responseXML;
						else
							ajax.text = ajax.http_request.responseText;
							
						ajax.handler_method();
						ajax.http_request = null;						
					} else if (ajax.http_request.status != 0) {
						/* alerts with exception on bad request */
						if(getQueryVariable("debug") != null)
						{
							if(confirm("A server '" + ajax.http_request.status + ": " + ajax.http_request.statusText + "' error has occured, would you like see the details?"));
								document.body.innerHTML = "Page: " + document.location + "<br />AJAX Request: " + url + "?" + request_params + "<br /><br />" + ajax.http_request.responseText;
						}
						else
						{
							alert("Server Error '" + ajax.http_request.status + ": " + ajax.http_request.statusText + "'\n\nPlease nofity the developer with the following information:\n\nPage: " + document.location + "\nAJAX Request: " + url + "?" + request_params);
						}
						ajax.http_request.abort();
					}
				} else {
					if (ajax.http_request.readyState == 1) { // Set up timeout function
						ajax.timeout = setTimeout(ajax.request_onTimeout, ajax.request_timeout);
					}
				}
			};
			
			if (request_params == null) {
				this.http_request.open('GET', url, true);
				this.http_request.send(null);
			} else {
				this.http_request.open('POST', url, true);
				this.http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
				this.http_request.setRequestHeader("Content-length", request_params.length);
				this.http_request.setRequestHeader("Connection", "close");
				this.http_request.send(request_params);
			}
		}			
	};
};
