// THIS IS A STANDARD JAVASCRIPT FILE USED FOR CDMS INTERNATIONAL
// THIS DECLARES ALL THE REGULAR EXPRESSIONS THAT WE WILL BE USING
// FOR VALIDATION FUNCTIONS.

var msgWindow;
var regWhitespace                = /^\s+$/;
var regLetter                    = /^[a-zA-Z]$/;
var regAlphabetic                = /^[a-zA-Z]+$/;
var regAlphanumeric              = /^[a-zA-Z0-9]+$/;
var regDigit                     = /^\d/;
var regLetterOrDigit             = /^([a-zA-Z]|\d)$/;
var regInteger                   = /^\d+$/;
var regEmail                     = /^[\w-\.]{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,3}$/;
var regMoney                     = /^[0-9]+.\d\d$/;
var regHnW                       = /^\d+\.\d{4}$/;
var digits                       = "0123456789";
var lowercaseLetters             = "abcdefghijklmnopqrstuvwxyz";
var uppercaseLetters             = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var whitespace                   = " \t\n\r";
var phoneNumberDelimiters        = "()- ";
var validUSPhoneChars            = digits + phoneNumberDelimiters;
var validWorldPhoneChars         = digits + phoneNumberDelimiters + "+";
var SSNDelimiters                = "- ";
var validSSNChars                = digits + SSNDelimiters;
var digitsInSocialSecurityNumber = 9;
var digitsInUSPhoneNumber        = 10;
var ZIPCodeDelimiters            = "-";
var ZIPCodeDelimeter             = "-";
var validZIPCodeChars            = digits + ZIPCodeDelimiters;
var digitsInZIPCode1             = 5;
var digitsInZIPCode2             = 9;
var creditCardDelimiters         = " ";
var mPrefix                      = "THE FOLLOWING FIELD NEEDS A VALUE\n\n";
var mSuffix                      = "\n\nPLEASE ENTER A VALUE.\n";




// =========================== 
// FUNCTIONS
// ===========================

function isEmpty(s)       { return ((s == null) || (s.length == 0)) }
function isWhitespace (s) { return (isEmpty(s)  || regWhitespace.test(s)); }

function js_number(value) {
   if (regInteger.test(value) == true) {
   } else {
    alert("You must supply a number for this field.");
    value.value='';
    value.focus;
    return;
   }
}

function IsNumeric(sText)

{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
}


function disableSubmit( xForm ) {
  xForm.disabled=true;
}

function sumup_check( form ) {

  var total = 0;

  for (i=0;i<form.length;i++) {
     if (form[i].type == 'checkbox' && form[i].name == 'volume[]' ) {
     
        if ( form[i].checked ) {
        
           var item = form[i].value;
           
           if ( form.halfyear.checked ) {

              total += Number( eval( 'form.subtotal_H'+item+'.value' ) );
           
           }
           else {

              total += Number( eval( 'form.subtotal_F'+item+'.value' ) );
           
           }
        }
     }
  }

  total += Number( form.subtotal_setupfee.value );
  
  form.total.value = cent( total );
	
}

function sumup_radio( form ) {

  var total = 0;

  var radioLength = form.volume.length;
  
   for(var i = 0; i < radioLength; i++) {
		
		if(form.volume[i].checked) {

           var item = form.volume[i].value;
           
           if ( form.halfyear.checked ) {

              total += Number( eval( 'form.subtotal_H'+item+'.value' ) );
           
           }
           else {
              total += Number( eval( 'form.subtotal_F'+item+'.value' ) );
			  if (Number(eval('form.subtotal_FSetupFee'+item+'.value'))>0){
				  form.subtotal_setupfee.value=cent(Number(eval('form.subtotal_FSetupFee'+item+'.value')));
			  }else{
				  form.subtotal_setupfee.value=cent(Number(0));
			  }
				//if (i<3) {
					//total += Number(50);
					//cent(Number(50));
				//}else{
					//form.subtotal_setupfee.value=(Number(0));	
				//}
           }

		}
	}
	
  total += Number( form.subtotal_setupfee.value );
  
  form.total.value = cent(total);
	
}

function sumup_3( form ) {

 var total = 0;
  
  for (i=0;i<form.length;i++) {
     var tempobj=form.elements[i];
	  if (tempobj.name.substring(0,9)== "subtotal_") {
         total += Number(tempobj.value.replace(/[,|$]/g, ''));
      }
      
  }
  form.total.value = cent(total);

} 

function cent(amount) {
   amount -= 0;
   amount = (Math.round(amount*100))/100;
   return (amount == Math.floor(amount)) ? amount + '.00' : (  (amount*10 == Math.floor(amount*10)) ? amount + '0' : amount);
}

// =========================== 
//
// ===========================

function toggle( node ) {

    var target = document.getElementById(node);
    
    if (target.style.display == "none") {
       target.style.display = "block";
    } else {
       target.style.display = "none";

    }
}

function toggle_option( node, which) {

    var target = document.getElementById(node);
    
    target.style.display = which;
}


function clicktoggle( node ) {

    var target = document.getElementById(node);
    
    if (target.style.display == "none") {
       target.style.display = "block";
    } 
}


function flip(name,src) {
    if (document.images)
        document.images[name].src = src;
}

// =========================== 
//
// ===========================

function warnEmpty (theField, s) { 
	theField.focus();
	cName = convert_name(s);
	alert('You must complete "'+cName+'" to continue with your order.');
}

// =========================== 
//
// ===========================
function intgerOnly(i) {
	if(i.value.length>0) {
       i.value = i.value.replace(/[^\d|\.|\,]+/g, ''); 
	}
}
// =========================== 
//
// ===========================

function convert_name (s) {
	var v;
	if (s == 'NAME')      { v = 'Name'; }
	if (s == 'ATTENDEES')      { v = 'Number of attendees'; }
	if (s == 'ORDERED_NAME_FIRST')      { v = 'First Name'; }
	if (s == 'ORDERED_NAME_LAST')      { v = 'Last Name'; }
	if (s == 'ORDERED_EMAIL')      { v = 'Email'; }
	if (s == 'CHURCH_NAME')      { v = 'Church Name'; }
	if (s == 'CHURCH_ADDRESS')      { v = 'Church Address'; }
	if (s == 'CHURCH_CITY')      { v = 'Church City'; }
	if (s == 'CHURCH_POSTALCODE')      { v = 'Church Postal Code'; }
	if (s == 'YOUTH_LEADER_NAME_FIRST')      { v = 'Youth Leader Name First'; }
	if (s == 'YOUTH_LEADER_NAME_LAST')      { v = 'Youth Leader Name Last'; }
	if (s == 'YOUTH_LEADER_PHONE')      { v = 'Youth Leader Phone'; }
	if (s == 'YOUTH_LEADER_EMAIL')      { v = 'Youth Leader Email'; }
	if (s == 'ACCOUNT_NAME_FIRST')      { v = 'Account Administrator Name First'; }
	if (s == 'ACCOUNT_NAME_LAST')      { v = 'Account Administrator Name Last'; }
	if (s == 'ACCOUNT_PHONE')      { v = 'Account Adminstrator Phone'; }
	if (s == 'ACCOUNT_EMAIL')      { v = 'Account Administrator Email'; }
	if (s == 'ADDRESS')      { v = 'Address'; }
	if (s == 'CC_NUM')      { v = 'Credit Card Number'; }
	if (s == 'CC_CVV2')      { v = 'CVV2 Security Code'; }
	if (s == 'CITY')      { v = 'City'; }
	if (s == 'POSTALCODE')      { v = 'Postal Code'; }


	return v;
}
// =========================== 
//
//  This is an insanse level of javascript validations for no 
//  reason
//
//
// ===========================


function validate1( form ) {

 var valid = true;

 var fValue = parseFloat( form.required_attendees.value );
 
 if ( isNaN( fValue ) || parseInt( fValue ) == 0 ) {
    warnEmpty( form.required_attendees , 'ATTENDEES' ); 
    valid = false;
 }

 if (!valid) { return false; } else { return true; }

} // CLOSING THE VALIDATE FUNCTION

function xxx(form) {
 var valid = true;
 
 for (i=0;i<form.length;i++) {
 		var tempobj=form.elements[i]; 		
		alert( tempobj.name );
  }

} // CLOSING THE VALIDATE FUNCTION

function newWindow(file,window,wid,hei) {
	var wtop  = (screen.height - hei) / 2;
    var wleft = (screen.width - wid) / 2;
    msgWindow=open(file,window,"toolbar=no,resizable=yes,scrollbars=yes,width=" + wid + ",height=" + hei + ",top=" + wtop + ",left=" + wleft + ",screenX=" + wleft + ",screenY=" + wtop);
	msgWindow.focus();
    if (msgWindow.opener == null) msgWindow.opener = self;
}

function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + '$' + num + '.' + cents);
}

function validate(form) {
 var valid = true;
 
 for (i=0;i<form.length;i++) {
 		var tempobj=form.elements[i]; 		
		if (tempobj.name.substring(0,9)== "required_") {
			shortFieldName=tempobj.name.substring(9,100).toUpperCase();
			//alert(shortFieldName);
			// THIS IS TO CHECK TEXT BOXES AND TEXTAREA
			if (tempobj.type == 'text' || tempobj.type == 'textarea' || tempobj.type == 'hidden' || tempobj.type == 'password') {
  	            if (isWhitespace(tempobj.value)) { 
				   if (tempobj.type == 'hidden') { alert('Hidden');valid = false; break; }
  				   warnEmpty(tempobj,shortFieldName); valid = false; break; 
				
				}
			}
			//  THIS IS TO CHECK SELECT BOXES THAT ARE NOT SET TO THE FIRST OPTION
			if (tempobj.type.toString().charAt(0)=="s"&& tempobj.selectedIndex==0) { 
		     warnEmpty(tempobj,shortFieldName); valid = false; break; 
         }
		}
		
		
		if ( tempobj.name == "pict" || tempobj.name == "required_pict" ) {
		   if (!(isWhitespace(tempobj.value))) { 
            newWindow('/sms/modules/standard/screens/small-progress.php','progressbar','350','100');		
		   }
		}
  }
  


    if (!valid) { return false; } else { return true; }

} // CLOSING THE VALIDATE FUNCTION

function validate7(form) {
 var valid = true;
 
 for (i=0;i<form.length;i++) {
 		var tempobj=form.elements[i]; 		
		if (tempobj.name.substring(0,9)== "required_") {
			shortFieldName=tempobj.name.substring(9,100).toUpperCase();
			//alert(shortFieldName);
			// THIS IS TO CHECK TEXT BOXES AND TEXTAREA
			if (tempobj.type == 'text' || tempobj.type == 'textarea' || tempobj.type == 'hidden' || tempobj.type == 'password') {
  	            if (isWhitespace(tempobj.value)) { 
				   if (tempobj.type == 'hidden') { alert('Hidden');valid = false; break; }
  				   warnEmpty(tempobj,shortFieldName); valid = false; break; 
				
				}
			}
			//  THIS IS TO CHECK SELECT BOXES THAT ARE NOT SET TO THE FIRST OPTION
			if (tempobj.type.toString().charAt(0)=="s"&& tempobj.selectedIndex==0) { 
		     warnEmpty(tempobj,shortFieldName); valid = false; break; 
         }
		}
		
		
		if ( tempobj.name == "pict" || tempobj.name == "required_pict" ) {
		   if (!(isWhitespace(tempobj.value))) { 
            newWindow('/sms/modules/standard/screens/small-progress.php','progressbar','350','100');		
		   }
		}
  }
  
  if ( isWhitespace( form.shipping_address.value )  && isWhitespace( form.shipping_city.value ) && isWhitespace( form.shipping_postalcode.value ) ) {
  
  
  }
  else {
  
     if (isWhitespace( form.shipping_address.value )) { 
	    warnEmpty(tempobj,'ADDRESS'); valid = false;
     }
     else if (isWhitespace( form.shipping_city.value )) { 
	    warnEmpty(tempobj,'CITY'); valid = false;
     }
     else if (isWhitespace( form.shipping_postalcode.value )) { 
	    warnEmpty(tempobj,'POSTALCODE'); valid = false;
     }
     else {
     
     }
  
  }
  
  $lValid = false;

  if (!valid) { return false; } else { return true; }

} // CLOSING THE VALIDATE FUNCTION

// =========================== 
//
// ===========================
function comboItemSelected(oList1,oList2,cType) {
	if (oList2!=null){
 	    alert('oList2 Not Null and need to clear it out');
		clearComboOrList(oList2);
       if (oList1.selectedIndex==-1){
         oList2.options[oList2.options.length] = new Option('SELECT','');
       } else {
	  // fillCombobox(oList2,cType + '=' + oList1.options[oList1.selectedIndex].value);
	   fillCombobox(oList2,cType + oList1.options[oList1.selectedIndex].value);
	   }
	}
}

function clearComboOrList(oList) {
   for (var i = oList.options.length-1; i>=0 ; i--) {
	   oList.options[i]=null;
   }
   oList.selectedIndex = -1;
   if (oList.onchange) oList.onchange();
}

function fillCombobox(oList,vValue){
   if (vValue!=''){
	   if (assocArray[vValue]){
		   oList.options[0] = new Option('SELECT','');
		   var arrX = assocArray[vValue];
           for ( var i = 0; i<arrX.length ; i = i+2 ){
			   if (arrX[i]!='EOF') oList.options[oList.options.length] = new Option(arrX[i+1],arrX[i]);
           }
           if (oList.options.length == 1){
			   oList.selectedIndex=0;
			   if (oList.onchange) oList.onchange();
           }
       } else {
		 oList.options[0] = new Option('None Selected','');  
	   }
   }
}

function listboxItemSelected(oList1,oList2,cType) {
   if (oList2!=null) {
	   clearComboOrList(oList2);
      if (oList1.selectedIndex==-1) {
         oList2.options[oList2.options.length] = new Option('SELECT','');
      } else {
		 fillListbox(oList2,cType + oList1.options[oList1.selectedIndex].value)
		 // fillListbox(oList2,oList1.name + '=' + oList1.options[oList1.selectedIndex].value)
	  }
   }
   else {

   }
}

function fillListbox(oList,vValue){
   if (vValue!=''){
	   if (assocArray[vValue]){
		   var arrX = assocArray[vValue];
		   for ( var i = 0; i<arrX.length ; i = i+2 ){
			   
			   if (arrX[i]!='EOF') oList.options[oList.options.length] = new Option(arrX[i+1],arrX[i]);

           }
           if (oList.options.length == 1){
			   oList.selectedIndex=0;
			   if (oList.onchange) oList.onchange();
 
           }
       } else {
		 oList.options[0] = new Option('None Selected','');  
	   }

   }
   else {
     	
   }
 
}

//Pop-it menu- By Dynamic Drive

//
// ************************
// layer utility routines *
// ************************

function getStyleObject(objectId) {
    // cross-browser function to get an object's style object given its id
    if(document.getElementById && document.getElementById(objectId)) {
	// W3C DOM
	return document.getElementById(objectId).style;
    } else if (document.all && document.all(objectId)) {
	// MSIE 4 DOM
	return document.all(objectId).style;
    } else if (document.layers && document.layers[objectId]) {
	// NN 4 DOM.. note: this won't find nested layers
	return document.layers[objectId];
    } else {
	return false;
    }
} // getStyleObject

function changeObjectVisibility(objectId, newVisibility) {
    // get a reference to the cross-browser style object and make sure the object exists
    var styleObject = getStyleObject(objectId);
    if(styleObject) {
	styleObject.visibility = newVisibility;
	return true;
    } else {
	// we couldn't find the object, so we can't change its visibility
	return false;
    }
} // changeObjectVisibility

function moveObject(objectId, newXCoordinate, newYCoordinate) {
    // get a reference to the cross-browser style object and make sure the object exists
    var styleObject = getStyleObject(objectId);
    if(styleObject) {
	styleObject.left = newXCoordinate;
	styleObject.top = newYCoordinate;
	return true;
    } else {
	// we couldn't find the object, so we can't very well move it
	return false;
    }
} // moveObject

// ********************************
// application-specific functions *
// ********************************

// store variables to control where the popup will appear relative to the cursor position
// positive numbers are below and to the right of the cursor, negative numbers are above and to the left
var xOffset = -75;
var yOffset = 5;

function showPopup (targetObjectId, eventObj) {
    if(eventObj) {
	// hide any currently-visible popups
	hideCurrentPopup();
	// stop event from bubbling up any farther
	eventObj.cancelBubble = true;
	// move popup div to current cursor position 
	// (add scrollTop to account for scrolling for IE)
	var newXCoordinate = (eventObj.pageX)?eventObj.pageX + xOffset:eventObj.x + xOffset + ((document.body.scrollLeft)?document.body.scrollLeft:0);
	var newYCoordinate = (eventObj.pageY)?eventObj.pageY + yOffset:eventObj.y + yOffset + ((document.body.scrollTop)?document.body.scrollTop:0);
	moveObject(targetObjectId, newXCoordinate, newYCoordinate);
	// and make it visible
	if( changeObjectVisibility(targetObjectId, 'visible') ) {
	    // if we successfully showed the popup
	    // store its Id on a globally-accessible object
	    window.currentlyVisiblePopup = targetObjectId;
	    return true;
	} else {
	    // we couldn't show the popup, boo hoo!
	    return false;
	}
    } else {
	// there was no event object, so we won't be able to position anything, so give up
	return false;
    }
} // showPopup

function hideCurrentPopup() {
    // note: we've stored the currently-visible popup on the global object window.currentlyVisiblePopup
    if(window.currentlyVisiblePopup) {
	changeObjectVisibility(window.currentlyVisiblePopup, 'hidden');
	window.currentlyVisiblePopup = false;
    }
} // hideCurrentPopup



// ***********************
// hacks and workarounds *
// ***********************

function explorerMacResizeFix () {
    location.reload(false);
}

// initialize hacks whenever the page loads
window.onload = initializeHacks;

// setup an event handler to hide popups for generic clicks on the document
document.onclick = hideCurrentPopup;

function initializeHacks() {
    // this ugly little hack resizes a blank div to make sure you can click
    // anywhere in the window for Mac MSIE 5
    if ((navigator.appVersion.indexOf('MSIE 5') != -1) 
	&& (navigator.platform.indexOf('Mac') != -1)
	&& getStyleObject('blankDiv')) {
	window.onresize = explorerMacResizeFix;
    }
    resizeBlankDiv();
    // this next function creates a placeholder object for older browsers
    createFakeEventObj();
}

function createFakeEventObj() {
    // create a fake event object for older browsers to avoid errors in function call
    // when we need to pass the event object to functions
    if (!window.event) {
	window.event = false;
    }
} // createFakeEventObj

function resizeBlankDiv() {
    // resize blank placeholder div so IE 5 on mac will get all clicks in window
    if ((navigator.appVersion.indexOf('MSIE 5') != -1) 
	&& (navigator.platform.indexOf('Mac') != -1)
	&& getStyleObject('blankDiv')) {
	getStyleObject('blankDiv').width = document.body.clientWidth - 20;
	getStyleObject('blankDiv').height = document.body.clientHeight - 20;
    }
}

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
         }
      }
   }    
}

if (window.attachEvent)
{
   window.attachEvent("onload", correctPNG);
}

// 11/11/2002 - THE FOLLOWING FUNCTION HAS BEEN ADDED
// THIS ALLOWS FOR MOUSEOVER ALT TAGS AND EXPANDABLE 
// SECTIONS.




