
/*
this function assigns alternating row colors to table with id=tableId

Usage:
1. include this *.js file in the report page.
2. If dojo is used on the report page, make a call to paintRows() after </table>. E.g.:
	<script type="text/javascript">paintRows('table1');</script>

3. If dojo is not used, window.onload call to paintRows() can be used. E.g.:
	window.onload = function(){
		paintRows('table1');
	}

4. Default class names are 'even' and 'odd'.
*/
function paintRows(tableId){
	switch (arguments.length){
		case 1:
			var classEven = 'even';
			var classOdd = 'odd';
		break;
		case 2:
			var classEven = arguments[1];
			var classOdd = 'odd';
		break;
		case 3:
			var classEven = arguments[1];
			var classOdd = arguments[2];
	}
	var r = document.getElementById(tableId).tBodies[0].rows; //get reference to an array of table rows. Assign class names to even / odd rows.
	for(var i=0; i < r.length; i++){
		var tmp = r[i].className;
		if(tmp.length > 0)
			r[i].className = (i % 2 == 0) ? tmp+' '+classEven : tmp+' '+classOdd; 
		else	
			r[i].className = (i % 2 == 0) ? classEven : classOdd; 
	}
}

function doHighlightRows(TableId){
	NumArgs = arguments.length;
	ClsHighLight = (NumArgs >=2)? arguments[1]: "highLight"; //assuming arg. 2 contain tr highlight class name
	SkipFirstRow = (NumArgs >=3)?arguments[2]: false; //assuming arg 3 contain true/false, default false
	SkipLastRow = (NumArgs >=4)?arguments[3]: false; //assuming arg 4 contain true/false, default false	
	t = document.getElementById(TableId).tBodies[0];
	if(!t) t = document.getElementById(TableId).rows;
	if(t){
		r = t.rows		
		for(i=0; i < r.length; i++){
			if(SkipFirstRow && i==0){continue;} //skip first row
			if((i+1) == r.length && SkipLastRow){break;}	//skip last row			
			r[i].onmouseover = function(){
				this.setAttribute("OldClass", this.className);
				this.className = ClsHighLight;
			};
			r[i].onmouseout = function(){
				this.className = this.getAttribute("OldClass");
			};
		}
	}
}

function isValidDate(strDate) {
	var pattern = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
	var mDate = strDate.match(pattern);
	
	if (mDate == null) {
		//alert("here");	
		return false;
	}
	
	
	var validDay = [1, 2 ,3 ,4 ,5 ,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31];
	var validMon = [1,2,3,4,5,6,7,8,9,10,11,12];
	var monHasNo31Day = [4, 6, 9,11];
	var zeroPattern = /^0*/gi;
	var m = mDate[1];
		m = m.replace(zeroPattern, "");
		m = parseInt(m);
	var d = mDate[3];
		d = d.replace(zeroPattern, "");
		d = parseInt(d);
	var y = mDate[5];
		y = y.replace(zeroPattern, "");
		y = parseInt(y);
	var isLeapYear = (y % 4 == 0 && (y % 100 != 0 || y % 400 == 0));	
	if(jQuery.inArray(d, validDay) == -1 ||
		jQuery.inArray(m, validMon) == -1 ||
		(jQuery.inArray(m, monHasNo31Day) != -1 && d == 31) ||
		(m == 2 && (d > 29 || (d == 29 && !isLeapYear)))
	){
		return false;
	}

	return true; 
}
/*
* var time = new Date();
* time.setTime(time.getTime());	
* i.e => set_cookie( 'worksafe', worksafe, (time + 24 * 3600 * 90), '/', '.modelmayhem.com', '' );
*/
function set_cookie( name, value, expires, path, domain, secure ) {
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime(today.getTime());

	/*
	if the expires variable is set, make the correct
	expires time, the current script below will set
	it for x number of days, to make it for hours,
	delete * 24, for minutes, delete * 60 * 24
	*/
	if ( expires )
	{
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );

	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
	( ( path ) ? ";path=" + path : "" ) +
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}
function getCookieValue(cookie_name){
   var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );
   var value = '';
   if ( results ){
     value = ( unescape ( results[2] ) );
   }
   else{
    value = null;
   }
   return value;
}

function IsNumeric(sText)

{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;


   for (i = 0; i < sText.length && IsNumber == true; i++)
      {
      Char = sText.charAt(i);
      if (ValidChars.indexOf(Char) == -1)
         {
         IsNumber = false;
         }
      }
   return IsNumber;

   }

/////function allow input number only and one dot if dec equal true
function doDigit(myfield, e, dec){		
	var key, keychar;
	if (window.event){key = window.event.keyCode;}
	else if (e){key = e.which;}
	else{return true;}	
	
	//allow input (.)dot (once only) with money fields
	if(dec){if(key==46){
			oneDotOnly = /([.]{1})/;
			if(myfield.value.search(oneDotOnly) == -1){return true; }
			else{ return false;}}}	
	if(arguments[3]){ //assumming fourth param allow negative sign (-)
		if(key == 45){			
			if(myfield.value.indexOf("-") == -1){return true; }
			else{ return false;}
		}
	}
	keychar = String.fromCharCode(key);
	if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27) || key == 118 ){return true;}
	else if ((("1234567890").indexOf(keychar) > -1)){return true;}
	else{return false;}
}

function checkNumbers(myField, e) {
	var onlyNumbers = /^([0-9]+)$/;
	myField.value = $.trim(myField.value);
	if (myField.value.search(onlyNumbers) == -1) {
		myField.value = '';
	}
}

/************************************************
DESCRIPTION: Validates that a string contains only
    valid dates with 2 digit month, 2 digit day,
    4 digit year. Date separator can be ., -, or /.
    Uses combination of regular expressions and
    string parsing to validate date.
    Ex. mm/dd/yyyy or mm-dd-yyyy or mm.dd.yyyy

PARAMETERS:
   strValue - String to be tested for validity

RETURNS:
   True if valid, otherwise false.

REMARKS:
   Avoids some of the limitations of the Date.parse()
   method such as the date separator character.
*************************************************/
function validateUSDate( strValue ) {

  var objRegExp = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/

  //check to see if in correct format
  if(!objRegExp.test(strValue))
    return false; //doesn't match pattern, bad date
  else{
    var strSeparator = strValue.substring(2,3)
    var arrayDate = strValue.split(strSeparator);
    //create a lookup for months not equal to Feb.
    var arrayLookup = { '01' : 31,'03' : 31,
                        '04' : 30,'05' : 31,
                        '06' : 30,'07' : 31,
                        '08' : 31,'09' : 30,
                        '10' : 31,'11' : 30,'12' : 31}
    var intDay = parseInt(arrayDate[1],10);

    //check if month value and day value agree
    if(arrayLookup[arrayDate[0]] != null) {
      if(intDay <= arrayLookup[arrayDate[0]] && intDay != 0)
        return true; //found in lookup table, good date
    }

    //check for February (bugfix 20050322)
    //bugfix  for parseInt kevin
    //bugfix  biss year  O.Jp Voutat
    var intMonth = parseInt(arrayDate[0],10);
    if (intMonth == 2) {
       var intYear = parseInt(arrayDate[2]);
       if (intDay > 0 && intDay < 29) {
           return true;
       }
       else if (intDay == 29) {
         if ((intYear % 4 == 0) && (intYear % 100 != 0) ||
             (intYear % 400 == 0)) {
              // year div by 4 and ((not div by 100) or div by 400) ->ok
             return true;
         }
       }
    }
  }
  return false; //any other values, bad date
}

/*This function validates if the key pressed is different of a numeric value to just allow  <1,2,3,4,5,6,7,8,9,0> values */
function InputNumericValuesOnly(e){
	if (window.event){key = window.event.keyCode;}
	else if (e){key = e.which;}
	else{return true;}	
    if(!(key==48||key==49||key==50||key==51||key==52||key==53||key==54||key==55||key==56||key==57||key==8||key==0)){
    	return false;
    }
}
function getMMSERVICE(){
	var strMMSERVICE = $('meta[name=MMSERVICE]').attr("content");
	return  strMMSERVICE;
}

function empty(text){
	var trim = text.replace(/^\s+|\s+$/g,"");
	if( trim == '' ) return true;
	return false;
}
