/* ************************************** */
/* showAlt(), showPic, prepareGallery()   */
/* are all used to display prayer card    */
/* graphics. addLoadEvent(prepareGallery) */ 
/* executes the prepareGallery function   */
/* when this file is loaded.              */
/* ************************************** */


function showAlt(altText) {
	if (altText.getAttribute("alt")) 
	{
    var desc = altText.getAttribute("alt");
  } 
  else 
  {
    var desc = "";
  }
  if (!document.getElementById("carddesc")) return false;  
  var vcarddesc = document.getElementById("carddesc");
  vcarddesc.value=desc; // This sets a hidden field value so that php can read it and set a session variable.
  if (!document.getElementById("cdesc")) return false;  
  var vcdesc = document.getElementById("cdesc");  
  if (vcdesc.firstChild.nodeType == 3) 
  {
	  vcdesc.firstChild.nodeValue = desc;
  } 
  //if (!document.getElementById("c_id")) return false;
 	//var selection = document.getElementById("c_id");
  //selection.value = desc;
  return false;
}

function showPic(whichpic) 
{
  if (!document.getElementById("placeholder")) return true;
  var source = whichpic.getAttribute("href"); 
  
  if (!document.getElementById("imgsrc")) return false;
  document.getElementById("imgsrc").value = source;// This sets a hidden field value so that php can read it and set a session variable.
  
  var placeholder = document.getElementById("placeholder");
  placeholder.setAttribute("src",source);
  if (whichpic.getAttribute("title")) // The title of the thumbnails contains the image id's.
  {
	  var text = whichpic.getAttribute("title");
  } 
  else 
  {
    var text = "";
  }
    
  if (!document.getElementById("imgid")) return false;  
  var vimgid = document.getElementById("imgid");
  vimgid.value=text;  // This sets a hidden field value so that php can read it and set a session variable.
  if (!document.getElementById("vcid")) return false;    
  var vccid = document.getElementById("vcid");
  
  if (vccid.firstChild.nodeType == 3) 
  {
		 vccid.firstChild.nodeValue = text;	 // This places the card ID in viewing area.
  }
  
  /*
  if (!document.getElementById("imgid")) return false;    
  var vimgid = document.getElementById("imgid");
  vimgid.value = text; // This sets a hidden field value so that php can read it and set a session variable.
  */
  if (!document.getElementById("slctcard")) return false;
  var selectbutton = document.getElementById("slctcard");  
  selectbutton.disabled = false;
  return false;
}

function prepareGallery() 
{
  if (!document.getElementsByTagName) return false;
  if (!document.getElementById) return false;
  if (!document.getElementById("thumbnails")) return false;
  var gallery = document.getElementById("thumbnails");
  var links = gallery.getElementsByTagName("a");
  var images = gallery.getElementsByTagName("img");
  for ( var i=0; i < links.length; i++) 
  {
    links[i].onclick = function() 
    {
      return showPic(this);
		}
    images[i].onclick = function() 
    {
      return showAlt(this);
		}		
    links[i].onkeypress = links[i].onclick;
  }
}

function addLoadEvent(func) {
  var oldonload = window.onload;
//  alert("Loading the script.");
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}
addLoadEvent(prepareGallery); // executes the prepareGallery function when the script is loaded.
addLoadEvent(elqty);
/* ******************************************* */
/* End prayer card graphic handling functions. */
/* ******************************************* */

/* ************************************** */
/* elqty disables/enable the laminates    */
/* quantity text field when the laminates */
/* checkbox yes is checked/unchecked.     */
/* ______________________________________ */
function elqty()
{
  if (!document.getElementById("laminate")) return false;
	if (document.order.laminate.checked==true)
	{
		document.order.lqty.disabled=false;
		document.order.lqty.style.backgroundColor="#FFFFFF";			
	}
	else
	{
		document.order.lqty.value="";	
		document.order.lqty.disabled=true;
		document.order.lqty.style.backgroundColor="#CCCCCC";		
	}
	return;
}
/* ************************************** */
/* End of the elqty function.             */
/* ************************************** */

/* ************************************** */
/* isEmpty and validate check the order   */
/* form to make sure that required fields */
/* are filled out.                        */
/* ************************************** */
var whitespace=" \t \n \r \\";
function isEmpty(s)
{
	if ((s == null) || (s.length==0)) 
	{
		// alert("s=");	
		return true;
	}	
	// Search string looking for non-whitespace characters.
	for (i=0; i<s.length; i++)
	{
		var c=s.charAt(i);
		if (whitespace.indexOf(c) == -1) return false;
		// alert (c + "; " + whitespace.indexOf(c));
	}
	// At this point all characters will be whitespace.
	return true;
}

function validdateorder()
{
	var msg="Please correct the following errors: \n\n";
	if (isEmpty(document.order.funeralhome.value))
	{
		// alert("Error: Funeral Home name is not valid.");
		msg = msg + "Funeral Home name is not valid.\n";
		//document.order.funeralhome.focus();
		// return false;
	}
	if (isEmpty(document.order.emailaddress.value) && (isEmpty(document.order.phonenumber.value)))
	{
		// alert("Error: Email address is not valid.");
		msg = msg + "Please provide an Email Address or (preferably) a Phone Number.\n";
		//document.order.emailaddress.focus();
		// return false;
	}
/*
	if (isEmpty(document.order.phonenumber.value))
	{
		// alert("Error: Phone number is not valid.");
		msg = msg + "Phone number is not valid.\n";		
		// document.order.phonenumber.focus();
		// return false;
	}
*/	
	if (msg=="Please correct the following errors: \n\n")
	{
		return true;
	}
	else
	{
        	alert (msg);
        	window.document.order.funeralhome.focus();		
        	return false;
	}
	return false;	
}

function validatecontact()
{
	var msg="Please correct the following errors: \n\n";
	var c=0;
	if (isEmpty(document.contact.firstname.value))
	{
		msg = msg + "First name is blank or not valid.\n";
		c = c + 1;
	}
	if (isEmpty(document.contact.lastname.value))
	{
		msg = msg + "Last name is blank or not valid.\n";
		c = c + 3;
	}
	
	if (isEmpty(document.contact.emailaddress.value) && isEmpty(document.contact.phonenumber.value))
	{
		msg = msg + "Please provide an email address or phone number so that we may respond to your inquiry.\n";		
		c = c + 5;
	}
	if (msg=="Please correct the following errors: \n\n")
	{
		return true;
	}
	else
	{
          if ((c==1) || (c==4) || (c==6) || (c==9))
          {
            alert (msg);
            window.document.contact.firstname.focus();		
            return false;
          }
          if ((c==3) || (c==8))
          {
            alert (msg);
            window.document.contact.lastname.focus();		
            return false;
          }
          if (c==5)
          {
            alert (msg);
            window.document.contact.emailaddress.focus();		
            return false;
          }                           
	}	
	return false;	
}
/* *************************************** */
/* End of the form validation functions.   */
/* *************************************** */

