﻿//Guest Card
function GetGcBldgId(inputString)
{
    if(inputString!=undefined)
    {
        return inputString.split("|")[0]; 
    }
}

function GetGcUnitId(inputString)
{
    if(inputString!=undefined)
    {
        return inputString.split("|")[1]; 
    }
}

function GetGcFpcId(inputString)
{
    if(inputString!=undefined)
    {
        return inputString.split("|")[2]; 
    }
}

function GetGcUnitPrice(inputString) 
{
    if (inputString!=undefined) {
        return inputString.split("|")[3];
    }
}

var isReadyToSubmitArray = new Array();
var arrayItemNum = 0;

//guest card email validation 
function EmailVal(input) {
    var emailField;
    if(input=="vd")
    {
        emailField=document.getElementById("ctl00_Body_AptDetailControl1_EmailAddress"); 
    }
    
    else if(input=="gc")
    {
        emailField=document.getElementById("ctl00_Body_GuestCard1_EmailAddress");
    }
    var isValid=true;
    if(emailField!=undefined)
    {
        var emailValue=emailField.value
        if(emailValue!=undefined)
            {
                  //isValid=RegularExp(emailValue);
                  if(emailValue==undefined||emailValue==null||emailValue=="")
                  {
                    isValid=false;
                  }
                  else 
                  {
                    isValid=ValidEmail(emailValue);
                  } 
            }
    }

    if(isValid==false)
    {
        if(input=="gc")
        {
        $("#gcError").show();
        $(".gcEmailLbl").css("font-weight", "bold");
        $(".gcEmailLbl").css("color", "red");
        $(".emailAddress").css("border", "1px solid red");
        }
        
        else if(input=="vd")
        {
        $("#unitError").show();
        $(".gcEmailLbl2").css("font-weight", "bold");
        $(".gcEmailLbl2").css("color", "red");
        $(".emailAddress2").css("border", "1px solid red");
        }
        arguments.IsValid=false;
        
        
    }
    else
    {
        if(input=="gc")
        {
        $("#gcError").hide();
        $(".gcEmailLbl").css("font-weight", "normal");
        $(".gcEmailLbl").css("color", "#5b5b5b");
        $(".emailAddress").css("border", "1px solid #94afc7");
        }
        else if(input=="vd")
        {
        $("#unitError").hide();
        $(".gcEmailLbl2").css("font-weight", "normal");
        $(".gcEmailLbl2").css("color", "#5b5b5b");
        $(".emailAddress2").css("border", "1px solid #94afc7");
        }
        arguments.IsValid=true; 
    }
};

//guest card first name validation
function FirstNameVal(input)
{
    var firstNameField;
    if(input=="vd")
    {
        firstNameField=document.getElementById("ctl00_Body_AptDetailControl1_FirstName");
    }
    
    else if(input=="gc")
    {
        firstNameField=document.getElementById("ctl00_Body_GuestCard1_FirstName");
    }

    var isValid=true;
    if(firstNameField!=undefined)
    {
        var firstNameValue=firstNameField.value;
        if(firstNameValue==undefined||firstNameValue==null||firstNameValue=="")
        {
            isValid=false;
        }
    }
   
    if(isValid==false)
    {
        if(input=="gc")
        {
        $("#gcError").show();
        $(".gcFnameLbl").css("font-weight", "bold");
        $(".gcFnameLbl").css("color", "red");
        $(".gcFnameFld").css("border", "1px solid red");
        }
        else if(input=="vd")
        {
        $("#unitError").show();
        $(".gcFnameLbl2").css("font-weight", "bold");
        $(".gcFnameLbl2").css("color", "red");
        $(".gcFnameFld2").css("border", "1px solid red");
        }
        arguments.IsValid=false;
    }
    else
    {
        if(input=="gc")
        {
        $("#gcError").hide();
        $(".gcFnameLbl").css("font-weight", "normal");
        $(".gcFnameLbl").css("color", "#5b5b5b");
        $(".gcFnameFld").css("border", "1px solid #94afc7");
        }
        else if(input=="vd")
        {
        $("#unitError").hide();
        $(".gcFnameLbl2").css("font-weight", "normal");
        $(".gcFnameLbl2").css("color", "#5b5b5b");
        $(".gcFnameFld2").css("border", "1px solid #94afc7");
        }
        
        arguments.IsValid=true;
    }
    isReadyToSubmitArray[arrayItemNum++] = isValid;
};

//guest card last name validation
function LastNameVal(input)
{
    var lastNameField;
    if(input=="vd")
    {
        lastNameField=document.getElementById("ctl00_Body_AptDetailControl1_LastName");
    }
    
    else if(input=="gc")
    {
        lastNameField=document.getElementById("ctl00_Body_GuestCard1_LastName");
    }
    var isValid=true;
    if(lastNameField!=undefined)
    {
        var lastNameValue=lastNameField.value;
        if(lastNameValue==undefined||lastNameValue==null||lastNameValue=="")
        {
            //alert("empty");
            isValid=false;
        }
    }
    if(isValid==false)
    {
        if(input=="gc")
        {
        $("#gcError").show();
        $(".gcLnameLbl").css("color", "red");
        $(".gcLnameLbl").css("font-weight", "bold");
        $(".gcLnameFld").css("border", "1px solid red");
        }
        else if(input=="vd")
        {
        $("#unitError").show();
        $(".gcLnameLbl2").css("color", "red");
        $(".gcLnameLbl2").css("font-weight", "bold");
        $(".gcLnameFld2").css("border", "1px solid red");
        }
        arguments.IsValid=false;
       
    }
    else
    {
        if(input=="gc")
        {
        $("#gcError").hide();
        $(".gcLnameLbl").css("color", "#5b5b5b");
        $(".gcLnameLbl").css("font-weight", "normal");
        $(".gcLnameFld").css("border", "1px solid #94afc7");
        }
        else if(input=="vd")
        {
        $("#unitError").hide();
        $(".gcLnameLbl2").css("color", "#5b5b5b");
        $(".gcLnameLbl2").css("font-weight", "normal");
        $(".gcLnameFld2").css("border", "1px solid #94afc7");
        }
        arguments.IsValid=true;
    }
    isReadyToSubmitArray[arrayItemNum++] = isValid;
};

//guest card date validation 
function DateVal(input) {
    var dateField;
    if(input=="gc")
    {
        dateField=document.getElementById("ctl00_Body_GuestCard1_MoveInDate");
    }
    var isValid=true;
    if(dateField!=undefined)
    {
        var dateValue=dateField.value
        if(dateValue!=undefined)
            {
                if(dateValue==undefined||dateValue==null||dateValue=="") {
                    isValid=false;
                    }
                else {
                    isValid=ValidDate(dateValue);
                    if(isValid) {
                        //A previous Move-in-date is not allowed.
                        var today = new Date();
                        today.setHours(0);
                        today.setMinutes(0);
                        today.setSeconds(0);
                        today.setMilliseconds(0);
                        var dateSplit = dateValue.toString().split('/');
                        var month = parseInt(dateSplit[0]);
                        var day = parseInt(dateSplit[1]);
                        var year = parseInt(dateSplit[2]);
                        month = (month - 1) < 0 ? 1 : (month - 1);
                        var dateObjToCompare = new Date(year, month, day);
                        if(today > dateObjToCompare){
                            isValid = false;
                        }
                    }
                }
            }
    }

    if(isValid==false)
    {
        if(input=="gc")
        {
        $("#gcError").show();
        $(".gcMoveInDate").css("font-weight", "bold");
        $(".gcMoveInDate").css("color", "red");
        $(".move").css("border", "1px solid red");
        }
        arguments.IsValid=false;
    }
    else
    {
        if(input=="gc")
        {
        $("#gcError").hide();
        $(".gcMoveInDate").css("font-weight", "normal");
        $(".gcMoveInDate").css("color", "#5b5b5b");
        $(".move").css("border", "1px solid #94afc7");
        }
        arguments.IsValid=true; 
    }
    isReadyToSubmitArray[arrayItemNum++] = isValid;
};

//guest card phone number validation
// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
};

function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
};

function checkInternationalPhone(strPhone){
var charsStripped=stripCharsInBag(strPhone,validWorldPhoneChars);
return (isInteger(charsStripped) && charsStripped.length >= minDigitsInIPhoneNumber);
};

function PhoneVal(input)
{
    var phoneField;
    if(input=="vd")
    {
        phoneField=document.getElementById("ctl00_Body_AptDetailControl1_Phone");
    }
    
    else if(input=="gc")
    {
        phoneField=document.getElementById("ctl00_Body_GuestCard1_Phone");
    }
    var isValid=true;
    if(phoneField!=undefined)
    {
        var phoneValue=phoneField.value;
        if(phoneValue==undefined||phoneValue==null||phoneValue=="")
            {
                isValid=false;
            }
        isValid=checkInternationalPhone(phoneValue);   
    }

    if(isValid==false)
    {
        if(input=="gc")
        {
        $("#gcError").show();
        $(".gcPhoneLbl").css("color", "red");
        $(".gcPhoneLbl").css("font-weight", "bold");
        $(".gcPhoneFld").css("border", "1px solid red");
        }
        else if(input=="vd")
        {
        $("#unitError").show();
        $(".gcPhoneLbl2").css("color", "red");
        $(".gcPhoneLbl2").css("font-weight", "bold");
        $(".gcPhoneFld2").css("border", "1px solid red");
        }
        arguments.IsValid=false;    
    }
    else
    {
        if(input=="gc")
        {
        $("#gcError").hide();
        $(".gcPhoneLbl").css("color", "#5b5b5b");
        $(".gcPhoneLbl").css("font-weight", "normal");
        $(".gcPhoneFld").css("border", "1px solid #94afc7");
        }
        else if(input=="vd")
        {
        $("#unitError").hide();
        $(".gcPhoneLbl2").css("color", "#5b5b5b");
        $(".gcPhoneLbl2").css("font-weight", "normal");
        $(".gcPhoneFld2").css("border", "1px solid #94afc7");    
        }
        arguments.IsValid=true;
    }
};

function PhoneorEmailVal(input)
{
    var emailField;
    if(input=="vd")
    {
        emailField=document.getElementById("ctl00_Body_AptDetailControl1_EmailAddress"); 
    }
    
    else if(input=="gc")
    {
        emailField=document.getElementById("ctl00_Body_GuestCard1_EmailAddress");
    }
    var isEmailValid=true;
    if(emailField!=undefined)
    {
        var emailValue=emailField.value
        if(emailValue!=undefined) {
            (emailValue==undefined||emailValue==null||emailValue=="") ? isEmailValid=false : isEmailValid=ValidEmail(emailValue);
            }
    }
    
    var phoneField;
    if(input=="vd")
    {
        phoneField=document.getElementById("ctl00_Body_AptDetailControl1_Phone");
    }
    
    else if(input=="gc")
    {
        phoneField=document.getElementById("ctl00_Body_GuestCard1_Phone");
    }
    var isPhoneValid=true;
    if(phoneField!=undefined)
    {
        var phoneValue=phoneField.value;
        if(phoneValue!=undefined) {   
            (phoneValue==undefined||phoneValue==null||phoneValue=="") ? isPhoneValid=false : isPhoneValid=checkInternationalPhone(phoneValue);   
        }
    }
        
    if( (isEmailValid==true) || (isPhoneValid && emailValue=="") )
        {
            if(input=="gc")
                {
                    $("#gcError").hide();
                    $(".gcEmailLbl").css("font-weight", "normal");
                    $(".gcEmailLbl").css("color", "#5b5b5b");
                    $(".emailAddress").css("border", "1px solid #94afc7");
                }
            else if(input=="vd")
                {
                    $("#unitError").hide();
                    $(".gcEmailLbl2").css("font-weight", "normal");
                    $(".gcEmailLbl2").css("color", "#5b5b5b");
                    $(".emailAddress2").css("border", "1px solid #94afc7");
                }
        }
    else
        {
            if(input=="gc")
                {
                    $("#gcError").show();
                    $(".gcEmailLbl").css("font-weight", "bold");
                    $(".gcEmailLbl").css("color", "red");
                    $(".emailAddress").css("border", "1px solid red");
                }
            else if(input=="vd")
                {
                    $("#unitError").show();
                    $(".gcEmailLbl2").css("font-weight", "bold");
                    $(".gcEmailLbl2").css("color", "red");
                    $(".emailAddress2").css("border", "1px solid red");
                }
        }
   
        
    if( (isPhoneValid==true) || (isEmailValid && phoneValue==""))
        {
            if(input=="gc")
                {
                    $("#gcError").hide();
                    $(".gcPhoneLbl").css("color", "#5b5b5b");
                    $(".gcPhoneLbl").css("font-weight", "normal");
                    $(".gcPhoneFld").css("border", "1px solid #94afc7");
                }
            else if(input=="vd")
                {
                    $("#unitError").hide();
                    $(".gcPhoneLbl2").css("color", "#5b5b5b");
                    $(".gcPhoneLbl2").css("font-weight", "normal");
                    $(".gcPhoneFld2").css("border", "1px solid #94afc7");    
                }
        }
    else
        {
            if(input=="gc")
                {
                    $("#gcError").show();
                    $(".gcPhoneLbl").css("color", "red");
                    $(".gcPhoneLbl").css("font-weight", "bold");
                    $(".gcPhoneFld").css("border", "1px solid red");
                }
            else if(input=="vd")
                {
                    $("#unitError").show();
                    $(".gcPhoneLbl2").css("color", "red");
                    $(".gcPhoneLbl2").css("font-weight", "bold");
                    $(".gcPhoneFld2").css("border", "1px solid red");
                }
        }
        
    if(!((isEmailValid && isPhoneValid) || (isEmailValid && phoneValue=="") || (isPhoneValid && emailValue==""))){
        isReadyToSubmitArray[arrayItemNum++] = false;
    }   
};

//Function to retrieve the AJAX Request object for GuestCard asynchronous submission
var GuestCardAJAXRequestObj;
function GuestCardSubmissionStatus(sender, args){
    var prm = Sys.WebForms.PageRequestManager.getInstance();
    if (prm.get_isInAsyncPostBack() & args.get_postBackElement().id.indexOf('imgBtnSubmit') > -1) {
        //working on previous request, cancel this one.
        args.set_cancel(true);
    }
    else if (!prm.get_isInAsyncPostBack() & args.get_postBackElement().id.indexOf('imgBtnSubmit') > -1) {    
        //Get the AJAX Request object.
        GuestCardAJAXRequestObj = args.get_request();
        // Set the request method.
        GuestCardAJAXRequestObj.set_httpVerb("POST");
        // Cancel further processing. This prevents the browser to send
        // the GuestCard in a synchronous way.
        args.set_cancel(true);
        prm.abortPostBack();
    }
}

//Function to retrieve the AJAX Request object for UnitDetails asynchronous submission
var UnitDetailAJAXRequestObj;
function UnitDetailSubmissionStatus(sender, args){
    var prm = Sys.WebForms.PageRequestManager.getInstance();
    if (prm.get_isInAsyncPostBack() & args.get_postBackElement().id.indexOf('imgBtnGcSubmit') > -1) {
        //working on previous request, cancel this one.
        args.set_cancel(true);
    }
    else if (!prm.get_isInAsyncPostBack() & args.get_postBackElement().id.indexOf('imgBtnGcSubmit') > -1) {    
        //Get the AJAX Request object.
        UnitDetailAJAXRequestObj = args.get_request();
        // Set the request method.
        UnitDetailAJAXRequestObj.set_httpVerb("POST");
        // Cancel further processing. This prevents the browser to send
        // the GuestCard in a synchronous way.
        args.set_cancel(true);
        prm.abortPostBack();
    }
}

//Function to help process validation before submitting a GuestCard
function PreSubmitGC(input){
        //Init array and counter
            isReadyToSubmitArray = new Array();
            arrayItemNum = 0;
        //First validate
            LastNameVal('gc');
            FirstNameVal('gc');
            PhoneVal('gc');
            EmailVal('gc');
            PhoneorEmailVal('gc');
            DateVal('gc');

        //code to submit here
        var checkResults = isReadyToSubmitArray.toString();
        if(checkResults.indexOf("false", 0) == -1){
            $("#gcError").hide();
            //Hide the GuestCard Form
            $('#guestCard').jqm().jqmHide();
            //Initiates GuestCard Submission; used to capture AJAX Request object.
            WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions('ctl00$Body$GuestCard1$imgBtnSubmit', '', true, '', '', false, true));

            
            //Register the function to process the end of GuestCard submission.
            GuestCardAJAXRequestObj.add_completed(GuestCardSubmissionEndReq);   
            
            //This submits the GuestCard asynchronously
            GuestCardAJAXRequestObj.invoke();
            
            // Clear out the event target so that when button's submit the form we don't
            // Get the incorect eventtarget
            
            $('#__EVENTTARGET').val('');
            
            //Show the AJAX Progress indicator
            $('#AjaxProgressIndicator').jqm({toTop:true, modal:true}).jqmShow(); 
        }
        else {
            $("#gcError").show();
        }
        return false;
};

//Function to display Thank You message to user after GuestCard submission.
//Left with the name as "submitGC" due to refactoring issues, but not longer submits the GuestCard.
function submitGC(guestCardID) {
    //$('#guestCard').jqm().jqmHide();
    var commName = $('.gcCommName').val();
    var gcEmail = $('.emailAddress').val();
    $('#gcThankYouComm').html(commName);
    $('#gcThankYouEmail').html(gcEmail);
    $('#gcThankYou').jqm({toTop: true}).jqmShow();
    //$('.featuredHd').html("<img src='/copy/images/headings/youMightConsider.gif' alt='You Might Also Consider...' />");
    var propertyID = $('#ctl00_Body_GuestCard1_gcPropId')[0].value;
    var unit = $('#ctl00_Body_GuestCard1_gcUnitId')[0].value;
    oscGuestCardSubmit(propertyID,unit,guestCardID);
    
    //Hide the AJAX Progress indicator
     $('#AjaxProgressIndicator').jqm({toTop:true, modal:true}).jqmHide();
};

//Function to help process validation before submitting a ViewDetails
function PreSubmitVD(input){
        //Init array and counter
            isReadyToSubmitArray = new Array();
            arrayItemNum = 0;
        //First validate
            LastNameVal('vd');
            FirstNameVal('vd');
            PhoneVal('vd');
            EmailVal('vd');
            PhoneorEmailVal('vd');

        //code to submit here
        var checkResults = isReadyToSubmitArray.toString();
        if(checkResults.indexOf("false", 0) == -1){
            $("#unitError").hide();
            //Hide the GuestCard Form            
            $('#unitDetail').jqm().jqmHide();
            
            //Initiates GuestCard Submission; used to capture AJAX Request object.
            WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions('ctl00$Body$AptDetailControl1$imgBtnGcSubmit', '', true, '', '', false, true));

            
            //Register the function to process the end of GuestCard submission.
            UnitDetailAJAXRequestObj.add_completed(UnitDetailSubmissionEndReq);   
            
            //This submits the GuestCard asynchronously
            UnitDetailAJAXRequestObj.invoke();
            
            // Clear out the event target so that when button's submit the form we don't
            // Get the incorect eventtarget
            
            $('#__EVENTTARGET').val('');
            
            //Show the AJAX Progress indicator
            $('#AjaxProgressIndicator').jqm({toTop:true, modal:true}).jqmShow(); 
        }
        else {
            $("#unitError").show();           
        }
       
        return false;
};



//Function to display Thank You message to user after Unit Detials submission.
function submitVD(guestCardID) {
        var commName = $('.vdCommName').val();
        var vdEmail = $('.emailAddress2').val();
        $('#vdThankYouComm').html(commName);
        $('#vdThankYouEmail').html(vdEmail);
        $('#unitDetail').jqm().jqmHide();
	    $('#vdThankYou').jqm({toTop: true}).jqmShow();
        var propertyID = $('#ctl00_Body_AptDetailControl1_vdGcPropId')[0].value;
        var unit = $('#ctl00_Body_AptDetailControl1_vdGcUnitId')[0].value;
        oscGuestCardSubmit(propertyID,unit,guestCardID);
	    
	    //Hide the AJAX Progress indicator
        $('#AjaxProgressIndicator').jqm({toTop:true, modal:true}).jqmHide();
    };

//Funtion to process the end of GuestCard submission.
function GuestCardSubmissionEndReq(sender, args){
    //Removes this function from the list to stop processing other calls.
    GuestCardAJAXRequestObj.remove_completed(GuestCardSubmissionEndReq);
    
    //put post-submission code here and on
    var gcID = GetGuestCardID(sender._xmlHttpRequest.responseText,'gc');

    //Show Thank You Message
    submitGC(gcID);
    //WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions('featuresView', '', true, '', '', false, true));


}

//Funtion to process the end of GuestCard submission.
function UnitDetailSubmissionEndReq(sender, args){
    //Removes this function from the list to stop processing other calls.
    UnitDetailAJAXRequestObj.remove_completed(UnitDetailSubmissionEndReq);
    
    //put post-submission code here and on
    var udID = GetGuestCardID(sender._xmlHttpRequest.responseText,'ud');
    
    //Show Thank You Message
    submitVD(udID);
    //WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions('featuresView', '', true, '', '', false, true));


}

function GetGuestCardID(guestCardText,guestCardType){
    var guestCardID="";
    var gcIDStart;

    switch(guestCardType) {
        case 'gc':
            // guestcard.aspx may be unit or generic
            var gcNamePos = guestCardText.indexOf("ctl00_Body_GuestCard1_gcGuestCardID");
            var startOffset = 44;
            gcIDStart = gcNamePos + startOffset;
            guestCardID = GetTempGuestCardID(guestCardText,gcIDStart);
            if(guestCardID=='parseError') {
                var gcNamePos = guestCardText.indexOf("ctl00_Body_GuestCard1_gcUnitDCardID");
                var startOffset = 44;
                gcIDStart = gcNamePos + startOffset;
                guestCardID = GetTempGuestCardID(guestCardText,gcIDStart);
            }
            break;    
        case 'ud':
            var gcNamePos = guestCardText.indexOf("ctl00_Body_AptDetailControl1_vdUnitCardID");
            var startOffset = 50;
            gcIDStart = gcNamePos + startOffset;
            guestCardID = GetTempGuestCardID(guestCardText,gcIDStart);
            break;
        default:
            return guestCardID
    }    
    return guestCardID
}
function GetTempGuestCardID(guestCardText,gcIDStart) {
    var guestCardID;
    var gcIDEnd; 

    gcIDEnd = guestCardText.indexOf('"',gcIDStart);
    guestCardID = Mid(guestCardText,gcIDStart,gcIDEnd-gcIDStart)
    if(!IsNumeric(guestCardID)) guestCardID = "parseError";
    return guestCardID;
}
