// Allows console to alert in IE
if( !console ) { var console = { log: function(msg) { alert(msg); } } }

	//Card design
	var cardSelectName;
	function cardDisplayVal(cardSelectName){
		if (cardSelectName == "NewYork"){ cardSelectName="New York"; }
		if (cardSelectName == "CentralPark") { cardSelectName = "Central Park"; }
		if (cardSelectName == "Pietrasanta") { cardSelectName = "Pietrasanta C10.19"; }
		$("#cardDsignName").empty().append(cardSelectName);
	}
	//end
	//2. Enter number of cards you are ordering
	var getQtyTextBox;
	function setQty(getQtyTextBox) {
	    getQtyTextBox = getQtyTextBox == undefined ? 0 : getQtyTextBox;
	    
		var radioBut = $(".BlankHolidayCardRadioButton input");
		var pricePerCard = $("#perCardHidden").text();

		var defaultRBVal = Number(getQtyTextBox * pricePerCard);
		defaultRBVal = defaultRBVal.toFixed(2);
		if(defaultRBVal > 0 && defaultRBVal != Number.NaN) $("#totPrice").empty().append("$" + defaultRBVal);
		
		if (getQtyTextBox > 0) {
		    $("#cardCount").empty().append(getQtyTextBox);
			$("#cardsname1").empty().append(" cards x ");
			$("#perCard").empty().append(pricePerCard);
			$("#cardsname2").empty().append(" per card");
		}
		else {
			$("#cardCount").empty().append(0)
			$("#cardsname1").empty().append("");
			$("#perCard").empty().append("");
			$("#cardsname2").empty().append("");
			}
		var temp = 0;
		if ($("#totPrice").text().substring(0,1)=="$"){
			var temp=$("#totPrice").text().substring(1);
		}
		var grandTotal =  Number(temp) + Number($(".donationAmount").val());
		$("#totGrand2").empty().append(""+grandTotal);
		if (Number($("#totGrand2").text())<0){
			$("#totGrand2").empty().append(0);
		}
	}
	//end
	//3. Select your card printing and mailing options
	var cardPrice, cardMailingOpt;
	function setPrice(cardPrice, cardMailingOpt) {
	
    	$("#perCardHidden").empty().append(cardPrice);
		$("#cardMailingOpt").empty().append(cardMailingOpt);
		//SubTotal
		var quatityNumber = Number($(".quantityTextBox").val());
		var subTotal = quatityNumber * Number(cardPrice);
		subTotal = subTotal.toFixed(2);
        
        if (quatityNumber > 0)
        {
            $("#perCard").empty().append(cardPrice);
		    $("#totPrice").empty().append("$"+subTotal);
		    if (Number($("#totPrice").text())<0){
			    $("#totPrice").empty().append(0);
		    }
		}
		//Grand Total
		var temp = 0;
		if ($("#totPrice").text().substring(0,1)=="$"){
			var temp=$("#totPrice").text().substring(1);
		}
		var grandTotal =  Number(temp) + Number($(".donationAmount").val());
		$("#totGrand2").empty().append(""+grandTotal);
		if (Number($("#totGrand2").text())<0){
			$("#totGrand2").empty().append(0);
		}
	}
	//end

	var mailingTbl;
	function OnShowReturnAddress(mailingTbl){ 
		if ($("#HandCallAddressDiv").show()) {
	        $("#HandCallAddressDiv").hide();
	        $(".mailingInfo").hide();
	    } 
		if($("#addressSectionDiv1").hide()){
			$("#addressSectionDiv1").insertAfter("."+mailingTbl).show();
			$(".mailingInfo").show();
		}
    }
    function OnShowHandReturnAddress(mailingTbl) {
        if($("#addressSectionDiv1").show()){
		$("#addressSectionDiv1").hide();
		$(".mailingInfo").hide();
        } 
        if ($("#HandCallAddressDiv").hide()) {
            $("#HandCallAddressDiv").insertAfter("." + mailingTbl).show();
            $(".mailingInfo").show();
        }
    }
	function HideReturnAddress(){
		if($("#addressSectionDiv1").show()){
		$("#addressSectionDiv1").hide();
		$(".mailingInfo").hide();
        }    
        if ($("#HandCallAddressDiv").show()) {
	        $("#HandCallAddressDiv").hide();
	        $(".mailingInfo").hide();
	    }   
	}
	function HideHandReturnAddress() {
	    if ($("#HandCallAddressDiv").show()) {
	        $("#HandCallAddressDiv").hide();
	        $(".mailingInfo").hide();
	    }
	}
	function HideHandCallAddress() {
	   
	}
	
	function InfoOnCardTextBox(){
		var nameInsideCard = $(".nameInsideCard").val();
		$("#nameOnCardTextBox").empty().append(nameInsideCard);
		var addressInsideCard = $(".addressInsideCard").val();
		$("#returnAddressTxtBox").empty().append(addressInsideCard);
	}

	function HandInfoOnCardTextBox() {
	    var nameInsideCard = $(".handNameInsideCard").val();
	    $("#nameOnCardTextBox").empty().append(nameInsideCard);
	    var addressInsideCard = $(".handAddressInsideCard").val();
	    $("#returnAddressTxtBox").empty().append(addressInsideCard);
	}
		
	//donation check
	function checkDonationVal(control){
		$(".errorMSG").empty();
		var is_valid = CheckValidation();
	    if(is_valid){
		    if($(".donationAmount").val() <= 0 || $(".donationAmount").val() == ""){
			    Lightbox.showBoxByID('donationOpt', 488, 316);
		    } else {   
		        // validation passed and donation was greater then 0
		        //Util.DoPostBack(control, "AddToCart");
		        $(".hiddenAdd").click();
		   };
		};
	}
	

	
	function checkDonationVal2(control){

	    //control.onclick = null;

        $(".donationOptInfo").find(".errorMSG").remove();

		var is_valid = CheckValidation(control);
	    if(is_valid){
		    if($(".donationAmount2").val() <= 0 || $(".donationAmount2").val() == ""){
		        var errorMSG = "";
		        errorMSG += "<p class='errorMSG'>*Please specify your donation amount.</p>";
		        $(errorMSG).insertAfter(".donationOptInfo img:eq(0)");
	        } else {
	            // validation passed and donation was greater then 0
		        $(".hiddenAdd").click();
		    }
		};
		
		return false;
	}
	
	function checkDonationVal3(control) {
        $(".hiddenAdd").click();
	}
	
	function CheckValidation() {
	    var is_designVal = DesignVal();
	    var is_quantityVal = QuantityVal();
	    var is_handCallVal = HandCalligraphyVal();
	    var is_computerCallVal = ComputerCalligraphyVal();
	    var is_customVal = CustomImprintVal();
	    var is_cardTypeVal = CardTypeVal();

	    var is_valid = is_designVal && is_quantityVal && is_cardTypeVal && is_handCallVal && is_computerCallVal && is_customVal;

	    if ( is_valid ) {	    
	        return true;
	    } else {
	        return false;
	    }
	    
	}
	
	function DesignVal() {
	    var selected = false;
		$(".cardDisplay input").each(function(i, ele){
	        if( $(ele).attr("checked") ){
		        selected = true;
	        }
		});
		if (selected) {
		    return true;
		} else {
		
		    var errorMSG = "";
			errorMSG += "*Please select a card design.";
			//$(errorMSG).insertBefore(".errorMessageInfo img:eq(0)");
			$(".errorMessageInfo #errorMSG").html(errorMSG);
			$(".errorMessageInfo").removeClass("hidden");
	            window.location.href = "#orderDetails";
            return false;
        }
	}
	
	
	function CardTypeVal() {
		var comp0 = $(".BlankHolidayCardRadioButton input");
		var comp1 = $(".ComputerCalligraphyAndMailingRadio input");
		var comp2 = $(".CustomImprintRadioButton input");
		var comp3 = $(".HandCalligraphyRadioButton input");
		
		var cardTypeSelected = comp0[0].checked || comp1[0].checked || comp2[0].checked || comp3[0].checked;

		if (cardTypeSelected) {
		    return true;
		} else {
		
		    var errorMSG = "";
			errorMSG += "*Please select your card and mailing options.";
			//$(errorMSG).insertBefore(".errorMessageInfo img:eq(0)");
			$(".errorMessageInfo #errorMSG").html(errorMSG);
			$(".errorMessageInfo").removeClass("hidden");
	            window.location.href = "#orderDetails";
            return false;
        }
	}
	function ComputerCalligraphyVal() {
	    var comp = $(".ComputerCalligraphyAndMailingRadio input");

	    if (comp[0].checked)
	    {
	        var nameInsideCard = $(".nameInsideCard").val();
	        var addressInsideCard = $(".addressInsideCard").val();
	        if ((addressInsideCard != null && addressInsideCard != "") &&
	            (nameInsideCard != null && nameInsideCard != "")) {
    	        
	            return true;
	        }
	        else {
	            $(".errorMessageInfo #errorMSG").html("*Please enter name and return address.");
	            $(".errorMessageInfo").removeClass("hidden");
	            window.location.href = "#orderDetails";
	            return false;
	        }
	    }
	    return true;
	}
	function HandCalligraphyVal() {
	    var comp = $(".HandCalligraphyRadioButton input");

	    if (comp[0].checked) {
	        var nameInsideCard = $(".handNameInsideCard").val();
	        var addressInsideCard = $(".handAddressInsideCard").val();
	        if ((addressInsideCard != null && addressInsideCard != "") &&
	            (nameInsideCard != null && nameInsideCard != "")) {

	            return true;
	        }
	        else {
	            $(".errorMessageInfo #errorMSG").html("*Please enter name and return address.");
	            $(".errorMessageInfo").removeClass("hidden");
	            window.location.href = "#orderDetails";
	            return false;
	        }
	    }
	    return true;
	}
	
    function CustomImprintVal() {
        
        var custVal = $(".CustomImprintRadioButton input");
        //var custVal2 = $(".CustomImprintMailingRadioButton input");
        
        //if (custVal[0].checked || custVal2[0].checked)
        if (custVal[0].checked) 
        {
            if ($(".quantityTextBox:eq(0)").val() > 499) {
	            return true;
	        } else {
                $(".errorMessageInfo #errorMSG").html("*Required number of cards for Custom Imprint is 500. Please re-enter the number of cards.");
                $(".errorMessageInfo").removeClass("hidden");
	            window.location.href = "#orderDetails";
                return false;	
	        }
        }
        
        return true;
    }

	function QuantityVal() {
	    if ($(".quantityTextBox:eq(0)").val() > 0) {
	        return true;
	    } else {
            $(".errorMessageInfo #errorMSG").html("*Please specify the amount of cards you would like to order.");
            $(".errorMessageInfo").removeClass("hidden");
            window.location.href = "#orderDetails";
            return false;	
	    }
	}
	
	function CheckDonationValidation() {
	    if ($(".donationAmount:eq(0)").val() > 0) {
	        return true;
	    } else {
    	    $(".errorMessageInfo #errorMSG").html("*Please enter name and return address.");
            $(".errorMessageInfo").removeClass("hidden");
            window.location.href = "#orderDetails";
	        return false;	
	    }
	}
	
	function setDonation() {
		if ($(".donationAmount:eq(0)").val().substring(0,1)=="$"){
			var temp=$(".donationAmount:eq(0)").val().substring(1);
		}
		else {
			var temp=$(".donationAmount:eq(0)").val();
		}
		temp=temp.replace(",","");
		if (temp > 0) {
			$(".noDonation").hide();
			$(".myDonation").show();
			$(".donation").val(String(temp));
		} else {
			$(".noDonation").show();
			$(".myDonation").hide();
			$(".donation").val('0.00');
		}
		
		 var temp = 0;
        if ($("#totPrice").text().substring(0,1)=="$"){
            var temp=$("#totPrice").text().substring(1);
        }
        var grandTotal = Number(temp) + Number($(".donationAmount").val());
		$("#totGrand2").empty().append(grandTotal );
		if (Number($("#totGrand2").text())<0){
			$("#totGrand2").empty().append(0);
		}
	}

//	var prefix="";
//	var wd
//	function parseelement(thisone){
//	    if (thisone ==  null || thisone.charAt(0) == "$")
//		return
//		wd="w"
//		var tempnum=thisone
//		for (i=0;i<tempnum.length;i++){
//			if (tempnum.charAt(i)=="."){
//				wd="d"
//				break
//			}
//		}
//		if (wd=="w")
//			thisone=prefix+tempnum+".00"
//		else{
//			if (tempnum.charAt(tempnum.length-2)=="."){
//				thisone=prefix+tempnum+"0"
//			}
//			else{
//				tempnum=Math.round(tempnum*100)/100
//				thisone=prefix+tempnum
//			}
//		}
//	}
    
	function setQuantity(){
		getQtyTextBox = $(".quantityTextBox").val();
		setQty(getQtyTextBox);
		if($().find(".customOrderDetails")){ 
			//parseelement(getQtyTextBox);
		}
	}

	function preLoadValues(){
		//1. add selected card to summary
		$(".cardDisplay input").each(function(i, ele){
			if( $(ele).attr("checked") ){
				$(this).click();
			}
		});
		//2. number of cards
		setQuantity();
		//3. card printing and mailing option
		$("#notifyMethod input").each(function(i, ele) {
		    if ($(ele).attr("checked")) {
		        $(this).click();
		        if (i == 1) { HandInfoOnCardTextBox(); }
		        if (i == 2) { InfoOnCardTextBox(); }
		    }
		});
		//4. check donationAmount
		setDonation();
	}
	//end
	
// On Document Ready, run:
$(function(){
	Lightbox.init();
	
	$('.holidayCardHover img').hover(
      function () {
        $(this).addClass('over');
      }, 
      function () {
        $(this).removeClass('over');
      }
      );
});
//main


