var xmlHttp;

function init_dd() {
	if (document.getElementById("cat_1") && testSessionCookie()) {
		var value = getCookieValue("dd");
		if(value != false) {
			var values = value.split('_');
			for (var i=0; i<values.length; i++) {
				show_data(values[i], false);
			}
		}
	}
}

function show_data(div_id, cookies) {
	var cookies = (cookies == null) ? true : cookies;
	if (document.getElementById('sub_' + div_id))  {
		if(testSessionCookie() && cookies) {	
			var value = getCookieValue("dd");
			if(value != false) {
				//cookie already set
				value += "_"+div_id;
				deleteCookie("dd");
				writeSessionCookie("dd",value);
			}
			else {
				writeSessionCookie("dd",div_id);
			}
		}
		document.getElementById('sub_'+ div_id).style.display="block";
		document.getElementById('cat_'+ div_id).style.backgroundImage="url(/images/icons/bullet_toggle_minus.png)";
	}
}

function hide_data(div_id, cookies) {
	var cookies = (cookies == null) ? true : cookies;
	if (document.getElementById('sub_' + div_id)) {
		if(testSessionCookie() && cookies) {	
			var value = getCookieValue("dd");
			if(value != false) {
				//cookie is set
				var values = value.split('_');
				var newvalue = "";
				for (var i=0; i<values.length; i++) {
					if(values[i] != div_id) {
						newvalue += values[i] + "_";
					}
				}
				deleteCookie("dd");
				if(newvalue.length > 1) {
					newvalue = newvalue.substr(0,newvalue.length-1)
					writeSessionCookie("dd",newvalue);
				}
			}
		}
		document.getElementById('sub_'+ div_id).style.display="none";		
		document.getElementById('cat_'+ div_id).style.backgroundImage="url(/images/icons/bullet_toggle_plus.png)";
	}
}

function toggle_data(div_id) 
	{
	thisDiv = document.getElementById('sub_'+ div_id);
	if (thisDiv)
		if (thisDiv.style.display == "none") show_data(div_id);
		else hide_data(div_id);
	}

function toggle_subsection(div_id) { toggle_data(div_id); }

 function validate(email)
 {
	if(email == "")
	{
		alert("Email is required");
		return false;
	}

	if(email.search(/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/gi) == -1) {
		alert("The E-Mail Address does not appear to be a valid e-mail address. Address must be in the standard format, including domain and extension.");
		return false;
	}
	return true;	
 }
function addslashes(str) {
str=str.replace(/\'/g,'\\\'');
return str;
}
function stripslashes(str) {
str=str.replace(/\\'/g,'\'');
return str;
}
function setUnsubscribe(brand, id, list) {
	var mailbox = document.getElementById("mailbox");
	// This line was messed up, which is why the form was submitting and page was refreshing. The form was getting garbled from ' and " and \" and whatnot.
	// The 'OnSubmit' part needs to return the result of the unsubscribe, since OnSubmit will submit the form based off a true or false response
	mailbox.innerHTML = "<div class='header'>Unsubscribe from Mailing List</div><h3 align='left'><form action='' method='post' OnSubmit='return unsubscribe(\""+list+"\");' ><strong>Email:</strong> <input name='email' id='email' type='text' />	<input type='image' src='/images/content/unsubscribe.png' alt='Sign Up' border='0'></form></h3><h4 align='right' style='padding-top: 20px;'><a href='/customer/pages.php?pageid=50'>Privacy Statement</a> <a href='#' onclick='setSubscribe(\""+brand+"\", \""+id+"\", \""+list+"\"); return false;'>Subscribe</a></h4>";
}
function setSubscribe(brand, id, list) {
	var mailbox = document.getElementById("mailbox");
	mailbox.innerHTML = "<div class='header'>Want exclusive deals from Getzs.com?</div><h3 align='left' style=\"padding:5px; padding-bottom: 15px; color: #666666; font-family: 'Times New Roman', Times, serif; font-size: 12px; margin:0;\">Be the first to hear about great sales and new products. Sign up today!<br><br></h3><form action='' method='post' OnSubmit='return subscribe(\""+list+"\");' ><h3 align='left' style='margin-top:-10px; margin-bottom: 25px;'><strong>Email:</strong> <input name='email' id='email' type='text' /> <strong>Name:</strong> <input name='fullname' id='fullname' type='text' size='18' /><input type='image' src='/images/content/sign_up.png' alt='Sign Up' border='0'></form></h3><h4 align='right'><a href='/customer/pages.php?pageid=50'>Privacy Statement</a> <a href='#' onclick='setUnsubscribe(\""+addslashes(brand)+"\", \""+id+"\", \""+list+"\"); return false;'>Unsubscribe</a></h4>";
}
function createXMLHttpRequest() {
	if (window.ActiveXObject) {
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else if (window.XMLHttpRequest) {
		xmlHttp = new XMLHttpRequest();
	}
}

function unsubscribe(list) {
	createXMLHttpRequest();
	var email = document.getElementById("email").value;
	if(!validate(email)) return false;
	var query = "/customer/ajax/brand_page.php?email="+email+"&list="+list+"";
	xmlHttp.onreadystatechange = handleResponse;
	xmlHttp.open("GET", query, true);
	xmlHttp.send(null);
	
	// If you don't return false, then the form submits. I don't think we want that here?
	return false;
}

function subscribe(list, brandid, v) {
	createXMLHttpRequest();
	var email = document.getElementById("email").value;
	if(!validate(email)) return false;
	var query = "/customer/ajax/brand_page.php?email="+email+"&list="+list+"&fullname="+name+"&brandid="+brandid+"&v="+v;
	if(v == "3" || v == "4") {
		xmlHttp.onreadystatechange = handleResponseSub3;
	}
	else {
		xmlHttp.onreadystatechange = handleResponseSub;
	}
	xmlHttp.open("GET", query, true);
	xmlHttp.send(null);
	document.getElementById('email_response').innerHTML = "Thank You for signing up for our mailing list."
	// If you don't return false, then the form submits. I don't think we want that here?
	return false;
}

function handleResponseSub3() {
	var mailbox = document.getElementById("email_response");
	if (xmlHttp.readyState == 1) {
		mailbox.innerHTML = "Please Wait...";
	}
	if (xmlHttp.readyState == 4) {
		if (xmlHttp.status == 200) {
			var result = xmlHttp.responseText;
			mailbox.innerHTML = result;
		}
	}
}
function handleResponseSub() {
	var mailbox = document.getElementById("mailbox");
	if (xmlHttp.readyState == 1) {
		mailbox.innerHTML = "<h1>Sign up for Mailing List</h1><br><h3 align=\"left\">Please Wait...</h3><h4 align=\"right\"><a href=\"/customer/pages.php?pageid=50\">Privacy Statement</a></h4><br>";
	}
	if (xmlHttp.readyState == 4) {
		if (xmlHttp.status == 200) {
			var result = xmlHttp.responseText;
			mailbox.innerHTML = "<h1>Sign up for Mailing List</h1><h4 align=\"right\"><a href=\"#\" onclick=\"closeBox(); return false;\"><img src=\"/images/icons/action_stop.gif\" border=\"0\" alt=\"Close Box\" /></a></h4><br><h3 align=\"left\">"+result+"</h3><h4 align=\"right\"><a href=\"/customer/pages.php?pageid=50\">Privacy Statement</a></h4><br>";
		}
	}
}
function handleResponse() {
	var mailbox = document.getElementById("mailbox");
	if (xmlHttp.readyState == 1) {
		mailbox.innerHTML = "<h1>Unsubscribe from Mailing List</h1><br><h3 align=\"left\">Please Wait...</h3><h4 align=\"right\"><a href=\"/customer/pages.php?pageid=50\">Privacy Statement</a></h4><br>";
	}
	if (xmlHttp.readyState == 4) {
		if (xmlHttp.status == 200) {
			var result = xmlHttp.responseText;
			mailbox.innerHTML = "<h1>Unsubscribe from Mailing List</h1><h4 align=\"right\"><a href=\"#\" onclick=\"closeBox(); return false;\"><img src=\"/images/icons/action_stop.gif\" border=\"0\" alt=\"Close Box\" /></a></h4><br><h3 align=\"left\">"+result+"</h3><h4 align=\"right\"><a href=\"/customer/pages.php?pageid=50\">Privacy Statement</a></h4><br>";
		}
	}
}
function closeBox() {
	document.getElementById("mailboxHolder").innerHTML = "";
}

function changePromo(a,n,l,t,p) {
	play = p;
	
	var e = document.getElementById("promo_selector");
	var h = document.getElementById("promo_link");
	h.href=l;
	var r = "<ul>";
	for(var i=0; i<n; i++) {
		if(i==a) {
			r+="<li id='active'><a href='#'>"+(i+1)+"</a></li>";
		}
		else {
			r+="<li><a href='#' onclick='changePromo(\""+i+"\",\""+n+"\",\""+isf.links[i]+"\",\""+escape(isf.alts[i])+"\","+false+"); return false;'>"+(i+1)+"</a></li>";
		}
	}
	r+="</ul>";
	e.innerHTML = r;
	swapfade(document.getElementById('promo'), isf.imgs[a], '1', t);
}

function slideit() {		
	if(!play) { 
		clearInterval(promo_rotate);
		return false;
	}
	if(step<num_promos-1) step++;
	else step=0;
	changePromo(step,num_promos,isf.links[step],isf.alts[step],play);
	//setTimeout("slideit()",8000)
}

function emailFocus(field) {
	if (field.value.match('Enter email address here...')) {
    field.value = '';
  }
}

function emailBlur(field) {
  if (field.value.length == 0) {
    field.value = 'Enter email address here...';
    }
  }  
