// JScript File
var onloads = new Array();

function bodyOnLoad() 
    {

	      for ( var i = 0 ; i < onloads.length ; i++ )
	         onloads[i]();

    }
    
//   utilities.js
//
// This is a file of common JavaScript form validation 
// utilities which can be reused between various modules.
//
//  Created by: M.K
//  Created on: 07/23/2004
//

_editor_url = "./includes/htmlarea/htmlarea/";                     // URL to htmlarea files
var win_ie_ver = parseFloat(navigator.appVersion.split("MSIE")[1]);
if (navigator.userAgent.indexOf('Mac')        >= 0) { win_ie_ver = 0; }
if (navigator.userAgent.indexOf('Windows CE') >= 0) { win_ie_ver = 0; }
if (navigator.userAgent.indexOf('Opera')      >= 0) { win_ie_ver = 0; }
if (win_ie_ver >= 5.5) {
  document.write('<scr' + 'ipt src="' +_editor_url+ 'editor.js"');
  document.write(' language="Javascript1.2"></scr' + 'ipt>');  
} else { document.write('<scr'+'ipt>function editor_generate() { return false; }</scr'+'ipt>'); }


if (document.all)

	var browser_ie=true

else if (document.layers)

	var browser_nn4=true

else if (document.layers || (!document.all && document.getElementById))

	var browser_nn6=true
	

    var x,y;
      
      
if (self.innerHeight) // all except Explorer
{
	x = self.innerWidth;
	y = self.innerHeight;
}
else if (document.documentElement && document.documentElement.clientHeight)
	// Explorer 6 Strict Mode
{
	x = document.documentElement.clientWidth;
	y = document.documentElement.clientHeight;
}
else if (document.body) // other Explorers
{
	x = document.body.clientWidth;
	y = document.body.clientHeight;
}

    	

//trim a string by removing all external spaces
function trim(str) { 
    str.replace(/^\s*/, '').replace(/\s*$/, ''); 

   return str;
} 


// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
return true
}

//check if a field exists on a given form
function fieldExists(dform,fieldName)
    {
    for(i=0;i<dform.elements.length;i++)
        {
            if (dform.elements[i].name==fieldName)
                {
                return true;
                }
        }

    return false;
    }

//check if a form by given name exists
function formExists(formName)
    {
    for(i=0;i<document.forms.length;i++)
        {
            if (document.forms[i].name==formName)
                {
                return true;
                }
        }

    return false;
    }

//validate all required fields on a form
// (requires ASP function AddInputValidators called to set the list of required fields)
function validateRequiredFields(dform)
    {
   // alert("here");

    if (fieldExists(dform,'donotvalidate'))
        {
        if (dform.donotvalidate.value=="1")
            {
            return true;
            }
        }
    
    
    if (fieldExists(dform,'RequiredTitles'))
       {
       titleArray=dform.RequiredTitles.value.split('!!');
       fieldArray=dform.RequiredFields.value.split('!!');

       for(i=1;i<fieldArray.length;i++)
          {
            if (titleArray[i]!="")
                {
                    if (trim(dform.elements[fieldArray[i]].value)=="" || trim(dform.elements[fieldArray[i]].value)=="-1" || dform.elements[fieldArray[i]].value==null)
                        {
                        alert(titleArray[i]+" is a required field! Please complete.");
                        dform.elements[fieldArray[i]].focus();
                        return false;
                        }
                }
           }
        }

    return true;
    }



function ValidateNumber(fld,title)
    {
    if(isNaN(fld.value.replace("$",""))) 
        {
        alert(title+' is a numeric field! Please enter a number');
        fld.focus();
        return false;
        }

    return true;
    }

function ValidateDate(fld,Title)
    {
       if (trim(fld.value)!="" && !isDate(fld.value) )
          {
          //alert(Title+" is not a valid date! Please correct.");
          fld.focus();
          return false;
          }

    return true;
    }



function tpopup(s)
{
msg=window.open(s, "rblPopup", "scrollbars=yes,status=no,toolbar=no,directories=no,menubar=no,location=no,resizable=yes,width=400,height=400");
}

function cpopup(s,wdth,hght)
{
msg=window.open(s, "rblPopup", "scrollbars=yes,status=no,toolbar=no,directories=no,menubar=no,location=no,resizable=yes,width="+wdth+",height="+hght);
}

function DateAdd(startDate, numDays, numMonths, numYears)
{
	var returnDate = new Date(startDate.getTime());
	var yearsToAdd = numYears;
	
	var month = returnDate.getMonth()	+ numMonths;
	if (month > 11)
	{
		yearsToAdd = Math.floor((month+1)/12);
		month -= 12*yearsToAdd;
		yearsToAdd += numYears;
	}
	returnDate.setMonth(month);
	returnDate.setFullYear(returnDate.getFullYear()	+ yearsToAdd);
	
	returnDate.setTime(returnDate.getTime()+60000*60*24*numDays);
	
	return returnDate;

}

function YearAdd(startDate, numYears)
{
		return DateAdd(startDate,0,0,numYears);
}

function MonthAdd(startDate, numMonths)
{
		return DateAdd(startDate,0,numMonths,0);
}

function DayAdd(startDate, numDays)
{
		return DateAdd(startDate,numDays,0,0);
}

function disallowDate(date) {
  // date is a JS Date object
  //if ( date<DayAdd(new Date(),4) ) {
  //  return true; // disable everything prior to today
  //}
  return false; // enable other dates
}

function UploadFile(FormName,FieldName,Path)
    {
    window.open('car_upload.asp?formname='+FormName+'&fieldname='+FieldName+'&path='+Path,'upload_window','resizable=0,location=0,left=0,top=0,height=600,width=600,scrollbars=1');
    }
  
function HTMLEdit(FieldTitle,FieldName)
    {
    fld=document.getElementById(FieldName)
 
    
    FormName=escape(fld.form.name);
    FieldName=escape(FieldName);
    FieldTitle=escape(FieldTitle);
    FieldValue=escape(fld.value);

    window.open('car_htmledit.asp?FormName='+FormName+'&FieldName='+FieldName+'&FieldTitle='+FieldTitle,'html_window','resizable=1,location=1,left=10,top=10,height=500,width=700');
    }

     
function SubmitWithAction(dform,actionType)
     {
     dform.actiontype.value=actionType;
     dform.submit();
	 return false;
     }


function SubmitWithAction2(dform,recordID,actionType,IsPopup)
     {
    //dform.recordid.value=recordID;
    //dform.actiontype.value=actionType;
    
        if (actionType!="update" && actionType!="submit")
            {
                dform.donotvalidate.value="true";
            }

     
        if (actionType.toUpperCase().indexOf("DELETE")>=0 || actionType.toUpperCase().indexOf("SEND")>=0)
            {
              if (!confirm("Are you sure you want to delete this item?"))
                  {
                  return false;
                  }
                  
                  
            }
            
     actionKeyword="actiontype";
     recordKeyword="recordid";
     
     //determine if action is second level, or first level
     if (actionType.indexOf("attach")>=0 || actionType.indexOf("detach")>=0 || actionType.indexOf("2")>=0 )
         {
         actionKeyword="subactiontype";
         recordKeyword="subrecordid";         
         }          
     
     var str=window.location.href;

     //exclude # sign
     if (str.indexOf("#")>=0)
         {
         str=str.substring(0,str.indexOf("#"));
         }


     str=ExcludeQuerystring(str,actionKeyword);
     str=ExcludeQuerystring(str,recordKeyword);

     if (actionKeyword=="actiontype")
     {
     str=ExcludeQuerystring(str,"subactiontype");
     str=ExcludeQuerystring(str,"subrecordid");
     }

               
     

     myurl=str+ "&"+recordKeyword+"="+recordID+"&"+actionKeyword+"="+actionType;
     
     //alert(dform.actiontype.value);

     if(IsPopup=="1")
         {
         window.open(myurl +"&type=pfv");
         return false;
		 }
     else 
         {
         dform.elements[actionKeyword].value=actionType;
         dform.elements[recordKeyword].value=recordID;
         //dform.submit();
         }

     return true;
     }  

     
function SubmitWithAction2old(dform,recordID,actionType,IsPopup)
     {
    //dform.recordid.value=recordID;
    //dform.actiontype.value=actionType;
    
        if (actionType!="update" && actionType!="submit")
            {
                dform.donotvalidate.value="true";
            }

     
        if (actionType.toUpperCase().indexOf("DELETE")>=0 || actionType.toUpperCase().indexOf("SEND")>=0)
            {
              if (!confirm("Are you sure you want to delete this item?"))
                  {
                  return false;
                  }
                  
                  
            }
            
     actionKeyword="actiontype";
     recordKeyword="recordid";
     
     //determine if action is second level, or first level
     if (actionType.indexOf("attach")>=0 || actionType.indexOf("detach")>=0 || actionType.indexOf("2")>=0 )
         {
         actionKeyword="subactiontype";
         recordKeyword="subrecordid";         
         }          
     
     var str=window.location.href;

     //exclude # sign
     if (str.indexOf("#")>=0)
         {
         str=str.substring(0,str.indexOf("#"));
         }


     str=ExcludeQuerystring(str,actionKeyword);
     str=ExcludeQuerystring(str,recordKeyword);

     if (actionKeyword=="actiontype")
     {
     str=ExcludeQuerystring(str,"subactiontype");
     str=ExcludeQuerystring(str,"subrecordid");
     }

               
     

     myurl=str+ "&"+recordKeyword+"="+recordID+"&"+actionKeyword+"="+actionType;
     
     //alert(dform.actiontype.value);
	//alert("xxx");
     if(IsPopup=="1")
         {
         window.open(myurl +"&type=pfv");
         return false;
		 }
     else 
         {
         dform.elements[actionKeyword].value=actionType;
         dform.elements[recordKeyword].value=recordID;
         //dform.submit();
         }

     return true;
     }  
     
     
function CollectFormFields(dform)
    {
    var url="1=1";
    
        for(i=0;i<dform.elements.length;i++)
        {
        val="";
        
        if (dform.elements[i].type=="checkbox" || dform.elements[i].type=="radio" )
            {
            if (dform.elements[i].checked)
                {
                val=dform.elements[i].value;
                }
            }
        else
            {
            val=dform.elements[i].value;
            }
        
        
        url=url+"&"+escape(dform.elements[i].name)+"="+escape(val);
        }
        
        return url;
    }     
           
   
function ExcludeQuerystring(str,queryStr)
    {
    var localStr=str;
    
    
         if (localStr.indexOf(queryStr)>0)
         {
         indexStart=localStr.indexOf(queryStr);
         indexEnd=localStr.indexOf("&",indexStart);
         
             if (indexEnd==-1)
                {
                indexEnd=localStr.length;
                }             
         
         //alert(queryStr+" found at index"+indexStart+",ends at "+indexEnd+"!");        
         //alert(localStr.substring(localStr,indexEnd,localStr.length));
         localStr=localStr.replace(localStr.substring(indexStart,indexEnd),"");
         
         while (localStr.indexOf("&&")>=0)
             {
             localStr=localStr.replace("&&","");
             }
         
         
         //localStr=localStr.substring(localStr,0,indexStart)+localStr.substring(localStr,indexEnd,localStr.length);
         }
     
    return localStr;
    }     
     

function AddToElement(ElementID,Value)
    {
    var fld=document.getElementById(ElementID);

       if (win_ie_ver>5.5)
           {
           editor_insertHTML(ElementID,'\n<br>'+Value,'',0);
           }
       else
           {
           fld.value=fld.value+'\n<br>'+Value;
           }

    }


    
function AssignFormFocus(dform)
    {
    
    if (fieldExists(dform,"focuscontrol"))
        {
           if (dform.elements["focuscontrol"].value!="" && dform.elements["focuscontrol"].value!=null)
               {
               dform.elements[dform.focuscontrol.value].focus();
               }
        }
    
    }
    
 function PageLoadedChecks()
     {
     for (i=0;i<document.forms.length;i++)
         {
         AssignFormFocus(document.forms[i]);
         }
     
     Tooltip.init();    
     }   
    
 function RefreshWithFocus(ditem)
    {
    ditem.form.donotvalidate.value="1";
    ditem.form.focuscontrol.value=ditem.name;
    ditem.form.submit();
    }   
      
  function doTooltip(e, msg) {
  if ( typeof Tooltip == "undefined" || !Tooltip.ready ) return;
  Tooltip.show(e, msg);
}

function hideTip() {
  if ( typeof Tooltip == "undefined" || !Tooltip.ready ) return;
  Tooltip.hide();
}

function doHourglass()
{
  document.body.style.cursor = 'wait';
}

function ConfirmDelete(dform)
    {
    if (confirm("Are you sure?"))
        {
        dform.donotvalidate.value="1";
        return true;
        }
        
    return false;    
    }
    


function focusOnGrid()
    {
    if (document.getElementById("focuser"))
        {
        var el=document.getElementById("focuser");
        scroll(0,el.style.top+200);
        }
    }
    




function getObj(n,d) {

  var p,i,x; 

  if(!d)

      d=document;

   
  if((p=n.indexOf("?"))>0&&parent.frames.length) {

    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);

  }



  if(!(x=d[n])&&d.all)

      x=d.all[n];

 

  for(i=0;!x&&i<d.forms.length;i++)

      x=d.forms[i][n];

 

  for(i=0;!x&&d.layers&&i<d.layers.length;i++)

      x=getObj(n,d.layers[i].document);

 

  if(!x && d.getElementById)

      x=d.getElementById(n);



  return x;

}    

function setupDateField(FieldName)
   {
    Calendar.setup({
        disableFunc    :    disallowDate,
        align          :    "TL",
        inputField     :    FieldName,      // id of the input field
        ifFormat       :    "%m/%d/%Y",       // format of the input field
        showsTime      :    false,            // will display a time selector
        button         :    FieldName+"_button",   // trigger for the calendar (button ID)
        singleClick    :    true,           // double-click mode
        step           :    1                // show all years in drop-down boxes (instead of every other year as default)
    });
  } 
  
function appendOptionLast(elOptNew,elSel)
{
  try {
    elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
  }
  catch(ex) {
    elSel.add(elOptNew); // IE only
  }
}
  
function doScroll() 
{
   if(scrollPosition!="none")
	{
       if (scrollPosition=="left")
       {
	   document.getElementById("scroll3").scrollLeft -= 50;
       }
	   else
	   {
       document.getElementById("scroll3").scrollLeft += 50;
	   }

    
	}

   if(scrollPosition!="none")
	{
	setTimeout('doScroll()','15');
	}
   

}

function startScroll(direction)
{
    scrollPosition=direction;
	doScroll();
}

function stopScroll()
{
	scrollPosition="none";
}    
 
 
 function setupDateField(FieldName)
   {
    Calendar.setup({
        disableFunc    :    disallowDate,
        align          :    "TL",
        inputField     :    FieldName,      // id of the input field
        ifFormat       :    "%m/%d/%Y",       // format of the input field
        showsTime      :    false,            // will display a time selector
        button         :    FieldName+"_button",   // trigger for the calendar (button ID)
        singleClick    :    true,           // double-click mode
        step           :    1                // show all years in drop-down boxes (instead of every other year as default)
    });
  }         
   
   
 function ActivateSavingsManagement(vendorNumber)
     {
     alert(vendorNumber);
     }  
     
     
