﻿$(document).ready(function() {
    /*
    $("#coupon_options").find(".txtName").bind("focus", function(e) {
    if ($(this).val() === "Your Name") {
    $(this).val("").removeClass("text-box-mask");
    $(this).removeClass("error-box");
    }

    }).bind("blur", function(e) {
    if ($(this).val() === "") {
    $(this).removeClass("text-box-mask");
    $(this).addClass("error-box");
    $(this).val("Your Name");
    }
    });
    */
    $("#parking-coupon-form").find(".ddlNormalParkingLot").bind("focus", function(e) {
        $(this).removeClass("text-box-mask");
        $(this).removeClass("error-box");
        //if ($(this).val() === "Please Choose One") {
        //    $(this).val("").removeClass("text-box-mask");
        //    $(this).removeClass("error-box");
        //}

    }).bind("blur", function(e) {
        if ($(this).val() === "") {
            $(this).addClass("text-box-mask");
            $(this).addClass("error-box");
            //$(this).val("Please Choose One");
        }
        else {
            $(this).removeClass("text-box-mask");
            $(this).removeClass("error-box");
        }
    });
    $("#parking-coupon-form").find(".txtEmail").bind("focus", function(e) {
        $(this).removeClass("text-box-mask");
        $(this).removeClass("error-box");
        if ($(this).val() === "Enter email address") {
            $(this).val("");
            //    $(this).val("").removeClass("text-box-mask");
            //    $(this).removeClass("error-box");
        }

    }).bind("blur", function(e) {
        //alert($(this).val());
        if ($(this).val() === "" || $(this).val() === "Enter email address") {
            $(this).addClass("text-box-mask");
            //$(this).addClass("error-box");
            $(this).val("Enter email address");
        }
        else {
            $(this).removeClass("text-box-mask");
            $(this).removeClass("error-box");
        }
    });
    /*
    $("#coupon_options").find(".coupon-item").bind("click",function(e){
    var item = $(this);
    if($(item).attr("class") !== "coupon-item last-coupon-item"){
    $("#coupon_options").find(".coupon-item").removeClass("coupon-item-active");
    $(item).addClass("coupon-item-active");
    $("#coupon_options").find(".hCouponType").val($(item).find("input").val());
    //$("#coupon_page").val("park-and-save-coupon.aspx?coupon=" + $(item).find("input").val());
    }
    });
    */
    $("#parking-coupon-form").find(".submit").bind("click", function(e) {
        var error = [];
        var item = $("#parking-coupon-form");
        //var txtName = $(item).find(".txtName");
        var txtEmail = $(item).find(".txtEmail");
        var ddlNoramlParkingLot = $(item).find(".ddlNormalParkingLot");
        var couponType = $(item).find(".hCouponType");

        //do to check the drop down
        if ($(ddlNoramlParkingLot).val() === "Please Choose One" || $(ddlNoramlParkingLot).val() === "") {
            error.push('Your normal parking preference is required.');
        }


        //if($(txtName).val() === "Your Name" || $(txtName).val() === "")
        //error.push('Your name is required, ');
        if ($(txtEmail).val() === "Enter email address" || $(txtEmail).val() === "") {
            //error.push('Email address is required, ');
        }
        else {
            if (!echeck($(txtEmail).val()))
                error.push('Invalid email format.');
        }
        //if($(couponType).val() === "" || $(couponType).val() === "0")
        //error.push('Please click the desired coupon above.');


        if (error.join('').length > 0) {
            $("#output").html(error.join(''));
            return false;
        }
    })
});

function echeck(str) {
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	if (str.indexOf(at)==-1){return false;}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){return false;}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){return false;}
	if (str.indexOf(at,(lat+1))!=-1){return false;}
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){return false;}
	if (str.indexOf(dot,(lat+2))==-1){return false;}	
	if (str.indexOf(" ")!=-1){return false;}
	return true;
}
function showCoupon(page){
    var t = "coupon";
    var a = page+'?keepThis=true&TB_iframe=true&height=530&width=710&modal=true';
    var g = false;
    tb_show(t,a,g);
}
function notValid(){
    tb_remove();
    $("#output").html("invalid request");
}

