
function auto_populate(param_form){
    var f=$(param_form);
    
    var children=$A($(param_form).getElements());
    var efill=function(el)
    {
        return Try.these(
        function(){
                var d=new Date();
                if (el.value.length==0){
                     el.value="test"
                     //el.value="random" + d.getUTCmilliseconds().toString();
                    // alert(el.value.length);

                    }
                 }
        )||false;
    }
    children.each(efill);
}






// page_setup  called when page loads

function setup_page(){
    fix_height();
    //setup focus tracker (contained in 'scripts.js')
    var el=get_el('account_form');
    if (el){
        eval('setup_focus_tracking(\'account_form\',\'data_entry\');');
    }
    var el=get_el("membership_form");
    //alert(el);
    if (el){
        eval('setup_focus_tracking(\'membership_form\',\'data_entry\');');
    
    }
}
//  code added in August 2008 to initiate form tab tracking if relevant form in situ
function pickup_nav_menu(){
     var div_string=$('submenu_list').value ;   
    if (div_string.length!=0){
    	var adiv=div_string.split(",");
    	for (var d=0;d<adiv.length;d++){
    		var div_name='parent_div' + adiv[d];
            Event.observe(div_name, 'mouseover', set_sub_position);
    	}
    }
    
}




// added to validate promotional code (save user from having to return to the pre-payment screen)
function validate_booking(){
    var el=get_el("promo_code");
    if (el){
        if (el.value.length!=0){
            // need to check the promitional code before proceeding
            ibac_get_url('../asp/promo.asp','?p=' + el.value,'promo_contents', 'position_floating');
            //var n=window.open('../asp/promo.asp?p=' + el.value,'pop','height=400,width=400');
	        //if (window.focus) {n.focus()}

//            popup_editwindow(,'Promotional Code Validation')
            return false;
        }
    }
}


function changeFormDefinition(RoleID)
{
	token = '<%= request.querystring("t")%>';
	sURL = '../asp/primary_member.asp?t='+ token;
	window.location.href = sURL
}


function submit_this()
{
//Membership type
	var el = getElById("memberType");
	if (el.value.length==0 || el.value==0)
	{
		alert("Please select a Membership Type!")
		el.focus()
		return void[];
	}

//Call function (embedded in form definition)
	if (!validateForm())	return void[];
	
//Fill in the membership rate
	var el = getElById("fd_pager");
	el.value = '<%= iMemberShipRate%>';

//If we get to here then all ok - submit form
	document.member.submit()
}


function populateSubForms(sType)
{
	var el = getElById("fd_Members");	
	var iSubForms = el.value;

	var parent = 'fd_' + sType
	var el = getElById(parent);
	var parentValue = el.value;


	for (var i=2;i<=iSubForms;i++)
	{
		var child = 's' + i + '_' + sType	
		var el = getElById(child);
			
		el.value = parentValue;	
	}
}

function getElById(idVal) {
	if (document.getElementById != null) return document.getElementById(idVal)
	if (document.all != null) return document.all[idVal]
	return null
}

function validateForm()
{
//Title
	var el = getElById("fd_title");
	if (el.value.length==0)
	{
		alert("Please enter your Title (e.g. Mr. Ms.)!")
		el.focus();
		return false;
	}

//Organisation	
	el = getElById("fd_organisation");
	if (el.value.length==0)
	{
		alert("Please enter your Organisations Name!")
		el.focus();
		return false;
	}
	
//Firstname	
	el = getElById("fd_firstname");
	if (el.value.length==0)
	{
		alert("Please enter your First Name!")
		el.focus();
		return false;
	}

//Surname	
	var el = getElById("fd_surname");
	if (el.value.length==0)
	{
		alert("Please enter your Surname!")
		el.focus();
		return false;
	}	

//Address Line One	
	var el = getElById("fd_address1");
	if (el.value.length==0)
	{
		alert("Please enter the first line of your address!")
		el.focus();
		return false;
	}	

//Address Line Two
	var el = getElById("fd_address2");
	if (el.value.length==0)
	{
		alert("Please enter the second line of your address!")
		el.focus();
		return false;
	}	
	
//Email Address 
	var el = getElById("fd_email");
	if (el.value.length==0)
	{
		alert("Please enter a valid email address!")
		el.focus();
		return false;
	}	

//Email Address in correct format ?	
	var sval=el.value
	if (!fixstring(sval,true))
	{
		alert("Please enter a valid email address!")	
		el.focus();
		return false
	}

	if (!validateSubForm(2)) return false;
	
	if (!validateSubForm(3)) return false;
		
	return true;	
}

function validateSubForm(i)
{
//Title
	var el = getElById("s"+ i +"_title");
	var el = getElById("s"+ i +"_title");
	if (el.value.length==0)
	{
		alert("Please enter the Title (e.g. Mr. Ms.) for Member #"+ i +"!")
		el.focus();
		return false;
	}

//Organisation	
	el = getElById("s"+ i +"_organisation");
	if (el.value.length==0)
	{
		alert("Please enter the Organisations Name for Member #"+ i +"!")
		el.focus();
		return false;
	}
	
//Firstname	
	el = getElById("s"+ i +"_firstname");
	if (el.value.length==0)
	{
		alert("Please enter the First Name for Member #"+ i +"!")
		el.focus();
		return false;
	}

//Surname	
	var el = getElById("s"+ i +"_surname");
	if (el.value.length==0)
	{
		alert("Please enter the Surname for Member #"+ i +"!")
		el.focus();
		return false;
	}	

//Address Line One	
	var el = getElById("s"+ i +"_address1");
	if (el.value.length==0)
	{
		alert("Please enter the first line of the address for Member #"+ i +"!")
		el.focus();
		return false;
	}	

//Address Line Two
	var el = getElById("s"+ i +"_address2");
	if (el.value.length==0)
	{
		alert("Please enter the second line of the address for Member #"+ i +"!")
		el.focus();
		return false;
	}	
	
//Email Address 
	var el = getElById("s"+ i +"_email");
	if (el.value.length==0)
	{
		alert("Please enter a valid email address for Member #"+ i +"!")
		el.focus();
		return false;
	}	

//Email Address in correct format ?	
	var sval=el.value
	if (!fixstring(sval,true))
	{
		alert("Please enter a valid email address for Member #"+ i +"!")	
		el.focus();
		return false
	}
	
	return true;
}
var newwindow;
function popup_editwindow(url,page_title)
{
	newwindow=window.open(url,'name','height=500,width=400');
	if (window.focus) {newwindow.focus()}
}

function update_description(description_String)
{		
	//var radio_buttons = get_el("member_type")
	//var elLength = radio_buttons.length;
	//for(var i = 0; i < elLength; i++) {		
	//	if(radio_buttons[i].checked) {
	//		var selected_button = radio_buttons[i].value
	//	}
	//}
	
	var el = get_el("member_type_desc")
	el.innerHTML = '&nbsp;'+description_String
	
}
function deselect_radios(radio_name)
{
	var radio_buttons = get_el(radio_name)
	var elLength = radio_buttons.length;
	for(var i = 0; i < elLength; i++) {		
		if(radio_buttons[i].checked) {
			radio_buttons[i].checked = false
		}
	}
}
function clear_other(text_field_name)
{
	var el = get_el(text_field_name)
	el.value=''
}

function check_attendee_status()
{	
	var el = get_el("attendees_list").value	
	var attendee_list_array = new Array();
	attendee_list_array = el.split(',');	
	
	var el2 = get_el('attendees_status_list').value	
	var attendee_status_list_array = new Array();
    attendee_status_list_array = el2.split(',');
	
	var arrayLength = attendee_status_list_array.length;
	var alert_message
	alert_message = 0
	for (var i=0;i<attendee_list_array.length;i++){			
		if (attendee_status_list_array[i]==0)
		{
			alert_message = 1
		}
	}
		
	if (alert_message == 1){
		if (confirm("Some of the attendee details haven't been filled out. Do you wish to continue! ")==false){
	           return false;
	       }
	}
	return true;
	 
}

function check_subdetails(){
	
	var el = get_el("reset_pwd");
	if (el){		
		if (el.checked){
			if (confirm("You have chosen to reset the password.  This will generate a new password and email the password to the email address provided. Do you wish to continue! ")==false){
	           el.checked = false;
			   return false;
	       }
		}
	}
		
	return check_ibac_associate_member();
}

//function to check a member details - tests that required fields are complete
function check_member_local(){

	//var el=get_el("member_type")
	//alert(el.value)
	//if (el.value==0){
    //    alert("Please select a membership type !");
    //    $('member_type').focus();
   //     return false;
   // }
    
	var el=get_el("member_type");
	var radioChecked = 0;
	if(!el)
		return false;	
	var elLength = el.length;
	if(elLength == undefined)
		if(!el.checked)
			return false;		
	for(var i = 0; i < elLength; i++) {		
		if(el[i].checked) {
			radioChecked = 1;
		}
	}
	if (radioChecked == 0){
        alert("please select a membership type !");
        //el.focus();
        return false;
    }
	
	

 // check that organisation name has been entered
    if ($F('txt_company').length==0){
        alert("please enter the organisation name !");
        $('txt_company').focus();
        return false;
    }
 // check that salutation has been entered
    if ($F('txt_title').length==0){
        alert("please enter the salutation e.g. Mr. / Ms. !");
        $('txt_title').focus();
        return false;
    }
 // check that firstname has been entered
    if ($F('txt_firstname').length==0){
        alert("please enter your first name !");
        $('txt_firstname').focus();
        return false;
    }
 // check that surname has been entered
    if ($F('txt_lastname').length==0){
        alert("please enter your last name !");
        $('txt_lastname').focus();
        return false;
    }
 // check that email address has been entered and is of the correct format
    if ($F('txt_email').length==0){
        alert("please enter your email address !");
        $('txt_email').focus();
        return false;
    }
    var sval=$F('txt_email')
    // check email is correct format
	if (!fixstring_local(sval,true)){
		alert("Your email address is invalid this system only supports the format 'yourname@address.com' !");
		$('txt_email').focus();
		return false;
	}
 // check that address 1,2 & 3 have been completed
    if ($F('txt_address1').length==0){
        alert("please enter your complete address !");
        $('txt_address1').focus();
        return false;
    }
   // if ($F('txt_address2').length==0){
  //      alert("please enter your complete address !");
   //     $('txt_address2').focus();
   //     return false;
   // }
   // if ($F('txt_address3').length==0){
    //    alert("please enter your complete address !");
   //     $('txt_address3').focus();
    //    return false;
   // }
 // check that country has been selected
    
 // check that either a telephone number or mobile number have been entered
    if (($F('txt_telephone').length==0)&&($F('txt_mobile').length==0)){
        alert("please enter a telephone or mobile number !");
        $('txt_telephone').focus();
        return false;
    }
		
	
    return true;

}

//function to check associate member details - tests that required fields are complete
function check_ibac_associate_member(){	

 // check that firstname has been entered
    if ($F('txt_firstname').blank()){
        alert("please enter your first name !");
        $('txt_firstname').focus();
        return false;
    }
 // check that surname has been entered
    if ($F('txt_lastname').blank()){
        alert("please enter your last name !");
        $('txt_lastname').focus();
        return false;
    }
 // check that surname has been entered
    if ($F('txt_job_title').blank()){
        alert("please enter your Job Title !");
        $('txt_job_title').focus();
        return false;
    }
 // check that email address has been entered and is of the correct format
    if ($F('txt_email').blank()){
        alert("please enter your email address !");
        $('txt_email').focus();
        return false;
    }
    var sval=$F('txt_email')
    // check email is correct format
	if (!fixstring_local(sval,true)){
		alert("Your email address is invalid this system only supports the format 'yourname@address.com' !");
		$('txt_email').focus();
		return false;
	}
 // check that address 1,2 & 3 have been completed
    if ($F('txt_address1').blank()){
        alert("please enter your complete address !");
        $('txt_address1').focus();
        return false;
    }
    
    if ($F('txt_address2').blank()){
        alert("please enter your complete address !");
        $('txt_address2').focus();
        return false;
    }
    
   // if ($F('txt_address3').length==0){
    //    alert("please enter your complete address !");
   //     $('txt_address3').focus();
    //    return false;
   // }
 // check that country has been selected
    
 // check that either a telephone number or mobile number have been entered
    if (($F('txt_telephone').blank())&&($F('txt_mobile').blank())){
        alert("please enter a telephone or mobile number !");
        $('txt_telephone').focus();
        return false;
    }
    //check that telephone / mobile are numberic and > 6
    var telephone=fix_telephone($F('txt_telephone'));
    $('txt_telephone').value=telephone;
    var mobile=fix_telephone($F('txt_mobile'));
    $('txt_mobile').value=mobile;
    
    if (((telephone.blank())&&(mobile.blank()))||((6 > telephone.length)&&(9>mobile.length))){
        alert("please check your telephone or mobile number as they do not seem to be valid!");
        $('txt_telephone').focus();
        return false;
    }
    return true;

}

function fix_telephone(param_value){
    var ret_value='';
    var wip_value=param_value;
    var a_values='1234567890'
    //alert(wip_value.length);
    for (var i=0;i<=wip_value.length;i++){
    //alert(wip_value.charAt(i))
        for (var x=0;x<=a_values.length;x++){
            
            if (wip_value.charAt(i)==a_values.charAt(x)){
                ret_value+=wip_value.charAt(i)
            }
        }
    } 
    //alert(ret_value);
    return ret_value;   
}


/* generic string handler - indicates if dodgy characters exist in a posted string*/
function fixstring_local(svalue, bmail){
var str=svalue;
var sinvalid="!,£,\$,%,\&";

if (bmail==true){
	var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)|(%)|(")/; // not valid
	var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid
}
else{
	var reg1 = /(\.\.)|(^\.)|(%)|(")/; // not valid
	var reg2 = /([a-zA-Z0-9])|([a-zA-Z])|([0-9])/; // valid

}
if (!reg1.test(str) && reg2.test(str)) { // if syntax is valid
	return true;
	}
return false;
}

//function to check associate member details - tests that required fields are complete
function check_local_attendee(){	    

 // check that firstname has been entered
    if ($F('txt_firstname').length==0){
        alert("please enter your first name !");
        $('txt_firstname').focus();
        return false;
    }
 // check that surname has been entered
    if ($F('txt_lastname').length==0){
        alert("please enter your last name !");
        $('txt_lastname').focus();
        return false;
    }
 // check that email address has been entered and is of the correct format
    if ($F('txt_email').length==0){
        alert("please enter your email address !");
        $('txt_email').focus();
        return false;
    }
    var sval=$F('txt_email')
    // check email is correct format
	if (!fixstring_local(sval,true)){
		alert("Your email address is invalid this system only supports the format 'yourname@address.com' !");
		$('txt_email').focus();
		return false;
	} 	
    return true;
}
var current_X, current_Y
/* edit_document_group: creates a form for editing a document group */
function edit_participant_account(record_id,event_id){
//alert("here")
        var token=$('js_token').value; //token must be part of the parameters to maintain user identity
        var url="../asp/ibac_ajax.asp"; //handler for editing client
        
        var params="RecordID="+record_id+"&EventID="+ event_id +"&t="+token
        
        current_X=280; // over-ride default current_X because we want to control placement of the dialogue // current_X, current_Y
        current_Y=190;
        $('editor_heading').innerHTML='edit participant account'
      
        ibac_get_url(url,params,'editor_contents', 'position_floating');    
        //ibac_get_url(url,params,'editor_contents', debug_ajax);  
        
}

function position_floating(){
    new Rico.Effect.SizeAndPosition( 'floating_dialogue', current_X, current_Y, null, null, 10, 1,{complete: show_floating});
}

/* show_floating: displays the floating dialogue and checks if there are any elements that should be watched for focus and blur events */
function show_floating(){
    //alert($('editor_contents').innerHTML);
    Element.show('floating_dialogue') // display floating div   
    var el=get_el("watch_list_elements"); //get list of elements to observe
    if (el) {
        watch_list_array=$('watch_list_elements').value.split(",");
        for (var i=0; i < watch_list_array.length;i++){
            
            Event.observe(watch_list_array[i], 'focus', switch_hilite_on);        
            Event.observe(watch_list_array[i], 'blur', switch_hilite_off);        
        }
    }
    
}

//close_editor: generic form submission script
function close_editor(){
    $('floating_dialogue').hide();
}
function update_content(recordID){
    if (check_local_attendee()){
        var surl='../asp/ibac_ajax.asp'
        var attendee_name=$F('txt_title') + ' '+$F('txt_firstname') + ' '  + $F('txt_lastname');
        var attendee_email=$F('txt_email')
        var attendee_mem_num = $('txt_mem_num').value
        if (attendee_mem_num == 0){
            attendee_mem_num = 'N/A' 
        }
        //alert(attendee_mem_num)  
        //$('name_' + recordID).getElementsByClassName('link')[0].innerText=attendee_name;
        $('name_' + recordID).getElementsByClassName('link')[0].innerHTML=attendee_name;
        $('email_' + recordID).innerHTML = attendee_email
        $('memnum_' + recordID).innerHTML = attendee_mem_num
        var p=$('attendee_form').serialize();
        post_url(surl,p, completed_save());
        return false;
    }
}
function completed_save(){   
    close_editor();
}


// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
function post_url(url, p, f){
//alert(url);
  var d=new Date();
    var myAjax = new Ajax.Updater('', 
        url, 
        {
            method: 'post', 
            parameters: p + '&amp;d=' + d.toString(),
            onComplete: f
        });  
}
// when we post a form (normal post) to save.asp the result of the post will be a javascript function that can execute using eval
function post_result(obj){
   if (obj){
        if (obj.responseText){
                if (debugPanel){
                    $('debug_panel').innerHTML=obj.responseText;
                    }
           // eval(obj.responseText);
            }
   }
}




function set_sub_position(event){
    //alert(div_name)
	current_edit_element=Event.findElement(event,'div')
  	parent_id=current_edit_element.id    
    //alert(parent_id)
    if (parent_id.indexOf("parent_div")!= -1){
    	u_id= parent_id.replace("parent_div","")
    	subdiv_name = 'subdiv'+u_id
    	$('display_div').innerHTML = $(subdiv_name).innerHTML
    	
    	Element.setStyle('display_div',{left: findPosX(current_edit_element)+'px'});
    	Element.setStyle('display_div',{top: findPosY(current_edit_element)+17 +'px'});
    	 
    	Element.show('display_div')
    }
}

function hide_submenu(){
	Element.hide('display_div')
}
function show_submenu(){
	Element.show('display_div')
}
function findPosX(obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }
function findPosY(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }

// opens a url using ajax
function ibac_get_url(url,p, ctarget, chandler){
  var d=new Date();
  
  var handler=chandler;
  
  if (chandler.length!=0){
    handler+='();'
  }
  
  //alert(url);
  //alert(p);
  //alert(ctarget)
  //alert(chandler)
  if (handler.length!=0){
    var myAjax = new Ajax.Updater(ctarget, 
        url, 
        {
            method: 'get', 
            parameters: p + '&d=' + d.toString(),
            onComplete: eval(handler)
        });        
  }
  else{
    var myAjax = new Ajax.Updater(ctarget, 
        url, 
        {
            method: 'get', 
            parameters: p + '&d=' + d.toString()
        });        
  }
}



function fix_height(){
    var h=(document.viewport.getHeight()-174).toString() + 'px';
    var r=(document.viewport.getHeight()-174).toString() + 'px';
    
    var b=$('main_panel').getStyle('height')
    if (parseInt(b.toString().replace('px',''))< parseInt(h.replace('px',''))){
        $('main_panel').setStyle({height: h});
        $('content_container').setStyle({height: h});
        $('rhs_panel').setStyle({height: r});
    }
    else{
        $('main_panel').setStyle({height: 'auto'});
        $('content_container').setStyle({height: 'auto'});
        $('rhs_panel').setStyle({height: document.viewport.getHeight() + 'px'});
    }
    /*
    var el=get_el('e_terms');
    if (el){
        var t=(document.viewport.getHeight()-100).toString() + 'px';
        $('e_terms').setStyle({top: t}); 
    }
     */
}



function check_reminder(){
    if ($F('txt_email').blank()){
        alert("please enter your email address!");
        $('txt_email').focus();
        return false;
    }
    var e=$F('txt_email');
	if (!fixstring(e,true))
	{
		alert("Please check the email address that you entered as it does not seem to be valid!")	
        $('txt_email').focus();
        return false;
	}
    return true;    
}




