/* call these events when the page loads */
addLoadEvent(blurLinks);
addLoadEvent(correctPNG);
addLoadEvent(setFieldActions);


Date.prototype.addDays = function(days) 
{
	this.setDate(this.getDate()+days);
} 



function addLoadEvent(func) 
{
	var oldonload = window.onload;
	if (typeof window.onload != 'function') 
	{
		window.onload = func;
	}
	else 
	{
		window.onload = function()
		{
			oldonload();
			func();
		}
	}
}



/* remove all focus lines from links when clicked */
function blurLinks() 
{
	var links = document.getElementsByTagName('a');
	for (i=0;i<links.length;i++) 
	{
		links[i].onfocus = function() 
		{
			this.blur();
		}
	}
}




/*  basic engine for parsing form values
	accepts list of required fields as array */

var valCol = '#FFFFFF'; 	// regular background color
var invalCol = '#FFCCCC';	// highlighted (alerted) field color

function isValid(reqFields) 
{
  
	var bStatus = true;
	
	for(i = 0; i < reqFields.length; i++) 
	{
		var currentField = document.getElementById(reqFields[i]);
		
		// reset bg color to clear all previous errors
		currentField.style.backgroundColor=valCol;
		if( (!currentField.value) || (currentField.value=="") ) 
		{
			
			// set background color to alert
			currentField.style.backgroundColor=invalCol;
			currentField.focus();
			bStatus = false;
			break;
		}
	}

  return bStatus;

}



function rollOverListings(name) 
{
	
	if (!document.getElementById) return false;
	if (!document.getElementsByTagName) return false;
	
	var listRow = document.getElementById(name).getElementsByTagName('ul');
	for (i=0;i<listRow.length;i++) 
	{
	
	  var upStateColor = '';
	
		listRow[i].onmouseover = function() 
		{
			
			if (this.className!='heading' && this.className!='grandTotal' ) 
			{
				upStateColor = this.style.backgroundColor;
				this.style.backgroundColor='#ffeded';
			}
		}
		
		listRow[i].onmouseout = function() 
		{
			
			if (this.className!='heading' && this.className!='grandTotal' ) 
			{
				this.style.backgroundColor=upStateColor;
			}
		}
	}
}


function openWindow(URL, intWidth, intHeight, scrolling) 
{
		var left = ((parseInt(screen.availWidth)/2)-(intWidth/2));
		var top = ((parseInt(screen.availHeight)/2)-(intHeight/2));

		newWindow = window.open(URL, "loading", "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars="+scrolling+",resizable=0,width="+intWidth+",height="+intHeight+",left="+left+",top="+top+"")
		newWindow.focus();
}



function correctPNG()
{ // correctly handle PNG transparency in Win IE 5.5 & 6.


   var arVersion = navigator.appVersion.split("MSIE")
   var version = parseFloat(arVersion[1])
   if ((version >= 5.5) && (document.body.filters)) 
   {
      for(var i=0; i<document.images.length; i++)
      {
         var img = document.images[i]
         var imgName = img.src.toUpperCase()
         if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
         {
            var imgID = (img.id) ? "id='" + img.id + "' " : ""
            var imgClass = (img.className) ? "class='" + img.className + "' " : ""
            var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
            var imgStyle = "display:inline-block;" + img.style.cssText 
            if (img.align == "left") imgStyle = "float:left;" + imgStyle
            if (img.align == "right") imgStyle = "float:right;" + imgStyle
            if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
            var strNewHTML = "<span " + imgID + imgClass + imgTitle
            + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
            + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
            + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
            img.outerHTML = strNewHTML
            i = i-1
         }
      }
   }    
}


// strips all non alpha-numeric characters from a form field
function clearChars(id) 
{
	var frmField = $(id);
    frmField.value = filterNum(frmField.value);
}

function filterNum(str) 
{
	 re = /\$|,|@|#|~|`|\%|\*|\^|\&|\(|\)|\+|\=|\[|\-|\_|\]|\[|\}|\{|\;|\:|\'|\"|\<|\>|\?|\||\\|\!|\$|/g;
	 // remove special characters like "$" and "," etc...
	 return str.replace(re, "");
}


function white_space(field) 
{
    field.value = (field.value).replace(/^\s*|\s*$/g,'');
}


function removeSpaces(id) 
{
	var frmField = $(id);
	var val = frmField.value;
    var clearVal = val.split(' ').join('');
	frmField.value = clearVal;
}



/* validateEmail */
function echeck(str) 
{

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1)
		{
		   //alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
		{
		   //alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
		{
		   // alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1)
		 {
		    //alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
		 {
		    //alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1)
		 {
		    //alert("Invalid E-mail ID")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1)
		 {
		    //alert("Invalid E-mail ID")
		    return false
		 }

 		 return true					
	}




	

function setFieldActions() 
{
	var txtFields = $$('.txt');
	for (i=0;i<txtFields.length;i++) 
	{
		txtFields[i].onfocus = function() {this.className = 'txtActive';}
		txtFields[i].onblur = function() 
		{
			
			// special action for final payment deposit field
			if (this.id=='txtPaymentAmt') 
			{
				clearChars(this);
			}
			
			this.className = 'txt';
		}
	}
	
	
	var txtFields = $$('.txtMed');
	for (i=0;i<txtFields.length;i++) 
	{
		txtFields[i].onfocus = function() {this.className = 'txtMedActive';}
		txtFields[i].onblur = function() 
		{
			
			// special action for final payment credit card field
			if (this.id=='txtCCNum') 
			{
				removeSpaces(this);
			}
			this.className = 'txtMed';
		}
	}
	
	
	var txtFields = $$('.txtShort');
	for (i=0;i<txtFields.length;i++) 
	{
		txtFields[i].onfocus = function() {this.className = 'txtShortActive';}
		txtFields[i].onblur = function() {this.className = 'txtShort';}
	}
	
	
	var txtFields = $$('.txtShortB');
	for (i=0;i<txtFields.length;i++) 
	{
		txtFields[i].onfocus = function() 
		{
			if (this.value=='MM') {this.value='';}
			if (this.value=='DD') {this.value='';}
			this.className = 'txtShortBActive';
		}
		txtFields[i].onblur = function() 
		{
			this.value = returnNumbers(this.value);
			this.className = 'txtShortB';
			if (trim(this.value) == '')
			{
			  switch (this.id)
			  {
				  case 'bdateM':
				    this.value ='MM';
						break;
					case 'bdateD':
					  this.value ='DD';
						break;
					case 'txtPartyDateM':
					  this.value ='MM';
						break;
					case 'txtPartyDateD':
					  this.value ='DD';
						break;
					default:
					  break;
				}
			}
		}
	}
	
	
	var txtFields = $$('.txtMedB');
	for (i=0;i<txtFields.length;i++) 
	{
		
		txtFields[i].onfocus = function() 
		{
			if (this.value=='YYYY') {this.value='';}
			this.className = 'txtMedBActive';
		}
		txtFields[i].onblur = function() 
		{
			this.value = returnNumbers(this.value);
			this.className = 'txtMedB';
			if (trim(this.value) == '')
			{
			  switch (this.id)
			  {
					case 'bdateY':
					  this.value ='YYYY';
						break;
					case 'txtPartyDateY':
					  this.value ='YYYY';
						break;
					default:
					  break;
				}
			}
		}
	}
	
	var txtFields = $$('textarea.inactive');
	for (i=0;i<txtFields.length;i++) 
	{
		txtFields[i].onfocus = function() {this.className = 'active';}
		txtFields[i].onblur = function() {this.className = 'inactive';}
	}
	
	
}



function daysInMonth(iMonth, iYear) 
{
	var days = 32 - new Date(iYear, iMonth, 32).getDate();
	return days;
}














var currentPackage = null;
var selectedPackage = null;

function showPackageDetails(id) 
{
			
	// locate all package groups on the page
	var packageGroups = $$('ul.packageDetails');
	
	// hide any currently open package
	if ( (currentPackage != null) && (('packageDetails'+id) != currentPackage)) 
	{
		Effect.BlindUp(currentPackage, { duration: .5 });
	} 
	
	// loop through all packages
	for (i=1;i<=packageGroups.length;i++) 
	{
		// clear styling on all packages
		$('partyPackage'+i).className='partyPackage';
	}
	
	// set the currently selected package to selected status
	if (selectedPackage != null) 
	{
		$(selectedPackage).className='partyPackageSelected';
	}
	
	// show details of selected package (if not already open)
	if ( ('packageDetails'+id) != currentPackage) 
	{
		currentPackage = 'packageDetails'+id;
		Effect.BlindDown(currentPackage, { duration: .5 });
		
	} 
	

	// highlight package if it is not the currently selected package
	if ($('partyPackage'+id).className!='partyPackageSelected') 
	{
		$('partyPackage'+id).className='partyPackageActive';
	}

}




function showAddOnDetails(id) 
{
	
	var addOn = 'addOnDetails'+id;
	if ( $(addOn).style.display=='none' ) {
		$(addOn).style.display='block';
	} else {
		$(addOn).style.display='none';
	}
	//Effect.toggle(addOn, 'slide');

}



function getAddOnState(id) {
	
	var cbID = 'cbAddOn'+id;
	var cbValue = false;
	
	if ($(cbID).checked==true) {
		cbValue = true;
	} else {
		cbValue = false;
	}
	
	
	// show relevant state change
	var addOnGroup = 'partyAddOn'+id;
	
	if (cbValue == true) {
		$(addOnGroup).className = 'partyAddOnSelected';
	} else {
		$(addOnGroup).className = 'partyAddOn';
	}
	
}









function selectPackage(id) 
{
	
	// assign selected package and assign radio action
	selectedPackage = 'partyPackage'+id;
	$('rdoPackage'+id).checked=true;
	
	// target all package groupings
	var packageGroup = $$('input.radPackage');
	for (i=1;i<=packageGroup.length;i++) 
	{

		// clear all current style states
		$('partyPackage'+i).className='partyPackage';
		
		// edit all button text
		$('btnSel'+i).innerHTML = js_label_select_pkg; // variable dynamically assigned in step3.php
		$('btnSel'+i).className = 'unsel';
	}
	
	// assign selected state
	$('btnSel'+id).innerHTML = js_label_pkg_selected; // variable dynamically assigned in step3.php
	$('btnSel'+id).className='sel';
	$('partyPackage'+id).className='partyPackageSelected';
	

}







function matchCCDetails() 
{
	if ($('chMatchDetails').checked==true)
	{
		//update values
  	$('txtFNameCC').value = $('txtFName').value;
  	$('txtLNameCC').value = $('txtLName').value;  
		$('txtPhone1CC').value = $('txtPhone1').value;
		$('txtPhone2CC').value = $('txtPhone2').value;
		$('txtCCEmail').value = $('txtEmail').value;
		$('txtCCEmailCnfm').value = $('txtEmailCnfm').value;
		$('txtAddressCC').value = $('txtAddress').value;
		$('txtAddress2CC').value = $('txtAddress2').value;
		$('selStateCC').value = $('selState').value;
		$('txtCityCC').value = $('txtCity').value;
		$('txtZipCC').value = $('txtZip').value;
	}
	
	//switch readonly
	$('txtFNameCC').disabled = ($('chMatchDetails').checked);
	$('txtLNameCC').disabled = ($('chMatchDetails').checked);
	$('txtPhone1CC').disabled = ($('chMatchDetails').checked);
	$('txtPhone2CC').disabled = ($('chMatchDetails').checked);
	$('txtCCEmail').disabled = ($('chMatchDetails').checked);
	$('txtCCEmailCnfm').disabled = ($('chMatchDetails').checked);
	$('txtAddressCC').disabled = ($('chMatchDetails').checked);
	$('txtAddress2CC').disabled = ($('chMatchDetails').checked);
	$('selStateCC').disabled = ($('chMatchDetails').checked);
	$('txtCityCC').disabled = ($('chMatchDetails').checked);
	$('txtZipCC').disabled = ($('chMatchDetails').checked);
}



function addSingle(id) 
{
	
	var el = 	$('partyAddOn'+id);
	el.onblur = function() 
	{
		if (this.value > 0) 
		{
			this.value=1;
		}
	}
	
}


function selTime(intTime) 
{
	var timeSlot = $$('#partyTimes div');
	
	for(i=0;i<timeSlot.length;i++) 
	{
		timeSlot[i].className="selTimeSlot";
	}
	
	// set actual selected time and make selection stick
	$('selTimeSlot'+intTime).className="selTimeSlotSelected";
	$('rdoTimeSlot'+intTime).checked=true;
	$('rdoTimeSlot00').value = $('rdoTimeSlot'+intTime).value;
	
}


function populateCCInfo() 
{
	if ($('popTestCC').checked==true)
	{
		$('selCCType').value='Visa';
		$('selCCExpMonth').value='01';
		$('selCCExpYear').value='15';
		$('txtCCNum').value='4111111111111111';
		$('txtCCV').value='123';
	}
	
	
}

function returnNumbers(str)
{
	return str.replace(/\D/gi, '');
}

function trim(str)
{
	return str.replace(/(^[\s\xA0]+|[\s\xA0]+$)/g, ''); 
}
							
							
function showLanguageOptions() {
	
	var listing =  $('langList');

	if (listing.style.display=='none') {		
		$('selector').className='down';
	} else {
		$('selector').className='up';
	}
	
	Effect.toggle(listing, 'blind', { duration: .25 });
}




function getStateListings() {
	
	
	var countryCode = $('selCountry').value;
	
	// make sure a value exists
	if (countryCode != '') {
		
		// populate first state select list
		new Ajax.Updater('selState', 'assets/phajax/getStateListing.php?countryCode='+countryCode+'&lblSelect='+lblSelect);
		
		// populate second state select list
		new Ajax.Updater('selStateCC', 'assets/phajax/getStateListing.php?countryCode='+countryCode+'&lblSelect='+lblSelect);
	}
	
	
	
}



