
$(document).ready(function() {
    var pathname = window.location.pathname;
	GetStatesByCountryID('Yes');
	if (pathname.startsWith("/p-")) {
        $.getJSON('CustomerDataHandler.ashx', { dataType: 'json' }, function(customerData) {
            $.each(customerData, function() {
				$("#txtPhysicalBroucherFirstName").attr("value", this['FirstName'].toString());
                $("#txtDigitalBroucherFirstName").attr("value", this['FirstName'].toString());
                $("#txtPhysicalBroucherLastName").attr("value", this['LastName'].toString());
                $("#txtDigitalBroucherLastName").attr("value", this['LastName'].toString());
                $("#txtPhysicalBroucherPhoneNo").attr("value", this['Phone'].toString());
                $("#txtDigitalBroucherPhoneNo").attr("value", this['Phone'].toString());
                $("#txtPhysicalBroucherEmailAddress").attr("value", this['Email'].toString());
                $("#txtDigitalBroucherEmailAddress").attr("value", this['Email'].toString());
                $("#txtPhysicalBroucherAddress1").attr("value", this['Address1'].toString());
                $("#txtPhysicalBroucherAddress2").attr("value", this['Address2'].toString());
                $("#txtPhysicalBroucherCity").attr("value", this['City'].toString());
                $("#txtPhysicalBroucherZip").attr("value", this['Zip'].toString());


                $("#ddPhysicalBroucherCountry").attr("value", this['Country'].toString());
				$("#ddPhysicalBroucherState").val(this['State'].toString());				

            });
        });


        // $('#facebox #ddPhysicalBroucherCountry >option').remove();
        $.getJSON('CountryDataHandler.ashx', { dataType: 'json' }, function(customerData) {
            $.each(customerData, function() {


                //                if (this['Name'].toString() == 'United States') {
                //                    $('#ddPhysicalBroucherCountry').append(
                //               $('<option selected ></option>').val(this['Name'].toString()).html(this['Name'].toString()));
                //                }
                //                else {
                //                    $('#ddPhysicalBroucherCountry').append(
                //               $('<option></option>').val(this['Name'].toString()).html(this['Name'].toString()));
                //                }



                if (this['Name'].toString() == 'United States') {
                    $('select[name="ddPhysicalBroucherCountry"]').append(
                        $('<option selected ></option>').val(this['Name'].toString()).html(this['Name'].toString()));
                }
                else {
                    $('select[name="ddPhysicalBroucherCountry"]').append(
                        $('<option></option>').val(this['Name'].toString()).html(this['Name'].toString()));
                }

            });
        });
        //GetStatesByCountryID('Yes');
    }


});

String.prototype.startsWith = function(str) {
    return (this.indexOf(str) === 0);
}


function GetStatesByCountryID(calledfrompageload) {
    var _selected = $("#facebox").find("#ddPhysicalBroucherCountry").val();
    $("#facebox").find("#ddPhysicalBroucherState > option").remove();
	$.ajax({
	  url: 'StatesDataHandler.ashx?CountryName=' + _selected,  
	  dataType: 'json',  
	  data: '',  
	  async: false,  
	  success: function(customerData){  
			$.each(customerData, function() {
			var selectedStateFound = false;
			if(this['selS'] != null)
			{
				if(this['selS'].toString() != '')
				{
					if (this['Abbreviation'].toString() == this['selS'].toString()) 
					{
					selectedStateFound = true;
					}
				}
			}
			if(selectedStateFound)
			{
				$('select[name="ddPhysicalBroucherState"]').append(
				$('<option selected></option>').val(this['Abbreviation'].toString()).html(this['Name'].toString()));
			}
			else
			{
				 $('select[name="ddPhysicalBroucherState"]').append(
				  $('<option></option>').val(this['Abbreviation'].toString()).html(this['Name'].toString()));
			}
			});			
		}
	});
    /*$.getJSON('StatesDataHandler.ashx?CountryName=' + _selected,{ dataType: 'json' }, function(customerData) {
        $.each(customerData, function() {
//            if (calledfrompageload == 'Yes') {
//                $('#ddPhysicalBroucherState').append(
//               $('<option></option>').val(this['Name'].toString()).html(this['Name'].toString()));
//            }
//            else {
//                $('#facebox #ddPhysicalBroucherState').append(
//               $('<option></option>').val(this['Name'].toString()).html(this['Name'].toString()));
//            }
            
             $('select[name="ddPhysicalBroucherState"]').append(
              $('<option></option>').val(this['Abbreviation'].toString()).html(this['Name'].toString()));

        });
    });*/

   

}


function DigitalBrocherSignUp() {
    var FirstName, LastName, PhoneNumber, EmailAddr, CaptchaText;
    if (getDigitalBroucherEnquiryType() == '') {
        alert("Please select Type Of Inquiry.");
        return false;
    }
    FirstName = $('#facebox input[name="txtDigitalBroucherFirstName"]').val();
    LastName = $('#facebox input[name="txtDigitalBroucherLastName"]').val();
    PhoneNumber = $('#facebox input[name="txtDigitalBroucherPhoneNo"]').val();
    EmailAddr = $('#facebox input[name="txtDigitalBroucherEmailAddress"]').val();
    CaptchaText = $('#facebox input[name="txtDigitalBroucherCaptaText"]').val();

    if (FirstName == "undefined" || FirstName.replace(/\s+/g, '') == '') {
        alert("Please Enter First Name.");
        return false;
    }
    if (LastName == "undefined" || LastName.replace(/\s+/g, '') == '') {
        alert("Please Enter Last Name.");
        return false;
    }
    if (PhoneNumber == "undefined" || PhoneNumber.replace(/\s+/g, '') == '') {
        alert("Please Enter Phone Number.");
        return false;
    }
    if (EmailAddr == "undefined" || EmailAddr.replace(/\s+/g, '') == '') {
        alert("Please Enter Email Address.");
        return false;
    }
    else {

        var regEmail = new RegExp("\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
        if (EmailAddr.match(regEmail) == null) {
            alert("Please Enter valid Email Address.");

            return false;
        }
        else if (EmailAddr.charAt(EmailAddr.length - 1) == ".") {
            alert("Please Enter valid Email Address.");

            return false;
        }
    }

    if (CaptchaText == "undefined" || CaptchaText.replace(/\s+/g, '') == '') {
        alert("Please Enter Captcha Text.");
        return false;
    }

    $.ajax({ type: "POST",
        url: "FormHandler.ashx?FirstName=" + FirstName + "&LastName=" + LastName + "&Email=" + EmailAddr + "&Phone=" + PhoneNumber + "&Capta=" + CaptchaText + "&EnquiryType=" + getDigitalBroucherEnquiryType(),
        cache: false,
        timeout: 15000,
        error: function(xhr, ajaxOptions, thrownError) { alert(thrownError + " Error is occuring while submitting information"); },
        success: function(response) {
            if (response != "OK") {
                $("#facebox #lblError").html(response);
            }
            else {

                window.location.href = '/t-digitalbrochure-thankyou.aspx';
            }
        }
    });


    return false;

}

function getPhysicalBroucherEnquiryType() {
    var EnquriyType = '';

    //    if ($("#facebox #rdbPhysicalBroucherForPersonalHomeUse").attr('checked') == true || $("#facebox #rdbPhysicalBroucherForPersonalHomeUse").attr('checked') == 1 || $("#facebox #rdbPhysicalBroucherForPersonalHomeUse").attr('checked') == "true")
    //    {
    //        EnquriyType = $("#facebox #rdbPhysicalBroucherForPersonalHomeUse").val();

    //    }
    //    if ($("#facebox #rdbPhysicalBroucherForCommercialFitnessBusiness").attr('checked') == true) {
    //        EnquriyType = $("#facebox #rdbPhysicalBroucherForCommercialFitnessBusiness").val();

    //    }

    //    if ($("#facebox #rdbPhysicalBroucherForPhysacalTherapyorOtherMedical").attr('checked') == true) {
    //        EnquriyType = $("#facebox #rdbPhysicalBroucherForPhysacalTherapyorOtherMedical").val();

    //    }

    if ($('input[name="inquiry"]:checked').length > 0) {
        EnquriyType = ($('input[name="inquiry"]:checked').val());
    }



    return EnquriyType;

}
function getDigitalBroucherEnquiryType() {
    var EnquriyType = '';


    //    if ($("#facebox #rdbDigitalBroucherForPersonalHomeUse").attr('checked') == true) {
    //        EnquriyType = $("#facebox #rdbDigitalBroucherForPersonalHomeUse").val();

    //    }
    //    if ($("#facebox #rdbDigitalBroucherForCommercialFitnessBusiness").attr('checked') == true) {
    //        EnquriyType = $("#facebox #rdbDigitalBroucherForCommercialFitnessBusiness").val();

    //    }

    //    if ($("#facebox #rdbDigitalBroucherForPhysacalTherapyorOtherMedical").attr('checked') == true) {
    //        EnquriyType = $("#facebox #rdbDigitalBroucherForPhysacalTherapyorOtherMedical").val();

    //    }

    if ($('input[name="inquiry"]:checked').length > 0) {
        EnquriyType = ($('input[name="inquiry"]:checked').val());
    }
    return EnquriyType;

}

function PhysicalBrocherSignUp() {
    var FirstName, LastName, PhoneNumber, EmailAddr, Country, Address1, Address2, City, State, Zip, CaptchaText;

    if (getPhysicalBroucherEnquiryType() == '') {
        alert("Please select Type Of Inquiry.");
        return false;
    }
    FirstName = $('#facebox input[name="txtPhysicalBroucherFirstName"]').val();
    LastName = $('#facebox input[name="txtPhysicalBroucherLastName"]').val();
    PhoneNumber = $('#facebox input[name="txtPhysicalBroucherPhoneNo"]').val();
    EmailAddr = $('#facebox input[name="txtPhysicalBroucherEmailAddress"]').val();
    //Country = $('#facebox input[name="txtPhysicalBroucherCountry"]').val();
    Country = $("#facebox #ddPhysicalBroucherCountry").val();
    Address1 = $('#facebox input[name="txtPhysicalBroucherAddress1"]').val();
    Address2 = $('#facebox input[name="txtPhysicalBroucherAddress2"]').val();
    City = $('#facebox input[name="txtPhysicalBroucherCity"]').val();
    // State = $('#facebox input[name="txtPhysicalBroucherState"]').val();
    State = $("#facebox #ddPhysicalBroucherState").val();
    Zip = $('#facebox input[name="txtPhysicalBroucherZip"]').val();
    CaptchaText = $('#facebox input[name="txtPhysicalBroucherCaptaText"]').val();

    if (FirstName == "undefined" || FirstName.replace(/\s+/g, '') == '') {
        alert("Please Enter First Name.");
        return false;
    }
    if (LastName == "undefined" || LastName.replace(/\s+/g, '') == '') {
        alert("Please Enter Last Name.");
        return false;
    }
    if (PhoneNumber == "undefined" || PhoneNumber.replace(/\s+/g, '') == '') {
        alert("Please Enter Phone Number.");
        return false;
    }
    if (EmailAddr == "undefined" || EmailAddr.replace(/\s+/g, '') == '') {
        alert("Please Enter Email Address.");
        return false;
    }
    else {

        var regEmail = new RegExp("\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
        if (EmailAddr.match(regEmail) == null) {
            alert("Please Enter valid Email Address.");

            return false;
        }
        else if (EmailAddr.charAt(EmailAddr.length - 1) == ".") {
            alert("Please Enter valid Email Address.");

            return false;
        }
    }


   
    if (Address1 == "undefined" || Address1.replace(/\s+/g, '') == '') {
        alert("Please Enter Address.");
        return false;
    }
    if (City == "undefined" || City.replace(/\s+/g, '') == '') {
        alert("Please Enter City.");
        return false;
    }
   
    if (Zip == "undefined" || Zip.replace(/\s+/g, '') == '') {
        alert("Please Enter Zip code.");
        return false;
    }
    if (CaptchaText == "undefined" || CaptchaText.replace(/\s+/g, '') == '') {
        alert("Please Enter Captcha Text.");
        return false;
    }
  
    if (Country == "undefined" || Country.replace(/\s+/g, '') == '') {
        alert("Please Enter Country.");
        return false;
    }
    if (State == "undefined" || State.replace(/\s+/g, '') == '') {
        alert("Please Enter State.");
        return false;
    }
   
    $.ajax({ type: "POST",
        url: "PhysicalBrocherFormHandler.ashx?FirstName=" + FirstName + "&LastName=" + LastName + "&Email=" + EmailAddr + "&Phone=" + PhoneNumber + "&Address1=" + Address1 + "&Address2=" + Address2 + "&Country=" + Country + "&City=" + City + "&State=" + State + "&ZipCode=" + Zip + "&Capta=" + CaptchaText + "&EnquiryType=" + getPhysicalBroucherEnquiryType(),
        cache: false,
        timeout: 15000,
        error: function(xhr, ajaxOptions, thrownError) { alert(thrownError + " Error is occuring while submitting information"); },
        success: function(response) {
            if (response != "OK") {
                $("#facebox #lblPhysicalBroucherError").html(response);
                
            }
            else {
                window.location.href = '/t-thankyou.aspx';
            }
        }
    });


    return false;

}


