﻿
// add to favorites
function Add_Bookmark_Favorite(url, title) {
    if (window.sidebar) { // Mozilla Firefox Bookmark
        //Make sure "Load this bookmark in the sidebar is deselected
        window.sidebar.addPanel(title, url, "");
        return false;
    }
    else if (window.external) { // IE Favorites
        window.external.AddFavorite(url, title);
        return false;
    }
    else if (window.opera && window.print) { // Opera Bookmark
        return !addToFav(url, title)
    }
}


//function prints received content
function printContent(id1, window_width) {

    // str= '<b>' + document.getElementById(title).innerHTML + '</b><br><br>' + document.getElementById(id1).innerHTML + '<br><br>' + document.getElementById(id2).innerHTML

    str = document.getElementById(id1).innerHTML

    newwin = window.open('', 'printwin', 'left=300,top=100,width=' + window_width + ',height=700')

    newwin.document.write('<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">\n <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n<HEAD>\n')

    newwin.document.write('<LINK REL="Stylesheet" HREF="CSS/css.css" TYPE="text/css">')

    newwin.document.write('<TITLE></TITLE>\n')

    newwin.document.write('<script>\n')

    newwin.document.write('function chkstate(){\n')

    newwin.document.write('if(document.readyState=="complete"){\n')

    newwin.document.write('window.close()\n')

    newwin.document.write('}\n')

    newwin.document.write('else{\n')

    newwin.document.write('setTimeout("chkstate()",3000)\n')

    newwin.document.write('}\n')

    newwin.document.write('}\n')

    newwin.document.write('function print_win(){\n')

    newwin.document.write('window.print();\n')

    newwin.document.write('chkstate();\n')

    newwin.document.write('}\n')

    newwin.document.write('<\/script>\n')

    newwin.document.write('</HEAD>\n')

    newwin.document.write('<BODY onload="print_win()">\n')
   // newwin.document.write('<BODY style="margin:0; padding:0;" >\n')

    newwin.document.write('<div style="margin:10px auto; direction:rtl; text-align:center; width:90%;"><div id="PrintContent" style="text-align:right; font-size:12px;">' + str + '</div></div>')

    newwin.document.write('</BODY>\n')

    newwin.document.write('</HTML>\n')

    newwin.document.close()

}







//function prints received content
function printContent2(id1, id2, window_width) {

    // str= '<b>' + document.getElementById(title).innerHTML + '</b><br><br>' + document.getElementById(id1).innerHTML + '<br><br>' + document.getElementById(id2).innerHTML

    str = document.getElementById(id1).innerHTML + document.getElementById(id2).innerHTML

    newwin = window.open('', 'printwin', 'left=300,top=100,width=' + window_width + ',height=700')

    newwin.document.write('<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">\n <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n<HEAD>\n')

    newwin.document.write('<LINK REL="Stylesheet" HREF="CSS/css.css" TYPE="text/css">')

    newwin.document.write('<TITLE></TITLE>\n')

    newwin.document.write('<script>\n')

    newwin.document.write('function chkstate(){\n')

    newwin.document.write('if(document.readyState=="complete"){\n')

    newwin.document.write('window.close()\n')

    newwin.document.write('}\n')

    newwin.document.write('else{\n')

    newwin.document.write('setTimeout("chkstate()",3000)\n')

    newwin.document.write('}\n')

    newwin.document.write('}\n')

    newwin.document.write('function print_win(){\n')

    newwin.document.write('window.print();\n')

    newwin.document.write('chkstate();\n')

    newwin.document.write('}\n')

    newwin.document.write('<\/script>\n')

    newwin.document.write('</HEAD>\n')

    newwin.document.write('<BODY onload="print_win()">\n')
    // newwin.document.write('<BODY style="margin:0; padding:0;" >\n')

    newwin.document.write('<div style="margin:10px auto; direction:rtl; text-align:center; width:90%;"><div id="PrintContent" style="text-align:right; font-size:12px;">' + str + '</div></div>')

    newwin.document.write('</BODY>\n')

    newwin.document.write('</HTML>\n')

    newwin.document.close()

}










// receives string, checks for actual value
// return false for "", null and empty spaces " "
function isStringEmpty(strValue) {
    var _strValue = strValue;
    var bool_isEmpty = true;

    switch (_strValue) {
        case "":
            break;
        case null:
            break;
        default:
            _strValue = (escape(_strValue))
            _strValue = _strValue.replace(/%20/g, ''); //replaces all " "
            if (_strValue != "")
                bool_isEmpty = false;
            break;
    }

    return bool_isEmpty;
}

// submits search
function SearchSubmit(inputId) {
    var strSearchValue = document.getElementById(inputId).value;
    if (isStringEmpty(strSearchValue) || strSearchValue == 'חיפוש') {
        alert("אנא הזן ערך לחיפוש");
        return false;
    }

    var strUrl = "default.aspx?pageid=22&q=" + escape(strSearchValue);
    window.location.href = strUrl;
    return false;
}

// submits community search
function SearchCommunity(inputId) {
    var strSearchValue = document.getElementById(inputId).value;
    if (isStringEmpty(strSearchValue)) {
        alert("אנא הזן ערך לחיפוש");
        document.getElementById(inputId).focus();
        return false;
    }

    var strUrl = "default.aspx?pageid=10&q=" + escape(strSearchValue);
    window.location.href = strUrl;
    return false;
}


// checks numeric value
function IsNumeric(inputVal) 
{
     if (isNaN(parseFloat(inputVal)))
          return false;

     return true;
}


// submits recipes search
function RecipesSearchSubmit(inputId, listboxID) {
    var strSearchValue = document.getElementById(inputId).value;
    // categoriesListBox
    var listBox = document.getElementById(listboxID);
    var iCategory = -1;
    if (IsNumeric(listBox.value))
        iCategory = listBox.value;
    
    if (isStringEmpty(strSearchValue) || strSearchValue == 'מילות חיפוש') {
       // alert("אנא הזן ערך לחיפוש");
        // return false;
       
    }

    var strUrl = "default.aspx?pageid=15&recipeQ=" + escape(strSearchValue) + "&categoryID=" + iCategory;
    window.location.href = strUrl;
    return false;
}

// newsletter
function newsletterSignBarak(inputId) {
    var strEmailValue = document.getElementById(inputId).value;
    
    if (isStringEmpty(strEmailValue) || strEmailValue == 'הזן מייל') {
        alert("אנא הזן כתובת מייל");
        document.getElementById(inputId).focus();
        return false;
    }

    // validate format
    if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(strEmailValue)) {
        //proper format -  do nothing      
    }
    else {
        alert("אנא הזן כתובת מייל תקינה");
        document.getElementById(inputId).focus();
        return false;
    }

    return true; // if reached here, success
}


function ValidateForm(strFormName) {
    var form_name = document.getElementById(strFormName);
    var form_elements = new Array();
    form_elements = form_name.elements;
    var form_field
    // iterates through form elements
    for (i = 0; i < form_elements.length; i++) {
        //alert(form_elements[i].name + " = " + form_elements[i].value);
        // ignore clear button and submit values
        if (form_elements[i].type == "button" || form_elements[i].type == "submit") {
            //break;
        }
        else {
            // alert(form_elements[i].name + " = " + form_elements[i].value);
            form_field = form_elements[i];

            // if mandatory field
            if (form_elements[i].getAttribute("mandatory_field") == "1") {
                // if textbox or textarea empty
                if (isStringEmpty(form_elements[i].value) || form_elements[i].type == "checkbox" && form_elements[i].checked == false) {
                    alert_msg = form_elements[i].getAttribute("mandatory_msg");
                    if (alert_msg == undefined) {
                        alert_msg = "Mandatory field"
                    }
                    alert(alert_msg);
                    form_field.focus();
                    return false;
                }
            }

            // if password
            if (form_elements[i].getAttribute("password_field") == "1") {
                if (!confirmPassword(form_elements[i].id, form_elements[i].getAttribute("otherPassInputID"))) {
                    alert_msg = form_elements[i].getAttribute("confirm_msg");
                    alert(alert_msg);
                    form_field.focus();
                    return false;
                }

                if (!validatePassword(form_elements[i].id, form_elements[i].getAttribute("otherPassInputID"))) {
                    alert_msg = form_elements[i].getAttribute("password_msg");
                    alert(alert_msg);
                    form_field.focus();
                    return false;
                }
            }

            // if numeric
            if (form_elements[i].getAttribute("numeric_field") == "1") {
                if (!IsNumeric(form_elements[i].value)) {
                    alert_msg = form_elements[i].getAttribute("numeric_msg");
                    if (alert_msg == undefined) {
                        alert_msg = "Numeric values only"
                    }
                    alert(alert_msg);
                    form_field.focus();
                    return false;
                }


                // if min value
                if (form_elements[i].getAttribute("min_numeric") != "" && form_elements[i].getAttribute("min_numeric") != null) {
                    if (parseInt(form_elements[i].value) < parseInt(form_elements[i].getAttribute("min_numeric"))) {
                        alert_msg = form_elements[i].getAttribute("min_numeric_msg");
                        if (alert_msg == undefined) {
                            alert_msg = "Value not in proper range"
                        }
                        alert(alert_msg);
                        form_field.focus();
                        return false;
                    }
                }
                
            }
            
            

            // if email - test format
            if (form_elements[i].getAttribute("email_field") == "1") {
                if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(form_elements[i].value)) {
                    //proper format -  do nothing      
                }
                else {
                    alert_msg = form_elements[i].getAttribute("email_msg");
                    if (alert_msg == undefined) {
                        alert_msg = "Invalid email";
                    }
                    alert(alert_msg);
                    form_field.focus();
                    return false;
                }
            }
        } // end if buttons
    }


    // if reached here successfully, submit
    return true;
}


// mouseover on recipe image + text
function mouseoverRecipe(divId, strColor, op) {
    var recipeDiv = document.getElementById(divId);
    var imgDiv = getFirstChild(recipeDiv);
    var textDiv = getNextSibling(imgDiv);

    if (op == 1) {
        opacityOnThis(imgDiv.id);
        colorThis(textDiv.id, strColor);
    }
    else {
        removeOpacity(imgDiv.id);
        colorThis(textDiv.id, '');
    }
}


// styles with opacity
function opacityOnThis(id) {
    document.getElementById(id).style.opacity = 0.7;
    document.getElementById(id).style.filter = 'alpha(opacity = 70)';
}

function removeOpacity(id) {
    document.getElementById(id).style.opacity = 1.0;
    document.getElementById(id).style.filter = 'alpha(opacity = 100)';
}

function colorThis(id, strColor) {
    document.getElementById(id).style.color=strColor;
}



// corrects difference between browsers IE and FF
// equivalent to div.firstChild
function getFirstChild(elm) {
    if (!elm.childNodes.length) {
        return;
    }
    var children = elm.childNodes.length;
    for (var i = 0; i <= children; ++i) {
        if (elm.childNodes[i].nodeType == 1) {
            return elm.childNodes[i];
        }
    }
    return;
}

// equivalent to div.nextSibling
function getNextSibling(startBrother) {
    endBrother = startBrother.nextSibling;
    while (endBrother.nodeType != 1) {
        endBrother = endBrother.nextSibling;
    }
    return endBrother;
}


function confirmPassword(passInputId, confirmInputID) {
    var strPassword = document.getElementById(passInputId).value;
    var strConfirmPassword = document.getElementById(confirmInputID).value;

    if (strPassword != strConfirmPassword)
        return false;

    return true;

}

function validatePassword(passInputId, confirmInputID) {
    var strPassword = document.getElementById(passInputId).value;
    var strConfirmPassword = document.getElementById(confirmInputID).value;

    // count 6 chars
    if (strPassword.length < 6)
        return false;

    return true;
}

function validateCheckbox(checkboxID) {
    if (document.getElementById(checkboxID).checked == false) {
        alert("יש לאשר את תנאי התקנון");
        document.getElementById(checkboxID).focus();
        return false;
    }

    return true;
}

function alertConfirm(strAlert) {
    if (confirm(strAlert))
        return true;

    return false;
}

function OpenWindow(URL, width, height) {
    popUpWindow = window.open(URL, "", "menubar=0,scrollbars=0,directories=0,location=0,top=210,left=200,toolbar=0,status=0,titlebar=0,width=" + width +",height=" + height);
    popUpWindow.focus();
}


// cache images
var leftBlueImg = new Image();
leftBlueImg.src = 'images/personalBlueLeft.png';

var rightBlueImg = new Image();
rightBlueImg.src = 'images/personalRightBlue.png';

var leftGreyImg = new Image();
leftGreyImg.src = 'images/personalGrayLeft.png';

var rightGreyImg = new Image();
rightGreyImg.src = 'images/personalGrayRight1.png';

function toggleAddTalkbackDisplay() {
    // if talkbacks are displayes, display form
    if (document.getElementById('talkbacksContainerDiv').style.display == 'block') {
        $('#talkbacksContainerDiv').css('display', 'none');
        $('#addTalkbackFormDiv').css('display', 'block');
        $('#addTalkbackTd').html("בחזרה לתגובות");
    }
    else {
        $('#talkbacksContainerDiv').css('display', 'block');
        $('#addTalkbackFormDiv').css('display', 'none');
        $('#addTalkbackTd').html("הוספת תגובה");
    }
}

// toggles tips at the bottom of menu
function toggleMenuTipsDisplay(dataItemIndex) {
    $('.grayMenu').removeClass('grayMenu').addClass('blueMenu'); // change all gray back to blue
    $('#menuTipsdiv' + dataItemIndex).removeClass('blueMenu').addClass('grayMenu'); // change selected to blue
    $('#tipsTextHere').html($('#menuHiddenText' + dataItemIndex).html());
}

// validates checkedbox
function validateCheckbox(checkboxID) {
    if (document.getElementById(checkboxID).checked != true) {
        alert('יש לאשר את תנאי התקנון');
        document.getElementById(checkboxID).focus();
        return false;
    }

    return true;
}
