///<remarks>
///====================================================================
/// Name: activities.js
/// Javascript function for activity
/// Construction Date: 06/11/2009
/// Author: Sanjeet Shrivastva
/// Last Revision Date: 
/// Last Revision By:  
/// Last Revision Change: 
/// ====================================================================
/// Copyright (c) 2009 Vail Resorts, Inc.
/// ====================================================================
///</remarks>

//This function validates general info entered by customer.
function ValidateGeneralDataInfo(val, args)
 {    
    var adultId = document.getElementById('columnCenter_ctl00_adultNo');
    var kidId = document.getElementById('columnCenter_ctl00_kidsNoAdded');
    //var adultId = document.getElementById('ConfigureActivity_adultNo');
    //var kidId = document.getElementById('ConfigureActivity_kidsNoAdded');
     
     if (ValidateTotalNumberOfTraveler(adultId,kidId) == true)
     {
          return (args.IsValid = true);
     }
     else
     {
        return (args.IsValid = false);
     }
     return(args.IsValid = false);
}

// This function displays as many child age drop down as required by the customer.
function ToggleDropDowns(id) {

    var loopCount = 0;
    var innerLoop = 0;
    var dropDowns = document.getElementsByTagName("li");
    var selector = id.value;    
    var selector = Number(selector);    
         
    for (loopCount = 0; loopCount < dropDowns.length; loopCount++) {
        if (dropDowns[loopCount].id.indexOf("childAgeRepeater") > -1) {
            document.getElementById(dropDowns[loopCount].id).className = 'adultsKids2 hideDiv';                 
        }      
    }  
    
    for (loopCount = 0; loopCount < dropDowns.length; loopCount++) {  
        
        if (dropDowns[loopCount].id.indexOf("childAgeRepeater") > -1) {
            for (innerLoop = loopCount; innerLoop < (loopCount + selector); innerLoop++) {
                  document.getElementById(dropDowns[innerLoop].id).className = 'adultsKids2';
            }
            document.getElementById('childrenRange').className = '';
            break;
        }        
    }  
    
    if(selector == 0)
         document.getElementById('childrenRange').className = 'hideDiv';    
    
}

//This function validates if total number of people is greater than 19.
function ValidateTotalNumberOfTraveler(adultId, childId) {
        
    if ((parseInt(adultId.value) + parseInt(childId.value)) > 19) {       
        document.getElementById('errorPeopleLimit').innerHTML = "Total number of people cannot exceed 19.";
        document.getElementById('errorPeopleLimit').style.display = 'block';
        return (false);
    }
    document.getElementById('errorPeopleLimit').style.display = 'none';
    return (true);
}

$(document).ready(function() {

$("[id$='btnContinue']").click(ContinueButtonClick);


//    // Get the url.
//    var fullURL = parent.document.URL;
//    if(fullURL.indexOf('Search=') != -1)
//    {
//        // Get the search string from url.
//        var extractURL = fullURL.substring(fullURL.indexOf('productcode='), fullURL.length);
//        var search = extractURL; 
//        var splitedQueryString;           
//           
//        if(extractURL.indexOf('&') >= 0)
//        {    
//            splitedQueryString = search.toString().split('&');
//        }
//        
//        if(splitedQueryString !="")
//        {        
//        }
//     }

});


function ContinueButtonClick()
{
 var productCode=$("[id$='columnCenter_ctl00_hdnActivityProductCodeID']").val();

 var adultCount = $("[id$='columnCenter_ctl00_adultNo']");
 if (adultCount.length > 0) {
    adultCount = adultCount.val();
 } else {
    adultCount = 1;
 }

 var childCount = $("[id$='columnCenter_ctl00_kidsNoAdded']");
 if (childCount.length > 0) {
    childCount = childCount.val();
 } else {
    childCount = 0;
 }

 var quantity = $("[id$='columnCenter_ctl00_SelectQuantityList']");

 if (quantity.length > 0) {
    quantity = quantity.val();
 } else {
    quantity = 1;
 }
 
 var childrenAges = [];
    $("li.adultsKids2 select:lt(" + childCount + ")").each(function() {
        childrenAges.push($(this).val());
    });
    childrenAges.join(",");    
 
 //var cartProductId=$("[id$='hdnCartProductId']").val();
 var activitySystemName=$("[id$='columnCenter_ctl00_hdnActivitySystemName']").val();

 $.ajax
        ({
            type: "POST",
            url: "/vailresorts/sites/PlanningAndBooking/WebServices/ActivityWebService.svc/RedirectToCartSummary",
            async: false,
            data: "{\"activityProductCodeID\" : \"" + productCode
                    + "\",\"adultNo\" : \"" + adultCount
                    + "\",\"kidNo\" : \"" + childCount
                    + "\",\"childrensAge\" : \"" + childrenAges
                    + "\",\"activitySystemName\" : \"" + activitySystemName
                    + "\",\"quantity\" : \"" + quantity + "\"}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {
                var response = msg.d;

                if (response.indexOf('http') >= 0) {
                    window.close();
                    parent.window.location = msg.d;
                }
                else if (response == "ErrorTraveller") {
                    document.getElementById('errorPeopleLimit').innerHTML = "Total number of people cannot exceed 19.";
                    document.getElementById('errorPeopleLimit').style.display = 'block';
                    document.getElementById('errorDateSelection').style.display = 'none';
                    document.getElementById('lblErrorMessage').style.display = 'none';
                }
                else if (response == "ErrorDate") {
                    document.getElementById('errorDateSelection').innerHTML = "Please complete the following required fields:<br><ul><li>Select Dates </li></ul>";
                    document.getElementById('errorDateSelection').style.display = 'block';
                    document.getElementById('errorPeopleLimit').style.display = 'none';
                    document.getElementById('lblErrorMessage').style.display = 'none';
                }
                else {
                    document.getElementById('lblErrorMessage').innerHTML = response;
                    document.getElementById('lblErrorMessage').style.display = 'block';
                    document.getElementById('errorDateSelection').style.display = 'none';
                    if(document.getElementById('errorPeopleLimit')!=null)
                    {
                        document.getElementById('errorPeopleLimit').style.display = 'none';
                    }
                }
            },
            error: function(xhr, status, error) {
                handlePnBActivitiesError(xhr, status, error);
            }
        });
}

function handlePnBActivitiesError(xhr, status, error)
{
    var errMsg = getErrorMessage(xhr,status,error);
    if(document.getElementById('lblErrorMessage') != null)
    {
        document.getElementById('lblErrorMessage').innerHTML = errMsg;
        document.getElementById('lblErrorMessage').style.display = 'block';
    }
}

/* Same method available in Book a dining.js at global/assets */
/* 
$(document).ready(function() {
	// modal window 
	//setting up the modal window attributes
	$('.iFrameModalBookActivity').dialog({
		bgiframe: true,
		autoOpen: false,
		width: 960,
		height: 500,
		modal: true,
		resizable: false,
		closeOnEscape: true,
		position: 'top'
	});
	
	$('.linkPrimary').click(function() {	
		// grabs url from href and loads it into the modal window
		var urlContent = $(this).attr('href');
		$('#iframeloadBookActivity')
		    .attr('src',urlContent)
		    .attr('width','100%')
		    .attr('height','100%')
		    .attr('scrolling','auto');
		$('.iFrameModalBookActivity').dialog('open');
		return false;
	});
});*/