﻿// Click correct button on enter. Primarily for non-Microsoft browsers.
// Used on public pages that have multiple form areas and search box.
function entersubmit(id, argument, e)
{
	var code;
	if (!e) var e = window.event;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	if (code == 13)
	{
		__doPostBack(id,argument);
		e.cancelBubble = true;
		if (e.stopPropagation) e.stopPropagation();
	}
}

// Click correct button on enter. Primarily for non-Microsoft browsers.
// Used on public pages that have multiple form areas and search box.
function enternosubmit(e)
{
	var code;
	if (!e) var e = window.event;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	if (code == 13)
	{
		e.cancelBubble = true;
		if (e.stopPropagation) e.stopPropagation();
	}
}

// Function used to stop the input of < and > on all input pages.
function safeinput(e)
{
	var code;
	if (!e) var e = window.event;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	if (code == 60 || code == 62)
	    return false;
}

// Set the text size
function setPref(name, value)
{
    var cookieValue = ""; // to hold the cookie value
    if (readCookie("PREF"))
    {
        var pref = unescape(readCookie("PREF"));
        var prefSplit = pref.split("&");
        for (var i=0; i<prefSplit.length; i++) // keep whatever was there already
        {
            var vp = prefSplit[i].split("="); // name-vlaue pair
            if (vp[0] != name) // existing value is different
            {
                cookieValue += vp[0] + "=" + vp[1] + "&";
            }
        }
    }
    // Now add the requested name value pair
    cookieValue = cookieValue + name + "=" + value;
    setPrefCookie("PREF", cookieValue, new Date("December 31, 2010 23:59:59"),"/");
    location.reload();
}

// Read specific preference from PREFS cookie
function readPrefs(name)
{
    if (!readCookie("PREF"))
        return "x";
	var prefs = unescape(readCookie("PREF"));
    var prefSplit = prefs.split("&");
    for (var i=0; i<prefSplit.length; i++) // Step through each name-value pair
    {
        var vp = prefSplit[i].split("="); // name-vlaue pair
        if (vp[0] == name) // this was requested
        {
            return vp[1];
        }
    }
    return "x";
}

// Set cookie
function setPrefCookie(name, value, expires, path, domain, secure) {
    var curCookie = name + "=" + value +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
    document.cookie = curCookie;
}

// Read cookie
function readCookie(name)
{
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++)
    {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

// Pre-load the hover images
function preload (path)
{
    var image_url = new Array();
    image_url[0] = path + "images/go-1b.gif";
    image_url[1] = path + "images/go-2b.gif";
    image_url[2] = path + "images/go-3b.gif";
    image_url[3] = path + "images/close-3b.gif";
    image_url[4] = path + "images/back-3b.gif";
    image_url[5] = path + "images/a_1.gif";
    image_url[6] = path + "images/a_2.gif";
    image_url[7] = path + "images/a_3.gif";
    image_url[8] = path + "images/w_1.gif";
    image_url[9] = path + "images/w_2.gif";
    image_url[10] = path + "images/w_3.gif";
    image_url[11] = path + "images/expand-3b.png";
    image_url[12] = path + "images/close-4b.gif";

    var i = 0;
    for (i=0; i<image_url.length; i++) 
    {
         var preload_image_object = new Image();
         preload_image_object.src = image_url[i];
    }
}

// Set the correct number of messages
function links ()
{
    // Read cookie
    var whole = readCookie("GROP");
    var cp = null;
    var cu = null;
    var me = '0';
    
    if (whole != null)
    {
        var pairs = whole.split("&");
        for (i=0; i<pairs.length; i++)
        {
            ind = pairs[i].split("=");
            switch (ind[0])
            {
                case "me":
                    me = ind[1];
                    break;
                case "cp":
                    cp = ind[1];
                    break;
                case "cu":
                    cu = ind[1];
                    break;
                default:
                    break;
            }
        }
        
        // Set the correct new message count
        if (returnObjById('messages'))
            returnObjById('messages').firstChild.nodeValue = "New Messages: " + me;
        
        // Set the URL for current unit
        switch (cp)
        {
            case "1":
                cp = "de";
                break;
            case "2":
                cp = "sm";
                break;
            case "3":
                cp = "tm";
                break;
            case "4":
                cp = "qs";
                break;
            default:
                //Nothing has started, leave string empty
                break;
        }
        if (cp != null && cu != null && cu != "0")
        {
            if (returnObjById('unitlink'))
                returnObjById('unitlink').href = "http://www.growerz.com/members/program_" + cp + "/unit" + cu.padLeft(2, '0') + ".aspx";
        }    
    }
}

// Return the string padded to the left with the requested character.
String.prototype.padLeft = function(nLength, cChar)
{
    if( this.length < nLength )
    {
        var nExtraLen = nLength - this.length;
        var cTemp = "";
        for( var i = 0; i < nExtraLen; i++ )
        {
            cTemp += cChar;
        }
        cTemp += this;
    }
    else
    {
        cTemp = this;
    }
    return cTemp;
}

// Get elements by class name instead of ID.
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

// Cross browser compatible "get element" function.
function returnObjById(id)
{
    if (document.getElementById)
        var returnVar = document.getElementById(id);
    else if (document.all)
        var returnVar = document.all[id];
    else if (document.layers)
        var returnVar = document.layers[id];
    return returnVar;
}

// Focus on specified element
function focus(id)
{
    if (returnObjById(id))
    {
	    returnObjById(id).focus();
	}
}

// Shows the bubble text.
function bubble (say, cursor)
{
    // Set the correct text
    returnObjById('bubblewords').firstChild.nodeValue = say;
    
    // position the popup window
    cursor[0] = cursor[0] + 30;
    cursor[1] = cursor[1] - 55;
    returnObjById('bubble').style.left = cursor[0] + 'px';
    returnObjById('bubble').style.top = cursor[1] + 'px';
 
    // Make it visible
    returnObjById('bubble').style.display = "block";
}

// Hides the bubble text.
function closebubble()
{
    returnObjById('bubble').style.display = "none";
}

// Find position of element. NOT CURRENTLY USED!
function findPos(obj)
{
    var curleft = curtop = 0;
    if (obj.offsetParent)
    {
        curleft = obj.offsetLeft
        curtop = obj.offsetTop
        while (1)
        {
            if (!obj.offsetParent)
            {
                break;
            }
            obj = obj.offsetParent;
        }
    }
    else if (obj.x)
    {
        curleft += obj.x;
        curtop += obj.y;
    }
    return [curleft,curtop];
}

// Find position of cursor
function getCursor(e) {
     if (!e) var e = window.event;
     var x = y = 0;
     if (e.pageX || e.pageY) {
         x = e.pageX;
         y = e.pageY;
     }
     else {
         x = e.clientX +
             (document.documentElement.scrollLeft ||
             document.body.scrollLeft) -
             document.documentElement.clientLeft;
         y = e.clientY +
             (document.documentElement.scrollTop ||
             document.body.scrollTop) -
             document.documentElement.clientTop;
     }
     return [x,y];
}

// Mute the status bar
function showStatus(text)
{
    window.status=text;
    return true;
}

// Move an element directly on top of another element (and optionally
// make it the same size)
function Cover(bottom, top, ignoreSize)
{
    var location = Sys.UI.DomElement.getLocation(returnObjById(bottom));
//    returnObjById(top).style.position = 'absolute';
    returnObjById(top).style.top = location.y + 'px';
    returnObjById(top).style.left = location.x + 'px';
    if (!ignoreSize)
    {
        returnObjById(top).style.height = returnObjById(bottom).offsetHeight + 'px';
        returnObjById(top).style.width = returnObjById(bottom).offsetWidth + 'px';
    }
}

// Add a number of days to a date.
function addDays(myDate, days)
{
    return new Date(myDate.getTime() + days*24*60*60*1000);
}

// Function to return selected text
function getSelText()
{
    var txt = '';
    if (window.getSelection)
        txt = window.getSelection();
    else if (document.getSelection)
        txt = document.getSelection();
    else if (document.selection)
        txt = document.selection.createRange().text;
    return txt;
}

// Function to populate value of FCK editor
function populateEditor()
{
	this.UpdateEditorFormValue = function()
	{
		for ( i = 0; i < parent.frames.length; ++i )
			if ( parent.frames[i].FCK )
				parent.frames[i].FCK.UpdateLinkedField();
	}
}
