/**
* All the JavaScript for the PhatInvestor Game
*/

// Page init scripts
$(document).ready(function(){
    // Tabboxes
    $(".tabbox.inline").each( function(i){
	SetupTabBox(this);
    });
});
    
    
    $(document).ready(function(){
	$(".toggle-button").click(function () {
	    $(".form-toggle").toggle();
    	});
    });
    

    /*
* Tab box control
* @param {DomNode} tabbox The tab box container
*/
    function SetupTabBox(tabbox){
	
	// Get tabs and panels
	tabboxElem = tabbox;
	tabbox = $(tabbox);
	var tabs = tabbox.find("ul.tabs:first");
	var panels = tabbox.find(".panel");
	
	if(tabs.get(0) && panels.get(0)){
	    
	    tabs._tabbox = tabboxElem;
	    tabboxElem._tabs = tabs;
	    tabboxElem._panels = panels;
	    
	    // Click handler
	    tabs.find("a").click(function(evt){
		var anchor = $(this);
		var tab = anchor.parents("li:first");
		var tabbox = tab.parents(".tabbox:first").get(0);
		
		// Deselect all
		tabbox._tabs.find("li.selected").removeClass("selected");
		tabbox._panels.removeClass("selected");
		
		// Get ID
		var id = anchor.get(0).getAttribute("href");
		id = id.split("#")[1];
		
		// Select correct tab and panel
		tab.addClass("selected");
		$(tabbox).find("#"+ id +".panel").addClass("selected");
		
		
		return false;
	    });
	    
	}

    }
    function chkblank(strng) {
	if (strng.length == 0) {

	    return false;
	}
	return true;	  
    }
    function checkEmail (strng) 
    {
	if (strng == "")
	{
	    alert("Please enter an email address\n");
	    return false;
	}

	var emailFilter=/^.+@.+\..{2,3}$/;
	if (!(emailFilter.test(strng))) { 
	    alert("Please enter a valid email address\n");
	    return false;
	}
	else {
	    //test email for illegal characters
	    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
		if (strng.match(illegalChars)) {
		    alert("The email address contains illegal characters\n");
		    return false;
		}
	}
	return true;
    }
    function subreg()
    {
	
        if(!chkblank(document.getElementById('first_name').value))
	{ 
            alert("Please Enter First Name");
	    document.getElementById('first_name').focus();
            return false;
	}

	if(!chkblank(document.getElementById('last_name').value))
	{ 
            alert("Please Enter Last Name");
	    document.getElementById('last_name').focus();
            return false;
	}
	if(!checkEmail(document.getElementById('email').value))
	{ 
	    document.getElementById('email').focus();
            return false;
	}
	if(!chkblank(document.getElementById('password').value))
	{ 
            alert("Please Enter Password");
	    document.getElementById('password').focus();
            return false;
	}
	if(!document.getElementById('agree').checked)
	{ 
            alert("Please accept agrement");
	    
            return false;
	}
        return true
        
    }
