/*------------------------------------------------------*/
function rvOnLoad(){
	if(document.getElementById('loginInfo')){	//only happens when page head exist
		//var userInfo = ReadCookie('usfdui');
		//SetCookie('usfdui', ???);
		//document.getElementById('userLogin').value = ??;
		//document.getElementById('userPasswod').value = ??;
	}
}

/*------------------------------------------------------*/
function whichBrowser () {

   browser_name    = navigator.appName;
   browser_version = parseFloat(navigator.appVersion);
  
   if (browser_name == "Netscape")   {
       ret_value = 'Firefox';
   } else if (browser_name == "Microsoft Internet Explorer")  {
       ret_value = 'IE';
   } else {
       ret_value = 'Other';
   }
   return ret_value;
}

/*------------------------------------------------------*/
function openBrowserWindow( pUrl, pWinName, pAttributes, pFocus  ) {

  newwindow = window.open( pUrl, pWinName, pAttributes );
  if ( pFocus == 'y' ) {
    newwindow.focus();
  }
  return newwindow;
 }
 
/* ----------------------------------------- */
function scrollFunc( x, y ) {
	if (whichBrowser()=='Firefox')  window.scroll(x, y);
	else  window.scrollTo(x, y); 	
}


/*------------------------------------------------------*/
function logout() {
	location.href = "../user/welcome.php?at=lo";
}

/*------------------------------------------------------*/
function handleAjaxError(o){
	alert(o.statusText);
}



/*------------------------------------------------------*/
function pad( tstr, wchr, wlen) {
	tstr = String(tstr);
	var ret = "";
	for ( var i=0; i< (wlen-tstr.length); i++ ) {
		ret += wchr; 
	}
	ret += tstr;
	return ret;
}



/*------------------------------------------------------*/
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
	var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
	if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

/*------------------------------------------------------*/
function convertReturnToBr(strWithReturns){
	var strFinal = strWithReturns.replace(/\r\n/g, '<br>');
	strFinal = strFinal.replace(/\r/g, '<br>');
	strFinal = strFinal.replace(/\n/g, '<br>');
	strFinal = strFinal.replace(/\"/g, '&#34;');	//encode " because json strings seperated by it
	return strFinal;
	//return strWithReturns.replace(/\\n/g, '<br>');
}

/*------------------------------------------------------*/
function ReadCookie(cookieName) {
	var theCookie=""+document.cookie;
	var ind=theCookie.indexOf(cookieName);
	if (ind==-1 || cookieName=="") return ""; 
	var ind1=theCookie.indexOf(';',ind);
	if (ind1==-1) ind1=theCookie.length;
	var cookieValue = theCookie.substring(ind+cookieName.length+1,ind1);
	cookieValue = cookieValue.replace(/\+/,' ');		//since '' is replaced by + when we set the cookie on the server, and + can not be escaped, so...
	return unescape(cookieValue);
}

/*------------------------------------------------------*/
function SetCookie(cookieName,cookieValue) {
	var nDays = 365;	//we set the cookie for 1 year
	var today = new Date();
	var expire = new Date();
	if (nDays==null || nDays==0) nDays=1;
	expire.setTime(today.getTime() + 3600000*24*nDays);
	document.cookie = cookieName+"="+escape(cookieValue)+ ";expires="+expire.toGMTString();
}

/*------------------------------------------------------*/
function validEmail(email){
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(email)) 
		return true;
	else
		return false;
}


/*------------------------------------------------------*/
function delay(ts){
	if ( ts != '' ) {
		var date = new Date();
		var curDate = null;
		do { curDate = new Date(); }
		while(curDate-date < ts);
	}
}
/*------------------------------------------------------*/
function pause(milliseconds) {
    var now = new Date();
    var exitTime = now.getTime() + milliseconds;
    while(true)  {
      now = new Date();
      if(now.getTime() > exitTime) return;
    }
}

/*------------------------------------------------------*/
String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

/*------------------------------------------------------*/
/* check date, the format is like mm/dd/yyyy
 * @param input: the date value 
 */
function checkDate(entry) {
    var mo, day, yr;
    var re = /\b\d{1,2}[\/-]\d{1,2}[\/-]\d{4}\b/;
    var retVal = false;
    
    if (re.test(entry)) {
        var delimChar = (entry.indexOf("/") != -1) ? "/" : "-";
        var delim1 = entry.indexOf(delimChar);
        var delim2 = entry.lastIndexOf(delimChar);
        mo = parseInt(entry.substring(0, delim1), 10);
        day = parseInt(entry.substring(delim1+1, delim2), 10);
        yr = parseInt(entry.substring(delim2+1), 10);
        var testDate = new Date(yr, mo-1, day);

        if (testDate.getDate() == day) {
            if (testDate.getMonth() + 1 == mo) {
                if (testDate.getFullYear() == yr) {
                    retVal = true;
                } else {
                    retVal = "Invalid year";
                }
            } else {
                retVal = "Invalid month";
            }
        } else {
            retVal = "Invalid date";
        }
    } else {
        retVal = "Incomplete or not specified";
    }
    return retVal;
}

/*------------------------------------------------------*/
function clientInfo() {
	var x = navigator;
	var content = "";
	
	content  = "<center><table border='1' width='980' cellpadding='2' bgcolor='#000066'>\n"
	content += "<tr class='debugHeader'><td colspan='2' align='center'>CS NAVIGATOR</td></tr>\n"
	content += "<tr class='debugDetails'><td>App Code Name</td><td>" + x.appCodeName + "</td></tr>\n"
	content += "<tr class='debugDetails'><td>App Minor Version</td><td>" + x.appMinorVersion + "</td></tr>"
	content += "<tr class='debugDetails'><td>App Name</td><td>" + x.appName + "</td></tr>\n"
	content += "<tr class='debugDetails'><td>App Version</td><td>" + x.appVersion + "</td></tr>\n"
	content += "<tr class='debugDetails'><td>Cookie Enabled</td><td>" + x.cookieEnabled + "</td></tr>\n"
	content += "<tr class='debugDetails'><td>CPU Class</td><td>" + x.cpuClass + "</td></tr>\n"
	content += "<tr class='debugDetails'><td>MIME Types Class</td><td>" + x.mimeTypes + "</td></tr>\n"
	content += "<tr class='debugDetails'><td>OnLine</td><td>" + x.onLine + "</td></tr>\n"
	content += "<tr class='debugDetails'><td>Platform</td><td>" + x.platform + "</td></tr>\n"
	content += "<tr class='debugDetails'><td>Plugins</td><td>" + x.plugins + "</td></tr>\n"
	content += "<tr class='debugDetails'><td>User Agent</td><td>" + x.userAgent + "</td></tr>\n"
	content += "<tr class='debugDetails'><td>User Profile</td><td>" + x.userProfile + "</td></tr>\n" 
	content += "<tr class='debugDetails'><td>System Language</td><td>" + x.systemLanguage + "</td></tr>\n" 
	content += "<tr class='debugDetails'><td>User Language</td><td>" + x.userLanguage + "</td></tr>\n"
	content += "</table></center>\n"
	
	document.getElementById('clientSideVars').innerHTML = content;

}

