// JavaScript Document
function toggleSubmenu(submenuId, show){
    var theSub=document.getElementById('submenu-'+submenuId);
    var theMenu=document.getElementById(submenuId);
    if(show==1){
        theSub.className='submenu';
        theMenu.className='menu-over';
    }
    else{
        theSub.className='submenu-hide';
        theMenu.className='';
    }
}

function toggleSubmenuH(submenuId, show){
    var theSub=document.getElementById('submenu-'+submenuId);
    var theMenu=document.getElementById(submenuId);
    if(show==1){
        theSub.className='submenu-h';
    }
    else{
        theSub.className='submenu-hide';
    }
}


$(function(){
    //Controll the main menu drop down list
    $('ul li',$('#mainMenu')).hover(
        function(){
            $(this).addClass('over');
        },
        function(){
            $(this).removeClass('over');
        }
        );
		
    //Make the shipping details same as billing details
    $('#shippingController').click(function(){
        var theForm=document.forms['cart'];
        if(theForm.elements['controller'].checked){
            theForm.elements['dFirstName'].value=theForm.elements['bFirstName'].value;
            theForm.elements['dLastName'].value=theForm.elements['bLastName'].value;
            theForm.elements['dCompany'].value=theForm.elements['bCompany'].value;
            theForm.elements['dAddress'].value=theForm.elements['bAddress'].value;
            theForm.elements['dAddress1'].value=theForm.elements['bAddress1'].value;
            theForm.elements['dCity'].value=theForm.elements['bCity'].value;
            theForm.elements['dState'].value=theForm.elements['bState'].value;
            theForm.elements['dPostcode'].value=theForm.elements['bPostcode'].value;
            theForm.elements['dCountry'].value=theForm.elements['bCountry'].value;
            theForm.elements['dPhone'].value=theForm.elements['bPhone'].value;
        }else{
            theForm.elements['dFirstName'].value='';
            theForm.elements['dLastName'].value='';
            theForm.elements['dCompany'].value='';
            theForm.elements['dAddress'].value='';
            theForm.elements['dAddress1'].value='';
            theForm.elements['dCity'].value='';
            theForm.elements['dState'].value='';
            theForm.elements['dPostcode'].value='';
            theForm.elements['dCountry'].value='';
            theForm.elements['dPhone'].value='';
        }
    });
		
    //control the entry form address entry
    $('#addressControl').click(function(){
        if($(this).attr('checked')){
            $('#address_physical').attr('value',$('#address_postal').attr('value'));
        }else{
            $('#address_physical').attr('value','');
        }
    });
		
    //Add more rows for education achievements
    $('#rowControl').click(function(){
        var currentCount=parseInt($('#rowCount').attr('value'));
        var newCount=currentCount+2;
        var moreRows='';
        for(i=currentCount+1; i<=newCount; i++){
            moreRows+="<tr><td><input type='text' name='institution"+i+"' size='12' value='' />"+
            "<td><input type='text' name='place"+i+"' size='16' value='' />"+
            "<td><input type='text' name='from"+i+"' size='10' value='' />"+
            "<td><input type='text' name='to"+i+"' size='10' value='' />"+
            "<td><input type='text' name='achievement"+i+"' size='40' value='' />";
        }
        $('#achievementTable').append(moreRows);
        $('#rowCount').attr('value',newCount);
    });
		
    //Verify none empty filed before entry form is submitted
    $('#entryForm').submit(function(){
        if(!$('#agreement').attr('checked')){
            alert('Please read and tick the agreement box before you can proceed with this submission!');
            return false;
        }
											
        var error='';
        $('input,textarea').each(function(){
																	
            if($(this).attr('class')=='none-empty'  && $(this).attr('value')==null){
                error+=$(this).attr('title')+"\n";
            }
        });
        if(error!=''){
            alert("The following fields can not be empty: \n"+error);
            return false;
        }

        $('#entryform-submit').attr('disabled','disabled');
        $('#submit-message').css('display','block');
											
        return true;
    });
		
    //User registration form verification
    $('#loginForm').submit(function(){
        if($('#member_pass').attr('value')!=$('#member_pass1').attr('value')){
            alert('Passwords entered are not the same!');
            return false;
        }
    });
		
    //Blog commment area
    $('.reply-button').click(function(){
        $('#cancel-reply').css('display','block');
											
        $('#parent_id').attr('value',$(this).attr('title'));
        $('#commentform-section').css('display','none');
        $(this).parent().append($('#commentform-section').remove());
        $('#commentform-section').slideDown('slow');
											
        $('#cancel-reply').click(function(){
            $('#cancel-reply').css('display','none');
            $('#parent_id').attr('value',0);
            $('.comment-section').append($('#commentform-section').remove());
            return false;
        });
											
        return false;
    });
});		