//Adds text to any part of the body of a HTML
function addNode(tagParent,strText,boolAddToBack, boolRemoveNode)
{
  var strNode = document.createTextNode(strText);//holds the test which will be added
   
  //gets the properties of the node
  tagParent = getDocID(tagParent);
  
  //checks if the user whats to replace the node in order to start with a clean slate
  //it also checks if there is a chode node to replace
  if (boolRemoveNode == true && tagParent.childNodes.length > 0)
	//replaces the current node with what the user wants
	tagParent.replaceChild(strNode,tagParent.childNodes[0]);
  else
  {
  	//checks if the user whats to added to the back of the id or the front
  	if(boolAddToBack == true)
		tagParent.appendChild(strNode);
  	else
		//This is a built-in function of Javascript will add text to the beginning of the child
  		insertBefore(strNode,tagParent.firstChild);
  }//end of if else
  
  //returns the divParent in order for the user to use it for more uses
  return tagParent;
}//end of addNode()

function checkEmail(strEMail)
{
	var strFilter = /^.+@.+\..{2,3}$/;//holds the filtter for the Email

	//checks if there is E-Mail
	if (strEMail == "")
	{
		alert('You must have e-mail address');
		return false;
	}//end of if	
	
	//checks if there the E-Mail Format is current
	if (strFilter.test(strEMail) == false)
	{
		alert("The address " + strEMail + ". Please check the entry for 'Your e-mail address.'");
		return false;
	}//end of if
	else if (strEMail.match(/[\(\)\<\>\,\;\:\\\/\"\[\]]/))
	{
		alert('Your e-mail address contains illegal characters.');
		return false;
	}//end of else if
	
	return true;
}//ebd of checkEmail()

//clear the text from tagLayer
function clearText(tagLayer)
{
	tagLayer = getDocID(tagLayer);
	
	if(tagLayer != null)
		tagLayer.value = "";	
}//end of clearText()

//removes from view all tags in tagContainer with the expection of tagActive
//It assumes the tagActive and tagContiner already have the properties
function classToggleLayer(tagContainer,tagActive,strClassName,strTAGName)
{
	var arrTAG = tagContainer.getElementsByTagName(strTAGName);//holds all strTAGName in tagContainer
	
	//goes around the for each tag that getElementsByTagName found in tagContainter
	for(var intIndex = arrTAG.length - 1; intIndex > -1 ; intIndex--) 
	{
		//checks if the class name is the same as strClassName and it is not active if it is active then change the dispaly to block
		if(arrTAG[intIndex].className == strClassName && arrTAG[intIndex].id != tagActive.id)
			arrTAG[intIndex].style.display = arrTAG[intIndex].style.display? "":"";
		else if(arrTAG[intIndex].id == tagActive.id && tagActive.style.display == "")
			arrTAG[intIndex].style.display = arrTAG[intIndex].style.display? "":"block";
	    
	}//end of for loop
}//end of classToggleLayer()

//does the display the a message in a on the page weather then an alert
function displayMessage(tagMessage,strMessText,boolAddToBack, boolRemoveNode)
{
	//gets the message properties and sets the text furthermore it does the display
	tagMessage = addNode(tagMessage,strMessText,boolAddToBack, boolRemoveNode);
	tagMessage.style.display = "block";	
	
	return tagMessage;
}//end of displayMessage()

//this is for the duel layers that sometimes is need
function duelToggleLayer(whichLayer,layer1,layer2)
{
	var activeLayer = "";//holds the active Layer	
	var style2 = "";//holds the style of layer1
	var style3 = "";//holds the style of layer2

	// this is the way the standards work
	if (whichLayer != ''){activeLayer = getDocID(whichLayer);}
	if (layer1 != ''){style2 = getDocID(layer1);}
	if (layer2 != ''){style3 = getDocID(layer2);}

	//Checks if there is an active layer
	if (activeLayer != "")
	{
		//checks if the activeLayer is already active and if so then skips code
		//since the layer cannot be turn off and leave a hole in the review layer
		if (activeLayer.style.display == "")
		{
			//removes the block from the display in order to make the layer to disapper	
			if (style2 != ''){style2.style.display = style2.style.display? "":"";}

			//checks if there is a style3
			if (style3 != ''){style3.style.display = style3.style.display? "":"";}
	
			//displays the new active Layer and updates its id
			activeLayer.style.display = activeLayer.style.display? "":"block";
		}//end of if
	}//end of if
}//end of duelToggleLayer()

//gets the document properties in order to use them as there are many types of browers with different versions
function getDocID(tagLayer)
{
	var tagProp = "";//holds the proerties of tagLayer

	//gets the whichLayer Properties depending of the differnt bowers the user is using
	if (document.getElementById)//this is the way the standards work
		tagProp = document.getElementById(tagLayer);
	else if (document.all)//this is the way old msie versions work
		tagProp = document.all[tagLayer];
	else if (document.layers)//this is the way nn4 works
		tagProp = document.layers[tagLayer];
		
	return tagProp;
}//end of getDocID()

//removes all new lines and replaces them with a <br/> html tag
function nl2br(strText)
{
	//checks if there is anything inside strText
	if (strText != "")
	{
 		var re_nlchar = "";//holds the different newlines that the OS uses
		strText = escape(strText);//in codes strText to be more like a URL to find the newlines
			
		//finds the either \r or \n or both since \r is for Linex and Apple and \n is for MS
		if(strText.indexOf('%0D%0A') > -1)
			re_nlchar = /%0D%0A/g ;
		else if(strText.indexOf('%0A') > -1)
			re_nlchar = /%0A/g ;
		else if(strText.indexOf('%0D') > -1)
			re_nlchar = /%0D/g ;
	
		//checks if there is any new lines in strText
		if (re_nlchar != "")
			//changes the strText back to normal with all of the newlines changes to <br/> tag
			return unescape(strText.replace(re_nlchar,'<br />'));
	}//end of if
	
	return strText;
}//end of nl2br()

//shoes and hides a <div> using display:block/none from the CSS
function toggleLayer(tagLayer,tagGrayOut)
{
	var tagStyle = '';//holds the style of tagLayer

	//gets the tagLayer and tagGrayOut Properties
	tagStyle = getDocID(tagLayer);
	tagGrayOut = getDocID(tagGrayOut);
	
	if (tagStyle != null){tagStyle.style.display = tagStyle.style.display? "":"block";}
	if (tagGrayOut != null)
	{
		tagGrayOut.style.display = tagGrayOut.style.display? "":"block";

		//for IE
		if (navigator.userAgent.indexOf('MSIE') != -1)
		{
			tagGrayOut.attachEvent('onclick',function () {
				toggleLayer(tagStyle.id,tagGrayOut.id)
			});
		}//end of if
		//for the other browsers
		else
		{
			tagGrayOut.addEventListener('click',function () {
				toggleLayer(tagStyle.id,tagGrayOut.id);
			},false);
		}//end of else
	}//end of if
}//end of toggleLayer()


//shoes and hides a <div> using display:block/none from the CSS
function toggleLayerEvent(e,tagLayer,tagGrayOut)
{
	var tagStyle = '';//holds the style of tagLayer

	//gets the tagLayer and tagGrayOut Properties
	tagStyle = getDocID(tagLayer);
	tagGrayOut = getDocID(tagGrayOut);
	
	if (tagStyle != null){tagStyle.style.display = tagStyle.style.display? "":"block";
	var holder = getDocID('divPrayers');
	tagStyle.style.top =(holder.offsetHeight - 140) + 'px'; // check on negative value ??
	tagStyle.style.left = (holder.offsetWidth - 20) + 'px';
	//tagStyle.style.position = 'position: absolute';
 }
	if (tagGrayOut != null)
	{
		tagGrayOut.style.display = tagGrayOut.style.display? "":"block";

		//for IE
		if (navigator.userAgent.indexOf('MSIE') != -1)
		{
			tagGrayOut.attachEvent('onclick',function () {
				toggleLayer(tagStyle.id,tagGrayOut.id)
			});
		}//end of if
		//for the other browsers
		else
		{
			tagGrayOut.addEventListener('click',function () {
				toggleLayer(tagStyle.id,tagGrayOut.id);
			},false);
		}//end of else
	}//end of if
}//end of toggleLayer()


/* search input behavior */
//does the search bar onForcus Event
function searchBarForce(tagContainer,strClassName,strTAGName,strColor,strValue)
{

	//gets the search bar properties
	var arrTAG = getDocID(tagContainer).getElementsByTagName(strTAGName);//holds all strTAGName in tagContainer
	//goes around the for each tag that getElementsByTagName found in tagContainter
	for(var intIndex = arrTAG.length - 1; intIndex > -1 ; intIndex--) 
	{
		//checks if the class name is the same as strClassName
		if(arrTAG[intIndex].className == strClassName)
		{
			//changes the color and text to blank when the user frocus on the textbox
		    arrTAG[intIndex].style.color = strColor;
    		arrTAG[intIndex].value = strValue;
		}//end of if
	}//end of for loop
}//end of searchBarForce()

//does the search bar onblur Event(focus off)
function searchBarFocusOff(tagContainer,strClassName,strTAGName, strTAGValue)
{

	//gets the search bar properties
	var arrTAG = getDocID(tagContainer).getElementsByTagName(strTAGName);//holds all strTAGName in tagContainer

	//goes around the for each tag that getElementsByTagName found in tagContainter
	for(var intIndex = arrTAG.length - 1; intIndex > -1 ; intIndex--) 
	{
		//checks if the class name is the same as strClassName
		if(arrTAG[intIndex].className == strClassName)
		{
			if(arrTAG[intIndex].value=="")
			{
				//changes the color and text when the user leave the textbox
	    		arrTAG[intIndex].style.color = "#313131";
   				arrTAG[intIndex].value = strTAGValue;
			}//end of if	
		}//end of if
	}//end of for loop
}//end of searchBarFocusOff()

//gets the document properties in order to use them as there are many types of browers with different versions
function getDocID(tagLayer)
{
	var tagProp = "";//holds the proerties of tagLayer

	//gets the whichLayer Properties depending of the differnt bowers the user is using
	if (document.getElementById)//this is the way the standards work
		tagProp = document.getElementById(tagLayer);
	else if (document.all)//this is the way old msie versions work
		tagProp = document.all[tagLayer];
	else if (document.layers)//this is the way nn4 works
		tagProp = document.layers[tagLayer];
		
	return tagProp;
}//end of getDocID()

// ========= update listings ============

var XmlHttp;
var ErrMsg = 'There was a problem retrieving data from the server.';
var listingHolderObj = null;

var lastListing="";
 
//    function setUniqId ( hiddServiceObj){
//        listingHolderObj = hiddServiceObj;
//        alert(listingHolderObj);
//    }
    
function LoadListings()
{
    try{
        var hiddObj = getDocID('hiddListingVal');
        //alert(hiddObj);
        if(hiddObj != null){
            alert(hiddObj.value);
            if(hiddObj.value == "")
                hiddObj.value = "listServices";
            UpdateListings(hiddObj.value);
        }
    }
    catch(er){
        alert('LoadListings');
    }
}

function CreateXmlHttp()
{
	// IE
	try
	{
		XmlHttp = new ActioveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(os)
		{
			XmlHttp = null;
		}
	}
	// Mozilla and Safari	
	if(!XmlHttp && typeof XMLHttpRequest != "undefined")
	{
		XmlHttp = new XMLHttpRequest();
	}
}

function UpdateListings(listingName)//, hiddListName)
{
//    alert(listingName);

//    if(listingName == ""){
//        if(lastListing == "")
//            lastListing = listingName = "listServices";
//        else
//            listingName = lastListing;
//    }else
//       lastListing = listingName ;  
        
        //alert(listingName);
    
    var hiddObj = getDocID('hiddListingVal');
    if(hiddObj!=null){
        hiddObj.value = listingName;
        
        //alert(hiddObj.value);
        }
        
    try{
	    var requestUrl = 'AjaxListings.ashx?t=' + encodeURIComponent(listingName);
	    requestUrl += '&rand=' + randomNotCache();
    	
	    CreateXmlHttp();
	    if(XmlHttp)
	    {
		    XmlHttp.onreadystatechange = HandleUpdateListingsResponse;
		    XmlHttp.open("GET", requestUrl, true);
		    XmlHttp.send(null);
	    }	
	}
	catch(e){
	    alert(e);
	}
	
	__doPostBack('callPostBack', hiddObj.value);

}

function HandleUpdateListingsResponse()
{
	if(XmlHttp.readyState == 4)
	{
		if(XmlHttp.status == 200)
		{
			setupList(XmlHttp.responseText);		
		}
		else
		{
			alert(ErrMsg);
		}
	}
}

function setupList(resultDoc)
{	
	if(resultDoc != null)
	{
		try
		{
			var resultHolder = getDocID('listingsHoldertag');
            if(resultHolder != null){			
			    resultHolder.innerHTML = resultDoc;
			}
		}
		catch(e)
		{
			window.alert(ErrMsg);
		}
	}
	else
	{
		window.alert(ErrMsg);
	}
}

function randomNotCache()
{
	var i = Math.round(1000*Math.random())
	return i;
}
/* =========== Advanced Search ======== */
var MonthDays = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
var IgnoredWords = new Array('a','b','d','f','g','h','i','k','l','m','n','o','p','q','r','s','t','u','v','w','y','z','an','as','at','be','do','he','if','in','is','it','my','of','on','or','to','up','we','all','any','are','and','but','can','did','for','get','got','has','had','her','him','his','how','now','our','out','see','the','too','was','way','who','you','also','been','both','came','come','each','from','have','here','into','like','make','many','more','most','much','must','only','over','said','same','some','such','take','than','that','them','then','they','this','very','well','were','what','with','your','its','about','after','being','could','might','never','other','since','still','their','there','these','those','under','where','which','while','would','before','should','another','because','between','himself','through','not','and','near','about','after','all','also','another','as','at','be','$','because','been','before','being','between','both','but','by','came','can','come','could','do','he','have','here','himself','if','in','into','might','must','my','never','now','our','out','see','should','since','still','the','their','there','these','those','through','too','under','was','way','where','which','while','who','would','you');
var msie;


function showSearch(SearchType) {
	toShow = 'visible'
	toHide = 'hidden'

	if (SearchType == 'Name') {
	
		getDocID('NameSearch').style.visibility = toShow; 
		getDocID('DateSearch').style.visibility = getDocID('KeywordSearch').style.visibility = toHide;
		
		
	}
	else if (SearchType == 'Date') {
		getDocID('DateSearch').style.visibility = toShow; 
		getDocID('NameSearch').style.visibility = getDocID('KeywordSearch').style.visibility = toHide;
	}
	else if (SearchType == 'Keyword') {
		getDocID('KeywordSearch').style.visibility = toShow; 
		getDocID('NameSearch').style.visibility = getDocID('DateSearch').style.visibility = toHide;
	}
}

function showSearchonLoad(SearchType, rdbElt)
{
    getDocID('NameSearch').style.visibility = 'visible'; 
	getDocID('DateSearch').style.visibility = getDocID('KeywordSearch').style.visibility = 'hidden';
	
	var elt = document.getElementById(rdbElt);
	if(elt!=null) elt.checked=true;
	//alert(rdbElt + ":" + elt.checked);
}

// custom validators functions
function checkFNameWorlds(source, clientside_arguments)
{
    for( var i=0; i < IgnoredWords.length; i++)
    if (clientside_arguments.Value == IgnoredWords[i] )
      {
         clientside_arguments.IsValid=false;
         return;
      }
      else 
        clientside_arguments.IsValid=true;

}

function checkLNameWorlds(source, clientside_arguments)
{
    for( var i=0; i < IgnoredWords.length; i++)
    if (clientside_arguments.Value == IgnoredWords[i] )
      {
         clientside_arguments.IsValid=false;
         return;
      }
      else 
        clientside_arguments.IsValid=true;

}

function checkKeywordWorlds(source, clientside_arguments)
{
    for( var i=0; i < IgnoredWords.length; i++)
    if (clientside_arguments.Value == IgnoredWords[i] )
      {
         clientside_arguments.IsValid=false;
         return;
      }
      else 
        clientside_arguments.IsValid=true;

}

// change options data by user selected options
function loadDay(monthValue,objDay,yearValue){

	objDay.length = MonthDays[monthValue-1];
	for(var i=0; i<MonthDays[monthValue-1]; i++){
		objDay.options[i].value = objDay.options[i].text = i+1;
	}

	if(monthValue == 2 && yearValue/4 == Math.round(yearValue/4) 
	&& (yearValue/100 != Math.round(yearValue/100) || yearValue/400 == Math.round(yearValue/400))){
		objDay.length++;
		objDay.options[28].value = objDay.options[28].text = 29;
	}
}

function OnChangeIndexMonthStart(eltMonth, eltDay, eltYear)
{
    var eltY = document.getElementById(eltYear);
    loadDay(eltMonth.value,document.getElementById(eltDay), eltY.value);
}

function OnChangeIndexMonthEnd(eltMonth, eltDay,eltYear)
{
    var eltY = document.getElementById(eltYear);
    loadDay(eltMonth.value,document.getElementById(eltDay), eltY.value);
}

function OnChangeIndexYearStart(eltYear, eltDay,eltMonth)
{
    var eltM = document.getElementById(eltMonth);
    loadDay(eltM.value,document.getElementById(eltDay), eltYear.value);
}

function OnChangeIndexYearEnd(eltYear, eltDay,eltMonth)
{
    var eltM = document.getElementById(eltMonth);
    loadDay(eltM.value,document.getElementById(eltDay), eltYear.value);
}

/* show cemetery map */
    function showCemetryMap(divElt)
    {
        var divv = document.getElementById(divElt);
        if(divv != null)
            divv.style.display = (divv.style.display =='block') ? 'none' : 'block';
    }

/* * * * * * * Email Confirmation second * * * * * */
var EConfXmlHttp;
function CreateEConfXmlHttp()
{
	// IE
	try
	{
		EConfXmlHttp = new ActioveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			EConfXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(os)
		{
			EConfXmlHttp = null;
		}
	}
	// Mozilla and Safari	
	if(!EConfXmlHttp && typeof XMLHttpRequest != "undefined")
	{
		EConfXmlHttp = new XMLHttpRequest();
	}
}

function SendConfirmation2(subId)
{
    try{
	    var requestUrl = 'SendEConfirmation.ashx?sub=' + subId;
	    requestUrl += '&rand=' + randomNotCache();
    	
	    CreateEConfXmlHttp();
	    if(EConfXmlHttp)
	    {
		    EConfXmlHttp.onreadystatechange = HandleEConfResponse;
		    EConfXmlHttp.open("GET", requestUrl, true);
		    EConfXmlHttp.send(null);
	    }	
	}
	catch(e){
	    //alert(e);
	}
}

function HandleEConfResponse()
{
	if(EConfXmlHttp.readyState == 4)
	{
		if(EConfXmlHttp.status == 200)
		{
			setMessage(EConfXmlHttp.responseText);		
		}
		else
		{
			alert(ErrMsg);
		}
	}
}

function setMessage(resDoc)
{	
	if(resDoc != null)
	{
		try
		{
			window.alert('Email Confirmation has been send.');
//			var msg = document.getElementById(eltText);
//			if(msg != null)
//                msg.InnerHTML = 'Email Confirmation has been send.';
		}
		catch(e)
		{
			window.alert(ErrMsg);
		}
	}
	else
	{
		//window.alert(ErrMsg);
	}
}