var weekend = [0,6];
var gNow = new Date();  
var ggWinCal;
isNav = (navigator.appName.indexOf("Netscape") != -1) ? true : false;
isIE = (navigator.appName.indexOf("Microsoft") != -1) ? true : false;

Months = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"];

// Non-Leap year Month days..
DOMonth = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
// Leap year Month days..
lDOMonth = [0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

function get_month(monthNo) {  
	return Months[monthNo];
}

function getDaysOfMonth(monthNo, p_year) {
	if ((p_year % 4) == 0) {
		if ((p_year % 100) == 0 && (p_year % 400) != 0)
			return DOMonth[monthNo];

		return lDOMonth[monthNo];
	} else
		return DOMonth[monthNo];
}

function trim(str) {
 if (str == null)
  return "";
 if (str.length <1)
    return "";
 var i =0;
 while (str.charAt(i) == ' ')
        i++;
 str = str.substr(i);
 i = str.length;
 while (str.charAt(--i) == ' ')
        ;
 str = str.substr(0, (str.length + 1 + (i - str.length)));
 return str;
}//end of trim()
/*************************/
/**
* This function trims all text fields of the given form
*/
function trimFormFields(frm) {
  if (typeof(frm) != 'object'){
    alert("The argument supplied if not an object.\nThis function requires a document.form object.");
    return;
  }
  var i=0, len = frm.elements.length;
  for (i=0; i < len; i++){
    if (frm.elements[i].type == 'text' || frm.elements[i].type == 'textarea') {
       frm.elements[i].value = trim(frm.elements[i].value);
    }
  }
}//end of trimFormFields
/*************************/
/*
* This function returns true if 
* string contains " or '.
* Returns false otherwise
*/
function isContainsQuotes(str){
 str = trim(str);
 if (str.length < 1)
    return false;
 if (str.indexOf('\'')  >= 0)
    return true;
 if (str.indexOf('\"')  >= 0)
    return true;
   
  return false;

}

/*************************/
/*
* This function returns true if 
* string contains A-Z, a-Z, space, and a dot (.).
* Returns false otherwise
*/
function isValidName(str){
 str = trim(str);
 if (str.length < 1)
    return false;
    
  for (i=0; i < str.length; i++){
    if ((str.charAt(i) >= 'a') && (str.charAt(i) <= 'z'))
      continue;
    if ((str.charAt(i) >= 'A') && (str.charAt(i) <= 'Z'))
      continue;
    if (str.charAt(i) == ' ')
      continue;
    if (str.charAt(i) == '.')
      continue;
    return false;
  }
 
  if (str.indexOf('.') != str.lastIndexOf('.'))
    return false;
  if ((str.charAt(0) == '.') || (str.charAt(str.length-1) == '.'))
    return false;
   
  return true;

}
/*
* This function returns true if 
* string contains A-Z, a-Z, 0-9, _,dot and space.
* Returns false otherwise
*/
function isValidSentence(str){
 str = trim(str);
 if (str.length < 1)
    return false;
    
  for (i=0; i < str.length; i++){
    if ((str.charAt(i) >= 'a') && (str.charAt(i) <= 'z'))
      continue;
    if ((str.charAt(i) >= 'A') && (str.charAt(i) <= 'Z'))
      continue;
    if ((str.charAt(i) >= '0') && (str.charAt(i) <= '9'))
      continue;
    if (str.charAt(i) == '_')
      continue;
    if (str.charAt(i) == ' ')
      continue;
    if (str.charAt(i) == '.')
      continue;
     
    return false;
    
  }
 
  return true;

}
/*************************/
/*
* This function returns true if 
* string contains A-Z, a-Z, 0-9, _,space.
* Returns false otherwise
*/
function isValidUPostCord(str){
 str = trim(str);
 if (str.length < 1)
    return false;
    
  for (i=0; i < str.length; i++){
    if ((str.charAt(i) >= 'a') && (str.charAt(i) <= 'z'))
      continue;
    if ((str.charAt(i) >= 'A') && (str.charAt(i) <= 'Z'))
      continue;
    if ((str.charAt(i) >= '0') && (str.charAt(i) <= '9'))
      continue;
    if (str.charAt(i) == '_')
      continue;
    if (str.charAt(i) == ' ')
      continue;  
	  
    return false;
    
  }
 
  return true;

}
/*************************/
/*
* This function returns true if 
* string contains A-Z, a-Z, 0-9, _ and space.
* Returns false otherwise
*/
function isValidRegterNo(str){
 str = trim(str);
 if (str.length < 1)
    return false;
    
  for (i=0; i < str.length; i++){
    if ((str.charAt(i) >= 'a') && (str.charAt(i) <= 'z'))
      continue;
    if ((str.charAt(i) >= 'A') && (str.charAt(i) <= 'Z'))
      continue;
    if ((str.charAt(i) >= '0') && (str.charAt(i) <= '9'))
      continue;
    if (str.charAt(i) == '_')
      continue;
	if (str.charAt(i) == ' ')
      continue;
     
    return false;
    
  }
 
  return true;

}
/*************************/
/*
* This function returns true if 
* string contains A-Z, a-Z, 0-9, _.
* Returns false otherwise
*/
function isValidUID(str){
 str = trim(str);
 if (str.length < 1)
    return false;
    
  for (i=0; i < str.length; i++){
    if ((str.charAt(i) >= 'a') && (str.charAt(i) <= 'z'))
      continue;
    if ((str.charAt(i) >= 'A') && (str.charAt(i) <= 'Z'))
      continue;
    if ((str.charAt(i) >= '0') && (str.charAt(i) <= '9'))
      continue;
    if (str.charAt(i) == '_')
      continue;
     
    return false;
    
  }
 
  return true;

}

/*************************/
function isANumber(str){
  str = trim(str);
  for (i=0; i < str.length; i++){
    if ((str.charAt(i) >= '0') && (str.charAt(i) <= '9'))
      continue;
    if (str.charAt(i) == '.')
      continue;
    return false;
  }
 
  //Can not have two dots (.)
  if (str.indexOf('.') != str.lastIndexOf('.'))
    return false;
   
  return true;

}
/*************************/
function isADoubleNumber(str){
  str = trim(str);
  for (i=0; i < str.length; i++){
    if ((str.charAt(i) >= '0') && (str.charAt(i) <= '9'))
      continue;
    if (str.charAt(i) == '.')
      continue;
	if (str.charAt(i) == ',')
      continue;
    return false;
  }
 
  //Can not have two dots (.)
  if (str.indexOf('.') != str.lastIndexOf('.'))
    return false;
   
  return true;

}


/*************************/
function isValidTelNum(str){
  if (str == null)
     return false;
  str = trim(str);
    
  if (str.length < 3 )
     return false;
     
  for (i=0; i < str.length; i++){
    if ((str.charAt(i) >= '0') && (str.charAt(i) <= '9'))
      continue;
    if (str.charAt(i) == '-')
      continue;
    if (str.charAt(i) == ' ')
      continue;
    return false;
  }
    
  return true;

}
/*************************/
function isValidMobile(str){
  if (str == null)
     return false;
  str = trim(str);
    
  if (str.length < 8 || str.length > 15)
     return false;
    
  for (i=0; i < str.length; i++){
    if ((str.charAt(i) >= '0') && (str.charAt(i) <= '9'))
      continue;
    return false;
  }
    
  return true;

}
/*************************/
function isInteger(str){
  if (!isANumber(str))
   return false;
  if (str.indexOf('.') != -1)
    return false;
   
  return true;

}
/*************************/
function isValidZip(str){
  str = trim(str);

  if (str.length < 5)
   return false;
  for (i=0; i < str.length; i++){
    if ((str.charAt(i) >= '0') && (str.charAt(i) <= '9'))
      continue;
    return false;
  }
   
  return true;

}
/*************************/
function isValidEMail(str) {
 if (str == null)
  return false;
  str = trim(str);
 if (str.length <6)    return false;   
 if (str.indexOf('@') == -1)    return false;
 if (str.indexOf('@') == 0)    return false;
 if (str.indexOf('@') == (str.length-1))    return false;
 if (str.indexOf("@@") != -1)    return false;
 if (str.indexOf('@') != str.lastIndexOf('@'))    return false;
 if (str.indexOf('.@') != -1)    return false;
 if (str.indexOf('@.') != -1)    return false;
 if (str.indexOf(' ') != -1)    return false;
 if (str.indexOf('.') == -1)    return false;
 if (str.indexOf('.') == 0)    return false;
 if (str.indexOf('.') == (str.length-1))    return false;
 if (str.indexOf('..') != -1)    return false;
 if (str.indexOf('<') != -1)    return false;
 if (str.indexOf('>') != -1)    return false;
 if (str.indexOf('=') != -1)    return false;
 if (str.indexOf('~') != -1)    return false;
 if (str.indexOf('^') != -1)    return false;
 if (str.indexOf('%') != -1)    return false;
 if (str.indexOf('+') != -1)    return false;
 if (str.indexOf('$') != -1)    return false;
 if (str.indexOf('#') != -1)    return false;
 if (str.indexOf('!') != -1)    return false;
 if (str.indexOf('(') != -1)    return false;
 if (str.indexOf(')') != -1)    return false;
 if (str.indexOf('\\') != -1)    return false;
 if (str.indexOf('/') != -1)    return false;
 if (str.indexOf('\"') != -1)    return false;
 if (str.indexOf('\'') != -1)    return false;
 if (str.indexOf('?') != -1)    return false;
 if (str.indexOf(';') != -1)    return false;
 if (str.indexOf(',') != -1)    return false;
 if (str.indexOf('\t') != -1)    return false;
 if (str.indexOf('*') != -1)    return false;
 if (str.indexOf('|') != -1)    return false;

 return true;
 
}//end of isValidEMail()
/*************************/


/*
* This function returns true is the given date parameters
* form a valid date. else returns false.
*/
function isValidDate(dd,mm,yyyy){
  if (mm < 0 || mm > 12)
     return false;
  if (dd < 0 || dd > 31)
     return false;
  if (getDaysOfMonth(mm,yyyy) < dd) 
     return false;
  return true; 
}

/*
* This function returns true if first date is before second date
* other wise  returns false.
*/
function isDate1BeforeDate2(d1,m1,y1, d2,m2,y2){
  date1 = new Date(y1, m1,d1);
  date2 = new Date(y2, m2,d2);
  if (date1.getTime() < date2.getTime())
     return true; 
  return false;
}

function isDate1BeforeOrEqualToDate2(d1,m1,y1, d2,m2,y2){
  date1 = new Date(y1, m1,d1);
  date2 = new Date(y2, m2,d2);
  if (date1.getTime() <= date2.getTime())
     return true; 
  return false;
}

function isDate1AfterDate2(d1,m1,y1, d2,m2,y2){
  date1 = new Date(y1, m1,d1);
  date2 = new Date(y2, m2,d2);
  if (date1.getTime() > date2.getTime())
     return true; 
  return false;
}


function isDate1AfterOrEqualToDate2(d1,m1,y1, d2,m2,y2){
  date1 = new Date(y1, m1,d1);
  date2 = new Date(y2, m2,d2);
  if (date1.getTime() >= date2.getTime())
     return true; 
  return false;
}




function dateAdd(p_Interval, p_Number, p_Date){
	if(!isDate(p_Date)){return "invalid date: '" + p_Date + "'";}
	if(isNaN(p_Number)){return "invalid number: '" + p_Number + "'";}	

	p_Number = new Number(p_Number);
	var dt = new Date(p_Date);
	switch(p_Interval.toLowerCase()){
		case "yyyy": {// year
			dt.setFullYear(dt.getFullYear() + p_Number);
			break;
		}
		case "q": {		// quarter
			dt.setMonth(dt.getMonth() + (p_Number*3));
			break;
		}
		case "m": {		// month
			dt.setMonth(dt.getMonth() + p_Number);
			break;
		}
		case "y":		// day of year
		case "d":		// day
		case "w": {		// weekday
			dt.setDate(dt.getDate() + p_Number);
			break;
		}
		case "ww": {	// week of year
			dt.setDate(dt.getDate() + (p_Number*7));
			break;
		}
		case "h": {		// hour
			dt.setHours(dt.getHours() + p_Number);
			break;
		}
		case "n": {		// minute
			dt.setMinutes(dt.getMinutes() + p_Number);
			break;
		}
		case "s": {		// second
			dt.setSeconds(dt.getSeconds() + p_Number);
			break;
		}
		case "ms": {		// second
			dt.setMilliseconds(dt.getMilliseconds() + p_Number);
			break;
		}
		default: {
			return "invalid interval: '" + p_Interval + "'";
		}
	}
	return dt;
}


function isDate(p_Expression){
	return !isNaN(new Date(p_Expression));		// <<--- this needs checking
}
function dateDiff(p_Interval, p_Date1, p_Date2, p_firstdayofweek, p_firstweekofyear){
	if(!isDate(p_Date1)){return "invalid date: '" + p_Date1 + "'";}
	if(!isDate(p_Date2)){return "invalid date: '" + p_Date2 + "'";}
	var dt1 = new Date(p_Date1);
	var dt2 = new Date(p_Date2);

	// get ms between dates (UTC) and make into "difference" date
	var iDiffMS = dt2.valueOf() - dt1.valueOf();
	var dtDiff = new Date(iDiffMS);

	// calc various diffs
	var nYears  = dt2.getUTCFullYear() - dt1.getUTCFullYear();
	var nMonths = dt2.getUTCMonth() - dt1.getUTCMonth() + (nYears!=0 ? nYears*12 : 0);
	var nQuarters = parseInt(nMonths/3);	//<<-- different than VBScript, which watches rollover not completion
	
	var nMilliseconds = iDiffMS;
	var nSeconds = parseInt(iDiffMS/1000);
	var nMinutes = parseInt(nSeconds/60);
	var nHours = parseInt(nMinutes/60);
	var nDays  = parseInt(nHours/24);
	var nWeeks = parseInt(nDays/7);


	// return requested difference
	var iDiff = 0;		
	switch(p_Interval.toLowerCase()){
		case "yyyy": return nYears;
		case "q": return nQuarters;
		case "m": return nMonths;
		case "y": 		// day of year
		case "d": return nDays;
		case "w": return nDays;
		case "ww":return nWeeks;		// week of year	// <-- inaccurate, WW should count calendar weeks (# of sundays) between
		case "h": return nHours;
		case "n": return nMinutes;
		case "s": return nSeconds;
		case "ms":return nMilliseconds;	// millisecond	// <-- extension for JS, NOT available in VBScript
		default: return "invalid interval: '" + p_Interval + "'";
	}
	
}
