//var oFCKeditor=false
function setup_page(){
    fix_height();
    
    
    
    var el=get_el("map");
    if (el){
        eval('load_map();');
    }
    var el=get_el("iitd_news");
    if (el){
        start_scroller();
    }
    
    
    var el=get_el('registration_form');
    if (el){ eval('setup_registration_form();');}
    
    var el=get_el('contact_form');
    if (el){eval('setup_focus_tracking(\'contact_form\',\'input_element\');'); }

    var el=get_el('upgrade_form');
    if (el){eval('setup_focus_tracking(\'upgrade_form\',\'input_element\');'); }

    var el=get_el("form_reminder");
    if (el){ eval('setup_focus_tracking(\'form_reminder\',\'input_element\');'); }

    var el=get_el("form_contact_us");
    if (el){ eval('setup_focus_tracking(\'form_contact_us\',\'input_element\');');}

    var el=get_el("billing_preference");
    if (el){ eval('setup_billing_preference();');}
    
    //login_form
    var el=get_el("login_form");
    if (el){eval('setup_focus_tracking(\'login_form\',\'input_element\');');}
    //password reminder
    var el=get_el("password_form");
    if (el){eval('setup_focus_tracking(\'password_form\',\'input_element\');');}
    //course_registration
    var el=get_el("course_registration");
    if (el){eval('setup_focus_tracking(\'course_registration\',\'input_element\');');}
    
    //new_messsage
    var el=get_el("new_messsage");
    if (el){eval('setup_focus_tracking(\'new_messsage\',\'input_element\');');}

    var el=get_el('tls_list');
    if (el){eval('update_tls_links();');}
    
    var el=get_el('training_schedule');
    if (el){eval('update_schedule_links();');}
    
    var el=get_el("credit");
    if (el){
        eval('intercept_credit();');
    }
    var p=outbound_links();  
    var el=get_el("buttons");
    if (el){
        eval('registration_buttons();');
    }
    
    var el=get_el('text_editor');
    if(el){
        eval('setup_editor();');
    }
    
    
}

//setup_editor: loads the fsk editor control and attach user stylesheet (if valid)
function setup_editor(){
/* Loads the new FCK Editor  */

    var datasource_name = 'fd_' + $('FCKDATASOURCE').value;
    var prefix=$('fck_base_url').value;
    
    //var sBasePath = document.location.pathname.substring(0,document.location.pathname.lastIndexOf('common')) ;
    var sBasePath = prefix + 'fck/fckeditor/';
    //alert(sBasePath);
    oFCKeditor = new FCKeditor( 'FCKeditor1' ) ;
    //alert(oFCKeditor);
    oFCKeditor.BasePath	= sBasePath ;
    oFCKeditor.Height	= 300 ;
    //oFCKeditor.Value	= 'This is some <strong>sample text<\/strong>. You are using <a href="http://www.fckeditor.net/">FCKeditor<\/a>.' ;
    oFCKeditor.Value = $(datasource_name).value
    var toolbar_set=$('html_editor_to_use').value
    var el=get_el("fck_css"); // this parameter set in enhanced_form.inc and can contain a comma delimited list of css files
    if (el){
        buffer=el.value;
        //alert(buffer);
        if (buffer.length!=0){
            var css=buffer.split(",");
            oFCKeditor.Config['EditorAreaCSS'] = css;
        }        
    }
    var el=get_el("fck_css_body"); // this parameter set in enhanced_form.inc and sets the element against which all styles are applied when presented in the editor
    if (el){
        buffer=el.value;
        oFCKeditor.Config['BodyId'] = buffer;
    }
    
        
    
    oFCKeditor.ToolbarSet = toolbar_set;
    oFCKeditor.Config['LinkUpload'] = false;
    oFCKeditor.Config['LinkBrowser'] = false;
    oFCKeditor.Create() ;
    
}














function registration_buttons(){
    var el=get_el('complete_later');
    if (el){
        Event.observe('complete_later','click',function(){
            window.location.href='../registration-z-guest_home-id-0.htm'});
    }
    var el=get_el('add_qualification');
    if (el){
        Event.observe('add_qualification','click',function(){
            window.location.href='../registration-z-edit_education-id-0.htm'});
    }
        
    var el=get_el('add_m_qualification');
    if (el){
        Event.observe('add_m_qualification','click',function(){
            window.location.href='../members-z-edit_m_qualification-id-0.htm'});
    }
        
    var el=get_el('goto_cpd');
    if (el){
        Event.observe('goto_cpd','click',function(){
            window.location.href='../registration-z-cpd.htm'});
    }
        
    var el=get_el('add_cpd');
    if (el){
        Event.observe('add_cpd','click',function(){
            window.location.href='../registration-z-edit_cpd-id-0.htm'});
    }
    var el=get_el('add_m_cpd');
    if (el){
        Event.observe('add_m_cpd','click',function(){
            window.location.href='../members-z-edit_m_cpd-id-0.htm'});
    }
    var el=get_el('goto_experience');
    if (el){
        Event.observe('goto_experience','click',function(){
            window.location.href='../registration-z-experience.htm'});
    }
        
    var el=get_el('add_experience');
    if (el){
        Event.observe('add_experience','click',function(){
            window.location.href='../registration-z-edit_experience-id-0.htm'});
    }
    var el=get_el('goto_review');
    if (el){
        Event.observe('goto_review','click',function(){
            window.location.href='../registration-z-review.htm'});
    }
        
        
        
        
}

//outbound_links: ensure that outbound links open in a new window
function outbound_links(){
    var children=$A($('main_panel').select('a'));
    //alert(children.length);
    var e_click=function(el)
    {   
        if ((!el.href.include('iitd'))&&(!el.href.include('javascript'))&&(!el.href.include('mailto'))){
            el.href='javascript: open_link(\'' + el.href + '\');';
         }
    }     
    children.each(e_click);     
    return void[0];
    
}
function intercept_credit(){
    var url=$('credit').href
    $('credit').href="javascript: open_link('" + url + "');"
    var url=$('credit2').href
    $('credit2').href="javascript: open_link('" + url + "');"
}









// fix_height; ensure that the main panel and footer fill screen
function fix_height(){
    var h=(document.viewport.getHeight()-140).toString() + 'px';
    var b=$('main_panel').getStyle('height')
    if (parseInt(b.toString().replace('px',''))< parseInt(h.replace('px',''))){
        $('main_panel').setStyle({height: h});
    }
    else{
        $('main_panel').setStyle({height: 'auto'});
    }
//    var c=(document.viewport.getHeight()+100).toString() + 'px';
//    $('credit_area').setStyle({top: c});
}

function load_map() {
    if (GBrowserIsCompatible()) {
        var map = new GMap(document.getElementById("map"));
        map.addControl(new GLargeMapControl())
        map.addControl(new GMapTypeControl());
              map.setCenter(new GLatLng(53.24184851164888, -6.667842864990234), 13);
        		
        	
        var point = new GPoint(parseFloat(-6.667842864990234), parseFloat(53.24184851164888));
        var marker = new GMarker(point);
        var icon = new GIcon();
        icon.image = "images/arrow_down_blue.jpg";
        icon.iconSize = new GSize(20,20);
        icon.iconAnchor = new GPoint(0,0);
        icon.infoWindowAnchor = new GPoint(10, 20);
        var marker = new GMarker(point,icon);
        
        map.addOverlay(marker);
        return marker
    }
 }

function update_tls_links(){
    var children=$('tls_list').select('a');
    var ev_click=function(el){
        var c = el.href;
            el.href="javascript: open_link('" + c + "');"
    }
    children.each(ev_click);


}
function update_schedule_links(){
    var children=$('training_schedule').select('a');
    var ev_click=function(el){
        var c = el.href;
            el.href="javascript: open_link('" + c + "');"
    }
    children.each(ev_click);
    $('tls_items').hide();


}
//access_denied: displays an alert to user who is trying to access something targetted at members only
function access_denied(param_context){

    switch (param_context){
        case 'HRD':
            alert("This feature is reserved for IITD members only, to access this resource you must either login or apply for membership!");
            return void[0];
            break;
        case 'AFF':
            alert("Details of our affinity partner offers are reserved for IITD members only, to access this resource you must either login or apply for membership!");
            return void[0];
            break;
        default:
    
    
    }
} 

//submit_form: utility that will perform client side validation of the passed form id and allow validator to post it 
function submit_form(param_form){
    //alert(param_form);
    var f=$(param_form)
    var cs_validation=f['cs_validation'];
    if (cs_validation){
        var f=$(param_form)
        var cs_validation=f['cs_validation'];
        if(eval($(cs_validation).getValue())==false){
            return void[0];
        }
//         return eval($(cs_validation).getValue());
    }
    else{
        $(param_form).submit();
    }
}




 // generic passowrd reminder trigger
function no_password_local(){
	var sURL = "password_request.asp";
	open_small(sURL);
}
function start_scroller(){
    var el=get_el("news_data_feed");
    pausecontent=new Array()
//pausecontent[0]='<a href="http://www.javascriptkit.com">JavaScript Kit</a><br />Comprehensive JavaScript tutorials and over 400+ free scripts!'
    
    if (el){
        var buffer=el.value;
        if (buffer.charAt(buffer.length - 1) == "|") buffer = buffer.substr(0,buffer.length - 1);
	    var news=buffer.split("|");
        for (var i=0;i<news.length;i++){
            var feed=news[i];
            if (feed.charAt(feed.length - 1) == "$") feed = feed.substr(0,feed.length - 1);
            var array_feed=feed.split("$");
            var html='<a class="news_headline" href="' + array_feed[2] + '">'+ array_feed[0] + '</a><p></p><a class="news_text" href="' + array_feed[2] + '">' + array_feed[1] + '</a>';
            pausecontent[i]=html;
        }
        new pausescroller(pausecontent, "iitd_news", "", 8000)
        //alert(buffer);
    
    }
    return void[0];
}


function open_link(url){
 var settings='Height=' + screen.height + ',Width=' + screen.width + ',Top=0,left=0,scrollbars=yes,resizable=1, location=1,status=1'
 var w=window.open(url,'iitdpopup',settings);    
}

//validate_search_form: test that user has entered valid keywords 
function validate_search_form(){
    var el=get_el("txt_keywords");
    if (el){
        
    }
    return true;
}

//directory_search: validate directory search form
function directory_search(){
    $('search_directory').submit();
}

// check_message: check that message form has been completed correctly
function check_message(){
    var i_status=parseInt($F("sender_status"));
    
    if (i_status==0){
        if ($F('m_name').blank()){
            alert("Please enter your name !");
            $('m_name').focus();
            return void[0];
        }
        
        if ($F('m_email').blank()){
            alert("Please enter your email address !");
            $('m_email').focus();
            return void[0];
        }
        var email=$F('m_email');
        if (!fixstring(email,true)){
            alert("the email address you have entered is not supported on this system, please check your entry!");
            $('m_email').focus();
            return void[0];
        }
        
        if ($F('m_telephone').blank()){
            alert("Please enter your contact number !");
            $('m_telephone').focus();
            return void[0];
        }
    }
    else{
        //message subject
        if ($F('m_title').blank()){
            alert("Please enter a subject !");
            $('m_title').focus();
            return void[0];
        }
        
    }
           
    if ($F('m_comment').blank()){
        alert("Please enter your message for this member !");
        $('m_comment').focus();
        return void[0];
    }
    $('message_details').value='';
    $('new_message').submit();
    
    
}


//setup_registration_form: populate the registration form (if edit)
function setup_registration_form(){
    enable_tab_tracking('registration_form');
}

//enable_tab_tracking: when displaying a form we set a highlight to indicate which 'line' a user has moved focus to
function enable_tab_tracking(param_el){
    var v=navigator.appVersion;
    if (v.toString().include('MSIE 6.0')){
        return void[0];
    }

    var cls_highlight='highlighted';
    var el=get_el('cls_highlight');
    if (el){
        cls_highlight=$('cls_highlight').value;
    }
    var children=$A($(param_el).select('input.input_element','select.input_element','textarea.input_element'));
    //alert(children.length);
    var evfocus=function(el)
    {   
        //alert(Rico);
         Event.observe(el,'focus',function(){
            var row_name='row_' + el.id;
            var r=get_el(row_name);
            if (r){
                if (!$(row_name).hasClassName(cls_highlight)){
                    $(row_name).toggleClassName(cls_highlight);
                }
            }
         });
         Event.observe(el,'blur',function(){
            var row_name='row_' + el.id;
            var r=get_el(row_name);
            if (r){
                $(row_name).toggleClassName(cls_highlight);
            }
         });
         //Rico.Corner.round(el.id);
    }
    children.each(evfocus);
   // $('save').focusFirstElement();
    
}

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;   
}


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.tagName=='INPUT'){
                    if (el.value.length==0){
                         el.value="rndtest"
                        }
                    }
                 }
        )||false;
    }
    children.each(efill);
}

//check_other: toggle display of the 'other' data entry field
function check_other(param_list){
//alert('check');
    if ($F(param_list).blank()){
        return false;
    }
    if ($F(param_list)=='other'){
        if ($('other_' + param_list).hasClassName('hide_me')){
            $(param_list + '_other').value='';
            $('other_' + param_list).toggleClassName('show_me');
            $(param_list + '_other').focus();
        }
    }
    else{
        if ($('other_' + param_list).hasClassName('show_me')){
            $('other_' + param_list).toggleClassName('show_me');
        }
       $('other_' + param_list).hide();
       $(param_list + '_other').value=$F(param_list); 
    }
}

//iitd_delete_row: delete a row from listing and refresh the list
function iitd_delete_row(param_id, param_title){
    //if (confirm('Are you sure you want to delete ' + param_title + '?')){
    var c_text='Are you sure you want to delete ' + param_title + '?'
            var c_url='../asp/ajax.asp';
            var c_winflag=2;
            var c_ajaxurl='../asp/ajax.asp';
            var c_ajaxparams='recordid=' + param_id + '&action=delete_education'
            var c_ajax_target=''
            var c_ajax_function=''
            var row_id='row_' + param_id
            if (confirm(c_text)){
                ajax_get_url(c_ajaxurl,c_ajaxparams,'','');
                Element.remove(row_id);
            }
    //}

}

//setup_billing_preference: ensures that the form displays the requried information on loading
function setup_billing_preference(){
    if ((parseInt($F('uid'))!=0)){
        $('list_method').selectedIndex=2;
        $('dd_details').show();
    }
    else{
        $('list_method').selectedIndex=1;
        
    }
    setup_focus_tracking('billing_preference','input_element');
}







