﻿
function get_cookie(Name,key) {
        var search = Name + "="
        var returnvalue = "";
        var returnvalue1 = "";
        if (document.cookie.length > 0) {
            offset = document.cookie.indexOf(search)
            // if cookie exists
            if (offset != -1) { 
            offset += search.length
            // set index of beginning of value
            end = document.cookie.indexOf(";", offset);
            // set index of end of cookie value
            if (end == -1) end = document.cookie.length;
                returnvalue=unescape(document.cookie.substring(offset, end))
            }
        }
        if (returnvalue.length > 0) {
            //alert(returnvalue)
            returnvalue1 = get_cookiekey(returnvalue,key)
        }
        return returnvalue1;
    }
    
    function get_cookiekey(str, check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	//alert(str)
	var a_all_cookies = str.split("&"); //document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f
	
	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );
		
		
		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
	
		// if the extracted name matches passed check_name
		//alert(cookie_name +"=="+ check_name);
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}

//==================================================================
//cookie doesn't need to exist,
//this function creates new cookie and ads a key/value if cookie doesn't already exist
//Updates changes to existing key/value if exists
//Adds new key/value if cookie exists but not the key
//in conjuction with: eraseCookie AND createCookie
function setCookieWithKeys(cookieName, key, value) {
    //alert("setCookieWithKeys " + cookieName + "/" + key + "/" + value);
    var amp = '';
    var search = cookieName + "=";
    var returnCookie = "";
    var cookieString = document.cookie
    if (document.cookie.length > 0) {
        offset = document.cookie.indexOf(search)
        // if cookie exists
        if (offset != -1) {
            offset += search.length
            // set index of beginning of value
            end = document.cookie.indexOf(";", offset);
            // set index of end of cookie value
            if (end == -1) end = document.cookie.length;
            returnCookie = unescape(document.cookie.substring(offset, end))
        }
    }

    //if no cookie, add and return
    if (returnCookie.length < 1) {
        returnCookie += amp + key + '=' + value;
        eraseCookie(cookieName);
        createCookie(cookieName, returnCookie, .2);
        return;
    }    
  
    search = key + '=';
    var startKey = returnCookie.indexOf(search);
    if (startKey > -1) {
        // set index of end of the key=value pair
        end = returnCookie.indexOf("&", startKey);
        // item may be last in the string
        if (end == -1) end = returnCookie.length;        
        var keyString = returnCookie.substring(startKey, end);
        //take out the old key=value;       
        returnCookie = returnCookie.replace(keyString, key + '=' + value);       
        eraseCookie(cookieName);
        createCookie(cookieName, returnCookie, .2);
        return;
    }

    //if any other keys exist then apend new key to the end.
    if (returnCookie.length > 0) amp = '&';
    //add new key=value pair
    returnCookie += amp + key + '=' + value;    
    eraseCookie(cookieName);
    createCookie(cookieName, returnCookie, .2);
}
//===========
function eraseCookie(name) {
    createCookie(name, "", -1);
}
//===========
function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + ";";
}
//_________________________________________________________________


//==================================================================
 function hidediv(id) {
	//alert("HideDiv " + id);
	//safe function to hide an element with a specified id
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'none';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'none';
		}
		else { // IE 4
			document.all.id.style.display = 'none';
		}
	}
}

function showdiv(id, controlVal) {

//safe function to show an element with a specified id
//alert("showdiv " + id);
	//stop the function unless they call for an edit box
if(controlVal != undefined)
{
	if(controlVal.indexOf('Edit') < 0)
	{
    //	alert("Edit/Add New");
	hidediv(id)
	    return false;
	}
}	
	if (document.getElementById) 
	{ // DOM3 = IE5, NS6
	
		document.getElementById(id).style.display = 'block';
	}
	else {
		if (document.layers) 
		{ // Netscape 4
			document.id.display = 'block';
		}
		else 
		{ // IE 4
			document.all.id.style.display = 'block';
		}
	}
}




//==================================================================
//disable the enter key
 function checkCR(evt) {

    var evt  = (evt) ? evt : ((event) ? event : null);
    
    var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);

    if ((evt.keyCode == 13) && (node.type=="text" || node.type=="radio")) {return false;}
  }

  document.onkeypress = checkCR;
 
 //==================================================================
 //check for ie6 and hide dropdowns which interfere with the edit form
 BrowserTest()
 function BrowserTest()
 {  var version;
    var agt=navigator.userAgent.toLowerCase();    
    var is_major = parseInt(navigator.appVersion);
      var is_ie     = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
       var is_ie6    = (is_ie && (is_major == 4) && (agt.indexOf("msie 6.")!=-1));
      //      alert(is_ie6);
      //return true;
    return is_ie6;
 }
 

   var totalTextBoxCount=0;
   var ddlArray;
   var txtArray;
   var isVisible = true;
      
   //called from the main form for Aerial, Chemical, Fertilizer, manure,
   //not yet needed on the custom invoice, scouting
   function MakeTextBoxes()
   {//alert("Make");
       if(!BrowserTest())
       {
        //if its not ie6 do nothing
        return false;
       }

       //set up textboxes and hide them to use later.
       var form = document.getElementById("form1");
       var counter = 0;
       ddlArray= new Array;
       txtArray = new Array;

            for (var i=0; i < form.elements.length; i++) 
            {
                var element = form.elements[i];
                if(element.type.indexOf('select') != -1)
                {
                counter++;               
                    //adjust settings from the dropdown lists.
                    sWidth = getInt(element.style.width)-5;
                    sHeight = getInt(element.style.height)-5;
                    sTop = getInt(element.style.top);
                    sLeft = getInt(element.style.left);               
                    var strStyle = "top:" + sTop + "px; left:" + sLeft + "px; height:" + sHeight + "px; " +
                          "width: " + sWidth + "px; position:absolute;"
                    //add textboxes to the form
                    document.write("<input type='text' id='txtTemp" + counter + "' style='" + strStyle + "' class='input'>");

                    txtArray[counter] = "txtTemp" + counter;
                    ddlArray[counter] = element.id;
                 }
            }
            totalTextBoxCount = counter;
        //alert(totalTextBoxCount);
   }
   
//this is wired up to ajax functions and fires after partial page load
function pageLoad()
{
    setVisibility(isVisible);
}

//value is optional and not used on close buttons
//sent the global variable and use in the pageload() function
function ShowHideDropDowns(visibility, value, control, index)
   {      
   //alert(value);
    //run if value of dropdown is edit and broser test finds ie6
    try{
        var ContainsEdit = value.indexOf("Edit");
    }catch(e){
    }    
    
    if(((ContainsEdit < 0) && (value != undefined)) || !BrowserTest())
        { 
          return false;
        }
    isVisible = visibility; 
    setVisibility(visibility);    
   }

function setVisibility(visibility)
{
//alert(visibility);
if(!BrowserTest())
   {
    return false;
   }
        var setting;
        var x;
       
       if(visibility)
       {    //show dropdowns
            for(x in txtArray)
               {
                    document.getElementById(txtArray[x]).style.visibility = "hidden";
                    document.getElementById(ddlArray[x]).style.visibility = "visible";
               }
              //alert("visible? " + document.getElementById(ddlArray[x]).style.visibility);
       }
       else
       {
           for(x in txtArray)
               {//hide dropdowns and replace with text boxes
               var ddl = document.getElementById(ddlArray[x]);
               var txt = document.getElementById(txtArray[x]);
               txt.value = ddl[ddl.selectedIndex].text;
                    
                    ddl.style.visibility = "hidden";
                    txt.style.visibility = "visible";                    
               }
        }
} 
   //helper function to get rid of the "px" so the setting can be 
   //adjusted mathmatically
   function getInt(strAttribute)
   {
        return strAttribute.replace("px","");
   }


//sometimes the control will have an index number appended to the id,
//this function is used to strip that number so the contorl array can be 
//itterated over in another function buy using the numberofcontrols
function GetBaseName(id)
{
    var x;
    var BaseID = "";
    var len = 0;
   
   try{
    len = id.length;
    }
    catch(e)
   {}
   
    for (x = 0; x < len; x++)
    {
       if ( isNaN(id.charAt(x)))
       {
            BaseID += id.charAt(x);
       } 
    }
    return BaseID;
}


