
var colorOption = '';

// -----------------------------------------------------------------------
//	Event handler / listener
//	----------------------------------------------------------------------
document.onclick=check;

// document.getElementById('sistema').contentWindow.document.body.onclick = check;


 function check(e){
	 var target = (e && e.target) || (event && event.srcElement);

		var sz = new Array();
		sz[0] = "ddinput";
		sz[1] = "sz33-1";
		sz[2] = "sz34-1";
		sz[3] = "sz35-1";
		sz[4] = "sz36-1";
		sz[5] = "sz37-1";
		sz[6] = "sz38-1";
		sz[7] = "sz39-1";
		sz[8] = "sz40-1";
		sz[9] = "lrgdownarrow";
		sz[10] = "smdroparrow268";
		sz[11] = "smdroparrow249";
		sz[12] = "sf268";
		sz[13] = "sf249";
		sz[14] = "sz33-268";
		sz[15] = "sz34-268";
		sz[16] = "sz35-268";
		sz[17] = "sz36-268";
		sz[18] = "sz37-268";
		sz[19] = "sz38-268";
		sz[20] = "sz39-268";
		sz[21] = "sz40-268";
		sz[22] = "sz33-249";
		sz[23] = "sz34-249";
		sz[24] = "sz35-249";
		sz[25] = "sz36-249";
		sz[26] = "sz37-249";
		sz[27] = "sz38-249";
		sz[28] = "sz39-249";
		sz[29] = "sz40-249";

		var inSelect = false;
		for (var i = 0; i < sz.length; i++) {
			if (target.id == sz[i]) {
				inSelect = true;
			}
		}

	if(inSelect == false) {
		if (document.getElementById('ddselect_' + colorOption)) {
			if (document.getElementById('ddselect_' + colorOption).style.display == 'block')
				document.getElementById('ddselect_' + colorOption).style.display="none";
		}

		if (document.getElementById('mm-sf268').style.display == 'block')
			document.getElementById('mm-sf268').style.display="none";

		if (document.getElementById('mm-sf249').style.display == 'block')
			document.getElementById('mm-sf249').style.display="none";

	}
	
 }

// -----------------------------------------------------------------------
//	Supporting function for the RDI set of functions
// -----------------------------------------------------------------------

// -----------------------------------------------------------------------
//	Get a parameter form the URL string.
// -----------------------------------------------------------------------
function getURLParams(name) {
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]" + name + "=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
	
  if( results == null )
    return "";
  else
    return results[1];
}

// -----------------------------------------------------------------------
//	Set the cookie name and value
// -----------------------------------------------------------------------
function rdi_setCookie(name, value) {
	var today = new Date();
	var expire = new Date();
	//alert(name+'='+value);
	//expire.setTime(today.getTime() + 10);

//	document.cookie = name + "=" + escape(value) + "; expires=" + expire.toGMTString();
	document.cookie = name + "=" + escape(value);
}

// -----------------------------------------------------------------------
//	Retrieves the value of a cookie
// -----------------------------------------------------------------------
function rdi_getCookie(pName){
	var value = "";
	var pCOOKIES = new Array();
	pCOOKIES = document.cookie.split('; ');
	for(i = 0; i < pCOOKIES.length; i++){
		NmeVal  = new Array();
		NmeVal  = pCOOKIES[i].split('=');
		if(NmeVal[0] == pName){
			value = unescape(NmeVal[1]);
		}
	}
	return value;
}

// -----------------------------------------------------------------------
//	delete the cookie for the specified name
// -----------------------------------------------------------------------
function rdi_deleteCookie(name) {
	document.cookie = name + "=; expires=Fri, 31 Dec 1999 23:59:59 GMT;";
}


// -----------------------------------------------------------------------
//	End of RDI Supporting function
// -----------------------------------------------------------------------

//
//
//
function rdi_onLoad() {

	if(document.getElementById("selcolor")) {

		//	Get the color option id
		if (document.getElementById('selcolor').hasChildNodes()) {
			var children = document.getElementById('selcolor').childNodes;

			// Loop through the childern
			for (var c = 0; c < children.length; c++) {
				if (children[c].nodeName == 'SELECT') {
					var selID = children[c].id;
					var colorOptId = children[c].value;
				}
			}
		}

		var option = document.getElementById(selID).value;
//		var swatch_div = document.getElementById('swatch' + option);

		var swatch_div = document.getElementById('swatch' + colorOptId);

			
		for(var i = 0; i < document.getElementById(selID).length; i++) {
			if(document.getElementById(selID)[i].value == option) {
				var imageIndex = i;
				break;
			}
		}

		var varThumb = document.getElementById('smcolor' + imageIndex);
		if(varThumb.length > 0)
			document.getElementById("product_thumbnail").src = varThumb.src;

	}

	if(document.getElementById("ddinput")) {
		if(document.getElementById('addtocart')) {
			document.getElementById('addtocart').disabled = true;
			document.getElementById('a2c_img').src = images_dir + '/addtocartoff.jpg';
		}
	}

}


// -----------------------------------------------------------------------
//	Changes the color and the images for the color
// -----------------------------------------------------------------------
function changeIt( selectId, selectOpt, selectCl ) {
	var prodThumb = document.getElementById("product_thumbnail");
	var testor = document.getElementById('po' + selectId);

	if (selectCl > -1) {
		var swatch_div = document.getElementById('swatch' + selectCl);
		var varThumb = document.getElementById('smcolor' + selectOpt);
		prodThumb.src = varThumb.src;
	}
	else {

		var option = document.getElementById('po' + selectId).value;
		var swatch_div = document.getElementById('swatch' + option);

		for(var i = 0; i < document.getElementById('po' + selectId).length; i++) {
			if(document.getElementById('po' + selectId)[i].value == option) {
				var imageIndex = i;
				break;
			}
		}

		var varThumb = document.getElementById('smcolor' + imageIndex);
		prodThumb.src = varThumb.src;
	}

	if (selectOpt > -1)
		testor.selectedIndex = selectOpt;


	if (document.getElementById('swatches').hasChildNodes()) {
		var children = document.getElementById('swatches').childNodes;

		// Loop through the childern
		for (var c = 0; c < children.length; c++) {
			if (children[c].style && children[c].nodeName == 'DIV') {
				children[c].style.display = 'none';
			}
		}
	}

	if (selectCl > -1) {
		if(document.getElementById('swatch' + selectCl))
			swatch_div.style.display="block";
	}
	else {
		var option = document.getElementById('po' + selectId).value;
		if(document.getElementById('swatch' + option))
			swatch_div.style.display="block";

	}

  

	if(document.getElementById("ddinput")) {
		document.getElementById('ddinput').value = 'select a size';
		if(document.getElementById('addtocart')) {
			document.getElementById('addtocart').disabled = true;
			document.getElementById('a2c_img').src = images_dir + '/addtocartoff.jpg';
		}
	}

	check_options();
};

// -----------------------------------------------------------------------
//	Show the correct size chart based on the which sizes are avaliable
//	for a specific color
// -----------------------------------------------------------------------
function showSizeChart() {

	//	Get the color option id
	if (document.getElementById('selcolor').hasChildNodes()) {
		var children = document.getElementById('selcolor').childNodes;

		// Loop through the childern
		for (var c = 0; c < children.length; c++) {
			if (children[c].nodeName == 'SELECT') {
				var colorOptId = children[c].value;
			}
		}
	}

	colorOption = colorOptId;
	document.getElementById('ddselect_' + colorOptId).style.display="block";
	document.getElementById('ddselect_' + colorOptId).focus();
};

// -----------------------------------------------------------------------
//	This changed the size in the stock x-cart size frop down selection
// -----------------------------------------------------------------------
function changeSize(selection, optionID) {

	// Set the size select to the correct selection
	var sizeOpt = document.getElementById('po' + optionID);

	for (i=0; i<sizeOpt.options.length; i++) {
		if (sizeOpt.options[i].text == selection) {
			sizeOpt.value = sizeOpt.options[i].value;
			var currOpt = sizeOpt.options[i].value;
			break;
		}
	}

	// Display size information in the text box
	switch(selection) {
		case 33:
			document.getElementById('ddinput').value = '33 - 5 - 35';
			break;
		case 34:
			document.getElementById('ddinput').value = '34 - 5.5-6 - 36';
			break;
		case 35:
			document.getElementById('ddinput').value = '35 - 6.5 - 37';
			break;
		case 36:
			document.getElementById('ddinput').value = '36 - 7 - 38';
			break;
		case 37:
			document.getElementById('ddinput').value = '37 - 7.5-8 - 39';
			break;
		case 38:
			document.getElementById('ddinput').value = '38 - 8.5-9 - 40';
			break;
		case 39:
			document.getElementById('ddinput').value = '39 - 9.5-10 - 41';
			break;
		case 40:
			document.getElementById('ddinput').value = '40 - 10.5-11 - 42';
			break;
	}

	// Close the size select selection box.
	//	Get the color option id
	if (document.getElementById('selcolor').hasChildNodes()) {
		var children = document.getElementById('selcolor').childNodes;

		// Loop through the childern
		for (var c = 0; c < children.length; c++) {
			if (children[c].nodeName == 'SELECT') {
				var colorOptId = children[c].value;
			}
		}
	}
	document.getElementById('ddselect_' + colorOptId).style.display="none";

	check_options();

	if(document.getElementById('product_avail')) {
		var qtySel = document.getElementById('product_avail').selectedIndex;
		if(document.getElementById('product_avail').options[qtySel].text == 'Out of stock')
			var stockAvail = false;
		else
			var stockAvail = true;
	}

	if(document.getElementById("ddinput")) {
		if(document.getElementById('addtocart') && stockAvail) {
			document.getElementById('addtocart').disabled = false;
			document.getElementById('a2c_img').src = images_dir + '/addtocart.jpg';
		}
		else {
			document.getElementById('addtocart').disabled = true;
			document.getElementById('a2c_img').src = images_dir + '/addtocartoff.jpg';

		}
	}
	
};

// -----------------------------------------------------------------------
//	Image swapper fot the swatch package
// -----------------------------------------------------------------------
function swatchSwap (optionId) {
	var imgId = document.getElementById('swat' + optionId);
	var prodThumb = document.getElementById("product_thumbnail");

	prodThumb.src = imgId.src
};

// -----------------------------------------------------------------------
//	Popup Cart
// -----------------------------------------------------------------------
function cartPopUp() {
	//alert(document.getElementById('cartPop').style.display);

//document.getElementById('test').innerHTML = item_added;
//		document.getElementById('cp_name').innerHTML = 'Hello';


//	if(document.getElementById('redisplay')) {
	if(item_added) {
		// Set the values
		document.getElementById('cp_image').src = rdi_getCookie('cp_image');
		document.getElementById('cp_name').innerHTML = rdi_getCookie('cp_name');
		document.getElementById('cp_color').innerHTML = rdi_getCookie('cp_color');
		document.getElementById('cp_price').innerHTML = rdi_getCookie('cp_price');
		document.getElementById('cp_size').innerHTML = rdi_getCookie('cp_size');
		document.getElementById('cp_qty').innerHTML = rdi_getCookie('cp_qty');
		
		document.getElementById('cartPop').style.display="block";

		item_added = false;
		
		// Remove the cookies
		rdi_deleteCookie('cp_image');
		rdi_deleteCookie('cp_name');
		rdi_deleteCookie('cp_color');
		rdi_deleteCookie('cp_price');
		rdi_deleteCookie('cp_size');
		rdi_deleteCookie('cp_qty');

	}
	else {
		//	Set the values

		if(document.getElementById('product_thumbnail')) 
			document.getElementById('cp_image').src = document.getElementById('product_thumbnail').src;
		else
			document.getElementById('cp_image').src = '';

		document.getElementById('cp_name').innerHTML = document.getElementById('product_name').innerHTML;
		
		//	Get the selected color value
		if (document.getElementById('selcolor').hasChildNodes()) {
			var children = document.getElementById('selcolor').childNodes;

			// Loop through the childern
			for (var c = 0; c < children.length; c++) {
				if (children[c].nodeName == 'SELECT') {
					var selIndex = children[c].selectedIndex;
					var selValue = children[c].options[selIndex].text;
				}
			}
		}
		document.getElementById('cp_color').innerHTML = selValue;
		document.getElementById('cp_price').innerHTML = document.getElementById('product_price').innerHTML;

		// Find the size value
		if(document.getElementById("ddinput")) {
			document.getElementById('cp_size').innerHTML = document.getElementById('ddinput').value.substr(0,2) + ' br';
		}
		else if (document.getElementById('selsize')) {
			//	Get the selected size value
			if (document.getElementById('selsize').hasChildNodes()) {
				var children = document.getElementById('selsize').childNodes;

				// Loop through the childern
				for (var c = 0; c < children.length; c++) {
					if (children[c].nodeName == 'SELECT') {
						var selIndex = children[c].selectedIndex;
						var selValue = children[c].options[selIndex].text;
					}
				}
			}
			document.getElementById('cp_size').innerHTML = selValue;
		}
		else {
			document.getElementById('cp_size').innerHTML = 'Standard';
		}

		var qtySel = document.getElementById('product_avail').selectedIndex;
		document.getElementById('cp_qty').innerHTML = document.getElementById('product_avail').options[qtySel].text;

		// Save to a cookie
		rdi_setCookie('cp_image', document.getElementById('cp_image').src);
		rdi_setCookie('cp_name', document.getElementById('cp_name').innerHTML);
		rdi_setCookie('cp_color', document.getElementById('cp_color').innerHTML);
		rdi_setCookie('cp_price', document.getElementById('cp_price').innerHTML);
		rdi_setCookie('cp_size', document.getElementById('cp_size').innerHTML);
		rdi_setCookie('cp_qty', document.getElementById('cp_qty').innerHTML);

	}
}

// -----------------------------------------------------------------------
//
// -----------------------------------------------------------------------
function cpuCheckout() {
	document.getElementById('cartPop').style.display="none";

}

// -----------------------------------------------------------------------
//
// -----------------------------------------------------------------------
function cpuContinue() {
	document.getElementById('cartPop').style.display="none";

}

// -----------------------------------------------------------------------
//	This changed the size filter section box
// -----------------------------------------------------------------------
function changeSizeFilter(selection, selID, url) {

	// Display size information in the text box
	switch(selection) {
		case 33:
			document.getElementById(selID).value = '33 - 5 - 35';
			break;
		case 34:
			document.getElementById(selID).value = '34 - 5.5-6 - 36';
			break;
		case 35:
			document.getElementById(selID).value = '35 - 6.5 - 37';
			break;
		case 36:
			document.getElementById(selID).value = '36 - 7 - 38';
			break;
		case 37:
			document.getElementById(selID).value = '37 - 7.5-8 - 39';
			break;
		case 38:
			document.getElementById(selID).value = '38 - 8.5-9 - 40';
			break;
		case 39:
			document.getElementById(selID).value = '39 - 9.5-10 - 41';
			break;
		case 40:
			document.getElementById(selID).value = '40 - 10.5-11 - 42';
			break;
	}

	document.getElementById('mm-' + selID).style.display="none"

	//	Set the filter to the selction and display all product od thid value

	//	show the shoes
	if (selID == 'sf268')
		document.location=url + '/home.php?cat=273&size=' + selection;

	//	Show the boots
	if (selID == 'sf249')
		document.location=url+'/home.php?cat=253&size=' + selection;
};

//
//
//
function showSizeFilterCart(selID) {
	document.getElementById('mm-' + selID).style.display="block"
	
}

// -----------------------------------------------------------------------
//	set up the call to get all of the products for a given category
// -----------------------------------------------------------------------
function sizeFilter(url, category) {
	//	Show thsizes for the Shoes category
	if (category == 268) {
		alert(document.getElementById('sf268').text);
		var size = document.getElementById('filtersize268').options[document.getElementById('filtersize268').selectedIndex].text;
		document.location=url+'/home.php?cat=273&size='+size;
		}

		//	Show size for the Boots category
		if (category == 249) {
			var size = document.getElementById('filtersize249').options[document.getElementById('filtersize249').selectedIndex].text;
			document.location=url+'/home.php?cat=253&size='+size;
		}

}

// -----------------------------------------------------------------------
//	Turn on the notify me entry box
// -----------------------------------------------------------------------
function emailNotifyOn() {
	
	document.getElementById('instocknotify').style.display = 'block';
	document.getElementById('namerequired').style.display = 'none';
	document.getElementById('mailrequired').style.display = 'none';

}

// -----------------------------------------------------------------------
//	save some values before going into the product detail page
// -----------------------------------------------------------------------
function productDetail(cat_id, prod_id, anchor) {
	var sort = getURLParams('sort');
	var sortDir = getURLParams('sort_direction');


	if (sort.length > 0) rdi_setCookie('pd_sort', sort);
	if (sortDir.length > 0) rdi_setCookie('pd_direct', sortDir);
	rdi_setCookie('pd_catid', cat_id);
	rdi_setCookie('pd_prodid', prod_id);
	rdi_setCookie('pd_anchor', anchor);

}

// -----------------------------------------------------------------------
//	Check to see ig in the same category and if so set the product id
//	to the current prodcut id
// -----------------------------------------------------------------------
function setNextPrev(prod_id, anchor) {
	var new_cat = getURLParams('cat');
	var old_cat = rdi_getCookie('pd_catid');

	if(old_cat == new_cat) {
		rdi_setCookie('pd_prodid', prod_id);
		rdi_setCookie('pd_anchor', anchor);
	}
	
}

// -----------------------------------------------------------------------
//	take the user back to the product grid and positioned on the last
//	product viewed in the category
// -----------------------------------------------------------------------
function productClose(url) {

	var cat = rdi_getCookie('pd_catid');
	var product = rdi_getCookie('pd_prodid');
	var sort = rdi_getCookie('pd_sort');
	var sortDir = rdi_getCookie('pd_direct');
	var anchor = rdi_getCookie('pd_anchor');

	if (sort.length > 0) rdi_deleteCookie('pd_direct');
	if (sortDir.length > 0) rdi_deleteCookie('pd_sort');
	rdi_deleteCookie('pd_catid');
	rdi_deleteCookie('pd_prodid');
	rdi_deleteCookie('pd_anchor');

	if(sort.length > 0)
		document.location=url + '/home.php?cat=' + cat + '&sort=' + sort + '&sort_direction=' + sortDir;
	else
		document.location=url + '/home.php?cat=' + cat;

}



function getXMLHTTPRequest() {
	try {
		req = new XMLHttpRequest();
	} catch(e) {
  	try {
  		req = new ActiveXObject("Msxml2.XMLHTTP");
  	} catch (e) {
    	try {
    		req = new ActiveXObject("Microsoft.XMLHTTP");
    	} catch (e) {
      	req = false;
    	}	 
  	} 
	}
	return req;
}

var http = getXMLHTTPRequest();

function checkEmail() {
	var email = document.getElementById('cemails');
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(email.value)) {
		alert('Please provide a valid email address');
		Email.focus
		return true;
	}
	else {
		return true;
	}
}

function getMailer() {

	// setup the variables to send
	var myurl = 'rdi_email.php';
	var myRand = parseInt(Math.random()*999999999999999);
	
	var uname = escape(document.getElementById('cname').value);
	var email =  escape(document.getElementById('cemails').value);
	//var contact =  escape(document.getElementById('contact').checked);
//	var product = escape(document.getElementById('product').value);
//	var imgDir = document.getElementById('imgDir').value;
//	var pdf = document.getElementById('pdf').value;
	
	if (uname.length > 0 && email.length > 0) {
		var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if (filter.test(email)) {
//			var modurl = myurl+"?name="+uname+"&email="+email+"&contact="+contact+"&product="+product+"&imgDir="+imgDir+"&pdf="+pdf+"&rand="+myRand;  
			var modurl = myurl+"?name="+uname+"&email="+email+"&rand="+myRand;  
			http.open("GET", modurl, true);
			http.onreadystatechange = useHttpResponse;
			http.send(null);
		}
	}
	else {
		document.getElementById('resp').innerHTML = 'Required fields are not correct.';
	}
}

function useHttpResponse() {
   if (http.readyState == 4) {
    if(http.status == 200) { 
      var mytext = http.responseText;
      
 				document.getElementById('resp').innerHTML = 'you have been added to the list!';
 			document.getElementById('emailhold').style.display="none";
 			//document.getElementById('dlbrochure').removeAttribute('style', 'display');

      // document.getElementById('testDisplay').innerHTML = mytext;
    }
  }
}
