
// -------------------------------------------------------------------
// -------------------------------------------------------------------
// General functions
// -------------------------------------------------------------------
// -------------------------------------------------------------------

/* ----------------------------------------------------------------
   set_bg_color(color) - changes the background colore of a window
   ---------------------------------------------------------------- */

function set_bg_color(color)
{
	document.bgColor = color;
}

/* ----------------------------------------------------------------
   comments page: external links open in new window
   ---------------------------------------------------------------- */

function external_links()
{
	if (!document.getElementsByTagName) return;
	
	var anchors = document.getElementsByTagName("a");
	
	for (var i=0; i<anchors.length; i++)
	{
		var anchor = anchors[i];

		if ( anchor.getAttribute("href") )
		{
			switch( anchor.getAttribute("rel") )
			{
				case 'nofollow':	
						anchor.target = "_new";
						break;
				case 'external':	
						anchor.target = "_new";
						break;
				case 'internal':
						anchor.target = "_self";
						break;
				case 'opener': 
						anchor.target = "everyday-wonders";
						break;				
			}
		}
	}
}

/* ----------------------------------------------------------------
   open popup window ( comments, feedback form, contact form )
   ---------------------------------------------------------------- */

function open_popup_window ( link, name ) 
{
	window.name = "everyday-wonders";

	window.open( link, name, 'toolbar=no, directories=no, location=no, status=yes, menubar=no, resizable=yes, scrollbars=yes, width=700, height=600'); 
}

/* ----------------------------------------------------------------
   validate comment form
   ---------------------------------------------------------------- */

function validate_form()
{
	document.comment_form.email.value = trim( document.comment_form.email.value );

	if ( document.comment_form.comment.value )
	{
		document.comment_form.comment.value = format_comment( document.comment_form.comment.value );
	}

	/* ----------------------------------------------------------------------
	       Check email address:
	       First condition - name was not entered
	       Second condition - syntax of the email is incorrect
	   ---------------------------------------------------------------------- */
	
	if ( ( document.comment_form.name.value == "" ) && ( document.comment_form.comment.value == "" ) )
	{
		alert ("Please enter your name and a comment.");
        	document.comment_form.name.focus ();
        	document.comment_form.name.select ();		
		return false;
	}
	else if ( ( document.comment_form.name.value == "" ) && document.comment_form.comment.value )
	{
        	alert ("Please enter your name.");
        	document.comment_form.name.focus ();
        	document.comment_form.name.select ();
        	return false;
	}
	else if	( document.comment_form.name.value && ( document.comment_form.comment.value == "" ) )
	{
		alert ("Please enter your comment.");
       		document.comment_form.comment.focus ();
       		document.comment_form.comment.select ();
       		return false;
    	}
	else if ( ! document.comment_form.spam_guard.checked )
	{
		alert ("Please check the spam guard checkbox.");
		document.comment_form.spam_guard.focus ();
       		document.comment_form.spam_guard.select ();
       		return false;
	}     

    	if ( ( document.comment_form.email.value != "" ) && ! validate_email( document.comment_form.email.value ) )
    	{
        	alert ( "Email address is incorrect: " + document.comment_form.email.value );
        	document.comment_form.email.focus ();
        	document.comment_form.email.select ();
        	return false;
    	}

    	return true;
}

/* ----------------------------------------------------------------
   validate new image form
   ---------------------------------------------------------------- */

function validate_new_image_form()
{
	if ( document.new_image_form.comment.value )
	{
		document.new_image_form.comment.value = format_comment( document.new_image_form.comment.value );
	}

	if ( ! document.new_image_form.spam_guard.checked )
	{
		alert ("Please check the spam guard checkbox.");
		document.new_image_form.spam_guard.focus ();
       		document.new_image_form.spam_guard.select ();
       		return false;
	}     

    	return true;
}
function format_comment( comment_text )
{
	var re_1 = /\n\r/g;
	var re_2 = /\n/g;

	comment_text = comment_text.replace( re_1, "<br><br>" );	
	comment_text = comment_text.replace( re_2, "<br>" );
	comment_text = comment_text.replace( " & ", " &amp;" );

	return comment_text;	
}

function trim ( stringText )
{
    // this will get rid of leading spaces

    while ( stringText.substring(0, 1) == ' ' )
    {
        stringText = stringText.substring(1, stringText.length);
    }

    // this will get rid of trailing spaces

    while ( stringText.substring(stringText.length-1, stringText.length) == ' ')
    {
        stringText = stringText.substring(0, stringText.length-1);
    }

    return stringText;
}

function validate_email( email )
{
    invalid_chars = /[^A-Za-z0-9_@.-]/g; // regular expression - all non alphanumeric
                                        // characters excluding '_', '@', and '.'
    var dot_position = -1;

    /* ----------------------------------------------------------------------
       Check if there are invalid characters in the email address. If there
       are then return false otherwise go on and check how the email address
       is structured.
       ---------------------------------------------------------------------- */

    if ( invalid_chars.test( email ) )
        return false;

    var position = email.indexOf("@"); // check if there is '@' in email address

    /* ----------------------------------------------------------------------
       First condition is that there must be a '@' symbol in the email
       address - not satisfied then return false
       Second condition is that ther must be only one '@' symbol in the
       address - not satisfied then return false.
       ---------------------------------------------------------------------- */

    if ( (position == -1) || (email.indexOf("@", position + 1) > 0) )
    {
        return false;
    }

    while (position > 0) // loop through and get the position of the last '.' in email
    {
        position = email.indexOf(".", position + 1);

        if (position > 0)
            dot_position = position;
    }

    /* ----------------------------------------------------------------------
       First condition is that a '.' must be after '@' - return false if not
       satisfied.
       Second condition is that after the last '.' there must be at least 2
       more characters - return false if not satisfied.
       ---------------------------------------------------------------------- */

    if ( (dot_position == -1) || ( (dot_position + 3) > email.length ) )
    {
        return false;
    }

    return true;
}



/* ----------------------------------------------------------------
   process shopping cart info
   ---------------------------------------------------------------- */
function Dollar (val) {  // force to valid dollar amount
var str,pos,rnd=0;
  if (val < .995) rnd = 1;  // for old Netscape browsers
  str = escape (val*1.0 + 0.005001 + rnd);  // float, round, escape
  pos = str.indexOf (".");
  if (pos > 0) str = str.substring (rnd, pos + 3);
  return str;
}

function ReadForm (obj1) { // process selects
var i,j,amt=0, size="", des="",obj,pos,tok,val,
    op1a="",op1b="",op2a="",op2b="",itmn="";
var ary = new Array ();
var eml1 = "oddfolio";
var eml2 = "yahoo";
var eml3 = "com";

  if (obj1.baseamt) amt  = obj1.baseamt.value*1.0;  // base amount
  if (obj1.basedes) des  = obj1.basedes.value;  // base description
  if (obj1.baseon0) op1a = obj1.baseon0.value;  // base options
  if (obj1.baseos0) op1b = obj1.baseos0.value;
  if (obj1.baseon1) op2a = obj1.baseon1.value;
  if (obj1.baseos1) op2b = obj1.baseos1.value;
  if (obj1.baseitn) itmn = obj1.baseitn.value;
  for (i=0; i<obj1.length; i++) {     // run entire form
    obj = obj1.elements[i];           // a form element
    if (obj.type == "select-one") {   // just get selects
      if (obj.name == "quantity" ||   // don't mess with these
          obj.name == "amount") continue;
      pos = obj.selectedIndex;        // which option selected
      val = obj.options[pos].value;   // selected value
      ary = val.split (" ");          // break apart
      for (j=0; j<ary.length; j++) {  // look at all items
// first we do single character tokens...
        if (ary[j].length < 2) continue;
        tok = ary[j].substring (0,1); // first character
        val = ary[j].substring (1);   // get data
        if (tok == "@")
	{
		amt = val * 1.0;
		ary[j] = "";  
	}

        if (tok == "z")
	{
		val = val.replace( "x", "\" x " );
		op1b = op1b + " " + val + "\"";
		ary[j] = "";  
	}

        if (tok == "+") amt = amt + val*1.0;
        if (tok == "%") amt = amt + (amt * val/100.0);
        if (tok == "#") {             // record item number
          if (obj1.item_number) obj1.item_number.value = val;
          ary[j] = "";                // zap this array element
        }
// Now we do 3-character tokens...
        if (ary[j].length < 4) continue;
        tok = ary[j].substring (0,3); // first 3 chars
        val = ary[j].substring (3);   // get data
        if (tok == "s1=") {           // value for shipping
          if (obj1.shipping)  obj1.shipping.value  = val;
          ary[j] = "";                // clear it out
        }
        if (tok == "s2=") {           // value for shipping2
          if (obj1.shipping2) obj1.shipping2.value = val;
          ary[j] = "";                // clear it out
        }
      }
      val = ary.join (" ");           // rebuild val with what's left

      if (obj.name == "on0" ||        // let these go where they want
          obj.name == "os0" ||
          obj.name == "on1" ||
          obj.name == "os1") continue;

      tag = obj.name.substring (obj.name.length-2);  // get flag
      if      (tag == "1a") op1a = op1a + " " + val; // stuff data
      else if (tag == "1b") op1b = op1b + " " + val;
      else if (tag == "2a") op2a = op2a + " " + val;
      else if (tag == "2b") op2b = op2b + " " + val;
      else if (tag == "3i") itmn = itmn + " " + val;
      else if (des.length == 0) des = val;
      //else des = des + ", " + val;
    }
  }

op2b = op2b + " $" + Dollar (amt);

// Now summarize stuff we just processed, above
  if (op1a.length > 0) obj1.on0.value = op1a;  // stuff it away
  if (op1b.length > 0) obj1.os0.value = op1b;
  if (op2a.length > 0) obj1.on1.value = op2a;
  if (op2b.length > 0) obj1.os1.value = op2b;
  if (itmn.length > 0) obj1.item_number.value = itmn;
  obj1.item_name.value = des;
  obj1.amount.value = Dollar (amt);

  obj1.business.value = eml1 + "@" + eml2 + "." + eml3;

  if (obj1.tot) obj1.tot.value = "$" + Dollar (amt);
}

function ViewCartForm (obj1) {

var eml1 = "oddfolio";
var eml2 = "yahoo";
var eml3 = "com";

  obj1.business.value = eml1 + "@" + eml2 + "." + eml3;
}
