﻿// global variables
var g_strRegion=null;

// get object
function obj(id){
    var o = document.getElementById("ctl00_Content_" + id);
    if(!o) o = document.getElementById("ctl00_Content_ctl01_" + id);
    return o;
}

// get elements
function elm(nm){
    return document.getElementsByName("ctl00$Content$" + nm);
}

// page initialization
function init(strStepName){
    switch(strStepName){
        case "SubmissionType":
            for(var i = 1; i <= 7; i++) obj("rdoSubmissionType" + i).onclick = function(){ hideVE("SubmissionTypeBehavior"); showContentSample(); };
            showContentSample();
            break;
        case "SubmissionMode":
            obj("rdoNewSubmission").onclick=function(){ obj("UpdateExistingOptions").style.visibility = "hidden"; hideVE("SubmissionModeBehavior"); hideVE("UpdateExistingBehavior"); };
            obj("rdoUpdateExisting").onclick=function(){ obj("UpdateExistingOptions").style.visibility = "visible"; hideVE("SubmissionModeBehavior"); setFocus("txtWebsiteUrl"); };
            if(obj("rdoUpdateExisting").checked) setFocus("txtWebsiteUrl");
            
            obj("txtListingName").onchange = obj("txtWebsiteUrl").onchange = function(){
                if(obj("txtWebsiteUrl").value.length + obj("txtListingName").value.length < 2){
                    obj("txtWebsiteUrl").className = "ValidationError";
                    obj("txtListingName").className = "ValidationError";
                    showVE("UpdateExistingBehavior"); 
                }else{
                    obj("txtWebsiteUrl").className = "FormField";
                    obj("txtListingName").className = "FormField";
                    hideVE("UpdateExistingBehavior");
                }
            };
            break;
        case "Details":
            if(obj("rdoNewsRelease")){if(obj("rdoNewsRelease").checked)ValidatorEnable(obj("rfvPressCategory"), false);else ValidatorEnable(obj("rfvNewsCategory"), false);}
            if(obj("rdoNewsRelease"))obj("rdoNewsRelease").onclick = function(){obj("NewsCategories").style.display="";obj("PressCategories").style.display="none";};
            if(obj("rdoPressRelease"))obj("rdoPressRelease").onclick = function(){obj("NewsCategories").style.display="none";obj("PressCategories").style.display="";};
            if(obj("pnlOptionalPhone")){
                obj("txtPhoneAreaCode").onchange=obj("txtPhonePrefix").onchange=obj("txtPhoneSuffix").onchange=function(){ hideVE("OptionalPhoneBehavior"); };
            }
            if(obj("chkPhotoRights")){
                obj("chkPhotoRights").onclick = function(){ document.getElementById("PhotoRightsError").style.display = "none"; };
            }
            if(obj("chkCostFree")){
                obj("chkCostFree").onclick = function(){ if(obj("chkCostFree").checked)obj("txtCost").value = "0"; else obj("txtCost").value = ""; };
                obj("txtCost").onkeypress = obj("txtCost").onchange = function(){ obj("chkCostFree").checked = (obj("txtCost").value == "0"); };
            }

            if(obj("txtZip1") && typeof(ZipCode) != "undefined"){
                obj("txtZip1").onchange = function(){ ZipCode.GetRegion(obj("txtZip1").value, regionSuccess, regionFailed); }
            }else if(obj("txtZip2") && typeof(ZipCode) != "undefined"){
                obj("txtZip2").onchange = function(){ ZipCode.GetRegion(obj("txtZip2").value, regionSuccess, regionFailed); }
            }
            break;
        case "Submit":
            var strErrorMessage = obj("SubmissionErrorMessage").value;
            if(strErrorMessage.length > 0) alert(strErrorMessage);
            break;
    }
}

//ajax region check successfull
function regionSuccess(v){
    if(v=="INVALID"){
        document.getElementById("Region").className = "TextSmall";
        document.getElementById("Region").innerHTML = "&nbsp;";
    }else{
        document.getElementById("Region").className = "TextSmall";
        document.getElementById("Region").innerHTML = "&nbsp;&nbsp;" + v;
    }
    g_strRegion=v;
}

//ajax region check failed
function regionFailed(){
    document.getElementById("Region").className = "TextSmall";
    document.getElementById("Region").innerHTML = "&nbsp;";
    g_strRegion="";
} 

// show contenet sample image
function showContentSample(){
    var bChecked = false;
    for(var i = 1; i <= 7; i++){
        var b = obj("rdoSubmissionType" + i).checked;
        obj("img"+i).style.display = b ? "block" : "none";
        if(b)bChecked = true;
    }
    obj("img0").style.display = bChecked ? "none" : "block";
}

// valid the categorization
function validateCategorize(){
    var bCategoriesExist=false,bCategoriesSelected=false,bThemesExist=false,bThemesSelected=false,e=document.aspnetForm.elements;
    for(var i=0;i<e.length;i++){
        if(e[i].name.indexOf("chkCategory")>0){
            bCategoriesExist=true;
            if(e[i].checked) bCategoriesSelected=true;
        }
        bThemesSelected=true;
    }
    if(obj("CategoryRequired") && bCategoriesExist && !bCategoriesSelected){
        alert("Please select at least one category.");
        return false;
    }
    return true;
}

// validate file upload
function validateUpload(b) {
    var slFileUpload = $find("ctl00_Content_slFileUpload");
    if (slFileUpload) {
        var s = slFileUpload.get_element().Content.FileUploadObject.Validate(b);
        if (s == "UPLOADING") {
            alert("File transfer in progress!  Please wait until the transfer has completed.");
            return false;
        } else if (s == "PENDING") {
            alert("At least one file has not been uploaded.  Please complete the upload process or remove the file(s).");
            return false;
        } else if (s == "CANCELED") {
            alert("At least one file did not finish uploading.  Please complete the upload process or remove the file(s).");
            return false;
        } else if (s == "INCOMPLETE") {
            if (obj("FileUpload_IncludeCredit") == "Y") {
                alert("The Caption or Credit is missing from one of the uploaded files.");
            } else {
                alert("The Caption is missing from one of the uploaded files.");
            }
            return false;
        } else {
            obj("hiddenPendingFiles").value = s;
        }
    }
    return true;
}

// validate content details
function validateDetails() {
    if (!validateUpload(true)) return false;
    
    if(obj("pnlOptionalPhone")){
        var l = obj("txtPhoneAreaCode").value.length + obj("txtPhonePrefix").value.length + obj("txtPhoneSuffix").value.length;
        if(l != 0 && l != 10){
            showVE("OptionalPhoneBehavior");
            return false;
        }
    }
    if(obj("pnlAddMediaRequired") && obj("hiddenPendingFiles").value == ""){
        alert("Please upload at least one photo or video.");
        return false;
    }
    if(obj("chkPhotoRights")){
        if(obj("hiddenPendingFiles").value.length > 0 && obj("chkPhotoRights").checked == false){
            document.getElementById("PhotoRightsError").style.display = "block";
            return false;
        }
    }
    
    if(obj("rdoNewsRelease")){
        if(obj("rdoNewsRelease").checked){
            ValidatorEnable(obj("rfvNewsCategory"), true);
            ValidatorEnable(obj("rfvPressCategory"), false);
        }else{
            ValidatorEnable(obj("rfvPressCategory"), true);
            ValidatorEnable(obj("rfvNewsCategory"), false);
        }
    }
    
    return true;
}

// validate Submission Mode fields
function validateSubmissionMode(){
    if(!(obj("rdoNewSubmission").checked || obj("rdoUpdateExisting").checked)){
        showVE("SubmissionModeBehavior");
        return false;
    } else if(obj("rdoUpdateExisting").checked && obj("txtWebsiteUrl").value.length + obj("txtListingName").value.length < 2) {
        showVE("UpdateExistingBehavior");
        obj("txtWebsiteUrl").className = "ValidationError";
        obj("txtListingName").className = "ValidationError";
        return false;
    }
    return true;
}

// validate Submission Type fields
function validateSubmissionType(){
    var bChecked = false, ct = elm("SubmissionType");
    for(var i = 0; i < ct.length; i++) if(ct[i].checked) bChecked = true;
    if(!bChecked){
        showVE("SubmissionTypeBehavior");
        return false;
    }
    return true;
}

/* ------------ Content Details Script ------------ */

function AlterPanel(thePanel, theAction)
{

    var panel = document.all ? document.all[thePanel] : document.getElementById(thePanel);
    if(theAction == 'show')
    {
        panel.style.display = 'block';
    }
    else
    {
        panel.style.display = 'none';
    }
}

function SetVisibility(thePanel)
{
    var panel = document.all ? document.all[thePanel] : document.getElementById(thePanel);
    if(panel.style.visibility == 'hidden')
    {
        panel.style.visibility = 'visible';
    }
    else
    {
        panel.style.visibility = 'hidden';
    }
}
function ValidateChecked(oSrc, args)
{
    var chk = obj("chkAcceptTerms");
    if(chk.checked == false)
    {
        args.IsValid = false;
    }
    else
    {
        args.IsValid = true;
    }
}
function startLytebox(href, title, rel) {
    var ctrl;
    var processedFiles;
    var origHRef = href;
    var el   = obj("lnkManageMedia");

    // Obtain the reference to the list of file already uploaded to the server and append the data to the URL
    ctrl = obj("hiddenPendingFiles");
    if (ctrl != null)
        processedFiles = ctrl.value;
    if (processedFiles != null && processedFiles.length > 0)
        el.href += escape(processedFiles);
    // If there is a title, continue to use it
    if (title != null && title.length > 0)
        el.title = title;
    // If there is a rel, continue to use it
    if (rel != null && rel.length > 0)
        el.rel = rel;
    else
        el.rel   = "lyteframe";
    // Show the Lytebox
    myLytebox.start(el, false, true);
    // Warning: The rel attribute prevents the javaScript onclick from working.  Clear it out.
    el.rel  = "";
    // Warning: The href option is cumlitive.  Restore the original href since we never know if the user will open the dialog multiple times
    el.href = origHRef;
}