/**
used to limit the number of characters users can type in a text area
field - an instance of textarea
max - the maximum allowable characters in the textarea
Usage on form,e.g.:
onkeyup="textCounter(this,127);"
*/
function textCounter(field,max) {
  var maxlimit=max;
  	//first compute the id of div, which is almost same as the text-area component.
	var val = field.id;
	var indx = val.lastIndexOf(':');
	var val2 = val.substring(0,indx+1);
	var divId = val2+'keyCount1';
	
  if ( field.value.length > maxlimit ){
    field.value = field.value.substring( 0, maxlimit );
    return false;
  }else{    
    var message = "Characters Remaining";
    document.getElementById(divId).innerHTML = maxlimit - field.value.length + " " + message;
  }
}

//used on viewProject.xhtml, to display message to user if he is not logged in
function informUser(button){
	if (button.disabled){
		alert("In order to place a bid on any project, login first.\nThanks");
	}else{
		//alert('opening');
	 	Richfaces.showModalPanel('mp',{width:550, top:200});
	}//else
}//func

//for closing the modal window, but only if no error msgs are displayed
function windowclose(panelName,msgId){//msgId is the id of the <h:messages element inside modal form
		if (document.getElementById(msgId)==null){
				Richfaces.hideModalPanel(panelName);
		};
}

//called in viewProject.xhtml page for populating the hidden fields when modal dialog opens
function populateReportViolationFields(formName,projectId, bidId, userId){
	document.getElementById(formName+":hiddenProjectId").value = projectId;
	document.getElementById(formName+":hiddenBidId").value = bidId;
	document.getElementById(formName+":hiddenUserId").value = userId;
}

//called from editNote.xhtml page for populating hidden fields when modal dialog is displayed
function populateEditBidNoteFields(formName,bidId,note){
	document.getElementById(formName+":hiddenBidId").value = bidId;
	document.getElementById(formName+":detailNote").value = 'Please wait...';//clear the previous
	updateBidId(bidId);//will call the bean methods 'AttacheNoteToBid' (jsFunction call)
}

        //a utility to restrict user's input
        //to call this function from a text filed use: [ onKeyPress="return restrictField(event,'()-0123456789')" ]
        // copyright 1999 Idocs, Inc. http://www.idocs.com
        // Distribute this script freely but keep this notice in place
        function restrictField(e,allowedChars){
              var key;
              var keychar;
              //var allowedChars = "()-0123456789";

              if (window.event)
              key = window.event.keyCode;
              else if (e)
              key = e.which;
              else
              return true;
              keychar = String.fromCharCode(key);
              keychar = keychar.toLowerCase();

              // control keys
              if ((key==null) || (key==0) || (key==8) ||
              (key==9) || (key==13) || (key==27) )
              return true;

              // alphas and numbers
              else if (((allowedChars).indexOf(keychar) > -1))
              return true;
              else
              return false;
        }//end func

//function invoked when input field looses focus
//field: the field object itself
//toDisplay: what to display if field looses focus
function inputFieldOnBlur(field,toDisplay){
	field.className = 'loginFormField';
	//if (field.value == '') field.value = toDisplay;
}
function inputFieldOnFocus(field,toDisplay){
	field.className = 'focus';
	//if (field.value == toDisplay) field.value = '';
}
/**
Called to dismiss a modal dialog window if an error message is not detected
parameter is the id of component within which child nodes are to be looked
*/
function findErrorImage(modalPanelId){
 var imgs,i,found;
 imgs=document.getElementsByTagName('img');
 for(i=0;i<imgs.length;i++){
 	var anSt = imgs[i].src;
 	var indx = anSt.search('error.gif');
 	if (indx!=-1) found = true;
 }
 //only hide modal panel if NO error condition was found
 if (!found) Richfaces.hideModalPanel(modalPanelId);
}

//code from digicert
function VerifySSL(num,old)
{
	var width = 480;
	var height = 525;
	var ww = screen.width;
	var hh = screen.height;
	var left = (ww - width) / 2;
	var top = (hh - height) / 2;
	window.open('https://www.digicert.com/custsupport/sspopup.php?order_id=' + num + '&hostname=','oo',"dependent=1,height="+height+",width="+width+",left="+left+",top="+top+",location=0,menubar=0,resizable=0,scrollbars=0,status=0,toolbar=0");
	return false;
}

//from: http://doc.infosnel.nl/javascript_trim.html
function trim(s){
	var l=0; var r=s.length -1;
	while(l < s.length && s[l] == ' ')
	{	l++; }
	while(r > l && s[r] == ' ')
	{	r-=1;	}
	return s.substring(l, r+1);
}

//when login form is loaded focus should be put on username if empty, otherwise on password field
//Note: this script will be executed on the onload event of body tag, hence all pages since we are using template (fulltemplate.xhtml)
function setFocus(){
	var userName = document.getElementById("loginForm:username");
	if (!userName) return;//don't proceed as this is not the login form
	if (trim(userName.value)==''){//if user name is empty then put the focus there
		userName.focus();
		return;
	}
	document.getElementById("loginForm:password").focus();//else put focus on password field
	/*the timezone offset, 'tzo' contains the time difference in hours (probably for future use)
	var tzo=(new Date().getTimezoneOffset()/60)*(-1);
	var hiddenField = document.getElementById("loginForm:hiddenTimezone");
	hiddenField.value = tzo;*/
}


