function loginWindow(){
	var login = document.createElement("div");
	login.setAttribute("class","login");
	login.setAttribute("id","loginWindow");
	var form = document.createElement("form");
	var formTitle = document.createElement("h2");
	formTitle.setAttribute("align","center");
	var titleText = document.createTextNode("Please Login:");
	formTitle.appendChild(titleText);
	var fieldName = document.createElement("input");
	var fieldPassword = document.createElement("input");
	fieldName.setAttribute("name","id");
	var nameLabel = document.createElement("label");
	nameLabel.setAttribute("for","id");
	var nameText = document.createTextNode("Email:");
	nameLabel.appendChild(nameText);
	var cancelButton = document.createElement("input");
	cancelButton.setAttribute("type","button");
	cancelButton.setAttribute("value","Cancel");
	cancelButton.setAttribute("onclick","document.getElementById('loginWindow').className='hide'");
	form.appendChild(formTitle);
	form.appendChild(nameLabel);
	form.appendChild(fieldName);
	form.appendChild(cancelButton);
	login.appendChild(form);
	var ctr = document.getElementById("container");
	var attach = ctr.appendChild(login);
}
function checkLogin(form){
	var validmail = /^[\w\.\-_]+@[A-Za-z0-9.\-_]+\.[A-Za-z0-9]+/;
	if (!form.id.value.match(validmail)){
		alert("This is not a valid email address");
		form.id.focus();
		return false;
	}
}
function checkSignUp(form){
	var validzip = /^\d{5}$/;
	var countrycheck = 0;
	for (var x=0; x<form.elements.length; x++){
		if ((form.elements[x].type == "radio") && (form.elements[x].checked)){
			countrycheck++;
		}
	}
	if (countrycheck == 0){
		alert("Please select the country where you live.");
		return false;
	}
	var usa = document.getElementById("usa");
	if ((usa.checked) && (!form.zip.value.match(validzip))){
		alert("This is not a valid USA zip code.");
		form.zip.focus();return false;
	}
	var shortzip = form.zip.value.substring(0,3);
	for (var i=0; i< xzip.length; i++){
		if (shortzip == xzip[i]){
			form.action = "cd_pages/sorry.php?action=zip&zip=" + xzip[i];
			return true;
		}
	}
}

function whatis(it){
	var ajaxResponse = it.responseText;
	var container = document.getElementById("container");
	var ref = document.createElement("div");
	ref.setAttribute("id","ref");
	ref.className = "refwin";
	var closebar = document.createElement("div");
	closebar.className = "closebox";
	closebar.innerHTML = "<a href='javascript:closeref()'>CLOSE [X]</a>";
	var refgif = document.createElement("div");
	refgif.className = "refimg";
	refgif.innerHTML = ajaxResponse;
	ref.appendChild(closebar);
	ref.appendChild(refgif);
	container.appendChild(ref);
}

function updateCSAmount(params){
	// params is XML format.
	var xml = params.responseXML;
	var hiddenfields = xml.getElementsByTagName("input");
	for (var x = 0;  x < hiddenfields.length; x++){
		var name = hiddenfields[x].getAttribute("name");
		var value = hiddenfields[x].getAttribute("value");
		createHiddenField(name,value);
	}
}

function closeref(){
	var container = document.getElementById("container");
	var ref = document.getElementById("ref");
	container.removeChild(ref);
}

function ajax(query,fn,async){
   var request = false;
	if (typeof window.ActiveXObject != 'undefined')
	{
	  try { request = new ActiveXObject('Microsoft.XMLHTTP'); }
	  catch (err) { request = null; }
	}
	else if (typeof window.XMLHttpRequest != 'undefined')
	{
	  request = new XMLHttpRequest();
	}
	if (request != null){
		request.onreadystatechange = function(){
		if (request.readyState == 4){
			if (request.status == 200){
				fn(request);
			}
		}
	  }
	  request.open("GET", query, async);
	  request.send(null);
	}
}

function roundTo(base, precision){
	var m = Math.pow(10, precision);
	var a = Math.round(base * m) / m;
	return a;
}
function formatTo(base, precision){
	var a = roundTo(base, precision);
	var s = a.toString();
	
	var decimalIndex = s.indexOf(".");
	if (precision > 0 && decimalIndex < 0){
		decimalIndex = s.length;
		s += ".";
	}
	while (decimalIndex + precision + 1> s.length){
		s += "0";
	}
	return s;
}

function disableSubmit(whichButton) { 
	if (document.getElementById){ 
		// this is the way the standards work 
		document.getElementById(whichButton).disabled = true; 
	} else if (document.all) { 
		// this is the way old msie versions work 
		document.all[whichButton].disabled = true; 
	} else if (document.layers) { 
		// this is the way nn4 works 
		document.layers[whichButton].disabled = true; 
	} 
}

function enableSubmit(whichButton) { 
	if (document.getElementById){ 
		// this is the way the standards work 
		document.getElementById(whichButton).disabled = false; 
	} else if (document.all) { 
		// this is the way old msie versions work 
		document.all[whichButton].disabled = false; 
	} else if (document.layers) { 
		// this is the way nn4 works 
		document.layers[whichButton].disabled = false; 
	} 
}

function updateLastSignUp(req){
	var customer = req.responseText;
	var users = document.getElementById("users");
	if (users){
		users.innerHTML = customer;
	}
}

function updateUsers(){
	ajax("http://www.cookiedietonline.com/cd_pages/en/last_signup.php",updateLastSignUp,true);
	setInterval(function(){
		ajax("http://www.cookiedietonline.com/cd_pages/en/last_signup.php",updateLastSignUp,true);
	}, 10000);
}

function updateNow(){
}

function addLoadListener(fn)
{
  if (typeof window.addEventListener != 'undefined')
  {
    window.addEventListener('load', fn, false);
  }
  else if (typeof document.addEventListener != 'undefined')
  {
    document.addEventListener('load', fn, false);
  }
  else if (typeof window.attachEvent != 'undefined')
  {
    window.attachEvent('onload', fn);
  }
  else
  {
    var oldfn = window.onload;
    if (typeof window.onload != 'function')
    {
      window.onload = fn;
    }
    else
    {
      window.onload = function()
      {
        oldfn();
        fn();
      };
    }
  }
}
