
var isDHTML=0;
var isID=0;
var isAll=0;
var isLayers=0;
var oldMenu=null;
var oldSMenu=null;

if (document.getElementById) { isID=1; isDHTML=0; }
else
{
    if (document.all) { isAll=1; isDHTML=1; }
    else
    {
        browserVersion = parseInt(navigator.appVersion);
        if ((navigator.appName.indexOf('Netscape') != -1) && (browserVersion == 4))
        { isLayers=1; isDHTML=1; }
    }
}

function findDOM(objectID,withStyle)
{
    if (withStyle==1)
    {
        if (isID) { return (document.getElementById(objectID).style); }
        else
        {
            if (isAll) { return (document.all[objectID].style); }
            else
            {
                if(isLayers) { return (document.layers[objectID]); }
            };
        }
    }
    else
    {
        if (isID) { return (document.getElementById(objectID)); }
        else
        {
            if (isAll) { return (document.all[objectID]); }
            else
            {
                if(isLayers) { return (document.layers[objectID]); }
            };
        }
    }
}

function LogOut()
{
	if (confirm("Are you sure you want to log out?"))
	{
		location = "index.php?OpenPage=Logout";
	}
}


function DisabledBotton(obj,status)
{
	mydom = findDOM(obj,0);
	mydom.disabled = status;
	if (status)
	{
		mydom.className = 'ButtonDisabled';
	}
	else
	{
		mydom.className = 'Button';
	}
}


//other functions
var win= null;
function NewWindow(mypage,myname,w,h,scroll,menu)
{
  var winl = (screen.width-w)/2;
  var wint = (screen.height-h)/2;
  var settings  ="height="+h+",";
      settings +="width="+w+",";
      settings +="top="+wint+",";
      settings +="left="+winl+",";
      settings +="scrollbars="+scroll+",";
      settings +="menubar="+menu+",";
      settings +="resizable=yes";
  win=window.open(mypage,myname,settings); 
  if(parseInt(navigator.appVersion) >= 4){win.window.focus();}
}

function ToggleSection(obj,field,flag)
{
    mydom = findDOM(obj,1);

    if (flag=="ON")
    {
    	mydom.display='block';
        mydom.visibility = 'visible';
    }
    else if (flag=="OFF") 
    {
    	mydom.display='none';
        mydom.visibility = 'hidden';
    }
    else
    {
	    if (mydom.visibility == 'visible' || mydom.visibility=="")
	    {
	        mydom.display='none';
	        mydom.visibility = 'hidden';
	        if (field)
	        	field.value="visibility: hidden; display: none;";
	    }
	    else
	    {
	        mydom.display='block';
	        mydom.visibility = 'visible';
	        if (field)
	        	field.value="visibility: visible; display: block;";
	    }
    }
}

function showSection(obj,field)
{
    dom = findDOM(obj,0);
    if (dom)
    {
        domStyle = findDOM(obj,1);

        domStyle.display='block';
        domStyle.visibility = 'visible';
        if (field)
        {
        	field.value="visibility: visible; display: block;";
        }
    }
    return false;
}

function hideSection(obj,field)
{
    dom = findDOM(obj,0);
    if (dom)
    {
        domStyle = findDOM(obj,1);

        domStyle.display='none';
        domStyle.visibility = 'hidden';
        if (field)
        {
        	field.value="visibility: hidden; display: none;";
        }
    }
    return false;
}

/*function ToggleSection(obj)
{
	dom = findDOM(obj,0);
    if (dom)
    {
        domStyle = findDOM(obj,1);

        if (domStyle.display=='none')
        {
	        domStyle.display='block';
	        domStyle.visibility = 'visible';
        }
        else
        {
        	domStyle.display='none';
	        domStyle.visibility = 'hidden';
        }
    }
    return false;
}*/

function ShowTips(objectID,aboveObj,resize)
{

    //alert("test");
    dom = findDOM(objectID,0);
    domStyle = findDOM(objectID,1);
    aoDom = findDOM(aboveObj,0);
    aoDomStyle = findDOM(aboveObj,1);

    if (domStyle.visibility == 'visible')
    {
        domStyle.visibility = 'hidden';
        domStyle.display = 'none';
        aoDomStyle.pixelLeft = 0;
        aoDomStyle.left=0;
        
        if (resize==1)
	        if (aoDomStyle.pixelWidth)
	        	aoDomStyle.pixelWidth=aoDomStyle.pixelWidth+280;
	        else
	        	aoDomStyle.width = '99%';
    }
    else
    {
        if (domStyle.pixelTop != null)
        {
            //if resize=1 then
            if (resize==1)
            {
            	aoDomStyle.pixelWidth = aoDomStyle.pixelWidth - 280;
            }
            else
            {
        		//move table over for half the size of the tips
            	aoDomStyle.pixelLeft = - 136 - 5;
            }
            
            domStyle.pixelTop =  aoDom.offsetTop+3;
            domStyle.pixelLeft = aoDom.offsetLeft + aoDom.offsetWidth + 10;
            //domStyle.pixelLeft = aoDomStyle.pixelLeft + aoDom.offsetWidth + 10;

        }
        else
        {
            if (resize==1)
            {
            	//calculate tip percentage width
            	TipPercentWidth = (290/window.innerWidth)*100;
            	aoDomStyle.width = (99 - TipPercentWidth) + "%";
            }
            else
            {
        		aoDomStyle.left = - 136 - 5;
            }
            domStyle.top =  aoDom.offsetTop+3;
            domStyle.left = aoDom.offsetLeft + aoDom.offsetWidth + 10;
        }

        domStyle.visibility = 'visible';
        domStyle.display = 'block';
    }
}

function SetObjSize(obj,w,h)
{
    dom = findDOM(obj,0);
    domStyle = findDOM(obj,1);
	
	if (domStyle.pixelWidth != null)
	{
    	if (domStyle.pixelWidth == 0) 
    	{    	
	    	domStyle.pixelWidth = w;
	    	domStyle.pixelHeight = h;
	    }
	    else
	    {
	    	domStyle.pixelWidth = 0;
	    	domStyle.pixelHeight = 0;	    	
	    }    	
    }    
    else
    {    
	    //alert(domStyle.width);
    	if (domStyle.width == 0 || domStyle.width == '0pt')
	    {
	    	domStyle.width = w;
			domStyle.height = h;
	    }
	    else
	    {
	    	domStyle.width = 0;
			domStyle.height = 0;
	    }
    }
    
}

function DeleteConfirmation(Msg,GoToPage)
{
    if (confirm(Msg))
        location=GoToPage;
}

function CenterObject(obj)
{	
	dom = findDOM(obj,0);
    domStyle = findDOM(obj,1);

    if (domStyle.pixelTop != null)
   	{
   		domStyle.pixelLeft = (document.body.clientWidth/2)-(dom.offsetWidth/2);
   		domStyle.pixelTop = (document.body.clientHeight/2)-(dom.offsetHeight/2);
   	}
   	else
   	{
   		//alert(dom.offsetWidth);
   		domStyle.left =  (window.innerWidth/2)-(dom.offsetWidth/2);
        domStyle.top = (window.innerHeight/2)-(dom.offsetHeight/2);
   	}
    
}

function BillingAsMailing(myForm)
{
    //alert(myForm.chkAsMailing.value);
    if (myForm.chkAsMailing.checked)
    {
        myForm.txtbAddress.value = myForm.txtAddress.value;
        myForm.txtbCity.value = myForm.txtCity.value;
        myForm.txtbProvince.value = myForm.txtProvince.value;
        myForm.txtbPostalCode.value = myForm.txtPostalCode.value;
        myForm.txtbCountry.value = myForm.txtCountry.value;
    }
    else
    {
        myForm.txtbAddress.value = "";
        myForm.txtbCity.value = "";
        myForm.txtbProvince.value = "";
        myForm.txtbPostalCode.value = "";
        myForm.txtbCountry.value = "";
    }

}
/*
var win=null;
function NewWindow(mypage,myname,w,h,scroll,menu)
{
  var winl = (screen.width-w)/2;
  var wint = (screen.height-h)/2;
  var settings  ="height="+h+",";
      settings +="width="+w+",";
      settings +="top="+wint+",";
      settings +="left="+winl+",";
      settings +="scrollbars="+scroll+",";
      settings +="menubar="+menu+",";
      settings +="resizable=yes";
  win=window.open(mypage,myname,settings); 
  if(parseInt(navigator.appVersion) >= 4){win.window.focus();}
}
*/
function CloseWindow(myWindow)
{
	if (win)
	{
		if (typeof myWindow !='undefined') 
		{
	      if (!myWindow.closed) myWindow.close()
		}
	}
}

// date format should be yyyy-mm-dd
// it returns the new date with the incremented year
function AddYear(myDate,offset)
{
	if (myDate!="" && myDate!=null)
	{
		var temp = myDate.split("-");
		NewYear = Number(offset) + Number(temp[0]);
		return NewDate = NewYear + "-"+temp[1]+"-"+temp[2];
	}
	
	return "";
}



function AddToCriteria(criteria_field,field,operator,value,action)
{
	// attach to main criteria
	
	// if main critera is blank then do not add action (AND or OR)
	if (criteria_field.value != "")
	{
		criteria_field.value += " "+action;
	}
	
	switch (operator.value)
	{
		case "IN":
			criteria_field.value += " "+ field.value + " " + operator.value + " (" + value.value + ")";
			break;
		case "BETWEEN":
			criteria_field.value += " "+ field.value + " " + operator.value + " " + value.value + "";
			break;
		case "IS NULL":
			criteria_field.value += " "+ field.value + " " + operator.value;
			break;
		case "IS NOT NULL":
			criteria_field.value += " "+ field.value + " " + operator.value;
			break;
		default:
			criteria_field.value += " "+ field.value + " " + operator.value + " '" + value.value + "'";
	}
	
}



/************************************************************************************************************************/
// Calendar functions
/************************************************************************************************************************/
var CalArray;
var FirstDay;
var monthname = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");

function ShowCalendar(calName,month,year)
{
            
	GenerateMonth(month,year);
    
    NextID=0;
    for (i=0; i < 6; i++)
    {
    	
        for (a=0; a < 7; a++)
        { 
        
            myCell = findDOM(calName + "_" + NextID);
            if (myCell)
            {
                myCell.firstChild.nodeValue = CalArray[i][a];
                if (CalArray[i][a] < 0)
                {
                    myCell.firstChild.nodeValue = myCell.firstChild.nodeValue * -1;
                    myCell.className = "FadedCell";
                    
                }
                else
                {
                    myCell.firstChild.nodeValue = myCell.firstChild.nodeValue;
                    myCell.className = "Cell";
                }
                
            }
            
            
            myCell = null;
            NextID++; 
        }
    }
    
    SetTitle(calName+"_CalTitle");
    //alert("Test");
}

function HighlightCell(calName,obj,objField)
{
	myCell = findDOM(obj);
    if (myCell.className != "FadedCell")
    {
        for (i=0; i<42; i++)
        {
            myCell = findDOM(calName+"_"+i);
            if (myCell)
            {
                if (calName+"_"+i == obj)
                {                    
                	myCell.className = "HighlightedCell";
                    objField.value = FormatDate(GetClickedDate(calName,myCell),"yyyy-mm-dd");
                }
                else
                    if (myCell.className != "FadedCell")
                        myCell.className = "Cell";
            }
        }
    }
    else
        return false;
}

function GetLastDay(myDate)
{
    mMonth = myDate.getMonth();
    mYear = myDate.getYear();
    //alert(month);
    if (mMonth==3 || mMonth==5 || mMonth==8 || mMonth==10)
        mLastDay=30;
    else if (mMonth==1)
    {
        if((mYear%4)==0)
        {
            if ((mYear%100)==0)
            {
                if ((mYear%400)==0)
                    mLastDay=29;
                else
                    mLastDay=28;
            }
            else
                mLastDay=29;
        }
        else
            mLastDay=28;
    }
    else
        mLastDay=31;
        
    return mLastDay;
}
function GenerateMonth(month, year)
{
    //reset the cal array
    nSize = 6 ;
    CalArray = null;
    CalArray = new Array(nSize);
    for (i=0; i < nSize; i++) { CalArray[i]=new Array(7) } ;    

    //get months start and end days
    if (year==null && month==null)
    {
        FirstDay = new Date();  
        FirstDay = new Date(FirstDay.getFullYear(),FirstDay.getMonth(),1);   
    }
    else
        FirstDay = new Date(year,month,1);

    LastDay=0;
    LastDay = GetLastDay(FirstDay);
    FirstWeekDay = FirstDay.getDay();

    //if the first day is not sunday then the previous month end days have to be displayed
    if (FirstWeekDay > 0)
    {
        //get the last days of the previous month        
        pMonth = month-1;
        if (pMonth < 0 ) { pMonth = 11; pYear=year-1;}
        else
            pYear = year;
            
        var PreviousMonth = new Date(pYear,pMonth,1);
        PLastDay = GetLastDay(PreviousMonth);
        
        for (i=FirstWeekDay; i>0; i--)
        {
            CalArray[0][i-1] = PLastDay * (-1);
            PLastDay--;
        }
    }
    
    //continue with adding the dates of the first week in addition to the previous month
    row = 0;
    LastDayEntered=FirstWeekDay;
    //alert (LastDay);
    for (i=1; i<LastDay+1; i++)
    {
        if (LastDayEntered%7 == 0)
        {
            if (LastDayEntered!=0)
                row++;
            LastDayEntered = 0;
        }
        //alert (LastDayEntered);
        CalArray[row][LastDayEntered] = i;
        
        LastDayEntered++;        
    }
    
    /*continue with adding the dates for the next month*/
    for (i=1; i + LastDay + FirstWeekDay < 43; i++)
    {
        if (LastDayEntered%7 == 0)
        {
            row++;
            LastDayEntered = 0;
        }
                
        CalArray[row][LastDayEntered] = i * (-1);
        LastDayEntered++;
        
    }
    
        
}

function DisplayArray(myArray)
{
    for (i=0; i<6; i++)
    {
        for (a=0; a<7; a++)
            document.write(CalArray[i][a] + " ");
            
        document.write("<br>");
    }
    
}

function NextMonth(CalName)
{
    month = FirstDay.getMonth();
    year = FirstDay.getFullYear();
    
    nMonth = month+1;
    
    if (nMonth>11) { nMonth=0; nYear=year+1; }
    else
        nYear=year;
        
    ShowCalendar(CalName,nMonth,nYear);
    
}

function PrevMonth(CalName)
{
    month = FirstDay.getMonth();
    year = FirstDay.getFullYear();
    
    nMonth = month-1;
    
    if (nMonth<0) { nMonth=11; nYear=year-1; }
    else
        nYear=year;
        
    ShowCalendar(CalName,nMonth,nYear);
}

function NextYear(CalName)
{
    month = FirstDay.getMonth();
    year = FirstDay.getFullYear();
        
    nYear = year+1;
    
    ShowCalendar(CalName,month,nYear);
}

function PrevYear(CalName)
{
	month = FirstDay.getMonth();
    year = FirstDay.getFullYear();
        
    pYear = year-1;
    
    ShowCalendar(CalName,month,pYear);
}
function SetTitle(obj)
{
    strTitle = monthname[FirstDay.getMonth()] + " " + FirstDay.getFullYear();
    myTitle = findDOM(obj);
    myTitle.firstChild.nodeValue =  strTitle; 
    myTitle = null;
}

function GetMonthNumber(strMonth)
{
    myMonth = new String(strMonth);
    for (x=0; x<12; x++)
    {
        if (strMonth==monthname[x])
        {
            
            myMonth = "0"+(x+1);
            if (myMonth.length > 2)
                myMonth=myMonth.substr(1,2);        
        }
    }/**//**/
    return myMonth;
}

function GetClickedDate(calName,dom)
{
    //get the clicked day
    myDay = new String(dom.firstChild.nodeValue);
    myDay = "0"+myDay;
    if (myDay.length > 2)
        myDay=myDay.substr(1,2);
    
    //get the month and the year from the title
    myTitle = findDOM(calName+"_CalTitle");
    var myDate = myTitle.firstChild.nodeValue.split(" ");
    myMonth = new String(GetMonthNumber(myDate[0]));
    myYear = myDate[1];
    myTitle = null;
    myDate = null;
    
    strDate = myMonth+myDay+myYear;
    return strDate;
}

function FormatDate(strDate,strFormat)
{
    // mm/dd/(yy)yy     
    strDate = strDate.replace(" ","");    
    if (strDate.indexOf("/")!=-1)
    {    
        var myDate = strDate.split("/");
        myYear = myDate[2];
        myMonth = myDate[0];
        myDay = myDate[1];
    }
    else
    {    
        // dd mm (yy)yy   &   mmdd(yy)yy
        //replace all the spaces in the string with 'nothing'    
        strDate = strDate.replace(" ","");
        myDay = strDate.substr(2,2);
        myMonth = strDate.substr(0,2);
        myYear = strDate.substr(4,4);
        if (myYear.length == 2)
            if (eval(myYear) > 20)
                myYear = "19" + myYear;
            else
                myYear = "20" + myYear;    
    }
    
    // format output types:  mm/dd/yyyy  or  yyyy-mm-dd
    if (strFormat == "mm/dd/yyyy")
        myStr=myMonth+"/"+myDay+"/"+myYear;
    else if (strFormat == "yyyy-mm-dd")
        myStr=myYear+"-"+myMonth+"-"+myDay;
    else        
        myStr=myYear+"-"+myMonth+"-"+myDay;

    return myStr;
}

function ParseDate(field,strFormat)
{
    strDate = field.value;
    field.value = FormatDate(strDate,strFormat);
}

function DisplayCal(obj,calName,field)
{
    
	//dom = findDOM(obj,0);
    domStyle = findDOM(obj,1);

    if (domStyle.visibility == 'visible')
    {
        domStyle.visibility = 'hidden';
    }
    else
    {
    	var CurrentMonth = null;
    	var CurrentYear = null;
    	if (field.value!="" && field.value!=null)
    	{
    		var temp = field.value.split("-");
    		CurrentMonth = temp[1]-1;
    		CurrentYear = temp[0];
    	}
    	
    	ShowCalendar(calName,CurrentMonth,CurrentYear);
    	domStyle.visibility = 'visible';
    	
    }

    return false;
}




// this function will get called at body.onload
function initHTMLArea(TextArea) {
	// cache these values as we need to pass it for both editors
	var css_plugin_args = {
  	combos : [
	    { label: "Syntax",
	                 // menu text       // CSS class
	      options: { "None"           : "",
	                 "Code" : "code",
	                 "String" : "string",
	                 "Comment" : "comment",
	                 "Variable name" : "variable-name",
	                 "Type" : "type",
	                 "Reference" : "reference",
	                 "Preprocessor" : "preprocessor",
	                 "Keyword" : "keyword",
	                 "Function name" : "function-name",
	                 "Html tag" : "html-tag",
	                 "Html italic" : "html-helper-italic",
	                 "Warning" : "warning",
	                 "Html bold" : "html-helper-bold"
	               },
	      context: "pre"
	    },
	    { label: "Info",
	      options: { "None"           : "",
	                 "Quote"          : "quote",
	                 "Highlight"      : "highlight",
	                 "Deprecated"     : "deprecated"
	               }
	    }
  	]
	};

	//---------------------------------------------------------------------
	// GENERAL PATTERN
	//
	//  1. Instantitate an editor object.
	//  2. Register plugins (note, it's required to have them loaded).
	//  3. Configure any other items in editor.config.
	//  4. generate() the editor
	//
	// The above are steps that you use to create one editor.  Nothing new
	// so far.  In order to create more than one editor, you just have to
	// repeat those steps for each of one.  Of course, you can register any
	// plugins you want (no need to register the same plugins for all
	// editors, and to demonstrate that we'll skip the TableOperations
	// plugin for the second editor).  Just be careful to pass different
	// ID-s in the constructor (you don't want to _even try_ to create more
	// editors for the same TEXTAREA element ;-)).
	//
	// So much for the noise, see the action below.
	//---------------------------------------------------------------------
	
	var config = new HTMLArea.Config();
		config.toolbar = [
		  [ "fontname", "space",
			  "fontsize", "space",
			  "formatblock", "space"],
			  
			["bold", "italic", "underline", "separator",
			  "strikethrough", "subscript", "superscript", "separator",
			  "copy", "cut", "paste", "space", "undo", "redo" ],
					
			[ "justifyleft", "justifycenter", "justifyright", "justifyfull", "separator",
			  "insertorderedlist", "insertunorderedlist", "outdent", "indent", "separator",
			  "forecolor", "hilitecolor", "separator",
			  "inserthorizontalrule", "createlink", "insertimage", "inserttable", "htmlmode"]
		];
	
	var loaded = false;
	for (i=0; i<TextArea.length; i++)
	{
		//alert(TextArea[i]);
		//if ()
		//{		
			//---------------------------------------------------------------------
			// CREATE FIRST EDITOR
			//
			
			var editor = new HTMLArea(TextArea[i],config);
			
			// plugins must be registered _per editor_.  Therefore, we register
			// plugins for the first editor here, and we will also do this for the
			// second editor.
			if (loaded!=true)
			{
				editor.registerPlugin(TableOperations);
				//editor.registerPlugin(SpellChecker);
				//editor.registerPlugin(CSS, css_plugin_args);
				loaded=true;
			}
			
			// custom config must be done per editor.  Here we're importing the
			// stylesheet used by the CSS plugin.
			editor.config.pageStyle = "@import url(custom.css);";
			
			// generate first editor
			editor.generate();
			//---------------------------------------------------------------------
		//}
	}
	
};



