// form validation function //

// FORMS **MUST** HAVE ID TAG AS WELL AS NAME TAG!!!!! //

function validate(form) {
  var formcontent=form.lettering.value
  var nochars=formcontent.length;
  var formcontent=form.firstname.value
  var nochars1=formcontent.length;
  var formcontent=form.lastname.value
  var nochars2=formcontent.length;
  var nocharskit=nochars1+nochars2;

  var stk1 = form.Stock.value;
  var stk2 = form.Stock2.value;
  var stk3 = form.Stock3.value;
  var stk4 = form.Stock4.value;
  var stk5 = form.Stock5.value;
  var stk6 = form.Stock6.value;
  var totstk = stk1+stk2+stk3+stk4+stk5+stk6;
  var ordercode = form.ordercode.value;
  var MyOrder = form.MyOrder.value;
  var itemcolor = form.itemcolor.value;
  var letterfont = form.letterfont.value;
  var letterstyle = form.letterstyle.value;
  var lettering = form.lettering.value;
  var lettercolor = form.lettercolor.value;
  var lettercolor2 = form.lettercolor2.value;
  var pattern = form.pattern.value;
  var lastname = form.lastname.value;
  var firstname = form.firstname.value;


  if (totstk==0) {
    inlineMsg('MyOrder','Sorry but this item is currently out of stock.  If you wish send us an email and we will let you know when we expect to have this item available.  To continue shopping click on the back button on your browser or you can bookmark this page for future reference.',12);
    return false;
	}

  if(MyOrder == '0') {
    inlineMsg('MyOrder','Please select an assortment/quantity.',3);
    return false;
  }

  if(itemcolor == '0') {
    inlineMsg('itemcolor','Please select an item color.',3);
    return false;
  }
  if(itemcolor == '1' && stk1==0) {
    inlineMsg('itemcolor','Sorry this color is currently unavailable. If you wish you can select another color.',3);
    return false;
  }
  if(itemcolor == '2' && stk2==0) {
    inlineMsg('itemcolor','Sorry this color is currently unavailable. If you wish you can select another color.',3);
    return false;
  }
  if(itemcolor == '3' && stk3==0) {
    inlineMsg('itemcolor','Sorry this color is currently unavailable. If you wish you can select another color.',3);
    return false;
  }
  if(itemcolor == '4' && stk4==0) {
    inlineMsg('itemcolor','Sorry this color is currently unavailable. If you wish you can select another color.',3);
    return false;
  }
  if(itemcolor == '5' && stk5==0) {
    inlineMsg('itemcolor','Sorry this color is currently unavailable. If you wish you can select another color.',3);
    return false;
  }
  if(itemcolor == '6' && stk6==0) {
    inlineMsg('itemcolor','Sorry this color is currently unavailable. If you wish you can select another color.',3);
    return false;
  }

  if(ordercode=="ST") {	
  	var mychoice="0";
	if(document.form.RadioButton[0].checked == true) {
		var mychoice="1";
		}
	if(document.form.RadioButton[1].checked == true)  {
		var mychoice="2";
		}
	if(document.form.RadioButton[2].checked == true)  {
		var mychoice="3";
		}
	if(mychoice == "0") {
    	inlineMsg('RadioButton','Please select whether you want a name only, name with pattern, or a monogram only (no pattern)',3);
    	return false;
		}
	if(mychoice == "1" || mychoice == "2") {
		var letterstyle='9';
		var lettering=" ";
		}
	if(letterstyle== '9' && firstname=="") {
    	inlineMsg('firstname','Please enter a name (up to 10 letters)',3);
    	return false;
		}
	if(nochars1>10) {
	    inlineMsg('firstname','There are 10 maximum letters allowed per name. Please re-enter',3);
	    return false;
	  }
	if(mychoice == "3") {
		var firstname=" ";
		var letterfont='9';
	  }
  }

  if(pattern == '0') {
    inlineMsg('pattern','Please select a pattern.',3);
    return false;
  }

  if(ordercode=="KT") {	
	  if(firstname == "") {
	    inlineMsg('firstname','Please enter a name (max of 10 letters, up to 18 letters for BOTH names combined).',3);
	    return false;
	  }
	  if(nochars1>10) {
	    inlineMsg('firstname','There are 10 maximum letters allowed per name. Please re-enter',3);
	    return false;
	  }
	  if(lastname == "" && (MyOrder=='2' || MyOrder=='4')) {
	    inlineMsg('lastname','Please enter a second name',3);
	    return false;
	  }
	  if(lastname > " " && (MyOrder=='1' || MyOrder=='3')) {
	    inlineMsg('lastname','You selected a single name to be embroidered.  You need to leave this blank or change your selection above',3);
	    return false;
	  }
	  if(nochars2>10) {
	    inlineMsg('lastname','There are 10 maximum letters allowed per name. Please re-enter',3);
	    return false;
	  }
	  if(nocharskit>18) {
	    inlineMsg('firstname','There are 18 maximum letters allowed for BOTH names combined. Please re-enter',3);
	    return false;
	    }
  }


  if(letterfont == '0') {
    inlineMsg('letterfont','Please select a font style.',3);
    return false;
  }
  if(letterstyle == '0') {
    inlineMsg('letterstyle','Please select a monogram style.',3);
    return false;
  }
  if(lettering == "") {
    inlineMsg('lettering','Please enter the appropriate number of letters for you monogram style selection.',3);
    return false;
  }

  // For tissue box covers and sports towels
  if((letterstyle=='1' || letterstyle=='2') && nochars==2 && (ordercode=="TB" || ordercode=="ST")) {
    inlineMsg('lettering','Your selected monogram style is only available as a one or three letter monogram.',4);
    return false;
  }
  if((letterstyle=='3' || letterstyle=='4') && nochars!=3 && (ordercode=="TB" || ordercode=="ST")) {
    inlineMsg('lettering','Your selected monogram style is only available as a three letter monogram.',4);
    return false;
  }
  if(letterstyle>'4' && letterstyle<'9' && nochars>1 && ordercode=="TB") {
    inlineMsg('lettering','Your selected monogram style is only available as a single letter monogram.',4);
    return false;
  }


  // For lavender sachets
  if(letterstyle=='1' && nochars!=1 && ordercode=="SA") {
    inlineMsg('lettering','Your selected monogram style is only available as a single letter monogram.',4);
    return false;
  }
  if(letterstyle=='2' && nochars!=2 && ordercode=="SA") {
    inlineMsg('lettering','Your selected monogram style is only available as a two letter monogram.',4);
    return false;
  }
  if(letterstyle=='3' && nochars!=3 && ordercode=="SA") {
    inlineMsg('lettering','Your selected monogram style is only available as a three letter monogram.',4);
    return false;
  }

  // For bath towels and gift sets
  if((letterstyle=='1' || letterstyle=='2') && nochars==2 && (ordercode=="BT" || ordercode=="GS")) {
    inlineMsg('lettering','Your selected monogram style is only available as a single or three letter monogram.',4);
    return false;
  }
  if(letterstyle>'2' && letterstyle<'9' && nochars!=3 && (ordercode=="BT" || ordercode=="GS")) {
    inlineMsg('lettering','Your selected monogram style is only available as a three letter monogram.',4);
    return false;
  }


  if(lettercolor == "") {
    inlineMsg('lettercolor','Please select a thread color.',4);
    return false;
  }
  if(lettercolor2 == "") {
    inlineMsg('lettercolor2','Please select a thread color for the second "name".',4);
    return false;
  }
  
  
  return true;
}

// START OF MESSAGE SCRIPT //

var MSGTIMER = 20;
var MSGSPEED = 5;
var MSGOFFSET = 3;
var MSGHIDE = 3;

// build out the divs, set attributes and call the fade function //
function inlineMsg(target,string,autohide) {
  var msg;
  var msgcontent;
  if(!document.getElementById('msg')) {
    msg = document.createElement('div');
    msg.id = 'msg';
    msgcontent = document.createElement('div');
    msgcontent.id = 'msgcontent';
    document.body.appendChild(msg);
    msg.appendChild(msgcontent);
    msg.style.filter = 'alpha(opacity=0)';
    msg.style.opacity = 0;
    msg.alpha = 0;
  } else {
    msg = document.getElementById('msg');
    msgcontent = document.getElementById('msgcontent');
  }
  msgcontent.innerHTML = string;
  msg.style.display = 'block';
  var msgheight = msg.offsetHeight;
  var targetdiv = document.getElementById(target);
  targetdiv.focus();
  var targetheight = targetdiv.offsetHeight;
  var targetwidth = targetdiv.offsetWidth;
  var topposition = topPosition(targetdiv) - ((msgheight - targetheight) / 2);
  var leftposition = leftPosition(targetdiv) + targetwidth + MSGOFFSET;
  msg.style.top = topposition + 'px';
  msg.style.left = leftposition + 'px';
  clearInterval(msg.timer);
  msg.timer = setInterval("fadeMsg(1)", MSGTIMER);
  if(!autohide) {
    autohide = MSGHIDE;  
  }
  window.setTimeout("hideMsg()", (autohide * 1000));
}

// hide the form alert //
function hideMsg(msg) {
  var msg = document.getElementById('msg');
  if(!msg.timer) {
    msg.timer = setInterval("fadeMsg(0)", MSGTIMER);
  }
}

// face the message box //
function fadeMsg(flag) {
  if(flag == null) {
    flag = 1;
  }
  var msg = document.getElementById('msg');
  var value;
  if(flag == 1) {
    value = msg.alpha + MSGSPEED;
  } else {
    value = msg.alpha - MSGSPEED;
  }
  msg.alpha = value;
  msg.style.opacity = (value / 100);
  msg.style.filter = 'alpha(opacity=' + value + ')';
  if(value >= 99) {
    clearInterval(msg.timer);
    msg.timer = null;
  } else if(value <= 1) {
    msg.style.display = "none";
    clearInterval(msg.timer);
  }
}

// calculate the position of the element in relation to the left of the browser //
function leftPosition(target) {
  var left = 0;
  if(target.offsetParent) {
    while(1) {
      left += target.offsetLeft;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.x) {
    left += target.x;
  }
  return left;
}

// calculate the position of the element in relation to the top of the browser window //
function topPosition(target) {
  var top = 0;
  if(target.offsetParent) {
    while(1) {
      top += target.offsetTop;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.y) {
    top += target.y;
  }
  return top;
}

// preload the arrow //
if(document.images) {
  arrow = new Image(7,80); 
  arrow.src = "images/msg_arrow.gif"; 
}