function gup( name )
{
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec( window.location.href );
    if( results == null )
        return "";
    else
        return URLDecode(results[1]);
}
            
function URLDecode(psEncodeString) 
{
    // Create a regular expression to search all +s in the string
    var lsRegExp = /\+/g;
    // Return the decoded string
    return unescape(String(psEncodeString).replace(lsRegExp, " ")); 
}            

$(function(){
    $("#error").css('visibility','hidden');

    // Set our default text
    var defaultText = "Enter your email address";

    // Set default state of input
    $("#email").val(defaultText);
    $("#email").addClass("ghost-text");                        

    // Apply onfocus logic
    $("#email").focus(function() {
        // If the current value is our default value
        if ($(this).val() == defaultText) {
            // clear it and set the text color to black
            $(this).val("");
            $(this).addClass("emailInput"); 
            $(this).removeClass("ghost-text");
            //$(this).style.color = "#000";
        }
    });

    $("#email").click(function() {
        // If the current value is our default value
        if ($(this).val() == defaultText) {
            // clear it and set the text color to black
            $(this).val("");
            $(this).addClass("emailInput"); 
            $(this).removeClass("ghost-text");
        //$(this).style.color = "#000";
        }
    });

    // Apply onblur logic
    $("#email").blur(function() {
        // If the current value is empty
        if ($(this).val() == "") {
            // set it to our default value and lighten the color
            $(this).val(defaultText);
            $(this).addClass("ghost-text");
            $(this).removeClass("emailInput"); 
                        
        }
    });
    
    $("#subscribe").click(function(){
        var emailPattern = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Za-z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|asia|jobs|museum)\b/;
        if($("#email").val() == defaultText || !emailPattern.test($("#email").val())){
            $("#error").css('visibility','visible');
        }
        else {
            //write email, city, % to file
            //window.location = 'thankyou.html';
            $("#subForm").submit();
        }
    });       

    var cashback = gup('cb');
    $(".percent").html(cashback);
    $("#cb").val(cashback);
    
    var city = gup('city');
    $(".cityname").html(city);
    $("#ct").val(city);
    $(".infoDiv").hide();
    
    var pg = "#"+gup('pg');
    $("#p").val(pg);
    $(pg).show();
});

