// assign the images
var theImages = new Array();


//theImages.push({src: '/mas_assets/adimages/main/blog.jpg', url: 'http://icraveit-club.blogspot.com'});
theImages.push({src: '/mas_assets/adimages/main/bargainBin.jpg', url: '/cgi-bin/category/9300'});
theImages.push({src: '/mas_assets/adimages/main/free_shipping.jpg', url: '/cgi-bin/category/9200'});
theImages.push({src: '/mas_assets/adimages/main/clearance.jpg', url: '/cgi-bin/category/9100'});
theImages.push({src: '/mas_assets/adimages/main/create.jpg', url: '/cgi-bin/adduser.cgi'});
theImages.push({src: '/mas_assets/adimages/main/weekly.jpg', url: 'http://www.icraveit.com/mas_assets/weekly.html'});
// theImages.push({src: '/mas_assets/adimages/current/9_09/10001045-21.jpg', url: '/cgi-bin/category.cgi?item=10001045-21'});
// theImages.push({src: '/mas_assets/adimages/current/9_09/10001099-21.jpg', url: '/cgi-bin/category.cgi?item=10001099-21'});

// theImages.push({src: '/mas_assets/adimages/current/9_09/258.jpg', url: '/cgi-bin/category/258'});




// show a random image in the id'd element indicated
var preBuffer = new Array();
var nextPreBuffer = 0;
var imageToShow = 0;
var shownImages = new Array();
var imageHtml = '';
function showImage(idToWriteTo)
{
  // prevent duplicates by keeping track of shown images
  do
  {
    imageToShow = Math.round(Math.random()*(theImages.length-1));
  }
  while( shownImages.length < theImages.length && shownImages[imageToShow] );
  shownImages[imageToShow] = true;

  // preload & display the image & link
  if( theImages[imageToShow] && theImages[imageToShow]['src'] && theImages[imageToShow]['url'] )
  {
    // preload the image (not really necessary)
    nextPreBuffer = preBuffer.length;
    preBuffer[nextPreBuffer] = new Image();
    preBuffer[nextPreBuffer].src = theImages[imageToShow]['src'];

    // display the image
    imageHtml = '<a href="'+theImages[imageToShow]['url']+'"><img src="'+theImages[imageToShow]['src']+'" border="0"></a>';
    if ( idToWriteTo && document.getElementById(idToWriteTo) )
    {
      document.getElementById(idToWriteTo).innerHTML = imageHtml;
    }
    else
    {
      document.write(imageHtml);
    }
  }
}




function confirmLink(url, message) {
	if (confirm(message)) {
		document.location.href = url;
	}

	return false;
}

function redirect(url) {
	if (typeof(exit) != "undefined") {
		exit = 0;
	}

	document.location.href = url;
}

function setStart(start) {

	var loc = '' + document.location.href;
	loc = loc.replace(/(\?|&)start=.*?&/, '$1');
	loc = loc.replace(/&$/, '');
	loc = loc.replace(/(\?|&)start=.*?$/, '');
	loc += '&start=' + start;

	document.location.href = loc;
}


function getObj(name) {
	if (document.getElementById) {
		this.obj = document.getElementById(name);
		this.style = document.getElementById(name).style;
	} else
	if (document.all) {
		this.obj = document.all[name];
		this.style = document.all[name].style;
	} else
	if (document.layers) {
		this.obj = document.layers[name];
		this.style = document.layers[name];
	}
}

function visib(objName, flag) {
	x = new getObj(objName);
	x.style.visibility = (flag) ? 'visible' : 'hidden';
}

function display(objName, flag) {
	x = new getObj(objName);
	x.style.display = (flag) ? '' : 'none';
}

function showWait() {
	if (!document.getElementsByTagName) {
		// do not error if viewed by an old browser
		return;
	}

	var elements = document.getElementsByTagName("*");
	for (var i = 0; i < elements.length; i++) {
		if (elements[i].className == 'pleasewait' || elements[i].id == 'pleasewait') {
			elements[i].style.visibility = 'visible';
		}
	}
}

function setSelected(select) {
	if (!select) {
		alert("Not a valid select element.");
		return;
	}

	var args = setSelected.arguments;
	for (var i = 1; i < args.length; i++) {
		for (var j = 0; j < select.length; j++) {
			if (select.options[j].value == args[i]) {
				select.options[j].selected = true;
				break;
			}
		}
	}
}


function setImgSrc(id, new_src) {
	document.images[id].src = new_src;
}


function enableCondCheckbox(checkbox) {
	var args = enableCondCheckbox.arguments;
	if (args.length < 2) {
		return;
	}

	var disabled = true;
	if (checkbox) {
		disabled = false;
	}

	for (var i = 1; i < args.length; i++) {
		args[i].disabled = disabled;
	}

}


/*  Old code from headers  */

function ValidateExtraData(thisObject)
{

        var ThisElement, CompleteErrorString = "";

        var SelectElements = new
Enumerator(thisObject.getElementsByTagName("SELECT"));

        for (;!SelectElements.atEnd();SelectElements.moveNext())
        {

                ThisElement = SelectElements.item();
                if (ThisElement.getAttribute("ErrorString"))
                {
                        if
(ThisElement.options(ThisElement.selectedIndex).value == "")
                        {
                                CompleteErrorString = CompleteErrorString +
ThisElement.getAttribute("ErrorString") + "\r\n";
                        }

                }

   }

        if (CompleteErrorString)
        {

                alert(CompleteErrorString);
                window.event.returnValue = false;

        }

}


function WM_netscapeCssFix() {

  if (document.WM.WM_netscapeCssFix.initWindowWidth != window.innerWidth || document.WM.WM_netscapeCssFix.initWindowHeight != window.innerHeight) {
    document.location = document.location;
  }
}

function WM_netscapeCssFixCheckIn() {

  if ((navigator.appName == 'Netscape') && (parseInt(navigator.appVersion) == 4)) {
    if (typeof document.WM == 'undefined'){
      document.WM = new Object;
    }
    if (typeof document.WM.WM_scaleFont == 'undefined') {
      document.WM.WM_netscapeCssFix = new Object;
      document.WM.WM_netscapeCssFix.initWindowWidth = window.innerWidth;
      document.WM.WM_netscapeCssFix.initWindowHeight = window.innerHeight;
    }
    window.onresize = WM_netscapeCssFix;
  }
}

WM_netscapeCssFixCheckIn()

// retrieving a cookie.
function getCookie(name){
  var cname = name + "=";
  var dc = document.cookie;
  if (dc.length > 0) {
    begin = dc.indexOf(cname);
    if (begin != -1) {
      begin += cname.length;
      end = dc.indexOf(";", begin);
      if (end == -1) end = dc.length;
        return unescape(dc.substring(begin, end));
    }
  }
  return null;
}

// setting a cookie.
function setCookie(name, value, expires, path, domain, secure) {
  document.cookie = name + "=" + escape(value) +
  ((expires == null) ? "" : "; expires=" + expires.toGMTString()) +
  ((path == null) ? "" : "; path=" + path) +
  ((domain == null) ? "" : "; domain=" + domain) +
  ((secure == null) ? "" : "; secure");
}

// deleting a cookie.
function delCookie (name,path,domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
    ((path == null) ? "" : "; path=" + path) +
    ((domain == null) ? "" : "; domain=" + domain) +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

//----------------------------
function MM_swapImgRestore() {
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() {
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) {
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); return x;
}

function MM_swapImage() {
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}



function getCookieVal(offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}

function isNull (val) {
 if (val == "null") {
 	return true;
 }
 if (val == null) {
 	return true;
 }
 if (val == undefined) {
 	return true;
 }
 if (val == 'undefined') {
 	return true;
 }
 if (val == '') {
        return true;
 }
 return false;
}



function confirmunsub2() {
	return confirm('You are about to unsubscribe\nfrom all your events.\n\nAre you sure ?');
	}


//Disable right mouse click Script
//By Maximus (maximus@nsimail.com)
//For full source code, visit http://www.dynamicdrive.com

/*
var message="Function Disabled!";
///////////////////////////////////

function clickIE() {if (document.all) {alert(message);return false;}}
function clickNS(e) {if
(document.layers||(document.getElementById&&!document.all)) {
if (e.which==2||e.which==3) {alert(message);return false;}}}
if (document.layers)
{document.captureEvents(Event.MOUSEDOWN);document.onmousedown=clickNS;}
else{document.onmouseup=clickNS;document.oncontextmenu=clickIE;}

document.oncontextmenu=new Function("return false")
*/


function Next() {
	setTimeout('Next()', 5000);

	if (document.forms.slideshow.pause.value == 0) {
		if (typeof(exit) != "undefined") {
			exit = 0;
		}
		document.forms.slideshow.submit();
	}
}


function slideshowToggle(img, pause, run) {
	var form = document.forms.slideshow;

	if (form.pause.value == 0) {
		form.pause.value = 1;

		var paused_loc = document.location.href;
		paused_loc = paused_loc.replace(/pause=\d/, '');
		paused_loc = paused_loc.replace(/&{2,}/, '&');
		paused_loc = paused_loc.replace(/&$/, '');
		document.location.href = paused_loc + '&pause=1';

		if (img) {
			img.src = run;
		}
		return 'Run Slideshow';
	} else {
		if (img) {
			img.src = pause;
		}
		form.pause.value = 0;
		return 'Pause Slideshow';
	}
}

function slideshowPause() {
	var form = document.forms.slideshow;

	var paused_loc = document.location.href;
	paused_loc = paused_loc.replace(/pause=\d/, '');
	paused_loc = paused_loc.replace(/&{2,}/, '&');
	paused_loc = paused_loc.replace(/&$/, '');
	document.location.href = paused_loc + '&pause=1';

	form.pause.value = 1;
	form.pauseButton.value = 'Run Slideshow';
}





// Showdetails page
function submitToParent(form) {
	var url = form.action + '?';

	for (var i = 0; i < form.elements.length; i++) {
		url += form.elements[i].name + '=' + form.elements[i].value + '&';
	}

	opener.location.href = url;
	self.close();

	return false;
}

var isNav4, isIE4;
if (parseInt(navigator.appVersion.charAt(0)) >= 4) {
	isNav4 = (navigator.appName == "Netscape") ? 1 : 0;
	isIE4 = (navigator.appName.indexOf("Microsoft") != -1) ? 1 : 0;
}

var v_padding = 300;
var h_padding = 40;

function fitWindowSize() {
	if (isNav4) {
		window.innerWidth = document.layers[0].document.images[0].width + h_padding;
		window.innerHeight = document.layers[0].document.images[0].height + v_padding;

		if  (window.innerWidth < 500) {
			window.innerWidth = 500;
		}

		if (window.innerHeight < 500) {
			window.innerHeight = 500;
		}
	}

	if (isIE4) {
		window.resizeTo(500, 500);
		width = 500 - (document.body.clientWidth -  document.images[0].width);
		height = 500 - (document.body.clientHeight -  document.images[0].height);
		height += v_padding;
		width += h_padding;

		if (width < 500) {
			width = 500;
		}

		if (height < 500) {
			height = 500;
		}

		window.resizeTo(width, height);
	}
}

function valid_alphanum_dash_underscore(value) {
	if (value + "" != "" && value.match(/^[a-zA-Z0-9\-_]+$/)) {
		return true;
	}

	return false;
}

function valid_sku(value) {
	return valid_alphanum_dash_underscore(value);
}

function valid_category(value) {
	return valid_alphanum_dash_underscore(value);
}

function valid_zone(value) {
	return valid_alphanum_dash_underscore(value);
}

function check_common(prefix, form, value) {
	if (!form) {
		form = document.forms.masform;
	}

	for (var i = 0; i < form.elements.length; i++) {
		var element = form.elements[i];

		if (element.type == "checkbox" && (!prefix || element.name.indexOf(prefix) == 0)) {
			if (value == -1) {
				element.checked = !element.checked;
			} else {
				element.checked = value;
			}
		}
	}

	return false;
}

function check_all(prefix, form) {
	return check_common(prefix, form, 1);
}

function uncheck_all(prefix, form) {
	return check_common(prefix, form, 0);
}

function invert_all(prefix, form) {
	return check_common(prefix, form, -1);
}

function get_param(name) {
	var params = location.search.split('&');
	for (var i = 0; i < params.length; i++) {
		var parts = params[i].split('=');
		if (parts[0] == name) {
			return parts[1];
		}
	}

	return '';
}

function item_preview_url(cgi_url, sku, thumb, template) {
	var url = cgi_url + '/category.cgi?';

	if (template != '') {
		url += 'settemplate=' + template;
	}

	if (thumb) {
		url += '&category=search&searchfields=sku&query=' + sku;
	} else {
		url += '&item=' + sku;
	}
	return url;
}


//When an input element receives the focus, check if the defaultValue is the same as the current value. If they are equal, the text box is cleared.
function clearDefault(el) {
  if (el.defaultValue == el.value) el.value = ""
}
