/****************** Navigation App Javascript File ****************
 Author: Carl Pryke
 Date: 22/07/2011
 Version: 1.0 
 Description: This defines a navigation hierachy, generating a 
 
 Notes: This could be extended to make hard-coded arrays of navigation names and links
 to be read in instead from an XML file.
 
 Need to read in URL's for page links. 
 
 Need to have a div id for innerHTML code.

 *************************************************************/ 

$(document).ready(function(){   
    $(".registerButton").click(function(){
       getEmails();
    });
    
    function getEmails(){
        $("#signUpPanel").html("<div class=\"signUpBox\"><input class=\"register\" id=\"registerField\" value=\"Enter Your Email Here\"></input></div><div class=\"registerButton\" id=\"registerButton\"></div>");
    
        $(".register").focus(function(){
            if($(".register").val() == "Enter Your Email Here"){
                $(".register").val("");
            }
        });
    
        $(".register").blur(function(){
            if($(".register").val() == ""){
                $(".register").val("Enter Your Email Here");
            }
        });
        
        $(".registerButton").click(function(){	
            if(checkEmail($(".register").val())){
				addToEmails($(".register").val());
            }
        });          
    }
    
    function addToEmails(value){
           var newXMLDocument = "<email>" + value + "</email></emails>";          
           $.ajax({type: "GET", url:"data/writeEmails.php", data: {"xmlDoc" : newXMLDocument, "email" : value}, success:function(result){
                $("#signUpPanel").html("<div class=\"registerText\" style=\"width: 250px\">Thank you for Registering With Incisive Edge (Solutions) Ltd</div>"); 
           }});
    }
  
});

function checkEmail(value){
	var correct = false;
	var atpos=value.indexOf("@");
	var dotpos=value.lastIndexOf(".");
	if (atpos<1 || dotpos<atpos+2 || dotpos+2 >= value.length)
    {
        correct = false;
  	}
  	else{
        correct = true;
  	}
  	return correct;
}
