﻿//### Check max length
function SetMaxLength(txt,maxLen){
    if (txt.value.length > (maxLen-1)){
        alert("This field is limited to " + maxLen + " characters.\n" + "You have reached the limit.")
        txt.focus()
        txt.value = txt.value.substring(0, maxLen)
    }
} 

/************************************************************************************************************/
/************************************************************************************************************/

//### Email validator
function emailValidator(elem){
    var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
    if(elem.value.length > 0){
        if(elem.value.match(emailExp)){
            return true;
        }else{
            alert('Not a valid email address');
            elem.focus();
            return false;
        }
    }
}  

/************************************************************************************************************/
/************************************************************************************************************/

//### Clock
function jsInitClock()
{
    jsUpdateClock(); 
    setInterval('jsUpdateClock()', 1000)
}

/************************************************************************************************************/
/************************************************************************************************************/

function jsUpdateClock()
{
  var currentTime = new Date();

  var currentHours = currentTime.getHours();
  var currentMinutes = currentTime.getMinutes();
  var currentSeconds = currentTime.getSeconds();

  //### Pad the minutes and seconds with leading zeros, if required
  currentMinutes = (currentMinutes < 10 ? "0" : "") + currentMinutes;
  currentSeconds = (currentSeconds < 10 ? "0" : "") + currentSeconds;

  //### Choose either "AM" or "PM" as appropriate
  var timeOfDay = (currentHours < 12) ? "AM" : "PM";

  //### Convert the hours component to 12-hour format if needed
  currentHours = (currentHours > 12) ? currentHours - 12 : currentHours;

  //### Convert an hours component of "0" to "12"
  currentHours = (currentHours == 0) ? 12 : currentHours;

  //### Compose the string for display
  var currentTimeString = currentHours + ":" + currentMinutes + ":" + currentSeconds + " " + timeOfDay;

  //### Update the time display
  document.getElementById("clock").firstChild.nodeValue = currentTimeString;
}

/************************************************************************************************************/
/************************************************************************************************************/

//### Field Limitor
function textCounter(field,counter,maxlimit,linecounter){

	//### Text width
	var fieldWidth =  parseInt(field.offsetWidth);
	var charcnt = field.value.length;        

	//### Ttrim the extra text
	if (charcnt > maxlimit) { 
		field.value = field.value.substring(0, maxlimit);
	}
	else 
	{ 
	    //### Progress bar percentage
	    var percentage = parseInt(100 - (( maxlimit - charcnt) * 100)/maxlimit) ;
	    document.getElementById(counter).style.width =  parseInt((fieldWidth*percentage)/100)+"px";
	    document.getElementById(counter).innerHTML="Limit: "+percentage+"%"
	    
	    //### Color correction on style from CCFFF -> CC0000
	    setcolor(document.getElementById(counter),percentage,"background-color");
	}
}

function setcolor(obj,percentage,prop){
	obj.style[prop] = "rgb(80%,"+(100-percentage)+"%,"+(100-percentage)+"%)";
}

