﻿
var PostControl_AddedClientIDs = "";
var PostControl_ClientSearchTypeIDs = "";
var PostControl_AddClientTypeID = 0;

function PostControl_Show() {

//    alert(getRef("PostControl_ClientID_Override").value);
//    alert(getRef("PostControl_LocationID_Override").value);
    
    if (Number(getRef("RealPerson").value) != 1) {
        ReCaptchaControl_Show(PostControl_Show);
        return;
    }
    
    PostControl_Reset();
    getRef("PostControlDiv").style.display = "";
    try { getRef("OverlayDiv").style.display=""; }catch(Overlay_Exc){} 
}

function PostControl_Show_SetClientLocation(iClientID, iLocationID, sClientName, bHasLogo) {
    PostControl_Reset();
    PostControl_HideElements();
    
    getRef("PostControl_ClientID").value = iClientID;
    getRef("PostControl_LocationID").value = iLocationID;
       
    getRef("PostControl_PostTypeID").value = 1; // EVENT POST    
    getRef("PostControl_HeaderTitle").innerHTML = "Post an Awesome Event at " + sClientName;
    if (bHasLogo) { PostControl_WhatTable_Open(); }
    else{ PostControl_LogoRequest_Show(iClientID, sClientName); }
    
    getRef("PostControlDiv").style.display = "";
    try { getRef("OverlayDiv").style.display=""; }catch(Overlay_Exc){} 
}

function PostControl_Show_CopyPost(iSpecialID, bSameLocation, iClientID, iLocationID, sClientName) {

    PostControl_Reset();
    PostControl_HideElements();
    
    getRef("PostControl_HeaderTitle").innerHTML = "Copy Post";

    getRef("PostControl_PostTypeID").value = 1; // EVENT POST    
    getRef("PostControl_CopySpecialID").value = iSpecialID;  // SET COPY
    if (bSameLocation) {
        getRef("PostControl_HeaderTitle").innerHTML = "Copy Post at " + sClientName;
        // need to do more here...
    }else{        
        getRef("PostControl_WhereTitle").style.display = "";
        PostControl_Where_Open();   
    }

    getRef("PostControlDiv").style.display = "";
    try { getRef("OverlayDiv").style.display=""; }catch(Overlay_Exc){} 
}

function PostControl_Close() {
    getRef("PostControlDiv").style.display = "none";
    try { getRef("OverlayDiv").style.display="none"; }catch(Overlay_Exc){} 
}

function PostControl_Reset() {

    PostControl_HideElements();
    
    getRef("PostControl_ClientID").value = 0;
    getRef("PostControl_LocationID").value = 0;
    getRef("PostControl_CopySpecialID").value = 0;
    
    PostControl_AddedClientIDs = "";
    PostControl_ClientSearchTypeIDs = "";
    PostControl_AddClientTypeID = 0;
    
    getRef("PostControl_HeaderTitle").innerHTML = "Post Something Awesome";    
    
    // WHERE
    getRef("PostControl_Venue").value = "";
    getRef("PostControl_Address1").value = "";
    getRef("PostControl_Address2").value = "";
    getRef("PostControl_City").value = "";
    getRef("PostControl_Zip").value = "";
    getRef("PostControl_Phone").value = "";
    getRef(getRef("PostControl_State_ClientID").value).value = "0";
    getRef(getRef("WebsiteInput_WID").value).value = "";
    getRef(getRef("WebsiteInput_MID").value).value = ""; 
    getRef(getRef("WebsiteInput_FID").value).value = "";
    getRef("PostControl_LocationSearchResults").innerHTML = "";
    
    // CLIENT TYPE
    getRef("PostControl_Restaurant").checked = false;
    getRef("PostControl_Bar").checked = false;
    getRef("PostControl_Club").checked = false;
    getRef("PostControl_RestaurantWithBar").checked = false;
    getRef("PostControl_OtherVenue").checked = false;
    
    // WHAT
    getRef("PostControl_Headline").value = "";
    getRef("PostControl_Description").value = "";    
    getRef("PostControl_PostTypeDiv").style.display = "";
    
    getRef("PostControl_AddClientSearchResults").innerHTML = "";
    
    // WHEN
    getRef("PostControl_SingleDate").value = "";
    getRef("PostControl_DateRange1").value = "";
    getRef("PostControl_DateRange2").value = "";
    getRef("PostControl_MultipleDates1").value = "";
    getRef("PostControl_MultipleDates2").value = "";
    getRef("PostControl_MultipleDates3").value = "";
    getRef("PostControl_MultipleDates4").value = "";
    getRef("PostControl_MultipleDates5").value = "";
    getRef("PostControl_M").checked = false;
    getRef("PostControl_TU").checked = false;
    getRef("PostControl_W").checked = false;
    getRef("PostControl_TH").checked = false;
    getRef("PostControl_F").checked = false;
    getRef("PostControl_SA").checked = false;
    getRef("PostControl_SU").checked = false;
    getRef("PostControl_Hours").value = "";
}

function PostControl_Reset_ByLocation(iClientID, iLocationID, sClientName, sClientHTMLTable) { 
    PostControl_Reset();
}

function PostControl_HideElements() {
    getRef("PostControl_PostTypeDiv").style.display = "none";

    // WHERE
    getRef("PostControl_WhereDiv").style.display = "none";
    getRef("PostControl_WhereTitle").style.display = "none";
    
    getRef("PostControl_ClientTypeTable").style.display = "none";
    getRef("PostControl_WhatTable").style.display = "none";
    
    // ADD CLIENTS
    getRef("PostControl_AddClientsTable").style.display = "none";
    
    // WHEN 
    getRef("PostControl_WhenTable").style.display = "none";
    
    // RESULT
    getRef("PostControl_ClientTable").style.display = "none";
    
    // LOGO
    getRef("PostControl_LogoRequestDiv").style.display = "none";
    getRef("PostControl_LogoUploadFrameLoadingDiv").style.display = "none";    
    getRef("PostControl_LogoUploadFrameLoadedDiv").style.display = "none";    
    getRef("PostControl_LogoUploadNoThanksDiv").style.display = "none";
    getRef("PostControl_LogoUploadFrameLoadedDiv").style.display = "none";
}

// POST TYPE - BEGIN

function PostControl_PostTypeClick(pType) {
    
    PostControl_HideElements();
    
    getRef("PostControl_PostTypeID").value = pType;
    pType = Number(pType);
    if (pType == 1) { // Event 
        getRef("PostControl_WhereTitle").style.display = "";
        getRef("PostControl_HeaderTitle").innerHTML = "Post an Awesome Event";
    }else{
        getRef("PostControl_HeaderTitle").innerHTML = "Post an Awesome Place";
    }
        
    getRef("PostControl_HeaderTitle").style.display = "";
    PostControl_Where_Open();
}

// POST TYPE - END

// LOCATION PREDICTIVE SEARCH - BEGIN

var PostControl_FoundSearchLocations = 0;
var PostControl_RunSearchLocations = false;

function PostControl_CheckRunSearchLocations(e){
    PostControl_RunSearchLocations = true;
    var key;
    if (window.event) key = window.event.keyCode;
	else if (e) key = e.which; 
	if (Number(key) == 8) { return; }
    if (getRef("PostControl_Address1").value != ""){ PostControl_RunSearchLocations = false; }
    if (getRef("PostControl_City").value != ""){ PostControl_RunSearchLocations = false; }
    if (Number(getRef(getRef("PostControl_State_ClientID").value).value) != 0){ PostControl_RunSearchLocations = false; }
    if (getRef("PostControl_Zip").value != ""){ PostControl_RunSearchLocations = false; }
}

function PostControl_SearchLocations_Zip(e){
    PostControl_CheckRunSearchLocations(e);
    if ((PostControl_FoundSearchLocations > 0) || PostControl_RunSearchLocations){ PostControl_SearchLocations_Internal(); }
}

function PostControl_ZipKeyUp(){
	if (getRef("PostControl_Zip").value.length != 5) { }
	else{ GetCityStateByZipCode(getRef("PostControl_Zip").value); }	
	if (getRef("PostControl_Zip").value.length >= 4){ PostControl_SearchLocations_Internal(); }
}

function PostControl_SearchLocations_Internal(){
    var sVenueName = getRef("PostControl_Venue").value;
    var sAddress1 = getRef("PostControl_Address1").value;
    var sCity = getRef("PostControl_City").value;
    var iStateID = Number(getRef(getRef("PostControl_State_ClientID").value).value);
    var sZip = getRef("PostControl_Zip").value;
    
    SearchLocations(sVenueName, sAddress1, sCity, iStateID, sZip);
}


function PostControl_SearchLocations_DisplayResults_Internal(result){
    //getRef("LocationSearchResults").innerHTML = "";
    var s = "";
    var rightArrow = "<img src=\"" + getRef("ProjectURL").value + "image/Arrows/ArrowRight.gif\" alt=\"\" style=\"border:0;\" />";
    PostControl_FoundSearchLocations = result.length;
    if (result.length > 0){
        for (i=0; i< result.length; i++){
            if (i < 12){
                s += "<div><a href=\"javascript:PostControl_LocationSelect('" + String(result[i].ClientID)
                    + "','" + String(result[i].LocationID)
                    + "','" + result[i].ClientName_QuoteEscaped
                    + "','" + result[i].Address1 + "','" + result[i].Address2
                    + "','" + result[i].City + "','" + result[i].StateID + "','" + result[i].Zip 
                s += "');\">";
                s += rightArrow + "<span style=\"position:relative;top:-5px;z-index:1;padding-left:5px; line-height:1.75em;\">";
                s += "<b>" + result[i].ClientName + "</b>, " + result[i].Address1;
                if (result[i].Address2 != ""){ s += ", " + result[i].Address2; }
                s += ", " + result[i].City + ", " + result[i].StateAbbr + " " + result[i].Zip;
                s += "</span></a></div>";
            }
        }
    }
    getRef("PostControl_LocationSearchResults").innerHTML = s;
}

function PostControl_LocationSelect(iClientID, iLocationID, sVenueName, sAddress1, sAddress2, sCity, iStateID, sZip){

    getRef("PostControl_ClientID").value = iClientID;
    getRef("PostControl_LocationID").value = iLocationID;

    getRef("PostControl_Venue").value = sVenueName;
    getRef("PostControl_Address1").value = sAddress1;
    getRef("PostControl_Address2").value = sAddress2;
    getRef("PostControl_City").value = sCity;
    getRef(getRef("PostControl_State_ClientID").value).value = String(iStateID);
    getRef("PostControl_Zip").value = sZip;
    
    PostControl_ClientService_GetClientInfo_Basic(iClientID);
}

function GetCityStateByZipCode_SucceededCallback_Internal(City, StateID, StateName, StateAbbr){
    getRef("PostControl_City").value = City;
    getRef(getRef("PostControl_State_ClientID").value).value = StateID;
}

// LOCATION PREDICTIVE SEARCH - END

// CLIENT PREDICTIVE SEARCH - BEGIN

function PostControl_AddClientSearch() {
    if (Number(getRef("PostControl_AddClientName").value.length) > 2) {
        ClientSearchByName(String(getRef("PostControl_AddClientName").value), PostControl_ClientSearchTypeIDs);
    }
}

function PostControl_ClientSearchByName_DisplayResults_Internal(ClientContainers){

    var s = "";
    var rightArrow = "<img src=\"" + getRef("ProjectURL").value + "image/Arrows/ArrowRight.gif\" alt=\"\" style=\"border:0;\" />";
    if (ClientContainers.length > 0){
        for (i=0; i< ClientContainers.length; i++){
            if (i < 12){
                s += "<div><a href=\"javascript:PostControl_ClientSearchByName_SelectClient(" + String(ClientContainers[i].ClientID)
                    + ",'" + String(ClientContainers[i].ClientName).replace("\'", "\\\'") + "');\">";
                s += rightArrow + "<span style=\"position:relative;top:-5px;z-index:1;padding-left:5px; line-height:1.75em;\">";
                s += "<b>" + ClientContainers[i].ClientName + "</b></span></a></div>";
            }
        }
    }
    getRef("PostControl_AddClientSearchResults").innerHTML = s;
    
}

function PostControl_AddClientIDToSpecial(iClientID) {
    if (String(PostControl_AddedClientIDs) != "") { PostControl_AddedClientIDs += ","; }
    PostControl_AddedClientIDs += String(iClientID);
}

function PostControl_ClientSearchByName_SelectClient(iClientID, sClientName) {

    PostControl_AddClientIDToSpecial(iClientID);    
    
    ClientService_GetClientInfo_Basic_SetFunction(iClientID, PostControl_ClientSearchByName_SelectClient_GetBasicInfo);
    
    getRef("PostControl_AddClientName").value = "";
    getRef("PostControl_AddClientSearchResults").innerHTML = "";
    
}

function PostControl_ClientSearchByName_SelectClient_GetBasicInfo(ClientContainer) {
    
    getRef("PostControl_AddedClientsDisplayDiv").innerHTML += "<div style=\"padding:5px 0px;\">" + ClientContainer.ClientHTMLTable + "</div>";
}

function PostControl_AddClientsDone() {
    PostControl_WhenTable_Open();
}

function PostControl_AddClientToSpecialButtonClick() {

    var hasError = false;
    if (getRef("PostControl_AddClientName").value.trim() == "") {        
        getRef("PostControl_AddClientToSpecialErrorDiv").innerHTML = "A name is required";
        if (PostControl_AddClientTypeID == 2) { getRef("PostControl_AddClientToSpecialErrorDiv").innerHTML = "A band name is required"; }
        hasError = true;
    }
    
    if (hasError) {
        getRef("PostControl_AddClientToSpecialErrorDiv").style.display = "";
        return;
    }else{ getRef("PostControl_AddClientToSpecialErrorDiv").style.display = "none"; }

    PostControl_AddClientToSpecial(getRef("PostControl_AddClientName").value, PostControl_AddClientTypeID)
}

function PostControl_AddClientToSpecial_Error() {
    getRef("PostControl_AddClientToSpecialErrorDiv").innerHTML = "Sorry dude, there was an error";
    getRef("PostControl_AddClientToSpecialErrorDiv").style.display = "";
}

function PostControl_AddClientToSpecial_Success(ClientContainer) {
    PostControl_AddClientIDToSpecial(ClientContainer.ClientID);
    getRef("PostControl_AddedClientsDisplayDiv").innerHTML += "<div style=\"padding:5px 0px;\">" + ClientContainer.ClientHTMLTable + "</div>";    
    getRef("PostControl_AddClientName").value = "";
}

// CLIENT PREDICTIVE SEARCH - END




// GET CLIENT INFO - BEGIN

function PostControl_ClientService_GetClientInfo_Basic(iClientID) {
    ClientService_GetClientInfo_Basic_SetFunction(iClientID, ClientService_GetClientInfo_Basic_SucceededCallback_Internal);
}

function ClientService_GetClientInfo_Basic_SucceededCallback_Internal(result) {
    var iClientID = result.ClientID;
    var sClientName = result.ClientName;
    var sClientHTMLTable = result.ClientHTMLTable;
    var bHasLogo = result.HasLogo;
    
    PostControl_HideElements();

    if (Number(getRef("PostControl_PostTypeID").value) == 1) { // EVENT
        if (Number(getRef("PostControl_CopySpecialID").value) == 0) {
            getRef("PostControl_HeaderTitle").innerHTML = "Post an Event at " + sClientName;
        }else{
            getRef("PostControl_HeaderTitle").innerHTML = "Copy Post to " + sClientName;
        }
        if (bHasLogo) { PostControl_WhatTable_Open(); }
        else{ PostControl_LogoRequest_Show(iClientID, sClientName); }
    }else if (Number(getRef("PostControl_PostTypeID").value) == 2) { // PLACE
        if (bHasLogo) {
            PostControl_DisplayAlreadyPostedPlace(sClientName, sClientHTMLTable); // END POINT
        }else{
            PostControl_LogoRequest_Show(iClientID, sClientName);
            PostControl_DisplayAlreadyPostedPlace_RequestLogo(sClientName, sClientHTMLTable);        
        }
    }
}

// GET CLIENT INFO - END 


// WHERE / ADD CLIENT - BEGIN

function PostControl_Where_Open() {
    getRef("PostControl_WhereDiv").style.display = "";
    getRef("PostControl_WhereTable").style.display = "";
    getRef("PostControl_WhereLoadingDiv").style.display = "none";
    getRef("PostControl_WhereLoadingDiv").innerHTML = "Adding Location... chillax for a sec";
}

function PostControl_Where_Loading() {
    getRef("PostControl_WhereTable").style.display = "none";
    getRef("PostControl_WhereLoadingDiv").style.display = "";
}

function PostControl_Where_Loaded() {
    getRef("PostControl_WhereTable").style.display = "";
    getRef("PostControl_WhereLoadingDiv").style.display = "none";
}

function PostLocation_Failure_Internal() { // FAILURE!
    getRef("PostControl_WhereTable").style.display = "none";
    getRef("PostControl_WhereLoadingDiv").style.display = "";
    var Where_ErrorMsg = "Dude, there was an error posting this location.  Our bad.<br />" +
        "Try again or email us";
    try { Where_ErrorMsg += " <a href='" + getRef("ProjectURL").value + "'>here</a>"; }
    catch(exc) { }
    Where_ErrorMsg += ".";
    getRef("PostControl_WhereLoadingDiv").innerHTML = Where_ErrorMsg;
}

function PostControl_AddLocationClick() {

    var validLocation = true;
    var msg = "";
    
    if (getRef(getRef("PostControl_State_ClientID").value).value == "0") { SetRed("PostControl_StateSpan"); msg = "Select a state dude"; validLocation = false;}
    else { SetBlack("PostControl_StateSpan"); }    
    
    if (getRef("PostControl_City").value == "") { SetRed("PostControl_CityDiv"); msg = "You need an address dude"; validLocation = false;}
    else { SetBlack("PostControl_CityDiv"); }
    
    if (getRef("PostControl_Venue").value == "") { SetRed("PostControl_Address1Div"); msg = "You need an address dude"; validLocation = false;}
    else { SetBlack("PostControl_Address1Div"); }
    
    if (getRef("PostControl_Venue").value == "") { SetRed("PostControl_VenueDiv"); msg = "You need a name for this place dude"; validLocation = false;}
    else { SetBlack("PostControl_VenueDiv"); }
    
    if (getRef("PostControl_Zip").value == "") { SetRed("PostControl_ZipSpan"); msg = "You need a zip dude"; validLocation = false;}
    else { SetBlack("PostControl_ZipSpan"); }
    
    if (!validLocation) {
        getRef("PostControl_InvalidLocationDiv").innerHTML = msg;
        getRef("PostControl_InvalidLocationDiv").style.display = "";
        return;
    }
    
    var w = "";
    if (IsValidURL(getRef(getRef("WebsiteInput_WID").value).value)) { w = getRef(getRef("WebsiteInput_WID").value).value; }
    var m = "";
    if (IsValidURL(getRef(getRef("WebsiteInput_MID").value).value)) { w = getRef(getRef("WebsiteInput_MID").value).value; }
    var f = "";
    if (IsValidURL(getRef(getRef("WebsiteInput_FID").value).value)) { w = getRef(getRef("WebsiteInput_FID").value).value; }

    PostControl_Where_Loading();

    PostLocation(getRef("PostControl_Venue").value, getRef("PostControl_Address1").value, getRef("PostControl_Address2").value, 
        getRef("PostControl_City").value, getRef(getRef("PostControl_State_ClientID").value).value, getRef("PostControl_Zip").value, 
        getRef("PostControl_Phone").value, w, m, f);    
}


function PostLocation_SucceededCallback_Internal(iClientID, iLocationID, bIsNewClient, bHasLogo, 
    sClientName, sClientHTMLTable, ErrorMsg) {

    getRef("PostControl_ClientID").value = iClientID;
    getRef("PostControl_LocationID").value = iLocationID;

    if (ErrorMsg != "") {
        getRef("PostControl_InvalidLocationDiv").innerHTML = ErrorMsg;
        getRef("PostControl_InvalidLocationDiv").style.display = "";
    }else{
        getRef("PostControl_InvalidLocationDiv").innerHTML = "";
        getRef("PostControl_InvalidLocationDiv").style.display = "none";
    }
    
    getRef("PostControl_SetLatLngFrame").src = getRef("ProjectURL").value + "Map/SetLocationLatLngPassedLocationID.aspx?ID=" + String(iLocationID);

    PostControl_HideElements();
    
    if (Number(getRef("PostControl_PostTypeID").value) == 1) { // EVENT
        if ((!Boolean(bIsNewClient)) && Boolean(bHasLogo)) { // Client Exists and HAS logo
            PostControl_WhatTable_Open();
            getRef("PostControl_HeaderTitle").innerHTML = "Post an Awesome Event at " + sClientName;
        }else if (!Boolean(bIsNewClient)) { // Client Exists
            getRef("PostControl_HeaderTitle").innerHTML = "Post an Awesome Event at " + sClientName;
            PostControl_LogoRequest_Show(iClientID, sClientName);
        }else{
            getRef("PostControl_ClientTypeTable").style.display = "";
        }
    }else if (Number(getRef("PostControl_PostTypeID").value) == 2) { // PLACE
        if ((!Boolean(bIsNewClient)) && Boolean(bHasLogo)) { // Client Exists and HAS logo            
            // END POINT
            PostControl_DisplayAlreadyPostedPlace(sClientName, sClientHTMLTable);
        }else if (!Boolean(bIsNewClient)) { // Client Exists, No Logo, request it
            PostControl_LogoRequest_Show(iClientID, sClientName);
            PostControl_DisplayAlreadyPostedPlace_RequestLogo(sClientName, sClientHTMLTable);        
        }else{
            getRef("PostControl_ClientTypeTable").style.display = "";
        }
    }
}

function PostControl_DisplayAlreadyPostedPlace(sClientName, sClientHTMLTable) {
    getRef("PostControl_HeaderTitle").innerHTML = "Thanks dude, " + sClientName + " is posted"; 
    getRef("PostControl_ClientTableTitleDiv").innerHTML = "Thanks dude, but this place is <b>aleady " +
        "posted</b>.  <br />Click below to go to the profile page for <b>" + sClientName + "</b>";
    getRef("PostControl_ClientTableLinkDiv").innerHTML = sClientHTMLTable;
    getRef("PostControl_ClientTable").style.display = "";
}

function PostControl_DisplayAlreadyPostedPlace_RequestLogo(sClientName, sClientHTMLTable) {
    getRef("PostControl_ClientTableTitleDiv").innerHTML = "This place is <b>already " +
        "posted</b>, but if you have a logo, you can add it above.  <br /><br />Click below to go to the profile page for " + sClientName;
    getRef("PostControl_ClientTableLinkDiv").innerHTML = sClientHTMLTable;
    getRef("PostControl_ClientTable").style.display = "";
}

// ADD CLIENT - END

// SET CLIENT TYPE - BEGIN

function PostControl_SetClientType(cType) {
    ClientService_SetClientType(Number(getRef("PostControl_ClientID").value), Number(cType))
}

function ClientService_SetClientType_SucceededCallback_Internal(iClientID, sClientName, sClientHTMLTable) {
    
    PostControl_LogoRequest_Show(iClientID, sClientName);
    
    if (Number(getRef("PostControl_PostTypeID").value) == 2) { // PLACE
        // END POINT (w / logo)
        getRef("PostControl_ClientTableTitleDiv").innerHTML = "Cool man, <b>" + sClientName + " is " +
            "posted</b>.  If you have a logo, you can add it above.  <br /><br />Click below to go to the profile page for " + sClientName;
        getRef("PostControl_ClientTableLinkDiv").innerHTML = sClientHTMLTable;
        getRef("PostControl_ClientTable").style.display = "";
    }
}

// SET CLIENT TYPE - END

// LOGO - BEGIN

function PostControl_LogoRequest_Show(iClientID, sClientName) { 

    PostControl_HideElements();

    getRef("PostControl_LogoRequestDiv").style.display = "";
    if (sClientName != "") { getRef("PostControl_LogoRequestTitle").value = "Have a logo for " + sClientName + "?"; }
    else { getRef("PostControl_LogoRequestTitle").value = "Have a logo?"; }
    getRef("PostControl_LogoUploadFrameLoadingDiv").innerHTML = "Loading...";    
    getRef("PostControl_LogoUploadFrameLoadingDiv").style.display = "";
    getRef("PostControl_LogoUploadFrameLoadedDiv").style.display = "none";
    
    if (Number(getRef("PostControl_PostTypeID").value) == 1) { // EVENT
        getRef("PostControl_LogoUploadNoThanksDiv").style.display = "";
    }
    
    getRef(getRef("PostControl_LogoUploadFrameID").value).src = getRef("PostControl_LogoUploadFrameURL").value + "?ID=" + String(iClientID);
}

function PostControl_LogoRequest_Close() { 
    getRef("PostControl_LogoRequestDiv").style.display = "none";
    getRef("PostControl_LogoUploadFrameLoadingDiv").style.display = "none";    
    getRef("PostControl_LogoUploadFrameLoadedDiv").style.display = "none";
}


function PostControl_UploadPageLoad() {
    getRef("PostControl_LogoUploadFrameLoadingDiv").style.display = "none";
    getRef("PostControl_LogoUploadFrameLoadedDiv").style.display = "";
}

function PostControl_LogoUploading() {
    getRef("PostControl_LogoUploadFrameLoadingDiv").innerHTML = "Logo uploading... chill for a sec";
    getRef("PostControl_LogoUploadFrameLoadingDiv").style.display = "";
    getRef("PostControl_LogoUploadFrameLoadedDiv").style.display = "none";
}

function PostControl_LogoUploadSuccessful(sClientName, sClientHTMLTable) {
    PostControl_HideElements();
    if (Number(getRef("PostControl_PostTypeID").value) == 1) { // EVENT
        PostControl_WhatTable_Open();
    }else if (Number(getRef("PostControl_PostTypeID").value) == 2) { // PLACE
        // END POINT
        getRef("PostControl_HeaderTitle").innerHTML = "Sweet dude, " + sClientName + " is posted"; 
        getRef("PostControl_ClientTableTitleDiv").innerHTML = "Sweet dude, <b>" + sClientName + " is " +
            "posted.</b>  <br /><br />Click below to go to the profile page for <b>" + sClientName + "</b>";
        getRef("PostControl_ClientTableLinkDiv").innerHTML = sClientHTMLTable;
        getRef("PostControl_ClientTable").style.display = "";
    }
}

// LOGO - END

// WHAT - BEGIN

function PostControl_WhatTable_Open() {
    PostControl_HideElements();
    if (Number(getRef("PostControl_CopySpecialID").value) != 0) { // SKIP WHAT, GOTO WHEN
        PostControl_WhenTable_Open();
        return;
    }
    getRef("PostControl_WhatTable").style.display = "";
}

function PostControl_SetSpecialTypeID(sType, sTypeText) {
    getRef("PostControl_SpecialTypeID").value = sType;
    getRef("PostControl_SpecialTypeText").value = sTypeText;    
}

function PostControl_WhatSubmit() {

    var What_ValidSubmit = true;
    
    if (getRef("PostControl_Headline").value.Trim() == "") {
        SetRed("PostControl_HeadlineDiv");
        getRef("PostControl_WhatErrorDiv").innerHTML = "You need a headline dude";
        What_ValidSubmit = false;
    }else{
        SetBlack("PostControl_HeadlineDiv");
    }
    
    if (getRef("PostControl_SpecialTypeID").value == "0") {
        SetRed("PostControl_WhatTypeDiv");
        getRef("PostControl_WhatErrorDiv").innerHTML = "You need to select a type dude";
        What_ValidSubmit = false;
    }else{
        SetBlack("PostControl_WhatTypeDiv");
    }
        
    if (!What_ValidSubmit) { return; }
    
    try {
        if (getRef("PostControl_SpecialTypeText").value.Trim() != "") {
            getRef("PostControl_HeaderTitle").innerHTML = getRef("PostControl_HeaderTitle").innerHTML.replace("Awesome Event at", "Awesome " + getRef("PostControl_SpecialTypeText").value + " event at");
        }
    }catch(exc){ }
    
    PostControl_HideElements();
    PostControl_ClientSearchTypeIDs = "";
    PostControl_AddClientTypeID = 0;
    if (Number(getRef("PostControl_SpecialTypeID").value) == 8) {
        PostControl_ClientSearchTypeIDs = "2"; // BANDS Client Type only
        PostControl_AddClientTypeID = 2; // Add clients as BANDS
        getRef("PostControl_AddClientsTitle").innerHTML = "Would you like to add bands / musicians?";
        getRef("PostControl_ClientNameTitleTD").innerHTML = "Band / Musician Name";
        getRef("PostControl_AddClientToSpecialButton").value = "Add Band / Musician";
        getRef("PostControl_AddClientsDoneButton").value = "I'm done adding bands / musicians";        
        getRef("PostControl_AddClientsTable").style.display = "";
    }else if (Number(getRef("PostControl_SpecialTypeID").value) == 12) {
        PostControl_ClientSearchTypeIDs = "5"; // COMEDIANS Client Type only
        PostControl_AddClientTypeID = 5; // Add clients as COMEDIANS
        getRef("PostControl_AddClientsTitle").innerHTML = "Would you like to add comedians?";
        getRef("PostControl_ClientNameTitleTD").innerHTML = "Comedian Name";
        getRef("PostControl_AddClientToSpecialButton").value = "Add Comedian";
        getRef("PostControl_AddClientsDoneButton").value = "I'm done adding comedians";        
        getRef("PostControl_AddClientsTable").style.display = "";
    }else if (Number(getRef("PostControl_SpecialTypeID").value) == 7) {
        PostControl_ClientSearchTypeIDs = "3"; // DJs Client Type only
        PostControl_AddClientTypeID = 3; // Add clients as DJs
        getRef("PostControl_AddClientsTitle").innerHTML = "Would you like to add DJs?";
        getRef("PostControl_ClientNameTitleTD").innerHTML = "DJ Name";
        getRef("PostControl_AddClientToSpecialButton").value = "Add DJ";
        getRef("PostControl_AddClientsDoneButton").value = "I'm done adding DJs";        
        getRef("PostControl_AddClientsTable").style.display = "";
    }else{
        PostControl_WhenTable_Open();
    }
}

function PostControl_InsertLinkURL_Change(){
    getRef("PostControl_InsertLinkConfirm").style.display = "none";
	var LinkURL = String(getRef("PostControl_InsertLinkURL").value);
	if (LinkURL != ""){
		if (String(WebsiteRegEx.test(LinkURL)) == "false"){ getRef("PostControl_InsertLinkInvalidDiv").style.display = ''; }
		else{ getRef("PostControl_InsertLinkInvalidDiv").style.display = 'none'; }
	}else{ getRef("PostControl_InsertLinkInvalidDiv").style.display = 'none'; }
	return false;
}

function PostControl_InsertLinkButton_Click(){
	var LinkURL = String(getRef("PostControl_InsertLinkURL").value);
	if (LinkURL != ""){
		if (String(WebsiteRegEx.test(LinkURL)) != "false"){
		    var LinkText = getRef("PostControl_InsertLinkText").value.Trim();
		    if (LinkText == "") { LinkText = LinkURL; }
		    getRef("PostControl_Description").value = getRef("PostControl_Description").value + " <a href=\"" + LinkURL + "\">" + LinkText + "</a>";
		    getRef("PostControl_InsertLinkURL").value = "";
		    getRef("PostControl_InsertLinkText").value = "";
		    getRef("PostControl_InsertLinkConfirm").style.display = "";
		    getRef("PostControl_InsertLinkInvalidDiv").style.display = "none";
		}
	}
}

function PostControl_InsertImageLinkURL_Change(){
    getRef("PostControl_InsertImageLinkConfirm").style.display = "none";
	var LinkURL = String(getRef("PostControl_InsertImageLinkURL").value);
	if (LinkURL != ""){
		if (String(WebsiteRegEx.test(LinkURL)) == "false"){ getRef("PostControl_InsertImageLinkInvalidDiv").style.display = ''; }
		else{ getRef("PostControl_InsertImageLinkInvalidDiv").style.display = 'none'; }
	}else{ getRef("PostControl_InsertImageLinkInvalidDiv").style.display = 'none'; }
	return false;
}

function PostControl_InsertImageLinkButton_Click(){
	var LinkURL = String(getRef("PostControl_InsertImageLinkURL").value);
	if (LinkURL != ""){
		if (String(WebsiteRegEx.test(LinkURL)) != "false"){
		    getRef("PostControl_Description").value = getRef("PostControl_Description").value + "\n\n<img src=\"" + LinkURL + "\" alt=\"\" />";
		    getRef("PostControl_InsertImageLinkURL").value = "";
		    getRef("PostControl_InsertImageLinkConfirm").style.display = "";
		    getRef("PostControl_InsertImageLinkInvalidDiv").style.display = "none";
		}
	}
}

function PostControl_InsertLink_ShowInstructions() {
    document.getElementById("PostControl_InsertLink_InstructionsDiv").style.display="";
    document.getElementById("PostControl_InsertLink_InstructionsLink").style.display="none";
}

function PostControl_WebImage_ShowInstructions() {
    document.getElementById("PostControl_WebImage_InstructionsDiv").style.display="";
    document.getElementById("PostControl_WebImage_InstructionsLink").style.display="none";
}

// WHAT - END 

// WHEN - BEGIN

function PostControl_WhenTable_Open() {
    PostControl_HideElements();
    getRef("PostControl_WhenTable").style.display = "";
}

var PostControl_SingleDate_CalendarLoaded = false;
var PostControl_DateRange_CalendarLoaded = false;
var PostControl_MultipleDates_CalendarLoaded = false;

function PostControl_GetDateType() {
    if (getRef("PostControl_SingleDateChoice").checked) { return 1; }
    else if (getRef("PostControl_WeeklySpecialChoice").checked) { return 2; }
    else if (getRef("PostControl_DateRangeChoice").checked) { return 3; }
    else if (getRef("PostControl_MultipleDatesChoice").checked) { return 4; }
    return 0;
}

function PostControl_DatesTypeChange(i) {

    getRef("PostControl_SingleDateRow").style.display = "none";
    getRef("PostControl_WeeklyPostRow").style.display = "none";
    getRef("PostControl_DateRangeRow").style.display = "none";
    getRef("PostControl_MultipleDatesRow").style.display = "none";
    
    try { 
        PostControl_SingleDate_CalendarLoaded = false;
        PostControl_DateRange_CalendarLoaded = false;
        PostControl_MultipleDates_CalendarLoaded = false;

        hideCalendar(); 
        crossobj = null; 
        
    } catch (exc) {}
    
    i = Number(i);
    if (i == 1) {
        getRef("PostControl_SingleDateRow").style.display = "";    
    }else if (i == 2) {
        getRef("PostControl_WeeklyPostRow").style.display = "";
    }else if (i == 3) {
        getRef("PostControl_DateRangeRow").style.display = "";
    }else if (i == 4) {
        getRef("PostControl_MultipleDatesRow").style.display = "";
    }
}

function PostControl_WhenSubmit() {

    var When_ValidSubmit = true;

    if (getRef("PostControl_SingleDateChoice").checked && 
            getRef("PostControl_SingleDate").value == "") {
        getRef("PostControl_WhenErrorDiv").innerHTML = "You need to pick a date dude";
        When_ValidSubmit = false;
    }
    
    if (getRef("PostControl_WeeklySpecialChoice").checked && 
        !getRef("PostControl_M").checked && 
        !getRef("PostControl_TU").checked && 
        !getRef("PostControl_W").checked && 
        !getRef("PostControl_TH").checked && 
        !getRef("PostControl_F").checked && 
        !getRef("PostControl_SA").checked && 
        !getRef("PostControl_SU").checked) {
        
        getRef("PostControl_WhenErrorDiv").innerHTML = "You need to pick at least one day of the week dude";
        When_ValidSubmit = false;
    }
    
    if (getRef("PostControl_DateRangeChoice").checked && 
            getRef("PostControl_DateRange2").value == "") {
        getRef("PostControl_WhenErrorDiv").innerHTML = "You need to pick an end date dude";
        When_ValidSubmit = false;
    }
    
    if (getRef("PostControl_DateRangeChoice").checked && 
            getRef("PostControl_DateRange1").value == "") {
        getRef("PostControl_WhenErrorDiv").innerHTML = "You need to pick a start date dude";
        When_ValidSubmit = false;
    }
    
    if (getRef("PostControl_MultipleDatesChoice").checked && 
        getRef("PostControl_MultipleDates1").value == "" && 
        getRef("PostControl_MultipleDates2").value == "" && 
        getRef("PostControl_MultipleDates3").value == "" && 
        getRef("PostControl_MultipleDates4").value == "" && 
        getRef("PostControl_MultipleDates5").value == "") {
        
        getRef("PostControl_WhenErrorDiv").innerHTML = "You need to pick at least one date dude";
        When_ValidSubmit = false;
    }
   
    if (!getRef("PostControl_SingleDateChoice").checked && 
            !getRef("PostControl_WeeklySpecialChoice").checked && 
            !getRef("PostControl_DateRangeChoice").checked && 
            !getRef("PostControl_MultipleDatesChoice").checked) {
        SetRed("PostControl_DateTypeTD");
        getRef("PostControl_WhenErrorDiv").innerHTML = "You need to select a date type dude";
        When_ValidSubmit = false;
    }else{
        SetBlack("PostControl_DateTypeTD");
    }
    
    if (!When_ValidSubmit) { return; }
    
    getRef("PostControl_WhenErrorDiv").innerHTML = "";
    
    PostControl_CreatePost();
}

// WHEN - END

// CREATE POST - BEGIN

function PostControl_CreatePost() {

    var iClientID = Number(getRef("PostControl_ClientID").value);
    var iLocationID = Number(getRef("PostControl_LocationID").value);
    var iSpecialTypeID = Number(getRef("PostControl_SpecialTypeID").value);
    var sHeadline = getRef("PostControl_Headline").value
    var sDescription = getRef("PostControl_Description").value
    var iDateTypeID = PostControl_GetDateType();
    var sD1 = getRef("PostControl_SingleDate").value;
    if (iDateTypeID == 3) { sD1 = getRef("PostControl_DateRange1").value; }
    else if (iDateTypeID == 4) { sD1 = getRef("PostControl_MultipleDates1").value; }
    var sD2 = getRef("PostControl_DateRange2").value;
    if (iDateTypeID == 4) { sD2 = getRef("PostControl_MultipleDates2").value; }
    var sD3 = getRef("PostControl_MultipleDates3").value;
    var sD4 = getRef("PostControl_MultipleDates4").value;
    var sD5 = getRef("PostControl_MultipleDates5").value;
    var bM = getRef("PostControl_M").checked;
    var bTU = getRef("PostControl_TU").checked;
    var bW = getRef("PostControl_W").checked;
    var bTH = getRef("PostControl_TH").checked;
    var bF = getRef("PostControl_F").checked;
    var bSA = getRef("PostControl_SA").checked;
    var bSU = getRef("PostControl_SU").checked;
    var sHours = getRef("PostControl_Hours").value;
    var sAddClientIds = PostControl_AddedClientIDs;
    var iCopySpecialID = Number(getRef("PostControl_CopySpecialID").value);
    
    SpecialsService_CreatePost(iClientID, iLocationID, 
        iSpecialTypeID, sHeadline, sDescription, 
        iDateTypeID, sD1, sD2, sD3, sD4, sD5, bM, bTU, bW, bTH, bF, bSA, bSU, sHours, 
        sAddClientIds, iCopySpecialID)

}

function PostControl_CreatePostSucceeded(iSpecialID, sPostLink, 
    bHasLogo, sClientName, sClientHTMLTable, 
    sErrorMsg, FeaturedClients) {
    
    if (sErrorMsg != "") {
        getRef("PostControl_WhenErrorDiv").innerHTML = sErrorMsg;
        return;
    }
    
    PostControl_HideElements();
    
    // END POINT
    getRef("PostControl_HeaderTitle").innerHTML = "Awesome dude, your post is, like, posted"; 
    var sPostMsg = "Awesome dude, here is a link to your post <br /> " +
        "<div style=\"padding:20px;\".><a href=\"" + sPostLink + "\">" + sPostLink + "</a></div>" +
        "<br /><br />Click below to go to the profile page for <b>" + sClientName + "</b>";
        
    getRef("PostControl_ClientTableTitleDiv").innerHTML = sPostMsg;
    getRef("PostControl_ClientTableLinkDiv").innerHTML = sClientHTMLTable;
    
    getRef("PostControl_ClientTable").style.display = "";    
    if (FeaturedClients.length > 0) {
        var Featured = "";
        for (var i = 0; i < FeaturedClients.length; i++) {
            Featured += "<div style=\"padding:5px 0px;\">" + FeaturedClients[i].ClientHTMLTable + "</div>";
        }
        if (Featured != "") {
            Featured = "<div style=\"padding:10px 0px;\">Featuring</div>" + Featured;
        }
        getRef("PostControl_ClientTableLinkDiv").innerHTML = getRef("PostControl_ClientTableLinkDiv").innerHTML + Featured;
    }
}

// CREATE POST - END
    