﻿var agt=navigator.userAgent.toLowerCase();
var brws;    
if (agt.indexOf("firefox") != -1) brws = 'ff';
if (agt.indexOf("msie") != -1) brws = 'ie';
if (agt.indexOf("safari")!=-1) brws = 'sf';
    
  
function scrollToCoordinates(scrollx, scrolly) {
    parent.window.scrollTo(scrollx, scrolly);
} 

function resetWindowScrol(){
    scrollToCoordinates(0,0);
}
    
function CurrencyFormatted(amount)
{
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
}

function trim(inputString)
        {
         if (typeof inputString != "string") { return inputString; }
         var retValue = inputString;
         var ch = retValue.substring(0, 1);
         while (ch == " ")
          { // Check for spaces at the beginning of the string
          retValue = retValue.substring(1, retValue.length);
          ch = retValue.substring(0, 1);
         }
         ch = retValue.substring(retValue.length-1, retValue.length);
         while (ch == " ")
         { // Check for spaces at the end of the string
          retValue = retValue.substring(0, retValue.length-1);
          ch = retValue.substring(retValue.length-1, retValue.length);
         }
         while (retValue.indexOf("  ") != -1)
         { 
         // Note that there are two spaces in the string
         // - look for multiple spaces within the string
          retValue = retValue.substring(0, 
            retValue.indexOf("  ")) + 
            retValue.substring(retValue.indexOf("  ")+1, 

         retValue.length);
         // Again, there are two spaces in each of the strings
         }
         return retValue; // Return the trimmed string back to the user
        }

function checkformgeneral(empties, empmessages, positives, posmesages, shouldResize,initialText)
{
    var errormessage = (initialText==undefined ? "" : initialText);
    var errorno = (((initialText==undefined) ||(initialText=="" ))  ? 0 : 1);
   // alert(initialText);
    document.getElementById('divErrorContent').innerHTML = "";
        document.getElementById('divError').style.display = "none";
    
        for (i=0; i<empties.length;i++)
        {
            
            if (trim(document.getElementById(empties[i]).value) == '')
            {
                errormessage += "- "+empmessages[i]+".<br>";
                errorno++;
            }
        }
        
         for (i=0; i<positives.length;i++)
        {
            if (parseFloat(document.getElementById(positives[i]).value) <= 0)
            {
                errormessage += "- "+posmesages[i]+".<br>";
                errorno++;
            }
        }
   
    
    if (errorno==0)
        return true;
    else
    {
        document.getElementById('divErrorContent').innerHTML = "<b>"+errorno+" error(s) prohibited this form from being saved</b><br><br>There were problems with the following fields:<br>"+errormessage;
        if(brws == 'ie')
            document.getElementById('divError').style.display = "inline";
        else
            document.getElementById('divError').style.display = "block";
        
        if (shouldResize)
         resizeIframe('frmMain');
        
        return false;
    }
    return false;
}

function isNumberKey(evt, hideDots,allowNegative)
{

 var charCode = (evt.which) ? evt.which : event.keyCode
 if(allowNegative)
 {
    //allow only one '-'
    if(charCode==45 && evt.srcElement.value.toString().length <= 0) return true;
 }
 if(!hideDots)
 {
    //is dot on first position
    if(charCode==46 && evt.srcElement.value.toString().length <= 0) return false;
    //is '-' on first position and '.' on second position
    if(charCode==46 && evt.srcElement.value.toString() == '-') return false;    
    //there is already a dot contained
    if(charCode==46 && evt.srcElement.value.toString().indexOf('.') >= 0) return false;
 }
 if (charCode > 31 && (charCode < 48 || charCode > 57) && (charCode !=46 || hideDots))
    return false;

 return true;
}
function isAlphanumeric(evt)
{
	var charCode = (evt.which)?evt.which:event.keyCode;
	if((charCode > 47 && charCode<58) || (charCode > 64 && charCode<91) || (charCode > 96 && charCode<123))
		  {
		  }
		else
		{
           return false;
		  
 		}
  return true;
}


function resizeIframe(iframeID) 
{   
        if(self==parent) 
            return false; /* Checks that page is in iframe. */ 
        else //if(document.getElementById&&document.all) /* Sniffs for IE5+.*/ 
            var FramePageHeight, FramePageWidth;
        //alert(brws);
        
        if (brws == 'ie' || brws == 'sf')
        {        
            FramePageHeight = document.getElementById('framePage').scrollHeight + 20;
            FramePageWidth = document.getElementById('framePage').scrollWidth; 
        }
        else if (brws =='ff')
        {            
            FramePageHeight = document.getElementById('contentDiv').offsetHeight;        
            FramePageWidth = document.getElementById('contentDiv').offsetWidth;        
        }
        
        if (FramePageHeight < 600)
            FramePageHeight = 600;
        if (FramePageWidth < 800)
            FramePageWidth += 40;   
                     
        //var oldWidth, newWidth = FramePageWidth + 200 + 22;//FramePageWidth is the width of ucHeader        
        //only when the new width is grater, we need to add some pixels extra
       // var sWidth = parent.document.getElementById('divTopHelper').style.width.toString(), idx = sWidth.indexOf('px');
       // if(idx > -1)
       // {   
      //      oldWidth = parseInt(sWidth.substring(0, idx));
      //      if(oldWidth < newWidth)
      //          { newWidth += 40; FramePageWidth +=40; }
      //  }
        
        parent.document.getElementById(iframeID).style.height=FramePageHeight+"px"; 
        //parent.document.getElementById(iframeID).style.width=FramePageWidth+"px"; 
       
        //parent.document.getElementById('divTopHelper').style.cssText ="width:" + newWidth + "px";
        //parent.document.getElementById('divBottomHelper').style.cssText ="width:" + newWidth + "px";
                
        /* "iframeID" is the ID of the inline frame in the parent page. */ 
        
        resetWindowScrol();
        
} 

//function resizeIframe(iframeID)
//{
//    var currentframe = parent.document.getElementById(iframeID);
//    alert(currentframe);
//    currentframe.style.display="block";
//    alert(currentframe.contentDocument.body.offsetHeight);
//   alert(document.getElementById('contentDiv').offsetHeight);

//}

//function changeHeight(iframeID)
//      {
//        try
//        {alert('cp1');
//            var iframe = parent.document.getElementById(iframeID);
//          var innerDoc = (iframe.contentDocument) ? iframe.contentDocument : iframe.contentWindow.document;
//          if (innerDoc.body.offsetHeight) //ns6 syntax
//          {alert('ff');
//             iframe.height = innerDoc.body.offsetHeight + 32; //Extra height FireFox
//          }
//          else if (iframe.Document && iframe.Document.body.scrollHeight) //ie5+ syntax
//          {alert('ie');
//             iframe.height = iframe.Document.body.scrollHeight;
//          }
//        }
//        catch(err)
//        {
//          alert(err.message);
//        }
//      }

 function ViewPrintableWithNewParams(text, newParams)
		{
		   var win;
		   if(window.location.search.length >0)
		   {
		        win = window.open(document.location+"&printable=yes&" + newParams, text, 'width=925,height=500,scrollbars=yes,resizable=yes');
		   }
		   else
		   {
		        win = window.open(document.location+"?printable=yes&" + newParams, text, 'width=925,height=500,scrollbars=yes,resizable=yes');
		   }
			win.focus();
		}
		
 function ViewPrintable(text)
		{
		    ViewPrintableWithNewParams(text, "");
		}
		
		function paintSpaces(level)
{
	
	var s= "";
	for (i=1; i<=level;i++)
		s+="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
	return s;
}

function ViewRecipe(id)
		{
			var win = window.open('/newinteraction/pages/community/recipes/recipedetailsprintable.aspx?id='+id, 'ViewRecipe', 'width=700,height=700,scrollbars=yes,resizable=no');
			win.focus();
		}
		
function ViewVendor(id)
		{
			var win = window.open('/newinteraction/pages/community/vendors/vendordetailsprintable.aspx?id='+id, 'ViewVendor', 'width=700,height=700,scrollbars=yes,resizable=yes');
			win.focus();
		}
		
function ViewEvent(id)
		{
			var loc = "/newinteraction/pages/community/reservationcalendar/eventprintable.aspx?id=" + id;
			win = window.open(loc, 'ViewEvent', 'width=600,height=500,resizable=NO,statusbar=YES,toolbar=NO,scrollbars=AUTO,left=300,top=100');
			win.focus();
		}
		
function ViewEvent(id, groupid)
		{
			var loc = "/newinteraction/pages/community/reservationcalendar/eventprintable.aspx?id=" + id + "&groupid=" + groupid;
			win = window.open(loc, 'ViewEvent', 'width=600,height=500,resizable=NO,statusbar=YES,toolbar=NO,scrollbars=AUTO,left=300,top=100');
			win.focus();
		}		
		
//String.prototype.startsWith = function(str) 
//{return (this.match("^"+str)==str)}

//String.prototype.endsWith = function(str) 
//{return (this.match(str+"$")==str)}


//scroll up after changing pages in a paging repeater inside an update panel
function doScrollUp()
		{
		    if(document.getElementById("hdNeedScroll").value.toString() == "1") //to do the scrool a single time, and after the repeater loaded
		    {
		        parent.scroll(0,0);
		        document.getElementById("hdNeedScroll").value = "0";
		        clearInterval(setScrollIntervalId);
		        setScrollIntervalId = 0;
		        resizeIframe('frmMain');
		    }
		}
		
function setScrollUp()
		{
		    if(setScrollIntervalId == 0)
		    {
		        setScrollIntervalId = setInterval('doScrollUp();', 200);
		    }
		}
function showMessage(message)
{
    document.getElementById('divMessage').innerHTML = message;
    window.scrollTo(0,0);
    $('#divMessage').slideToggle("slow").animate({opacity: 1.0}, 2000).slideToggle("slow");
}	
