﻿/* Logic to show the loading panel while the content is loaded in popupcontrol */
var showPopup = true;
var iFrame;
function Popup_OnInit(s) {
    iFrame = s.GetContentIFrame();
    ASPxClientUtils.AttachEventToElement(iFrame, 'load', OnContentLoaded);
}

function OnContentLoaded(e) {
    showPopup = false;
    PopupLoadingPanel.Hide();
}

function OnShown(s, e) {
    if (showPopup)
        PopupLoadingPanel.ShowInElement(iFrame);
}

/* End of loading panel logic */

/* Common functions */

function SetPopupSize(sender, height, width) {
    var popupWidth, popupHeight;
    var heightPercent;
    var widthPercent;

    heightPercent = (height / screen.height) * 100;
    widthPercent = (width / screen.width) * 100;

    popupWidth = screen.width * (widthPercent / 100);
    popupHeight = screen.height * (heightPercent / 100);

    sender.SetSize(popupWidth, popupHeight);
    sender.UpdatePosition();

}

function UpdateIndustryName(Name) {
    hdnIndustryName.Set("IndustryName", Name);
}

/* End of Common functions */


function ShowPopupWindow(id) {
    if (id == "48") {
        ShowGetStartedPopupWindow();
    }
    else if (id == "53") {
        ShowFeedbackPopupWindow();
    }
}

/* Job-Seekers GetStarted window */
function ShowGetStartedPopupWindow() {
    var height = 500;
    var width = 800;
    var IndustryName = "None";
    GetStartedPopupControl.SetContentUrl("");
    GetStartedPopupControl.RefreshWindowContentUrl();
//    if (hdnIndustryName.Get("IndustryName") != null) {
//        IndustryName = hdnIndustryName.Get("IndustryName");
//        if (IndustryName == "Aero") {
//            height = 300;
//            width = 800;
//        }
//        else if (IndustryName == "None") {
//            height = 250;
//            width = 800;
//        }
//        else {
//            height = 450;
//            width = 800;
//        }
//    }
//    else {
//        height = 250;
//        width = 800;
//    }
    SetPopupSize(window.parent.GetStartedPopupControl, height, width);
    showPopup = true;
    GetStartedPopupControl.SetContentUrl("JobSeekersGetStarted.aspx?Name=" + IndustryName);
    GetStartedPopupControl.Show();
    //setInterval(GetStartedPopupControl.Show(), 1000);
}


function ddlIndustry_OnClientSelectedIndexChanged() {
    var industryId = ddlIndustry.GetValue();
    if (industryId == 4) {
        OtherIndustryPanel.SetVisible(true);
    }
    else {
        OtherIndustryPanel.SetVisible(false);    
    }
    
//    var height = 300;
//    var width = 800;
//    var industryId = ddlIndustry.GetValue();

//    UploadPanel.SetVisible(false);
//    AeroPanel.SetVisible(false);
//    lblAeroContent.SetVisible(false);
//    lblOtherContent.SetVisible(false);

//    if (industryId == 1) {
//        height = 300;
//        width = 800;
//        AeroPanel.SetVisible(true);
//        lblAeroContent.SetVisible(true);
//    }
//    else if (industryId >= 2) {
//        height = 470;
//        width = 800;
//        txtName.SetErrorText("");
//        txtName.SetText("");
//        txtEmail.SetText("");
//        txtEmail.SetErrorText("");
//        ResumeUploadControl.ClearText();
//        ResumeTextMemoControl.SetText("");
//        NotValidFileMessageLabel.SetVisible(false);
//        EmptyUploadMessageLabel.SetVisible(false);
//        UploadPanel.SetVisible(true);
//        if (industryId == 4) {
//            lblOtherContent.SetVisible(true);
//        }

//    }
//    else {
//        height = 250;
//        width = 800;
//    }
//    SetPopupSize(window.parent.GetStartedPopupControl, height, width);
  
}

function ResumeUploadControl_OnUploadStart() {
    btnUpload.SetText("Uploading...");
    btnUpload.SetEnabled(false);
}
function ResumeUploadControl_OnUploadComplete(args) {
    LoadingPanel.Hide();
    if (args.isValid != null) {
        if (args.isValid) {
            if (args.callbackData == "Success") {
                SuccessPopup.Show();
            }
            else {
                ErrorPopup.Show();
            }
        }
        else {
            btnUpload.SetText("Upload");
            btnUpload.SetEnabled(true);
        }
    }

    if (args.result != null) {
        if (args.result == "Success") {
            SuccessPopup.Show();
        }
        else {
            ErrorPopup.Show();
        }
    }
}
// Function that decides whether it has call File Upload server method or do Call Back method call to send text version of the Resume
function Upload_ClientClick() {
    EmptyUploadMessageLabel.SetVisible(false);
    lblIndustryMessage.SetVisible(false);
    if (ddlIndustry.GetValue() == "0") {
        lblIndustryMessage.SetVisible(true);
    }
    else {
        if (ResumeUploadControl.GetText() != "") {
            if (ASPxClientEdit.ValidateGroup('FileUploadValidationGroup')) {
                LoadingPanel.Show();
                //ResumeUploadControl.UploadFile();
                ResumeDBCallBack.PerformCallback('Update');
            }
        }
        else if (ResumeTextMemoControl.GetText() != "") {
            if (ASPxClientEdit.ValidateGroup('FileUploadValidationGroup')) {
                LoadingPanel.Show();
                ResumeCallBack.PerformCallback();
            }
        }
        else {
            EmptyUploadMessageLabel.SetVisible(true);
        }
    }
}

var allowExtensions = ['txt', 'doc', 'docx', 'rtf', 'pdf'];

function OnTextChanged(uploadControl) {
    EmptyUploadMessageLabel.SetVisible(false);
    if (uploadControl.GetText() != "") {
        if (!isValidFileName(uploadControl.GetText())) {
            NotValidFileMessageLabel.SetVisible(true);
            btnUpload.SetEnabled(false);
        }
        else {
            NotValidFileMessageLabel.SetVisible(false);
            btnUpload.SetEnabled(true);
        }
    }
    else {
        NotValidFileMessageLabel.SetVisible(false);
        btnUpload.SetEnabled(true);
    }
}
function ResumeTextMemoControl_ClientTextChanged(s) {
    if (s.GetText() != "") {
        EmptyUploadMessageLabel.SetVisible(false);
    }
}

function isValidFileName(fileName) {
    var fileParts = fileName.split(".");
    var fileExtension = fileParts[fileParts.length - 1];
    return arrayIndexOf(allowExtensions, fileExtension.toLowerCase()) != -1;
}

function arrayIndexOf(array, element) {
    for (var i = 0; i < array.length; i++) {
        if (array[i] == element)
            return i;
    }
    return -1;
}

function CancelButton_OnClick() {
    MainPanel.SetVisible(false);
    window.parent.GetStartedPopupControl.Hide();
}
function OkButton_OnClick() {
    if (SuccessPopup.IsVisible()) {
        SuccessPopup.Hide();
    }
    MainPanel.SetVisible(false);
    window.parent.GetStartedPopupControl.Hide();
}

function GetStartedOkButton_OnClick(s) {
    LoadingPanel.SetText("Thank you.");
    LoadingPanel.Show();
    ResumeDBCallBack.PerformCallback('Submit');
}

function GetStartedErrorOkButton_OnClick() {
    if (ErrorPopup.IsVisible()) {
        ErrorPopup.Hide();
    }
    window.parent.GetStartedPopupControl.Hide();
}

function ResumeDBCallBack_OnCallbackComplete(args) {

    if (args.result == "UpdateSuccess") {
        ResumeUploadControl.UploadFile();
        //LoadingPanel.Hide();
    }
    else if (args.result == "SubmitSuccess") {
        LoadingPanel.Hide();
        if (SuccessPopup.IsVisible()) {
            SuccessPopup.Hide();
        }
        window.parent.GetStartedPopupControl.Hide();
    }
}
/* End of GetStarted.aspx */


/* Feeback.aspx */

function ShowFeedbackPopupWindow() {
    var popupHeight = 390;
    var popupWidth = 570;

    FeedbackPopupControl.RefreshWindowContentUrl();
    SetPopupSize(FeedbackPopupControl, popupHeight, popupWidth);
    showPopup = true;
    FeedbackPopupControl.SetContentUrl("FeedbackForm.aspx?Location=" + window.parent.location);
    FeedbackPopupControl.Show();
}


function btnSubmit_ClientClick() {
    if (ASPxClientEdit.ValidateGroup('FeedbackValidationGroup')) {
        SuggestionsCallBack.PerformCallback();
    }
}
function SuggestionsCallBack_OnComplete(args) {
    LoadingPanel.Hide();
    if (args.result == "Success") {
        txtFirstName.SetText("");
        txtLastName.SetText("");
        txtEmail.SetText("");
        txtPhone.SetText("");
        txtSuggestions.SetText("");
        SuccessPopup.Show();
    }
    else {

        ErrorPopup.Show();
    }
}

function SuggestionsCallBack_OnStart() {
    LoadingPanel.Show();

}
function SuggestionsCallBack_OnError() {
    LoadingPanel.Hide();
    ErrorPopup.Show();
}
function btnFeedbackCancel_ClientClick() {
    
    UploadContentPanel.SetVisible(false);
    window.parent.FeedbackPopupControl.Hide();

}
function btnClose_ClientClick(sender) {
    sender.RefreshWindowContentUrl();	
    sender.Hide();
}


function FeedBackOkButton_OnClick(s) {
    if (s.IsVisible()) {
        s.Hide();
    }
    window.parent.FeedbackPopupControl.Hide();
}

function setMaxLength(textAreaElement, length) {
    textAreaElement.maxlength = length;
    ASPxClientUtils.AttachEventToElement(textAreaElement, "keyup", createEventHandler("onKeyUpOrChange"));
    ASPxClientUtils.AttachEventToElement(textAreaElement, "change", createEventHandler("onKeyUpOrChange"));
}
function onKeyUpOrChange(evt) {
    processTextAreaText(ASPxClientUtils.GetEventSource(evt));
}
function processTextAreaText(textAreaElement) {
    var maxLength = textAreaElement.maxlength;
    var text = textAreaElement.value;
    var isAcceptable = (maxLength == 0) || (text.length <= maxLength);
    if (maxLength != 0 && text.length > maxLength)
        textAreaElement.value = text.substr(0, maxLength);
}
function createEventHandler(funcName) {
    return new Function("event", funcName + "(event);");
}

/* End of Feedback.aspx */


/* Employers GetStarted page */
var positionCount = 0;
function ShowEmployersGetStartedPopupWindow() {
    var height = 510;
    var width = 650;
    
    EmployersGetStartedPopupControl.RefreshWindowContentUrl();
    SetPopupSize(window.parent.EmployersGetStartedPopupControl, height, width);
    showPopup = true;
    EmployersGetStartedPopupControl.Show();
}

function btnUploadAnother_OnClientClick() {
    if (ASPxClientEdit.ValidateGroup('EmployerValidationGroup')) {
        EmployersCallbackPanel.PerformCallback();
    }
}

function btnSubmit_OnClientClick() {
    if (ASPxClientEdit.ValidateGroup('EmployerValidationGroup')) {
        EmployersSubmitCallback.PerformCallback('Update');
        LoadingPanel.Show();
    }
    
}
function EmployersSubmitCallback_OnClientEndCallback(s, e) {
    if (e.result == "UpdateSuccess") {
        LoadingPanel.Hide();
        SuccessPopup.Show();
    }
    else if (e.result == "SubmitSuccess") {
        LoadingPanel.Hide();
        SuccessPopup.Hide();
        window.parent.EmployersGetStartedPopupControl.Hide();
    }
}

function btnClose_OnClick() {
    LoadingPanel.SetText("Thank you.");
    EmployersSubmitCallback.PerformCallback('Submit');
    LoadingPanel.Show();
    
}

function ddlSource_ClientSelectedIndexChanged() {
    if ((ddlSource.GetValue() == "7") || (ddlSource.GetValue() == "17")) {
        txtSource.SetVisible(true);
    }
    else {
        txtSource.SetVisible(false);
    }
}
/* End of Employers GetStarted */

/* Video Page */

function ShowVideoPopupControl() {
    
    var height = 400;
    var width = 750;

    VideoPopupControl.RefreshWindowContentUrl();
    SetPopupSize(VideoPopupControl, height, width);
    showPopup = true;
    VideoPopupControl.Show();
}

function btnVideoResumeUpload_OnClientClick() {
    //UpdateIndustryName("Aero");
    window.parent.hdnIndustryName.Set("IndustryName", "Aero");
    window.parent.VideoPopupControl.Hide();
    window.parent.ShowGetStartedPopupWindow();
    window.parent.hdnIndustryName.Set("IndustryName", "None");
}

/* End of Video Page */


/* unsubscribe page */

function cmbReason_SelectedIndexChanged(s, e) {
    if (cmbReason.GetValue() == "5") {
        OtherPanel.SetVisible(true);
    }
    else {
        OtherPanel.SetVisible(false);
    }
}

function btnSubscribe_ClientClick(s, e) {
    if (ASPxClientEdit.ValidateGroup('UnSubscribeValidation')) 
    {
        UnSubscribeSubmitCallback.PerformCallback();
    }
}


function UnSubscribeSubmitCallback_OnClientEndCallback(s, e) {
    if (e.result == "Success") {
        SuccessPanel.SetVisible(true);
        lblSuccess.SetText("You are successfully unsubscribed from our mailing List.");
        MainContent.SetVisible(false);
    }
    else if (e.result == "SystemFailed") {
        SuccessPanel.SetVisible(true);
        lblSuccess.SetText("We are unable to unsubscribe you. Please try again after few hours.");
        MainContent.SetVisible(false);

    }
    else if (e.result == "UnSubscribed") {
        SuccessPanel.SetVisible(true);
        lblSuccess.SetText("You have been already unsubscribed from our mailing List.");
        MainContent.SetVisible(false);

    }
    else if (e.result == "Failure") {
        SuccessPanel.SetVisible(true);
        lblSuccess.SetText("Wrong URL provided. Please try to unsubscribe by clicking the url that we sent through our email.");
        MainContent.SetVisible(false);

    }
}