/* 
mushroom_stuff.js: library of javascript for animation / branding effects
    david.kelly@fanore.com (http://www.fanore.com/website_design.htm)
    
    
    
*/

function load_menus(){
    var el=get_el("js_menu_data");
    //alert(el.value);
    if (el){
       var buffer=el.value; 
       //alert(buffer);
       var h_lookup = new menu_lookup(buffer);
       hmenu=new make_menus(h_lookup,0);
       var el=get_el("content_menu");
       var shtml=hmenu.dhtml;
       el.innerHTML=shtml;
    }
    monitor_order_links();

}
//check_new_user: check new user form (displayed on checkout page)
function check_new_user(){
    var el=get_el("txt_email");
    if (el.value.length==0){
        alert("please enter the email address you wish to use with this service!");
        el.focus();
        return false;
    }
    var s=el.value;
    if (!fixstring(s,true)){
        alert("the email address you have entered is not supported on this system, please check your entry!");
        el.focus();
        return false;
    
    }
    return true;

}

//check_returning: validating 'returning customer' dialogue
function check_returning(){
// check that username field has been populated
    var el=get_el("txt_user");
    if (el.value.length==0){
        alert("please enter your email address !");
        el.focus();
        return false;
    }
    if (el.value=="e-mail address"){
        alert("please enter your email address !");
        el.value="";
        el.focus();
        return false;
    
    }
    var s=el.value;
    if (!fixstring(s,true)){
        alert("the email address you have entered is not supported on this system, please check your entry!");
        el.focus();
        return false;
    
    }
    
// check the password entry
    var el=get_el("txt_pass");
    if (el.value.length==0){
        alert("please enter your password !");
        el.focus();
        return false;
    }
    
    
    
return true;

}



//billing_details: capture billing details for customer accounts
function billing_details(){
    // company name
//    var el=get_el("txt_company");
//    if (el.value.length==0){
//        alert("please enter your  company name!");
//        el.focus();
//        return false;
//    }
    // first name
    var el=get_el("txt_firstname");
    if (el.value.length==0){
        alert("please enter your first name!");
        el.focus();
        return false;
    }
    // surname
    var el=get_el("txt_surname");
    if (el.value.length==0){
        alert("please enter your surname!");
        el.focus();
        return false;
    }
    // address line 1
//    var el=get_el("txt_address1");
//    if (el.value.length==0){
//        alert("please enter a two line address!");
//        el.focus();
//        return false;
//    }
//    // address line 2
//    var el=get_el("txt_address2");
//    if (el.value.length==0){
//        alert("please enter a two line  address!");
//        el.focus();
//        return false;
//    }
//    // address line 3 ==city
//    var el=get_el("txt_address3");
//    if (el.value.length==0){
//        alert("please enter your city!");
//        el.focus();
//        return false;
//    }
//    // address line 4 == state
//    var el=get_el("txt_address4");
//    if (el.value.length==0){
//        alert("please enter your state!");
//        el.focus();
//        return false;
//    }
//    // post-code == zip code
//    var el=get_el("txt_postcode");
//    if (el.value.length==0){
//        alert("please enter your zip-code!");
//        el.focus();
//        return false;
//    }
    // username /email
//    var el=get_el("txt_username");
//    if (el.value.length==0){
//        alert("please enter your email address (this is used for logging on to your web profile)!");
//        el.focus();
//        return false;
//    }
//    var m = el.value;
//    if (!fixstring(m,true)){
//        alert("the email address that you have entered contains characters that are not supported by our system please contact technical support!");
//        el.focus();
//        return false;
//    
//    }
//    // confirm email address
//    var el_confirm=get_el("txt_confirm");
//    if (el_confirm.value != m){
//        alert("your email address is critical for all actions on the web site, please confirm your email address");
//        
//        el_confirm.focus();
//        return false;
//        
//    }

    // mobile number or telephone number is required
    var el=get_el("txt_mobile");
    var el_telephone=get_el("txt_telephone");
    
    if ((el.value.length==0) && (el_telephone.value.length==0)){
        alert("please enter either a mobile or landline number!");
        el.focus();
        return false;
    }
	else {
		//validate mobile no
		var str = el.value
		var chr = ""	
		var nDigits = 0
		for (position = 0; position < str.length; position++){
		   chr = str.charAt(position)
		   if (!(chr >= "0" && chr <= "9")){
		   		alert("please enter a mobile phone number that contains only numbers");
                el.value="";
				el.focus();
				return false;
		   }
		}
		
		var str = el_telephone.value
		var chr = ""	
		var nDigits = 0
		for (position = 0; position < str.length; position++){
		   chr = str.charAt(position)
		   if (!(chr >= "0" && chr <= "9")){
		   		alert("please enter a phone number that contains only numbers");
                el_telephone.value="";
				el_telephone.focus();
				return false;
		   }
		}
	}


//    // telephone number
//    var el=get_el("txt_telephone");
//    if (el.value.length==0){
//        alert("please enter a land line number!");
//        el.focus();
//        return false;
//    }
    return true;


}


var iContext=false;
//display_addressbook: popup the addressbook div
function display_addressbook(record_id){
    icontext=record_id;

   $('addressbook').show();
   new Rico.Effect.SizeAndPosition('addressbook', 0, 10, 400, 400, 500, 20);
}
// check_participants: validates participants form - dynamic array of form items
function check_participants(){
   // pickup the list of events
   var el=get_el("event_payload")
   var buffer=el.value;
   	if (buffer.charAt(buffer.length - 1) == ",") buffer = buffer.substr(0,buffer.length - 1);
	var event_data=buffer.split(",");
    for (var i=0;i<event_data.length;i++){
        var event_id=event_data[i];
        //alert(event_id);
        var el=get_el("payload_" + event_id);
        var payload=el.value;
        if (!validate_payload(payload, event_id)){
           // alert("validate payload failed");
			return false;
        }
        
    }
    return true;
	

}

// validate_payload: process an event payload
function validate_payload(buffer, record_id){
   	if (buffer.charAt(buffer.length - 1) == ",") buffer = buffer.substr(0,buffer.length - 1);
	var payload_data=buffer.split(",");
    for (var i=0;i<payload_data.length;i++){
        var uid=payload_data[i];
        //alert(uid);
        var firstname=get_el("firstname_" + uid);
        var title=firstname;
        var surname=get_el("surname_" + uid);
        var email=get_el("email_" + uid);
        var telephone=get_el("telephone_" + uid);
        if (!validate_controls(title,firstname,surname,email,telephone, record_id, uid)){
            return false;
        }
    }
    return true;
}

//validate_controls: validate values of the passed controls
function validate_controls(title,firstname,surname,email,telephone, record_id, uid){

    // title
//    if (title.value.length==0){
//        alert("please enter this participant's job title!");
//        title.focus();
//        return false;
//    }
    
    
    // firstname
    if (firstname.value.length==0){
        alert("please enter this participant's firstname!");
        firstname.focus();
        return false;
    }
    
    
    // surname
    if (surname.value.length==0){
        alert("please enter this participant's surname!");
        surname.focus();
        return false;
    }
    
    
    // email
    if (email.value.length==0){
        alert("please enter this participant's email address!");
        email.focus();
        return false;
    }
    
    var m = email.value;
    if (!fixstring(m,true)){
        alert("the email address that you have entered contains characters that are not supported by our system please contact technical support!");
        email.value="";
        email.focus();
        return false;
    
    }
	//alert("testing emails");
	var account_uid = get_el("account_uid")
	var el = get_el("copy_" + account_uid.value + "_email")
	//alert("comparing: " + m + " to: " + el.value) 
    var a=get_el("accountid_" + uid).value;
	if ((m == el.value)&&(account_uid.value != a)){
		//alert("executing this address");
		icontext=uid;
		this_address(account_uid.value);
	}
	
    
    // title
    if (telephone.value.length==0){
        alert("please enter this participant's telephone number!");
        telephone.focus();
        return false;
    }
    
    
    return true;

}
function this_address(record_id){
    var prefix='copy_' + record_id + '_';
    var suffix='_' + icontext;
    var elname='';
    var elsource=false;
    var eldestination=false;
    
    elname='accountid';
    elsource=get_el(prefix + elname);
    eldestination=get_el(elname + suffix);
    if (eldestination.value==elsource.value){
        alert("you have already used this entry!");
        return void[0];
    }
    eldestination.value=elsource.value;
    elname='firstname';
    elsource=get_el(prefix + elname);
    eldestination=get_el(elname + suffix);
    eldestination.value=elsource.value;
    elname='surname';
    elsource=get_el(prefix + elname);
    eldestination=get_el(elname + suffix);
    eldestination.value=elsource.value;
//    elname='title';
//    elsource=get_el(prefix + elname);
//    eldestination=get_el(elname + suffix);
//    eldestination.value=elsource.value;
    elname='email';
    elsource=get_el(prefix + elname);
    eldestination=get_el(elname + suffix);
    eldestination.value=elsource.value;
    elname='telephone';
    elsource=get_el(prefix + elname);
    eldestination=get_el(elname + suffix);
    eldestination.value=elsource.value;
    $('link_' + record_id).href='javascript: void[0];'
   $('addressbook').hide();
   new Rico.Effect.SizeAndPosition('addressbook', 0, 100, 100, 30, 50, 2);
}

function hide_addressbook(){
   $('addressbook').hide();
   new Rico.Effect.SizeAndPosition('addressbook', 0, 100, 100, 30, 50, 2);

}

//cancellation: checks the 'cancellation comment' form - which is an optional feedback form 
function cancellation(){
    //txt_detail
    var el=get_el("txt_detail");
    if (el.value.length==0){
        alert("please complete the form if you wish to submit your feedback!");
        el.focus();
        return false;
    }
    
    return true;

}


//print_order: opens a new window ready for printing an order
function print_order(){
    var address='../asp/window.asp?z=print_order';
    var window_width=0;
    var window_height=0;
    var window_top=0;
    var window_left=0;
    
    // set width, height and positioning properties based on context
            window_width=600;
            window_height=600;
            window_top=0;
            window_left=0;
    var use_default=0; // over-ride default properties
    var k=open_new_window(address, 'order', use_default, window_width,window_height,window_top,window_left);
    return false;
}

function monitor_order_links(){
    var links=$('centrebar').getElementsByClassName('o_link');
    for (var i=0;i<links.length;i++){
       Event.observe(links[i], 'click', order_detail, false);

    }
}

function order_detail(event){
    var o=Event.element(event).id;
    var ajax_url="../asp/ajax.asp"
    var ajax_params="z=order_detail&o=" + o
    $('progress_panel').show();
    ajax_get_url(ajax_url,ajax_params,'utility_panel', display_utility);
}

function display_utility(){
    $('progress_panel').hide();
   $('utility_panel').show();
   //new Rico.Effect.SizeAndPosition('utility_panel', 230, 134, 400, 400, 100, 20);
}

function hide_utility(){
  $('utility_panel').hide();
  //new Rico.Effect.SizeAndPosition('utility_panel', 0, 1, 1, 1, 10, 2);
}
// mushroom_login: validates the login form that is presented to customers
function mushroom_login(){
// check that username field has been populated
    var el=get_el("txt_username");
    if (el.value.length==0){
        alert("please enter your email address !");
        el.focus();
        return false;
    }
    if (el.value=="e-mail address"){
        alert("please enter your email address !");
        el.value="";
        el.focus();
        return false;
    
    }
    var s=el.value;
    if (!fixstring(s,true)){
        alert("the email address you have entered is not supported on this system, please check your entry!");
        el.focus();
        return false;
    
    }
    
// check the password entry
    var el=get_el("txt_password");
    if (el.value.length==0){
        alert("please enter your password !");
        el.focus();
        return false;
    }
    
    
    
return true;
}

//mushroom_reminder: validates the password reminder dialogue (checking that user has entered an email address
function mushroom_reminder(){
// check that username field has been populated
    var el=get_el("txt_username");
    if (el.value.length==0){
        alert("please enter the email address you used before with this service!");
        el.focus();
        return false;
    }
    var s=el.value;
    if (!fixstring(s,true)){
        alert("the email address you have entered is not supported on this system, please check your entry!");
        el.focus();
        return false;
    
    }
return true;
    
}



