//This function will make the necessary Javascripts run at OnLoad
_spBodyOnLoadFunctionNames.push('DetectDesignMode', 'setNewHoverExpandJSCode');

/*----Javascript for THE BLOCK----*/
function DetectDesignMode() 
{
	if (IsInDesignMode())
	{
		PushWebPartZonesDown();
	}
}

function IsInDesignMode()
{
   if(document.getElementById("MSOLayout_InDesignMode").value == '1') return true;
   if(document.getElementById("MSOTlPn_SelectedWpId").value != "") return true;
   return false;
}

//MJTODD
//The BLOCK is a DIV on the ASDK MasterPage, which is responsible for
//pushing all the webpart zones down a bit, so that the user is able to interact
//with them. Due to the layout and z-indexing going on, normally you would be unable
//to interact with the webparts due to being covered up.
function PushWebPartZonesDown() {
	var theBlock = document.getElementById('theBlock');
	if (theBlock != null) {
		theBlock.style.height = '75px';
		theBlock.style.display = 'block';
	}
}
/*----End Javascript for THE BLOCK----*/

/*----Javascript responsible for custom searching----*/
function submitIntranetSearchQuery(ev){
	if(ev.keyCode == 13){
		var searchQuery = document.getElementById('query').value;
		window.location.href = "/pages/searchresults.aspx?k="+escape(searchQuery)+"";
		return false;
	}
	else{
		return true;
	}
}

function MonsantoSearchEngine(){
	var searchQuery = document.getElementById('query').value;
	document.location = "/pages/search.aspx?k="+escape(searchQuery)+"";
}
/*----End custom searching javascript----*/

/*----Javascript for Overriding the hover actions of the top navigation----*/		    
//Function to create overrides of currently existing functions
function setNewHoverExpandJSCode() {
	SetupInterceptors();
}

    var fw_Menu_Unhover;
    var fw_Menu_HoverDynamic;

	function SetupInterceptors(){   // called by onload event
		fw_Menu_HoverDynamic = Menu_HoverDynamic;
		Menu_HoverDynamic = my_Menu_HoverDynamic;
		fw_Menu_Unhover = Menu_Unhover;
		Menu_Unhover = my_Menu_Unhover;
	}
     
	function my_Menu_HoverDynamic(item) {
		fw_Menu_HoverDynamic(item);
		var x = Menu_FindParentItem(item);
		if(x && x.tagName.toLowerCase() != "body")
			Menu_HoverRoot(x);
	}

	function my_Menu_Unhover(item) {
		fw_Menu_Unhover(item);
		var x = Menu_FindParentItem(item);
		if(x && x.tagName.toLowerCase() != "body")
			Menu_Unhover(x);
	}
/*----End Javascript for Overriding the hover actions of the top navigation----*/

/*----Seed Resource Guide JavaScript----*/
function sendUserToSRG() {
	window.location = "/products/Pages/Seed-Resource-Guide.aspx";
}
/*----End Seed Resource Guide JavaScript---*/

/*----IPR JavaScript----*/
function sendUserToIPR() {
	var theCrop = document.getElementById('lpd_select_crop').value;
	var theZip = document.getElementById('lpd_select_zip').value;
	if (validateUSZip(theZip)) {
		window.location = "/yield/Pages/ipr.aspx?CropID=" + theCrop + "&zip_code_input=" + theZip;
	}
}
/*----End IPR JavaScript----*/
/*----SRG Javascript----*/
function submit_findaseed(theId)
{
	var theProduct = document.getElementById(theId).value;
	if (theProduct != "")
	{
		navigateWithReferrer("/products/Pages/SRGResults.aspx?ProdName=" + theProduct);
	}
}
function navigateWithReferrer(url)
{
    var fakeLink = document.createElement ("a");
    if (typeof(fakeLink.click) == 'undefined')
        location.href = url;  // sends referrer in FF, not in IE
    else
    {
        fakeLink.href = url;
        document.body.appendChild(fakeLink);
        fakeLink.click();   // click() method defined in IE only
    }
}

/*----End SRG Javascript----*/
/*----HomePage JavaScript----*/
function UpdateZipOnHome(theId) {
	var theZip = document.getElementById(theId).value;
	if (validateUSZip(theZip)) {
		window.location = location.pathname + "?zip_code_input=" + theZip;
	}
}
/*----End IPR JavaScript---*/

/*----General Utilities----*/
function validateUSZip(strValue) {
	var objRegExp  = /(^\d{5}$)/;
	return objRegExp.test(strValue);
}
	
function clearTheValueOfThis(theObject) {
	theObject.value = "";
}

var focusID;
function setFocus(id) {
	focusID = id;
}

function submitEnter(buttonID,thisID, e)
{
	if (thisID = focusID)
	{
		var keycode;
		if (window.event) keycode = window.event.keyCode;
		else if (e) keycode = e.which;
		else return true;
		
		if (keycode == 13)
		   {
		   document.getElementById(buttonID).click();
		   return false;
		   }
		else
		   return true;
	}
}

/*----End General Utilities----*/

/*----GK JavaScript----*/
function UpdateZipOnGkPages(theId) {
	var theZip = document.getElementById(theId).value;
	if (validateUSZip(theZip)) {	
		window.location = location.pathname + "?zip_code_input=" + theZip;
	}
	else{
		var msg = "Invalid zip code. Try again.";
		document.getElementById('gkErrorDiv').innerHTML = msg;
	}
	
}
/*----End GK JavaScript---*/

/*----Start Cookie JavaScript---*/
function getCookie(cookieName, noCookieMsg){
    var c_start = document.cookie.indexOf(cookieName + "=");
    if (c_start != -1)
    {
	    c_start = c_start + cookieName.length + 1;
	    var c_end = document.cookie.indexOf(";", c_start);
	    if (c_end == -1) c_end = document.cookie.length;
	    var theCookie = unescape(document.cookie.substring(c_start, c_end));
	  
		if(theCookie != ""){ 
		  	return theCookie; 
	    }
	    else {
	      	return noCookieMsg; 
	    }
    }
    else
    {
    	return noCookieMsg;
    }
}

function setCookie(cookieName,cookieValue,nDays) {
 	var today = new Date();
 	var expire = new Date();
 	if (nDays==null || nDays==0) nDays=1;
    expire.setTime(today.getTime() + 3600000*24*nDays);
 	document.cookie = cookieName+"="+escape(cookieValue) + ";expires="+expire.toGMTString() + ";path=/";
 }


function deleteCookie( cName, value) {
	if (getCookie('CropId','no CropId') != null){
	 document.cookie = cName + "=" +value +";expires=Thu, 01-Jan-1970 00:00:01 GMT;path=/;";
	}
}


function translateCropId(cropID){
	if (cropID == 'SOYBEAN'){
		return 'GLXMA';
	}
	if (cropID == 'CORN'){
		return 'ZEAMD';
	}
	if (cropID == 'SILAGE'){
		return 'ZEASC';
	}
	if (cropID == 'GRAIN_SORGHUM'){
		return 'SORGR';
	}
	else {
		return cropID;
	}
}
/*----End Cookie JavaScript---*/

/*----Start Get Query String JavaScript---*/
function getQryString(qryParam){
  	var regexS = "[\\?&]"+qryParam+"=([^&#]*)";
  	var regex = new RegExp( regexS );
  	var results = regex.exec( window.location.href );
	return results;
  }
/*----End Get Query String JavaScript---*/


/*START CYC & SRG CheckBox Validations*/

function SelectProduct(obj)	{
         if(obj.checked==true){        
         		if( document.getElementById('RowCount').value >= 5 ){            
         				alert('Please do not select more than 5 products.');            
         				obj.checked=false;        
         			}        
         			else{            
         				document.getElementById( 'RowCount' ).value = parseInt( document.getElementById( 'RowCount' ).value ) + 1;        
         			}    
         	}    
         	else {   
         	document.getElementById( 'RowCount' ).value = parseInt( document.getElementById( 'RowCount' ).value) - 1;   
            }
	}
                
	function CompareProducts(){
            if( document.getElementById('RowCount').value <2 ){alert('You must select at least 2 products to compare.');return false;  }
         }
	function CheckProdCount(){
                if( document.getElementById('RowCount').value == 0 ){alert('You must select at least one product.');return false;}
    }
    
	function LoadContent(DivIds,Container){ 
          var DivArray = DivIds.split(';');
          var showdiv = document.getElementById(DivArray[0]);
          showdiv.style.display = 'block';
          var TabContainer = document.getElementById(Container); // 
          var anchors =    TabContainer.getElementsByTagName('LI');
          anchors[0].id= "current";
          DivArray.splice(0,1);
          for(var i=0;i<DivArray.length;i++)
          {var hiddiv = document.getElementById(DivArray[i]);
           hiddiv.style.display = 'none';}
      }
        
        function SwitchTab(DivId,tag,Container)
        {        
            var DivIds = document.getElementById('TabIDs');
            var TabContainer = document.getElementById(Container); // 
            var anchor =    document.getElementById('current');
            anchor.removeAttribute('id');
            tag.parentNode.id='current';          
            var elems = DivIds.value.split(';');
            for(var i=0;i<elems.length;i++)
	          {var elem = document.getElementById(elems[i]);
	           if(elem.id.indexOf(DivId)>0){elem.style.display = 'block';elem.className ='tab_on';}else{elem.style.display = 'none';
	           elem.className='';}
	          }
            }       
            

/* END CYC & SRG CheckBox Validations */     

function FileDownLoad(Location)
{
window.open(Location,'FileDownload','location=0')
}
    

