﻿function SetContentSize() {
    var diffFromMaxWidth = 0;
    var diffUnit = 12;
    var units = 0;
    var maxWidth = 997;
    var maxEMWidth = 58;
    var newMaxEMWidth = 58;
    var currWidth = $(window).width();
    if (currWidth < maxWidth) {
        diffFromMaxWidth = maxWidth - currWidth;
        units = diffFromMaxWidth / diffUnit;
        if (units < 1)
            units = 0;

        newMaxEMWidth = maxEMWidth - Math.ceil(units);
        $(".results").css('max-width', newMaxEMWidth + 'em');
        $(".resultsPadding").css('max-width', newMaxEMWidth + 'em');
    }
    else {
        $(".results").css('max-width', '58em');
        $(".resultsPadding").css('max-width', '62em');
    }
}


function setSearchFocus() {
    tempObj = document.getElementsByName('q');
    if (tempObj.length > 0) {
        tempObj = tempObj[0];
        try {
            tempObj.focus();
        }
        catch (ex) {
        }
    }
}
//// MAKE TEXT DARK ////

function inputFocus(whichId, insertText) {
    if (document.getElementById(whichId)) {
        var d = document.getElementById(whichId);

        if (d.value == insertText) {
            d.style.color = '#000';
            d.value = '';
        }
    }
}

//// RESET TEXT - MAKE IT LIGHT ////

function inputBlur(whichId, insertText, color) {
    if (document.getElementById(whichId)) {
        var d = document.getElementById(whichId);

        if (d.value == '') {
            if (!color) {
                d.style.color = '#999';
            }
            d.value = insertText;
        }
    }
}

function getElementsByCondition(condition, container) {
    container = container || document
    var all = container.all || container.getElementsByTagName('*')

    var arr = []
    for (var k = 0; k < all.length; k++) {
        var elm = all[k]

        if (elm.nodeType != 1)
            continue;

        //if (elm.className == "srHelpful")
        //alert(condition);

        if (condition(elm, k)) {
            arr[arr.length] = elm

        }
    }

    return arr
}

function TrimString(string) {
    // If the incoming string is invalid, or nothing was passed in, return empty
    if (!string)
        return "";

    string = string.replace(/^\s+/, ''); // Remove leading whitespace
    string = string.replace(/\s+$/, ''); // Remove trailing whitespace

    // Replace all whitespace runs with a single space
    string = string.replace(/\s+/g, ' ');

    return string; // Return the altered value
}

function IsBoolean(obj) {
    if (typeof obj == "undefined")
        return false;

    var isBoolean = true;
    switch (obj) {
        case 0: isBoolean = false; break;
        case false: isBoolean = false; break;
        case 'false': isBoolean = false; break;
        case 'False': isBoolean = false; break;
        case '': isBoolean = false; break;
        case null: isBoolean = false; break;
        case NaN: isBoolean = false; break;
        default: isBoolean = true; break;
    }
    return isBoolean;
}

var _UncheckedMailsCount = 0;
function invertSelection(parentInput) {
    var selections = getElementsByCondition(function(el) { return (el.getAttribute("meaning") == "contactInput"); }, this.contactsList);
    _UncheckedMailsCount = 0;
    for (var i = 0; i < selections.length; ++i) {
        selections[i].checked = parentInput.checked;
        if (!parentInput.checked)
            _UncheckedMailsCount++;
    }

}

function SetParentInputCheckedStatus(checked) {

    var parentInput = document.getElementById("parentInput");
    if(checked)
        _UncheckedMailsCount--;
    else
        _UncheckedMailsCount++;

    parentInput.checked = _UncheckedMailsCount == 0;

}

function validateStep2() {
    return validateForm(shareBottom);
}

function DonePressed() {
//    alert("test")
//    if (InviterText.getAttribute("originalvalue") == InviterText.value) {
//        InviterText.value = "";
//        InviterText.style.color = "#000";
//    }

//    if (InviterName.getAttribute("originalValue") == InviterName.value) {
//        InviterName.value = "";
//        InviterName.style.color = "#000";
//    }

//    if (this.validateStep2()) {
        var selections = getElementsByCondition(function(el) { return (el.getAttribute("meaning") == "contactInput"); }, this.contactsList);
        var emails = getElementsByCondition(function(el) { return (el.getAttribute("meaning") == "contactEmail"); }, this.contactsList);
        //alert("selections: " + selections.length)
        //alert("emails: " + emails.length)
        if (emails.length == 0)
            return;

        var emailsString = "";
        
        for (var i = 0; i < emails.length; i++) {
            if (selections[i].checked) {
                //alert(emails[i].innerText + "--" + emails[i].innerHTML);
                emailsString += TrimString(emails[i].innerHTML) + ", ";
            }
        }
       
        //alert("emailsString: " + emailsString);
        var emailsList = document.getElementById(emailListID);
        //alert("emailListObj:  " + emailsList)
        emailsList.value = emailsString;
        //alert(emailsList.value);
      


        return true;
//    }
//    else
//        return false;

    }


    
//    function wireSmartObjects(container) {
//        
//        container = (container == null ? this.element : container)

//        var smartObj = getElementsByCondition(function(el) { return (el.getAttribute("smartid") != null); }, container);
//        
////        for (var i = 0; i < smartObj.length; ++i) {
////            eval("this." + smartObj[i].getAttribute("smartid") + "=smartObj[i]");
////           
////        }

//        for (var i = 0; i < smartObj.length; ++i) {
//            if (smartObj[i].getAttribute("smartinit") != null) {
//                var s_this = smartObj[i];
//                eval(smartObj[i].getAttribute("smartinit"));
//                alert( eval(smartObj[i].getAttribute("smartinit")));
//            }
//        }
    //    }


    