﻿        var stage = 1;
        var g_country = "";
        
        $(document).ready(function(){
            // show the graphical headers
            $("tr.headrow").removeClass("headrow");
            $(".next").css("display", "block");
            $(".stage").css("display", "none");
            $(".stage:first").css("display", "block");
            $("#stageonebtn").css("cursor", "pointer");
            $("#stageonebtn").click( function(){
                   stageOne();
            });
            $("input.inputadd").css("visibility", "hidden");
            
           // $("#ccodephone").focus(focus);
           //$("#areacode").focus(focus);
           //$("#phone").focus(focus);
            $("#country").change(showstatefield);
            doFooter();
        });

        function focus(){
            this.value= "";
            $(this).css({"color":"#000000"});
            $(this).unbind('focus');
        }
        
        function setUSCC(val){
            $("#ccodephone").attr("value", val);// = (val == "")? "":val;
            $("#ccodephone").css({"color":"#000000"});
            $("#ccodephone").unbind('focus');
        }

        function showstatefield(){
            var val = g_country = this.options[this.selectedIndex].value;
            if(val == "United States" || val == "Canada"){
                $("#us_states").css("display", "inline");
                $("#anz_states").css("display", "none");
                $("#state").css("display", "none");
	            setUSCC("001");
            }else if( val == "Australia" || val == "New Zealand"){
                $("#anz_states").css("display", "inline");
                $("#us_states").css("display", "none");
                $("#state").css("display", "none");
	            //setUSCC("");
            }else{
                $("#state").css("display", "inline");
                $("#anz_states").css("display", "none");
                $("#us_states").css("display", "none");
	            //setUSCC("");
            }
            $("#staterow").removeAttr("style");
            $("#statespacerrow").removeAttr("style");
        }
            
        function highlight(elm){
            $(elm).css({"borderColor": "orange"});
        }
            
        function stageOne(){
            // validate input and show next stage
            var f = document.forms[1];
            
        
	        if( f.salutation.value == "!" ){
	            display("Please supply your Title.");
	           // highlight(f.salutation);
	            return false;
	        }
	        if(  f.first_name.value == "" ){
	            display("Please supply your First Name.");
	            //highlight(f.first_name);
	            return false;
	        }
	        if(  f.last_name.value == "" ){
	            display("Please supply your Last Name.");
	           // highlight(f.last_name);
	            return false;
	        }
	        if(  f.title.value == "" ){
	            display("Please supply your Job Title.");
	            //highlight(f.title);
	            return false;
	        }
            
            // check country
	        if(f.country.options[f.country.selectedIndex].value == "-"){
	            display("Please select your Country.");
	            //highlight(f.country);
	            return false;
	        }else{
	            // store globally
	            g_country = f.country.options[f.country.selectedIndex].value;
	        }
    	    
	        if( f.email.value.length < 3){
	            display("Please supply an Email Address.");
	            return false;
	        }
    	    
            //var reg = "\w+([-+.\']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
           // var reg = "^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]*))\.([a-zA-Z0-9]{2,4})$";
            var reg = /^((([a-z]|[0-9]|!|#|$|%|&|'|\*|\+|\-|\/|=|\?|\^|_|`|\{|\||\}|~)+(\.([a-z]|[0-9]|!|#|$|%|&|'|\*|\+|\-|\/|=|\?|\^|_|`|\{|\||\}|~)+)*)@((((([a-z]|[0-9])([a-z]|[0-9]|\-){0,61}([a-z]|[0-9])\.))*([a-z]|[0-9])([a-z]|[0-9]|\-){0,61}([a-z]|[0-9])\.)[\w]{2,4}|(((([0-9]){1,3}\.){3}([0-9]){1,3}))|(\[((([0-9]){1,3}\.){3}([0-9]){1,3})\])))$/ 
            var RegE = new RegExp(reg);
            var email = RegE.test(f.email.value);
            
	        if( !email ){
	            display("Please supply a valid Email Address.");
	            //highlight(f.email);
	            return false;
	        }
	       
	        if( f.ccodephone.value == "" || isNaN(f.ccodephone.value) ||  f.ccodephone.value == " " ){
	            display("Please supply a valid Country Area Code - numeric values only.");
	           // highlight(f.ccodephone);
	            return false;
	        }
	        if( f.areacode.value == ""  || isNaN(f.areacode.value) ||  f.areacode.value == " " ){
	            display("Please supply a valid Area Code - numeric values only.");
	            //highlight(f.areacode);
	            return false;
	        }
	        if( f.phone.value == ""  || isNaN(f.phone.value) ||  f.phone.value == " "){
	            display("Please supply a valid contact Telephone Number - numeric values only.");
	            //highlight(f.phone);
	            return false;
	        }
	        
	        // activate stage two button
            $("#stagetwobtn").click( function(){
                   stageTwo();
            });
	        
	        
	        
	      
            // if all is good... show the next stage
            $("#stage1").css("display", "none");
            $("#stage2").css("display", "block");
            
            doFooter();
        }
        
        function backStageOne(){
            $("#stage2").css("display", "none");
            $("#stage1").css("display", "block");
            
            doFooter();
        }
        
        function stageTwo(){
            // validate input and show next stage
            var f = document.forms[1];
            // set the territory based upon country value
            setTerritory(g_country, f);
            
            if( f.company.value == "" ){
	            display("Please supply your Company Name.");
	            return false;
	        }
	        if( f.street.value == "" ){
	            display("Please supply a valid Address.");
	            return false;
	        }
	        if( f.city.value == "" ){
	            display("Please supply a City.");
	            return false;
	        }
	        if( f.zip.value == "" ){
	            display("Please supply a valid Zip.");
	            return false;
	        };
	        
	        var field;
	        switch(g_country){
	            case "United States":
	            case "Canada":
	                field = f.us_states.value;
	                break;
	            case "Australia":
	            case "New Zealand":
	                field = f.anz_states.value;
	                break;
	            default:
	                field = f.state.value;
	                break;
	        }
	        if( field == "" ){
	            display("Please supply a valid State/Province");
	            return false;
	        }
	        
	        
            // if all is good... show the next stage
            $("#stage2").css("display", "none");
            $("#stage3").css("display", "block");
            
            doFooter();
        }
        
        function backStageTwo(){
            $("#stage3").css("display", "none");
            $("#stage2").css("display", "block");
            
            doFooter();
        }
        
        
        
        
         function display(str)
        {
            alert(str);
        }
        
        function setTerritory(cString, fObj){
            switch(cString.toLowerCase()){
                case "united kingdom" :
                case "ireland" :
                    t = "UK & Ireland";
                    break;
                    
                case "us":
                case "usa":
                case "united states":
                case "canada":
                case "puerto rico":
                case "paraguay":
                case "peru":
                case "suriname":
                case "uruguay":
                case "venezuela":
                case "mexico":
                case "argentina":
                case "bolivia":
                case "brazil":
                case "chile":
                case "colombia":
                case "ecuador":
                case "guyana":
                case "french guiana":
                case "dominican republic":
                    t = "Americas";
                    break;
                    
                case "luxembourg":
                case "netherlands":
                case "belgium":
                case "the netherlands":
                case "holland":
                    t = "BENELUX";
                    break;
                    
                case "bosnia":
                case "montenegro":
                case "hungary":
                case "ukraine":
                case "romania":
                case "georgia":
                case "austria":
                case "switzerland":
                case "germany":
                case "poland":
                case "czech republic":
                case "russia":
                case "italy":
                case "croatia":
                case "belarus":
                case "latvia":
                case "lithuania":
                case "estonia":
                case "albania":
                case "macedonia":
                case "bulgaria":
                case "moldova":
                case "slovakia":
                case "slovenia":
                    t = "DACH";
                    break;
                    
                    
                case "australia":
                case "new zealand":
                case "bangladesh":
                case "india":
                case "pakistan":
                case "taiwan":
                case "vietnam":
                case "micronesia":
                case "papua new guinea":
                case "malaysia":
                case "hong kong":
                case "singapore":
                    t = "ANZ";
                    break;
                    
                default:
                    t = "ROW";
                    break;
            }
            fObj.territory.value = t;
            //fObj.territory.value = escape(t);
    }
    
    function toggleServers(elm){
        var elm = "input#" + elm;
        if($(elm).css("visibility") == "hidden"){
        $(elm).css("visibility", "visible");
        }else{
        $(elm).css("visibility", "hidden");
        $(elm).attr("value", "0");
        }
    }