/*
 * Delivery Agent - main.js
 *
 * This file contains the main js functions used by DA templates.
 */

// $( elementID ) - Recursive getElement function
function $da() {
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string')
			element = document.getElementById(element);
		if (arguments.length == 1)
			return element;
		elements.push(element);
	}
	return elements;
}
function $() {
	var elements = [];
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string'){
			element = document.getElementById(element);
		}
		if (arguments.length == 1){
			return element;
		}
		elements.push(element);
	}
	return elements;
}

// toggle( parentElementToToggle1, [parentElementToToggle2...] ) - Visibility toggler
function toggle() {
	for ( var i=0; i < arguments.length; i++ ) {
        // $(arguments[i]).style.display = ($(arguments[i]).style.display != 'none' ? 'none' : '' );
		$da(arguments[i]).style.display = ($da(arguments[i]).style.display != 'none' ? 'none' : '' );
	}
}

// itoggle( elementID, imageBefore, imageAfter ) - Image toggler
function itoggle() {
	// $(arguments[0]).src = ($(arguments[0]).src != arguments[1] ? arguments[1] : arguments[2]);
	$da(arguments[0]).src = ($da(arguments[0]).src != arguments[1] ? arguments[1] : arguments[2]);
}

// preloadImages( imgArray ) - Preloads rollover images
function preloadImages() {
	var preloadedImages = [];
	for (var i=0; i < arguments[0].length; i++ ) {
		preloadedImages[i] = new Image();
		preloadedImages[i].src = arguments[0][i];
	}
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}


// hbxStrip
function _hbxStrip( a ) {
	a = a.split("|").join("");
	a = a.split("&").join("");
	a = a.split("'").join("");
	a = a.split("#").join("");
	a = a.split("$").join("");
	a = a.split("%").join("");
	a = a.split("^").join("");
	a = a.split("*").join("");
	a = a.split(":").join("");
	a = a.split("!").join("");
	a = a.split("<").join("");
	a = a.split(">").join("");
	a = a.split("~").join("");
	a = a.split(";").join("");
	a = a.split(" ").join("+");
	a = a.split("%u2019").join("");
	a = a.split("%u201C").join("");
	a = a.split("%u201D").join("");
	a = a.split("%u2122").join("");
	return a;
}

function emailCheck() {
	if( document.getElementById("productfinder").email.value != '' ) {
		if( !IsValidEmail( document.getElementById("productfinder").email.value ) ) {
			alert( "You must enter your valid e-mail address.");
			return false;
		}
	}
	return ValidateForm('productfinder');
}


// addEvent and removeEvent, designed by Aaron Moore
function addEvent(element, listener, handler){
	//if the system is not set up, set it up, and
	// store any outside script's event registration in the first handler slot
	if(typeof element[listener] != 'function' || typeof element[listener + '_num'] == 'undefined'){
		element[listener + '_num'] = 0;
		if(typeof element[listener] == 'function'){
			element[listener + 0] = element[listener];
			element[listener + '_num']++;
		}
		element[listener] = function(e){
			var r = true;
			e = (e) ? e : window.event;
			for(var i = 0; i < element[listener + '_num']; i++){
				if(element[listener + i](e) === false){ r = false;}
			}
			return r;
		};
	}
	//if handler is not already stored, assign it
	for(var i = 0; i < element[listener + '_num']; i++){
		if(element[listener + i] == handler){ return; }
	}
	element[listener + element[listener + '_num']] = handler;
	element[listener + '_num']++;
}
function removeEvent(element, listener, handler){
	//if the system is not set up, or there are no handlers to remove, exit
	if(	typeof element[listener] != 'function' || 
		typeof element[listener + '_num'] == 'undefined' || 
		element[listener + '_num'] === 0){
		return;
	}
	//loop through handlers,
	//  if target handler is reached, begin overwriting each  handler with the handler in front of it until one before the last
	var found = false;
	for(var i = 0; i < element[listener + '_num']; i++){
		if(!found){
			found = element[listener + i] == handler;
		}
		if(found && (i+1) < element[listener + '_num']){
			element[listener + i] = element[listener + (i+1)];
		}
	}
	//if handler was found, decrement the handler count
	if(found){
		element[listener + '_num']--;
	}
}

function fixEvent(event) {
	// add W3C standard event methods
	event.preventDefault = fixEvent.preventDefault;
	event.stopPropagation = fixEvent.stopPropagation;
	return event;
}
fixEvent.preventDefault = function() {
	this.returnValue = false;
};
fixEvent.stopPropagation = function() {
	this.cancelBubble = true;
};


function visibilityByTagName(tag, action) {
	e = document.getElementsByTagName(tag);
	len = e.length;
	for (var n = 0; n < len; n++) {
		e[n].style.visibility = action;
	}
}

function facebook_share(id) {
        if (typeof id == "undefined") {
                u=location.href;
        } else {
                u="http://"+location.host+"/detail.php?p="+id;
        }
        t=document.title;
        window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
        return false;
}

function searchBoxCheck(field, defaultVal) {
	if( $(field).value == defaultVal || $(field).value == "" ) {
		alert( "Please enter a search term" );
		return false;
	}
	return true;
}

function addBookmark( title, url ) { 
    var ua=navigator.userAgent.toLowerCase();
    var isSafari=(ua.indexOf('webkit')!=-1);
    var isMac=(ua.indexOf('mac')!=-1);
    var buttonStr=isMac?'Command/Cmd':'CTRL';
	
	if(isSafari) { // Firefox, Netscape, Safari, iCab
		alert('You need to press '+buttonStr+' + D to bookmark our site.');
    }
	else if(!window.print || isMac) { // IE5/Mac and Safari 1.0
		alert('You need to press Command/Cmd + D to bookmark our site.');    
    }
	else if( window.sidebar ) {
		window.sidebar.addPanel(title, url,"");
	}
	else if( window.external ) {
		window.external.AddFavorite( url, title);
	}
	else if(window.opera) {
		void(0); // do nothing here (Opera 7+)
    }
	else {
		alert('In order to bookmark this site you need to do so manually through your browser.');
	}
}

function vistoggle( vis ) {
	var vMode = ( vis != 0 ? 'none' : 'block' );
	for ( var i=1; i < arguments.length; i++ ) {
		//$da(arguments[i]).style.display = ($da(arguments[i]).style.display != 'none' ? 'none' : '' );
		$da(arguments[i]).style.display = vMode;
	}
}

function visibilitytoggle( vis ) {
	var vMode = ( vis != 0 ? 'hidden' : 'visible' );
	for ( var i=1; i < arguments.length; i++ ) {
		$da(arguments[i]).style.visibility = ($da(arguments[i]).style.visibility != 'hidden' ? 'hidden' : 'visible' );
	}
}

function checkSearch( searchText ) {
    if( $('searchtext').value == '' ||  $('searchtext').value == ' ' || $('searchtext').value == searchText ) {
	  alert( 'Please enter a search term' );
	  return false;
	} else {
	  return true;
	}
}

function checkCoupon( couponText ) {
    if( $('couponcode').value == '' ||  $('couponcode').value == ' ' || $('couponcode').value == couponText ) {
	  alert( 'Please enter a coupon code' );
	  return false;
	} else {
	  return true;
	}
}

function removeCoupon( couponField, couponForm ) {
	$( couponField ).value = ''; 
	$( couponForm ).onsubmit = '';
	ajaxCall('cartcoup');
	return false;
}

function openCS( url, htdocs, network ) {
 	var url = escape( url );
	var urlString = '/'+htdocs+'/layout/cspopup.html?cs='+url;
	if( network ) { 
		urlString += '&net='+network;
	}
	window.open(urlString, 'cswin','height=630,width=750,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,directories=no,status=no');
}

//hard code live link for blog CS answers
function openCSblog( url, htdocs, network ) {
 	var url = escape( url );
	var urlString = 'http://hbo.seenon.com/'+htdocs+'/layout/cspopup.html?cs='+url;
	if( network ) { 
		urlString += '&net='+network;
	}
	window.open(urlString, 'cswin','height=630,width=750,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,directories=no,status=no');
}
function _hbLink() { }

	