// Parse criterion
function parseCriterion(data) {
//	isc.logWarn("##### data= "+isc.echoAll(data)); 
	var whereSQL = "";
	var pop = "";		//Parent operator
	if (typeof data=="undefined" || typeof data==null || data==null) {return whereSQL;}
	if (data.operator) {pop = data.operator;}
	if (data.criteria) {
		var cArray = data.criteria;
		if (cArray.length>0) {whereSQL += "(";}
		for (var i=0;i<cArray.length;i++){
//			isc.logEchoAll(cArray[i]);
			// Serialize date for mysql where
			if (typeof cArray[i].start != "undefined" && (isA.Date(cArray[i].start) || isA.Date(cArray[i].end))) {
				cArray[i].start = cArray[i].start.toSerializeableDate();
				cArray[i].end = cArray[i].end.toSerializeableDate();
			} else if (typeof cArray[i].value != "undefined" && isA.Date(cArray[i].value)) {
				cArray[i].value = cArray[i].value.toSerializeableDate();
			}
			
			// Handle JobID for Overhead (null) comparison
			if (cArray[i].fieldName == "JobID" && cArray[i].value == "OH") {
				switch (cArray[i].operator) {
					case "contains": case "iContains":
					case "startsWith": case "iStartsWith":
					case "endsWith": case "iEndsWith":
					case "equals": case "iEquals":
					case "isNull":
						whereSQL += cArray[i].fieldName + " is null"; break;
					default: 
						whereSQL += cArray[i].fieldName + " is not null"; break;
				}
			}
			else {
				switch (cArray[i].operator) {
					case "equals": whereSQL += cArray[i].fieldName + " = '" + cArray[i].value +"'"; break; //exactly equal to (match case)
					case "iEquals": whereSQL += cArray[i].fieldName + " = '" + cArray[i].value +"'"; break; //exactly equal to (case insensitive)
					case "notEqual": whereSQL += "(" + cArray[i].fieldName + " != '" + cArray[i].value + "' or " + cArray[i].fieldName + " is null)"; break; //not equal to (match case)
					case "iNotEqual": whereSQL += "(" + cArray[i].fieldName + " != '" + cArray[i].value + "' or " + cArray[i].fieldName + " is null)"; break; //not equal to (case insensitive)
					case "greaterThan": whereSQL += cArray[i].fieldName + " > '" + cArray[i].value +"'"; break; //Greater than 
					case "lessThan": whereSQL += cArray[i].fieldName + " < '" + cArray[i].value +"'"; break; //Less than 
					case "greaterOrEqual": whereSQL += cArray[i].fieldName + " >= '" + cArray[i].value +"'"; break; //Greater than or equal to 
					case "lessOrEqual": whereSQL += cArray[i].fieldName + " <= '" + cArray[i].value +"'"; break; //Less than or equal to 
					case "contains": whereSQL += cArray[i].fieldName + " like '%" + cArray[i].value + "%'"; break; //Contains as sub-string (match case) 
					case "startsWith": whereSQL += cArray[i].fieldName + " like '" + cArray[i].value + "%'"; break; //Starts with (match case) 
					case "endsWith": whereSQL += cArray[i].fieldName + " like '%" + cArray[i].value + "'"; break; //Ends with (match case) 
					case "iContains": whereSQL += cArray[i].fieldName + " like '%" + cArray[i].value + "%'"; break; //Contains as sub-string (case insensitive) 
					case "iStartsWith": whereSQL += cArray[i].fieldName + " like '" + cArray[i].value + "%'"; break; //Starts with (case insensitive) 
					case "iEndsWith": whereSQL += cArray[i].fieldName + " like '%" + cArray[i].value + "'"; break; //Ends with (case insensitive) 
					case "notContains": whereSQL += "("+ cArray[i].fieldName + " not like '%" + cArray[i].value + "%' or " + cArray[i].fieldName + " is null)"; break; //Does not contain as sub-string (match case) 
					case "notStartsWith": whereSQL += cArray[i].fieldName + " not like '" + cArray[i].value + "%'"; break; //Does not start with (match case) 
					case "notEndsWith": whereSQL += cArray[i].fieldName + " not like '%" + cArray[i].value + "'"; break; //Does not end with (match case) 
					case "iNotContains": whereSQL += "("+ cArray[i].fieldName + " not like '%" + cArray[i].value + "%' or " + cArray[i].fieldName + " is null)"; break; //Does not contain as sub-string (case insensitive) 
					case "iNotStartsWith": whereSQL += cArray[i].fieldName + " not like '" + cArray[i].value + "%'"; break; //Does not start with (case insensitive) 
					case "iNotEndsWith": whereSQL += cArray[i].fieldName + " not like '%" + cArray[i].value + "'"; break; //Does not end with (case insensitive) 
					case "regexp":  break; //Regular expression match 
					case "iregexp":  break; //Regular expression match (case insensitive) 
					case "isNull": whereSQL += cArray[i].fieldName + " is null"; break; //value is null 
					case "notNull": whereSQL += cArray[i].fieldName + " is not null"; break; //value is non-null. Note empty string ("") is non-null 
					case "inSet":  break; //value is in a set of values. Specify criterion.value as an Array 
					case "notInSet":  break; //value is not in a set of values. Specify criterion.value as an Array 
					case "equalsField": whereSQL += cArray[i].fieldName + " = " + cArray[i].value; break; //matches another field (specify fieldName as criterion.value) 
					case "notEqualField": whereSQL += cArray[i].fieldName + " != " + cArray[i].value; break; //does not match another field (specified fieldName as criterion.value) 
					case "and": whereSQL += parseCriterion(cArray[i]); break; //all subcriteria (criterion.criteria) are true 
					case "not": whereSQL += parseCriterion(cArray[i]); break; //all subcriteria (criterion.criteria) are false 
					case "or": whereSQL += parseCriterion(cArray[i]); break; //at least one subcriteria (criterion.criteria) is true 
					case "between": whereSQL += "(" + cArray[i].fieldName + " > '" + cArray[i].start + "' AND " + cArray[i].fieldName + " < '" + cArray[i].end + "')"; break; //shortcut for "greaterThan" + "lessThan" + "and". Specify criterion.start and criterion.end 
					case "betweenInclusive": whereSQL += cArray[i].fieldName + " between '" + cArray[i].start + "' AND '" + cArray[i].end + "'"; break; //shortcut for "greaterOrEqual" + "lessOrEqual" + "and". Specify criterion.start and criterion.end 
					default: break;
				}
			}
			if (i<cArray.length-1) {whereSQL += " " + pop + " ";}
		}
		if (cArray.length>0) {whereSQL += ")";}
	}
//	isc.logWarn(isc.echoAll(whereSQL));
	return whereSQL;
}

function trim(str) {
	return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}

function pad(number, length) {
    var str = '' + number;
    while (str.length < length) {
        str = '0' + str;
    }
    return str;
}

function makeTimestamp( dateobj ) {
	if(dateobj === undefined) {
		var date = new Date();
	} else {
		var date = new Date( dateobj );
	}
	var yyyy = pad(date.getFullYear(),2);
    var mm = pad(date.getMonth() + 1,2);
    var dd = pad(date.getDate(),2);
    var hh = pad(date.getHours(),2);
    var min = pad(date.getMinutes(),2);
    var ss = pad(date.getSeconds(),2);
	
	var mysqlDateTime = yyyy + '-' + mm + '-' + dd + ' ' + hh + ':' + min + ':' + ss;
       
    return mysqlDateTime;
}

function makeDatestamp( dateobj ) {
	if(dateobj === undefined) {
		var date = new Date();
	} else {
		var date = new Date( dateobj );
	}
	var yyyy = pad(date.getFullYear(),2);
    var mm = pad(date.getMonth() + 1,2);
    var dd = pad(date.getDate(),2);
	
	var mysqlDate = yyyy + '-' + mm + '-' + dd;
       
    return mysqlDate;
}

// Function to set cookie
function set_cookie ( name, value, exp_sec, path, domain, secure ) {
// exp_sec - expirey time in seconds
  var cookie_string = name + "=" + escape ( value );

  if ( exp_sec )
  {
  	var time = new Date();
    var expires = new Date(time.getTime()+exp_sec*1000);
    cookie_string += "; expires=" + expires.toGMTString();
  }

  if ( path )
        cookie_string += "; path=" + escape ( path );

  if ( domain )
        cookie_string += "; domain=" + escape ( domain );
  
  if ( secure )
        cookie_string += "; secure";
  
  document.cookie = cookie_string;
}

// Function to get cookie
function get_cookie ( cookie_name )
{
  var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );

  if ( results )
    return ( unescape ( results[2] ) );
  else
    return null;
}


// Function to delete cookie
function delete_cookie ( cookie_name )
{
  var cookie_date = new Date ( );  // current date & time
  cookie_date.setTime ( cookie_date.getTime() - 1 );
  document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}


