function refreshParent()
{
   window.opener.parent.location.reload();
   self.close();
}



function redirectParent( url )
{
   window.opener.parent.location = url;
   self.close();
}




function openWin(URL,width,height)
{

   if( URL != "" )
   {
      var winDetails = 'width=' + width + ', height=' + height + ',resizable=yes,scrollbars=yes,location=no';
      popWin = window.open(URL,'popWin',winDetails);
	  popWin.focus(); 
   }
}



function openWin2(URL,width,height)
{

   if( URL != "" )
   {
      var winDetails = 'width=' + width + ', height=' + height + ',resizable=yes,scrollbars=yes,location=no';
      popWin = window.open(URL,'popWinTwo',winDetails);
	  popWin.focus(); 
   }
}



function toggleHidden( hidden) {

		var hiddenElement = document.getElementById(hidden);	
			
        if ( hiddenElement.style.display == "" ){
		
		       hiddenElement.style.display = "none";

        } else if ( hiddenElement.style.display == "none" ){

			   hiddenElement.style.display = "";
	    }		   
}



    
var IE = document.all?true:false;
if (!IE) document.captureEvents(Event.MOUSEMOVE)
document.onmousemove = getMouseXY;
var tempX = 0;
var tempY = 0;

function getMouseXY(e) {
if (IE) { // grab the x-y pos.s if browser is IE
tempX = event.clientX + document.body.scrollLeft;
tempY = event.clientY + document.body.scrollTop;
}
else {  // grab the x-y pos.s if browser is NS
tempX = e.pageX;
tempY = e.pageY;
}  
if (tempX < 0){tempX = 0;}
if (tempY < 0){tempY = 0;}  




}



function toggleHiddenZ( hidden, location ) {


		var hiddenElement = document.getElementById(hidden);
		var locationElement = document.getElementsByName(location);	
			
        if ( hiddenElement.style.display == "" ){
		
		       hiddenElement.style.display = "none";

        } else if ( hiddenElement.style.display == "none" ){


               hiddenElement.style.left = tempX - 150 ;
			   hiddenElement.style.top = tempY - 50;
			   hiddenElement.style.display = "";
	    }		   
}








		function checkform(of)
		{
		

		
		// Test if DOM is available and there is an element called required
			if(!document.getElementById || !document.createTextNode){return;}
			if(!document.getElementById('required')){return;}

		// Define error messages and split the required fields
			var errorID='errormsg';
			var errorClass='error'
			var errorMsg='Sorry, there is a problem. Please update the highlighted fields and resubmit the form.';
			var errorImg='js/error-icon.gif';
			var errorAlt='Error';
			var errorTitle='This field has an error!';

			var reqfields=document.getElementById('required').value.split(',');			
			//reqfields[1]=document.getElementById('verify_email_fields').value.split(',');
			//reqfields[2]=document.getElementById('textareas_word_limit').value.split(',');
			//reqfields[3]=document.getElementById('radiobutton_comment_fields').value.split(',');
			
			



			
			
	    // Cleanup old mess
			// if there is an old errormessage field, delete it
			if(document.getElementById(errorID))
			{
				var em=document.getElementById(errorID);
				em.parentNode.removeChild(em);
			}
			

			//for(var j=0;j<reqfields.length;j++)
			//{		
			
			   // remove old images and classes from the required fields
			   for(var i=0;i<reqfields.length;i++)
			   {

				   var f=document.getElementById(reqfields[i]);
				   if(!f){continue;}
				   if(f.previousSibling && /img/i.test(f.previousSibling.nodeName))
				   {
					   f.parentNode.removeChild(f.previousSibling);
				   }
				   f.className='';
			   }
	
	        //}

            var checkedCount = 0;
			
			var checkboxMaxLimit = new Array();
			if( document.getElementById('checkboxMaxLimit') )
			{
			   checkboxMaxLimit=document.getElementById('checkboxMaxLimit').value.split(',');
            }
			
			
			if( checkboxMaxLimit[1] > 0 && checkboxMaxLimit[0] != "" )
			{
               var baseName = checkboxMaxLimit[0];

               for( formElement1 in of )
               {
                  formElementName = formElement1.toString();

                  if( formElementName.indexOf(baseName) != -1 )
                  {
				  
                     // If the element is a checkbox
                     // ----------------------------
                     if( of[formElement1].type.toLowerCase() == 'checkbox' )
                     { 
					    // if the element is check then up the count
						// -----------------------------------------
						if( of[formElement1].checked )
						{
						   checkedCount++;
						}
                     }
                 }
             }	    
         }

        if( checkedCount > checkboxMaxLimit[1] )
		{
		
		   var baseNamePure = _stripUnderScores( baseName );
		   alert( "You have exceeded the maximun number " + baseNamePure + " selections.\n Please select only " + checkboxMaxLimit[1] + " and resubmit." );
           return false;
		}

        
		// set the radio button unchecked val to true
		   var rb_checked = new Array();
		   var j = 0;
			
		// loop over required fields
			for(var i=0;i<reqfields.length;i++)
			{
			
			
		// check if required field is there
				var f=document.getElementById(reqfields[i]);
				if(!f){continue;}
		// test if the required field has an error, 
		// according to its type
		
				switch(f.type.toLowerCase())
				{
					case 'text':
						if(f.value=='' && f.id!='email'){cf_adderr(f, '  At least one required text field is empty.')}							
		// email is a special field and needs checking
						if(f.id=='email' && !cf_isEmailAddr(f.value)){cf_adderr(f, '   There is a malformed email address.')}							
					break;
					case 'textarea':
						if(f.value==''){cf_adderr(f, '  A required textarea is empty.')}							
					break;
					case 'file':
						if(f.value==''){cf_adderr(f, '  A file upload is required.')}							
					break;
					case 'checkbox':
						//if(!f.checked){cf_adderr(f, '  A checkbox selection is required.')}
						checkRadioButtonSelected( f, '  A checkbox selection is required.' );						
					break;
					case 'select-one':
						if(!f.selectedIndex && f.selectedIndex==0){cf_adderr(f, '  A menu selection is required.')}							
					break;
					case 'radio':
					    checkRadioButtonSelected( f, '  A radio button selection is required.' );
					break;
				}
			}
			
			
	
	
	
	       // Check to see that radio button associated text fields are filled out
		   if( document.getElementById('radiobutton_comment_fields') )
		   {
		   
              var radioButtonCommentFields = new Array();
			  var radioButtonFieldNames = new Array();

			  
		      if( document.getElementById('radiobutton_comment_fields').value != "" )
		      {	  
			  		   
		         radioButtonCommentFields = document.getElementById('radiobutton_comment_fields').value.split(',');
			     if ( radioButtonCommentFields[0] == "" ){ radioButtonCommentFields[0] = document.getElementById('radiobutton_comment_fields').value };
			     var countfields = 0;
			     while ( countfields < radioButtonCommentFields.length )
                 {
				 
				     radioButtonFieldNames = radioButtonCommentFields[countfields].split('::');
			         var e = document.getElementById(radioButtonFieldNames[0]);
					 var e_val = radioButtonFieldNames[1];
					 var e_other = document.getElementById(radioButtonFieldNames[2]);
					 
					 // remove old errors
					 if(e_other.previousSibling && /img/i.test(e_other.previousSibling.nodeName))
				     {
					    e_other.parentNode.removeChild(e_other.previousSibling);
				     }
				     e_other.className='';
					 
					 var i=0;
					 var selected_radio_button_value = "";
                     while(i<document.forms[0][e.name].length) {
   
	                    if (document.forms[0][e.name][i].checked) {
		                   selected_radio_button_value = document.forms[0][e.name][i].value;
	                    }
	                    i++;
                     }
					 var i=0;
                     if( selected_radio_button_value == e_val && e_other.value == "" )
				     {
					 
	                    var empty_radio_other_error_msg = " Please enter a value in the text field associated with the radio button response '" + e_val + "' or make another selection.";
				        cf_adderr(e_other, empty_radio_other_error_msg);
					    
				     }				
					  
                     countfields += 1;
                 }
				 
				 	
			     countfields = 0;
		      }	  
		   }			
			


	
	
	
	
	
	
	
	
			
			
			
			
           // Check for email fields and test
		   if( document.getElementById('verify_email_fields') )
		   {
              var emailFields = new Array();
			  
		      if( document.getElementById('verify_email_fields').value != "" )
		      {	  
			  		   
		         emailFields = document.getElementById('verify_email_fields').value.split(',');
			     if ( emailFields[0] == "" ){ emailFields[0] = document.getElementById('verify_email_fields').value };
			     var countemails = 0;
			     while ( countemails < emailFields.length )
                 {
			         var e = document.getElementById(emailFields[countemails]);
				     if( e.value != "" )
				     {
                        if(!cf_isEmailAddr(e.value)){
						   cf_adderr(e, '  There is a malformed email address.');
					    }
				     }	 
                     countemails += 1;
                 }
			     countemails = 0;
		      }	  
		   }			
			



           // Check for textareas with a word limit
		   if( document.getElementById('textareas_word_limit') )
		   {
              var textareaFields = new Array();
			  var textareaFieldMaxlength = new Array();
			  
		      if( document.getElementById('textareas_word_limit').value != "" )
		      {	  
			  		   
		         textareaFields = document.getElementById('textareas_word_limit').value.split(',');
			     if ( textareaFields[0] == "" ){ textareaFields[0] = document.getElementById('textareas_word_limit').value };
			     var counttextareas = 0;
			     while ( counttextareas < textareaFields.length )
                 {
				 
				     textareaFieldMaxlength = textareaFields[counttextareas].split('::');
			         var e = document.getElementById(textareaFieldMaxlength[0]);
					 var maxlimit = textareaFieldMaxlength[1];
				     if( e.value != "" )
				     {
                        var fullStr = e.value + " ";
                        if( fullStr != " " )
                        {
                           myregexp = new RegExp(/\s/);
                           var splitString = fullStr.split(myregexp);
                        }  
                        var word_count = splitString.length - 1;

                        if (word_count > maxlimit)
						{ 
                          var countup = 0;
	                      var over_by = word_count - maxlimit;
	                      var word_count_error_msg = " You have exceeded a field's word limit of " + maxlimit + " by " + over_by + " words.\nPlease review and edit your entry prior to submitting this form.";
						  cf_adderr(e, word_count_error_msg);
					    }
				     }	 
                     counttextareas += 1;
                 }
			     counttextareas = 0;
		      }	  
		   }			
			
			
			
			
			return !document.getElementById(errorID);

			/* Tool methods */
			function cf_adderr(o,specific_error)
			{
				// create image, add to and colourise the error fields
				var errorIndicator=document.createElement('img');
				errorIndicator.alt=errorAlt;
				errorIndicator.src=errorImg;
				errorIndicator.title=errorTitle;
				o.className=errorClass;
				o.parentNode.insertBefore(errorIndicator,o);

			// Check if there is no error message
				if(!document.getElementById(errorID))
				{
				// create errormessage and insert before submit button
					var em=document.createElement('div');
					em.id=errorID;
					var newp=document.createElement('p');
					newp.appendChild(document.createTextNode(errorMsg));
					newp.appendChild(document.createElement('p'));
					
					// clone and insert the error image
					newp.appendChild(errorIndicator.cloneNode(true));
					newp.appendChild(document.createTextNode(specific_error));
					
					em.appendChild(newp);
					// find the submit button 
					
                     
					 // place the error message above the element id error_msg
					 var sb = document.getElementById('error_msg');
					 sb.parentNode.insertBefore(em,sb);

			         					    
	
				} 
			}
			function cf_isEmailAddr(str) 
			{
			    return str.match(/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/);
			}
			
			
			
			
function checkRadioButtonSelected( f, checkerror )
{
   var checked = false;

   if( document.forms[0][f.name].length > 0 )
   {
      var i=0;
      while(i<document.forms[0][f.name].length) {
   
	      if (document.forms[0][f.name][i].checked) {
		     checked = true;
	      }
	      i++;
      }
   }else{
      // in case there is only one checkbox or radio button
      if (document.forms[0][f.name].checked) {
		     checked = true;
	  }
   
   }   
   if( !checked )
   {
      cf_adderr(f, checkerror);

   }


}			
			
			
			
}



// this utility function returns the string passed to it
// replacing '_' with ' '
// ----------------------------------------------------
function _stripUnderScores( str )
{
  // create an array to hold the tokens
  // ----------------------------------
  var tempArr =  new Array();

  // split up the word
  // ------------------
  tempArr = str.split('_');
  
  // if there were any underscores substitute them
  // with spaces and return the new string otherwise
  // just return the string itself
  // -----------------------------------------------
  if( tempArr.length != 0 )
  {     
    str = '';
    for( part in tempArr )
    { 
       if( tempArr[part] != 'req' && tempArr[part] != '-req' ){ str += tempArr[part] + ' '; }
    }
  }
  return str;

}
