var service_popups = {
  "links": [
    { "name": "services_webfwd", "height": 280, "width": 490 },
    { "name": "services_dns", "height": 650, "width": 490 },
    { "name": "services_email", "height": 175, "width": 490 },
    { "name": "services_parking", "height": 350, "width": 490 },
    { "name": "services_placeholder", "height": 185, "width": 490 },
    { "name": "services_security", "height": 400, "width": 490 },
    { "name": "services_online", "height": 635, "width": 490 }
  ]
}; //putting a ',' at the end of the last hash ref above breaks in ie.


// for cc validation
var validate = new Array("card_number", "cvv2", "name_on_card");


Event.observe( window, 'load', function(){
   preloadImages();
   placeFocus();
   if( $('links_to_remove') ){   
        disable_links();                                
   }
   if( document.getElementById && $('show_all_privacy') ){
      $('show_all_privacy').id = '';
   }

   if( $('redraw') && $('redraw').value == 1 ){
      show_element( 'yes', 'redraw' );
   }
   $$('.service_link').each( function(val){ 
            Event.observe(val, 'mouseover', function(){
                if( val.hasClassName('first_time_button') ){
                    window.status = "";
                }
                window.document.body.style.cursor='pointer';
                return true;
            }) 
            Event.observe(val, 'mouseout', function(){
                window.document.body.style.cursor='default';
                window.status = "";
                return true;
            }) 
   } );
   // sets double click trap
   if( $('processing') ){
       Event.observe( $('processing'), 'click', function(){
           disable_button();
       });
   }
   if( $('privacy') ) {
       privacy_toggle();
       Event.observe( $('privacy_verify'), 'change', function() {
           privacy_toggle();
       } );
    }
    
    privacy_box_check ();

    if ( $$('input') ) {
        $$('input').each(function(e){
            if(e.type == 'checkbox'){
                if (e.value == 'ALL') {
                    $$('input.'+e.name+'_priv').each(function(box){
                        if ( box.type == 'checkbox' ) {
                            Event.observe( box , 'change', function() {
                                privacy_box_check();
                            });
                        }
                    });
                    
                }
            }
        });
    }
    popUpLinks();

    if( $('edit_cc_exp_date') ){
        Event.observe( $('edit_cc_exp_date'), 'click', function(){
            $('card_exp').hide();
            $('months_years').show();

            $('remove').writeAttribute('src',"images/update.gif");
            $('remove').writeAttribute('alt', "Save");

            $('view_cc').action = $('save_exp_url').readAttribute('value');
            return false;
        } );
    }
    if( $('autorenewal_prefs_form') ){
        toggle_autorenewal_paysource();
        $$('.autorenewals').each( function(box){
            Event.observe( box, 'click', function(){
                toggle_autorenewal_paysource( 'click' ); 
            } );
        } );
    }
    // Fix for new user's credit card autorenewal
    if( $('enter_cc') ){
        $('renewal_section').style.display = 'none';
    }
    $$('.autorenewal_warning').each(function(msg){
        msg.style.display = 'none';
    });
} ); // end observe on load

// This function is desined for privacy_checkboxes 
function privacy_toggle() {
    if ( $('privacy_verify').checked == true ){
         $('privacy').show();
    }else if( $('privacy_verify').checked == false ){
         $('privacy').hide();
    }
}

// This function is designed for privacy_contact
// this function checks if the sub_checkboxes and if they are all checked 
// it checks the appropriate main_checkbox and the opposite case as well.

function privacy_box_check () {
var input_list = $$('input');
    if ( input_list ) {
        input_list.each(function(e){
            if(e.type == 'checkbox'){
                if (e.value == 'ALL') {
                    var boxes=[];
                    $$('input.'+ e.name +'_priv').each(function(box){
                        if ( box.type == 'checkbox' ) {
                            boxes.push(box.checked);
                        }
                    });
                    if (boxes.all()){
                        e.checked= true;
                    }else {
                        e.checked= false;
                    }
                }
            }
        });
    }
}

function newImage(arg) {
   if (document.images) {
       var rslt = new Image();
       rslt.src = arg;
       return rslt;
   }
}

function changeImages() {
   if (document.images && (preloadFlag == true)) {
       for (var i=0; i<changeImages.arguments.length; i+=2) {
            document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
       }
   }
}

var preloadFlag = false;
function preloadImages() {
   if (document.images) {
      var Services_over = newImage("images/Services-over.gif");
      var about_over = newImage("images/about-over.gif");
      var faq_over = newImage("images/faq-over.gif");
      var prices_over = newImage("images/prices-over.gif");
      var hosting_over = newImage("images/hosting-over.gif");
      var renew_over = newImage("images/renew-over.gif");
      var register_over = newImage("images/register-over.gif");
      var processing = newImage("images/processing.gif");
      var disabled = newImage('images/finish_disabled.png');
      var continue_gray = newImage('images/continue-button-gray.gif');
      preloadFlag = true;
   }

   //links_to_remove references the span tag that encapsulates the pertinent links.
}

function placeFocus() {
   if (document.forms.length > 0) {
       var field = document.forms[0];
       for (var i = 0; i < field.length; i++) {
         if ((field.elements[i].type == "text") || (field.elements[i].type == "textarea") || (field.elements[i].type.toString().charAt(0) == "s")) {
             try{
                document.forms[0].elements[i].focus();
             }catch(e){ 
                break;
             }
             break;
         }
       }
   }
}

function popUp(url,w,h) {
   window.name = "_pair";
   var HelpWin;
   url = "help.m?" + url;
   HelpWin = window.open(url, 'Help',"status=no,height="+h+",width="+w+",scrollbars=yes,resizable=yes,toolbar=no,location=no,menubar=no,directories=no");
}

function popUpLinks(){
    service_popups.links.each( 
        function( val ){
           var el = $$("."+val.name);
           el.each( function( ele) { 
                Event.observe( ele, 'click', function(){
                    popUp( "file="+val.name, val.width, val.height );
                });
           });
       });
}

function winstat() {
   window.status = "Get Help On This Topic";
   return true;
}

function targetopener(mylink, closeme, closeonly) {
    if (! (window.focus && window.opener))return true;
    window.opener.focus();
    if (! closeonly)window.opener.location.href=mylink.href;
    if (closeme)window.close();
    return false;
}

//////////////////////////////////////////
function get_text( it ){
   var change_id = it.id;
   change_id = change_id.replace("link_", "");
   var is_inline = ( $( change_id ).getStyle('display') == 'block' ); 

   // Cycle through all divs and set display to none
   var faq_divs = $$('.faq_answer');
   faq_divs.each( function( val ){
                      val.setStyle({display: 'none'})
                  } );

   // If the link clicked is not displayed, display it and set the link text to bold
   // Other wise set it to normal
   if( !is_inline ){
       $( change_id ).setStyle( {display: "block"} );
       $( it.id ).setStyle( {fontWeight: 'bold'} );
   }else{
       $( it.id ).setStyle( {fontWeight: 'normal'} );
   }

   //set all link tags to normal unless they are the clicked on element
   var links = $$('.remove');
   links.each( function( link ){
                    if( link.id != it.id ){
                        link.setStyle({fontWeight: 'normal'}); 
                    }
                } );  //end each
}

// cycle through all link(anchor <a>) tags and remove href
// this is used if javascript is enabled
function disable_links(){
    var id_hrefs = $$('.remove');
    id_hrefs.each( function( val ){
                   val.removeAttribute('href');
                   } );
}

//Hides or displays the cc field form of the select_pay_method template depending
// on the existence of active paysources.

function display_cc_info(it){
    $$('.cc_info_section').each(function(cc_display){
        if(it.value != 'new' ){
            cc_display.style.display = 'none';
            $('renewal_section').style.display = 'inline';
        }    if( it.value == 'new' ){
            cc_display.style.display = 'inline';
            $('renewal_section').style.display = 'none';
        }
    });
}
// Hiddes and displays the autorenewal_warning and synchronizes
// their check boxes.
function autorenewal_warn(box){
    if( box.checked == true ){
        $$('.autorenew_dom').each(function(msg){
            msg.checked = true;
        });
        $$('.autorenewal_warning').each(function(msg){
            msg.style.display = '';
        });
        if ( $('cc_remember') ){
            $('cc_remember').checked = true;
            $('cc_remember').disabled = true;
        }

    }else{
        $$('.autorenew_dom').each(function(msg){
            msg.checked = false;
        });
        $$('.autorenewal_warning').each(function(msg){
            msg.style.display = 'none';
        });
        if ( $('cc_remember') ){
            $('cc_remember').disabled = false;
        }
    }
}

// Checks paymethods. If there isn't a paymethod selected 
// then the form will be validated.
function paysource_check(){
    var radios = $A( document.getElementsByName('method') ); // gets radio buttons
    var diff_cc = undefined;
    var paysource;
    if( $$('.current_cc') ){ 
       paysource = 1; 
    }
     
    //check to see if "Use a diffent card" was selected
    if( $('diff_cc') ){  
        var diff_cc = $('diff_cc').checked;  
    }
     
    // If paysource == 1 a paysource exists. If diff_cc is false
    // then they did not choose to use a different card.
    if( paysource == 1 && diff_cc == false ){

        //Ensures that at least one button was clicked.
        var rad_button = radios.any( function( rad ){ 
                    if( rad.checked == true ){
    	                return true; 
                    }
            } );

	    // If any of the paysources were selected then make the img change
        // since we already know that a new card is not being entered.
	    if( rad_button == true ){ 
            return disable_button();
	    }else{ 
            // If no paymethod selected, return false to select_pay_method form
	        alert( "Please select a payment method." ); 
	        return false;
	    }
    }else{ 
        // At this point it has been determined that no paysource
        // has been selected. Either there are no radio buttons to 
        // choose from or the choice to add a new card has been 
        // selected. In any case, build a list of fields that need
        // validation.

        // validate 'nickname' if the remember checkbox is checked
        // or if this is the first & only card. Remove it first 
        // though if the form submission was already attempted
        validate = validate.without('nickname');
        if( ($('cc_remember') && $('cc_remember').checked == true) || 
                  $('add_cc') ){
            validate.push('nickname');
        }

        var check = form_check( validate, "credit card"  );
        if ( check == false ){
             return check; 
        }
        // At this point form validation has taken place and succeeded.
        // Calling disable_button.
        if( paysource == 1 && diff_cc == true ){
            return disable_button();
        }

        // This is required for new accounts where this is their
        // initial credit card entry. 'enter_cc' is used in 
        // select_pay_methods during a new customer sign up.
        if( $('enter_cc') ){
            return disable_button();
        }

        // Here it is assumed that validation was successful and a new 
        // paysource is not being added on the 'select_pay_method' page
        return true;
    }
}

// Replaces the finish/continue et. al images, in order to avoid
// customer double clicks, with a greyed out version. Also 
// added the processing.gif.

function disable_button(){
	//change image src on the input element 'finish' button & disable
    if( $('finish') ) {
	    $('finish').src='images/finish_disabled.png';
	    $('finish').disabled = true;
    }else if( $('processing') ){
	    $('processing').src='images/continue-button-gray.gif';
	    $('processing').disabled = true;
    }
	
	// insert processing.gif into a new row & cell of the table 
	// "content",  which starts in templates/main_header
	var table = $('content');
	var process_row = table.insertRow( table.rows.length );
	var process = process_row.insertCell(0);
	process.innerHTML='<center><image src="images/processing.gif"></center>';

    // force form submission for IE
    if( $('processing_form') ){
        $('processing_form').submit();
    }
    
    return true;
}

// toggles a list of checkboxes on or off.
function all_toggle(box){
    var type=box.id;
    var value = box.checked;
   
    if ( type == 'autorenewal_all' ) {
        var check_list = $$('.autorenewals');
    } else {
        var check_list = $$('.'+type+'_priv');
    }

    check_list.each( function( box ) { 
        // toggle all shouldn't affect the "future domains" checkbox.
        if ( box.id == 'autorenewal_future' ) {
            return;
        }
        box.checked = value;
    } );
}

function show_element( show, redraw ){ 

    var table_rows = $$('.gift_bcc');
    var input_rows = $$('.clear_me');

    if( redraw == 'redraw' ){ $('recip').checked = 1; }
        
    table_rows.each( 
        function( val ){
            if( show == 'yes'){
                    try{ 
                        val.style.display = 'table-row';
                    }catch(e){
                        val.style.display = 'block';
                    }
            }else if( show == 'no' ){
                table_rows.each( function( tr ){
                                     tr.style.display = 'none';
                                     });
                if( val.style.display == 'none' ){
                    input_rows.each( function( in_val ){
                                         in_val.value = '';
                                     } );
                }
            }
    } ); //end each
}
function toggle_visibility( what, on_off ){
    if( on_off == 'on' ){
        $(what).show();
    }else{
        $(what).hide();
    }
}

function toggle_autorenewal_paysource( param ){
    var renew_boxes = $$('.autorenewals');
    var count = 0;
    var click = param

    renew_boxes.each( function( box ){
        if( box.checked == true ){ count++; }
    });

    if( count > 0 || click ){
        toggle_visibility('show_cc_form', 'on'); 
    }else{
        toggle_visibility('show_cc_form', 'off'); 
    }
}



