var al=0;

function setCookie(NameOfCookie, value, expiredays, path, domain, secure)
{
  //the first lines in the function converts the number of days to a valid date.

  var ExpireDate = new Date();
  ExpireDate.setTime(ExpireDate.getTime() + (expiredays*24*3600*1000));

  //The next line stores the cookie, simple by assigning
  //the values to the document.cookie-object
  //Note the date is converted to Greenwich Meantime using
  //the 'toGMTstring()'-function

  document.cookie = NameOfCookie +"="+ escape(value) +
  ((expiredays == null)? "": ";expires="+ ExpireDate.toGMTString()) +
  ((path == null)? "": (";path=" + path)) +
  ((domain == null) ? "" : (";domain=" + domein)) +
  ((secure == true) ?";secure":"");
}

function getCookie(NameOfCookie)
{
   if(document.cookie.length > 0)
  {
     begin = document.cookie.indexOf(NameOfCookie+"=");
     if(begin != -1)
     {
        // our cookie was set.
        // The value stored in the cookie is returned from the function
        begin += NameOfCookie.length + 1;
        end = document.cookie.indexOf(";",begin);
        if(end == -1) end = document.cookie.length;
        return unescape(document.cookie.substring(begin,end));
     }
  }
  return null;
  // Our cookie was not set.
  // The value "null" is returned from the function
}

function delCookie(NameOfCookie)
{
// The function simply checks if the cookie is set.
// If so expiredate is set to Jan. 1st 1970
if(getCookie(NameOfCookie)){
document.cookie = NameOfCookie +"=" + ";expires= Thu, 01-Jan-70 00:00:01 GMT";
}
}

/* * This function parses comma-separated name=value argument pairs from * the
query string of the URL. It stores the name=value pairs in  * properties of an
object and returns that object. */
function getArgs() {
	var args = new  Object();

	var query = location.search.substring(1);  // Get query string.
	var pairs = query.split('&');              // Break at &.
	//window.alert("query : " + query);
	//window.alert("pairs : " + pairs);
	for(var i = 0; i < pairs.length; i++) {
		var pos = pairs[i].indexOf('=');    // Look for "name=value".
		if (pos == -1) continue;               // If not found, skip.
			var argname = pairs[i].substring(0,pos);  // Extract the name.
			var value = pairs[i].substring(pos+1);	// Extract the value.
			args[argname] = unescape(value);          // Store as a property.
	}
	return args;         // Return the object.
}


var args= getArgs();


function sanitized(inloc){

	var save = inloc;
	inloc=escape(inloc);

	// step 1
	var regexp = /\%3F/g
	inloc = inloc.replace(regexp, "?");

	// step 2

	regexp = /\%3D/g
	inloc =inloc.replace(regexp, "=");


	// step 3

	regexp = /\%26/g
	inloc =inloc.replace(regexp, "&");

	return inloc;


}



function emailCheck (emailStr) {

/* The following variable tells the rest of the function whether or not
to verify that the address ends in a two-letter country or well-known
TLD.  1 means check it, 0 means don't. */
emailStr=emailStr.toLowerCase();

var checkTLD=1;

/* The following is the list of known TLDs that an e-mail address must end with. */

var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;

/* The following pattern is used to check if the entered e-mail address
fits the user@domain format.  It also is used to separate the username
from the domain. */

var emailPat=/^(.+)@(.+)$/;

/* The following string represents the pattern for matching all special
characters.  We don't want to allow special characters in the address. 
These characters include ( ) < > @ , ; : \ " . [ ] */

var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";

/* The following string represents the range of characters allowed in a 
username or domainname.  It really states which chars aren't allowed.*/

var validChars="\[^\\s" + specialChars + "\]";

/* The following pattern applies if the "user" is a quoted string (in
which case, there are no rules about which characters are allowed
and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
is a legal e-mail address. */

var quotedUser="(\"[^\"]*\")";

/* The following pattern applies for domains that are IP addresses,
rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
e-mail address. NOTE: The square brackets are required. */

var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;

/* The following string represents an atom (basically a series of non-special characters.) */

var atom=validChars + '+';

/* The following string represents one word in the typical username.
For example, in john.doe@somewhere.com, john and doe are words.
Basically, a word is either an atom or quoted string. */

var word="(" + atom + "|" + quotedUser + ")";

// The following pattern describes the structure of the user

var userPat=new RegExp("^" + word + "(\\." + word + ")*$");

/* The following pattern describes the structure of a normal symbolic
domain, as opposed to ipDomainPat, shown above. */

var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

/* Finally, let's start trying to figure out if the supplied address is valid. */

/* Begin with the coarse pattern to simply break up user@domain into
different pieces that are easy to analyze. */

var matchArray=emailStr.match(emailPat);

if (matchArray==null) {

/* Too many/few @'s or something; basically, this address doesn't
even fit the general mould of a valid e-mail address. */

alert("Email address seems incorrect (check @ and .'s)");
return false;
}
var user=matchArray[1];
var domain=matchArray[2];

// Start by checking that only basic ASCII characters are in the strings (0-127).

for (i=0; i<user.length; i++) {
if (user.charCodeAt(i)>127) {
alert("Ths username contains invalid characters.");
return false;
   }
}
for (i=0; i<domain.length; i++) {
if (domain.charCodeAt(i)>127) {
alert("Ths domain name contains invalid characters.");
return false;
   }
}

// See if "user" is valid 

if (user.match(userPat)==null) {

// user is not valid

//alert("The username doesn't seem to be valid.");
return false;
}

/* if the e-mail address is at an IP address (as opposed to a symbolic
host name) make sure the IP address is valid. */

var IPArray=domain.match(ipDomainPat);
if (IPArray!=null) {

// this is an IP address

for (var i=1;i<=4;i++) {
if (IPArray[i]>255) {
alert("Destination IP address is invalid!");
return false;
   }
}
return true;
}

// Domain is symbolic name.  Check if it's valid.
 
var atomPat=new RegExp("^" + atom + "$");
var domArr=domain.split(".");
var len=domArr.length;
for (i=0;i<len;i++) {
if (domArr[i].search(atomPat)==-1) {
alert("The domain name does not seem to be valid.");
return false;
   }
}

/* domain name seems valid, but now make sure that it ends in a
known top-level domain (like com, edu, gov) or a two-letter word,
representing country (uk, nl), and that there's a hostname preceding 
the domain or country. */

if (checkTLD && domArr[domArr.length-1].length!=2 && 
domArr[domArr.length-1].search(knownDomsPat)==-1) {
alert("The address must end in a well-known domain or two letter " + "country.");
return false;
}

// Make sure there's a host name preceding the domain.

if (len<2) {
alert("This address is missing a hostname!");
return false;
}

// If we've gotten this far, everything's valid!
return true;
}

//  End -->

function subm(inloc,op_require){
	// validate email
	
	var val = document.forms[0].email.value;
	var lzone = document.forms[0].lzone_val.value;
	//alert(lzone);
	var vali = document.forms[0].moreinfo.value;
	//var vald = document.forms[0].deliverymode.options[document.forms[0].deliverymode.selectedIndex].value;
	vald = '';

       p=val.indexOf('@');
              
   if (!emailCheck(val) && (op_require)){

      alert('- email field must contain a valid e-mail address.\n- If you do not want to provide an email, click "Anoynymous".');
		document.forms[0].email.focus();
		document.forms[0].email.select();
		return false;
	}
	else{
	
		l = location.search.substring(1);
		inOrderID = '';

		if (l.indexOf("&inOrderID=")>-1){
			inOrderID = args.inOrderID;
		}

		ex = getCookie("eol__exit");
		
		if (document.forms[0].x_autolog.checked){
			setCookie("eol__email",val,180);
			setCookie("eol__autolog","true",180);
		}
		else{
			setCookie("eol__autolog","false",180);		
		}

		if (inOrderID.length>0){

			inloc = '/eolengine/exe/entry.php?t=Menu3.html&pathx=common&flava=red';

			document.forms[0].action = sanitized(inloc + "&email=" + val + "&deliverymode=" + vald + "&moreinfo=" + vali+"&inOrderID=" + args.inOrderID + "&inOrderID_getinfo=yes" + "&ex=" + ex + "&al=" + al + "&lzone=" + lzone);
		}else{

			document.forms[0].action = sanitized(inloc + "&email=" + val + "&deliverymode=" + vald + "&moreinfo=" + vali+"&inOrderID=" + args.inOrderID + "&ex=" + ex + "&al=" + al + "&lzone=" + lzone);
		}

		document.forms[0].submit();
		return true;
	}
}


function anonlogin(){
	// 20090903
	//anonn confirmation is optional.  If args[0] is false then no pop up apppears 
	 //window.alert(arguments[0]);
	 if (typeof(arguments[0])!='undefined'){
		popup=arguments[0];
		//alert('hola');
	 }else{
		 popup=true;
	 }
	 if (popup){
		 anon_next =  confirm("Feel free to browse anonymously, \nhowever you will have to provide a valid email address \nin order to redeem coupons and place a real order.\nContinue anonymously?");
	 }else{
		 anon_next=true;
	 }
	 
	if (anon_next){
		setCookie("eol__autolog","false",180);
		document.forms[0].email.value = "anon@anon.com";

		l = location.search.substring(1);
		inOrderID = '';

		if (l.indexOf("&inOrderID=")>-1){
			inOrderID = args.inOrderID;
		}


		if (inOrderID.length>0){
			inloc = '/eolengine/exe/entry.php?t=Menu3.html&pathx=common&inOrderID_getinfo=yes';
			xinOrderID = args.inOrderID;
		}else{
			inloc = "/eolengine/exe/entry.php?flava=red&t=entry2.html&pathx=common";
			xinOrderID="";
		}

		document.forms[0].action = sanitized(inloc + "&email=" + "anon@anon.com" + "&deliverymode=" + "anon"+"&inOrderID=" + xinOrderID);
		document.forms[0].submit();
	}
}

function firsttimers(ridm){

	var alog = getCookie("eol__exit");;
	var rst = args.eol__resetcookie;
	if (rst == "null" || rst == null){
		rst = false;
	}else{
		rst = (rst == "yes");
	}
	//rst = false;

	if( (alog == "null" || alog == null) && !rst){
		var outs = "<center><img src='../coupon/images/" + ridm + "_newuser.gif'></center>";
		document.write(outs);

	}


}


function cookiestuff(){

	if(args.eol__resetcookie=="yes"){
		delCookie("eol__email");
		delCookie("eol__autolog");
	}
	else{

		q = location.search.substring(1);
		var temail = '';
		if (q.indexOf("__email__")>-1){
			temail = args.__email__;

		}

		if (temail.length>0){
			al=1;
			document.forms[0].email.value = temail;
			document.forms[0].x_autolog.checked = true;
			setCookie("eol__email",temail,180);
			setCookie("eol__autolog","true",180);
			subm('/eolengine/exe/entry.php?flava=red&t=entry2.html&pathx=common',-1);

		}else{
			em = getCookie("eol__email");
			alog = getCookie("eol__autolog");

			//alert(em + " :hola: " + alog);

			if(alog == "false"){
				document.forms[0].x_autolog.checked = false;
			}else{
				if(em != "null" && em != null){
					//alert(em);
					document.forms[0].email.value = em;
					al=1;
					subm('/eolengine/exe/entry.php?flava=red&t=entry2.html&pathx=common',-1);
				}
			}
		}
	}
	document.forms[0].email.select();
	document.forms[0].email.focus();


}

function datestuff(){

var months=new Array(13);
months[1]="January";
months[2]="February";
months[3]="March";
months[4]="April";
months[5]="May";
months[6]="June";
months[7]="July";
months[8]="August";
months[9]="September";
months[10]="October";
months[11]="November";
months[12]="December";
var time=new Date();
var lmonth=months[time.getMonth() + 1];
var date=time.getDate();
var year=time.getYear();

if ((navigator.appName == "Microsoft Internet Explorer") && (year < 2000))		
year="19" + year;
if (navigator.appName == "Netscape")
year=1900 + year;
document.write("<div align='left'>..<font color='#000000'><left>" + lmonth + " ");
document.write(date + ", " + year + "</left></font></div>");


}

function hotspecial(inx){
   alert('Please log in first');
   document.forms[0].email.focus();
}





