// Check whether string s is empty.
function isEmpty(s){
   return ((s == null) || (s.length == 0))
}

// Returns true if string s is empty or 
// whitespace characters only.
function isWhitespace(s){
	var whitespace = " \t\n\r";
	var i;

    // Is s empty?
    if (isEmpty(s)){
		return true;
	}

    // Search for a non-whitespace character.
    for (i = 0; i < s.length; i++){   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1){
			return false;
		}
	}
    return true;
}

function do_focus(form_name){
	if (form_name == 'byname'){
		document.forms.byname.num_name.focus();
	}
}

function checkMe(thisForm){
	return true;
}

function checkMeNumName(thisForm){
	if (!isWhitespace(thisForm.num_name.value)) {
		return true;
	} else {
		alert('Please enter a name or number\n before clicking "Find Carpets"');
		return false;
	}
}

function checkMeColor(thisForm){
	if (!thisForm.theColor_dom[0].checked){
		return true;
	} else {
		allUnchecked = true;
		for (x=0; x<thisForm.elements.length; x++){
			theCB = thisForm.elements[x];
			if (theCB.type == 'checkbox' && theCB.checked == true){
				return true;
				break;
			}
		}
		if (allUnchecked = true){
			alert("Please choose at least one \ncolor submitting the form.");
			return false;
		}
	}
}

function checkShapes(box){
	
	var theseElems = document.forms.sizeShape.elements;
	var numBoxes = 6;
	
	// Get the index of box "Any"
	for(x=0; x<theseElems.length; x++){
		if (theseElems[x].name == 'any') {
			var anyBoxIndex = x;
			break;
		}
	}
	// If "Any" box was clicked
		// look at all the other boxes
			// and set them to the new value of the "Any" box
	if(box == 'any') {
		for(x=anyBoxIndex+1; x<anyBoxIndex+numBoxes; x++){
			theseElems[x].checked = theseElems.any.checked;
		}
	// else some other box was clicked	
		// prepare a flag for whether ALL other boxes are checked or not
		// and loop through those other boxes
			// if any of them are unchecked
				// flip the flag
	} else {
		var allChecked = true;
		for(x=anyBoxIndex+1; x<anyBoxIndex+numBoxes; x++){
			if(theseElems[x].checked == false) {
				allChecked = false;
				break;
			}
		}
		// check/uncheck the "Any" box to reflect all other boxes being checked/not
		theseElems.any.checked = allChecked;
	}
}


function checkMeMultiple(thisForm){
	cA = (thisForm.num_name.value != "");
	cB = (thisForm.theCollection.value != "any");
	cC = (thisForm.theStyle.value != "any");
	cD = (thisForm.theCountry.value != "any");

	if (cA || cB || cC || cD){
		return true;
	} else {
		if (!thisForm.theColor_dom[0].checked){
			return true;
		} else {
			for (x=0; x<thisForm.elements.length; x++){
				theCB = thisForm.elements[x];
				if (theCB.type == 'checkbox' && theCB.checked == true){
					return true;
					break;
				}
			}
		}
		alert("Please set at least one condition \nbefore submitting the form.");
		return false;
	}
	return true;
}

function makeArraysAndVars(){	
	rugsHot = new Array(0,1,2,3,0,4,4,0,5,5,5,0,6,6,7,8,9,10,11,12,13,14);
	furnitureHot = new Array(0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0);
	accessoriesHot = new Array(0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0);
	closeoutsHot = new Array(0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0);
	whereHot = new Array(0,1,2,3,3,3,0,4,4,4,0,5,5,0,0,0,0,0,0,0,0,0);
	contactHot = new Array(0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
	newsHot = new Array(0,1,2,3,3,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
	
	rugsUrl = new Array('#','style.html','showrugs.php?theStyle=1','showrugs.php?theStyle=2','showrugs.php?theStyle=4','name.html','bycountry.html','india.php','china.php','nepal.php','color.php','search.php','showrugs.php?closeOrIntro=over','showrugs.php?closeOrIntro=intro','showrugs.php?closeOrIntro=antique');
	furnitureUrl = new Array('#','showfurniture.php');
	accessoriesUrl = new Array('#','showaccessories.php');
	closeoutsUrl = new Array('#','showrugs.php?closeOrIntro=spec');
	whereUrl = new Array('#','map.html','name.html','showdlrs.php?queryType=byname','showdlrs.php?queryType=byname&nonusa=true','../links/index.html');	
	contactUrl = new Array('#','index.php');
	newsUrl = new Array('#','announcements.php','mailing.php','media.html');
}

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

function MM_showHideLayers() { //v9.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) 
  with (document) if (getElementById && ((obj=getElementById(args[i]))!=null)) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

function MM_preloadImages() { //v3.0
  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_swapImgRestore() { //v3.0
	notOverAButton = true;
	var i,x,a=document.MM_sr; 
	for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++){
		if (x.name != "swap"){
			x.src=x.oSrc;
		} else {
			delayPeriod=setTimeout("delay_swap()",25);
		}
	}
}

function delay_swap(){
	if (notOverAButton){
		var i,x,a=document.MM_sr; 
		for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++){
			if (x.name == "swap"){
				x.src=x.oSrc;
			}
		}
	}
}

function MM_findObj(n, d) { //v4.0
  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);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
	notOverAButton = false;
  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 whichPage(spot){
	if  (String(document.images["swap"].src).indexOf('swap_rugs.gif') != -1){
		if (rugsHot[spot] != 0) document.location = "/showroom/" + rugsUrl[rugsHot[spot]];
	} else if (String(document.images["swap"].src).indexOf('swap_furniture.gif') != -1){
		if (furnitureHot[spot] != 0) document.location = "/showroom/" + furnitureUrl[furnitureHot[spot]];
	} else if (String(document.images["swap"].src).indexOf('swap_accessories.gif') != -1){
		if (accessoriesHot[spot] != 0) document.location = "/showroom/" + accessoriesUrl[accessoriesHot[spot]];
	} else if (String(document.images["swap"].src).indexOf('swap_closeouts.gif') != -1){
		if (closeoutsHot[spot] != 0) document.location = "/showroom/" + closeoutsUrl[closeoutsHot[spot]];
	} else if (String(document.images["swap"].src).indexOf('swap_where_to_buy.gif') != -1){
		if (whereHot[spot] != 0) document.location = "/where_to_buy/" + whereUrl[whereHot[spot]];
	} else if (String(document.images["swap"].src).indexOf('swap_contact_us.gif') != -1){
		if (contactHot[spot] != 0) document.location = "/contact_us/" + contactUrl[contactHot[spot]];
	} else if (String(document.images["swap"].src).indexOf('swap_news.gif') != -1){
		if (newsHot[spot] != 0) document.location = "/news/" + newsUrl[newsHot[spot]];
	} else {
		// nothing	
	}
}

var notOverAButton = true;
makeArraysAndVars();

