/*
* Popup window script 
*  
* Example markup 
* <a href="mypage.html" onclick="popUpWin(this.href,'console',660,520);return false;" title="Open popup window">Link text</a>
*/

var newWindow = null;
function closeWin() {
    if (newWindow != null) {
        if (!newWindow.closed)
            newWindow.close();
    }
}



rateRecipe = function(url, rec, member, rating) {
    $.get(url + '?MEMID=' + member + '&RECID=' + rec + '&RATING=' + rating);
    $('#rating-spoons').get(0).innerHTML = '';
    $('#rating-spoons').attr('class', 'rating-spoons-' + rating);
    $('#rating-text').get(0).innerHTML = 'Thanks!';
}

function popUpWin(url, type, strWidth, strHeight) {
    closeWin();
    if (type == "fullScreen") {
        strWidth = screen.availWidth - 10;
        strHeight = screen.availHeight - 160;
    }
    var tools = "";
    if (type == "standard" || type == "fullScreen") tools = "resizable,toolbar=yes,location=yes,scrollbars=yes,menubar=yes,width=" + strWidth + ",height=" + strHeight + ",top=0,left=0";
    if (type == "console") tools = "resizable,toolbar=no,location=no,scrollbars=no,width=" + strWidth + ",height=" + strHeight + ",left=0,top=0";
    newWindow = window.open(url, 'newWin', tools);
    newWindow.focus();
}


function ShowSection(parentElement, selectedElement) {
    alert(parentElement + " " + selectedElement.value);

    // hide all child div's inside section_area div 
    $("#" + parentElement + " > div").hide();
    // show the element	
    $("#" + selectedElement.value).show();

}

/*
* Unobtrusive external links 
*  
* Example markup 
* <a href="mypage.html" rel="external" title="Take me to my link">Link text</a>
*/

$(document).ready(function() {
    $("a[rel=newWin]").click(function() {
        window.open($(this).attr('href'));
        return false;
    });
});


$(document).ready(function() {
    var currentLink = $("#lnkSendToAFriend").attr('href');
    $("#lnkSendToAFriend").attr('href', currentLink + '?page=' + document.location);
    $("#lnkSendToAFriendRecipeDetail").attr('href', currentLink + '?page=' + document.location);
});



$(document).ready(function() {
    $(".pic-tools").hide();

    $(".pic-holder").hover(function() {
        $(this).children('ul').fadeIn("normal");
    }, function() {
        $(this).children('ul').fadeOut("normal");
    });
});



// Form Validation
//browser detection
var strUserAgent = navigator.userAgent.toLowerCase();
var isIE = strUserAgent.indexOf("msie") > -1;
var isNS6 = strUserAgent.indexOf("netscape6") > -1;
var isNS4 = !isIE && !isNS6 && parseFloat(navigator.appVersion) < 5;

function GetMyObjectByID(itemID) {
    // In the future will need to build in the ability to handle v4 netscape browsers.
    if (document.getElementById) // Good browsers
        return document.getElementById(itemID)
    else if (document.all) // medium browsers
        return document.all[itemID];

    // Need to handle v4 browsers in the future.
}
function CheckField(itemID) {
    var o = GetMyObjectByID(itemID);
    if (o.value.length < 1)
        return false;
    else
        return true;
}

function CheckDD(itemID) {
    var o = GetMyObjectByID(itemID);
    var select = o.options[o.selectedIndex].value;

    if (select.length < 1 || select == "0")
        return false;
    else
        return true;
}

function CheckCheckbox(itemID) {
    var o = GetMyObjectByID(itemID);

    return (o.checked);
}

function CheckRadio(FormName, itemID) {
    //var o = GetMyObjectByID(itemID);
    var o = document.forms[FormName][itemID];

    var cnt = -1;
    for (var i = 0; i < o.length; i++) {
        if (o[i].checked) {
            cnt = i;
            i = o.length;
        }
    }
    if (cnt > -1)
        return true;
    else
        return false;
}

function CheckEmail(itemID) {
    var o = GetMyObjectByID(itemID);
    if (o != null) {
        // Make sure we let an email go through that is empty... The required field catcher will get that
        if (o.value.length < 1) {
            return false;
        }
        var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
        if (!regex.test(o.value)) {
            return false;
        }
        else {
            return true;
        }
    }
}

function isSpecialKey(strValue) {
    // Returns true if one of these special key codes is pressed
    //backspace Ctrl + C,enter, Ctrl + X, Ctrl + V, Ctrl + Z
    var reKeyboardChars = /[\x00\x03\x08\x0D\x16\x18\x1A]/;
    return reKeyboardChars.test(strValue);
}

/*
To filter the max length here is some example code 
to catch both key down and to catch paste events
onkeypress="return FilterMaxLength(event,this,250)" onpaste="return CheckPaste(this,250)"
*/
function FilterMaxLength(e, o, max) {
    // we must allow spaces , backspace etc to go through 
    var iKeyCode, strKey;

    if (isIE) {
        iKeyCode = e.keyCode;
    } else {
        iKeyCode = e.which;
    }
    strKey = String.fromCharCode(iKeyCode);
    // Make sure we let special keys go through.
    if (isSpecialKey(strKey))
        return true;
    var maxLength = max;
    if (o.value.length > maxLength - 1) {
        alert('You are only able to make a maximum payment of $99,999. Please reduce the amount and try again');
        return false;
    }
    return true
}
function CheckPaste(o, max) {
    var maxLength = max;
    // Get from clipboard
    var data = window.clipboardData.getData("Text");

    var CurrentLength = o.value.length + data.length;
    if (CurrentLength > maxLength) {
        alert('You are only able to make a maximum payment of $99,999. Please reduce the amount and try again');
        return false;
    }
    else
        return true;
}
//mask input to allow integer only
function CheckNumeric(objEvent) {
    var iKeyCode, strKey;
    var reValidChars = /\d/;
    var reKeyboardChars = /[\x00\x03\x08\x0D\x16\x18\x1A]/;
    if (isIE) {
        iKeyCode = objEvent.keyCode;
    } else {
        iKeyCode = objEvent.which;
    }

    strKey = String.fromCharCode(iKeyCode);

    if (!reValidChars.test(strKey) && !reKeyboardChars.test(strKey)) {
        return false;
    }
}



function JumpTo(e) {
    var s = e.options[e.selectedIndex].value;
    document.location.href = "#" + s;
}

//Retrieves Querystring Value
function gup(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return results[1];
}
