/*
	This utility file is created without any dependencies to avoid 
	conflict on what ever technology a developer will user.
	
	All scripts under here are basic and standard and that is compatible to any browser.

*/


// returns numeric character only  from 0-9
// usefull for keypress,key down etc.
function isNumericEvent(evt) {
    evt = (evt) ? evt : event;
    var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : 
        ((evt.which) ? evt.which : 0));
    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
        return false;
    }
    return true;
}
//check value if numeric
function isNumeric(content) {
 	if (parseFloat(content,10)==(content*1)) {
 		return true;
 	}
 	return false;
}

// check if the string is null or empty
function isEmpty(content){
	if (content==null)
		return true;
	if ((content.length == 0) || (content == "") || (content == "null")) return true;
	return false;
}

//check if user input is a valid email address
function isValidEmail(_obj){
	if (isEmpty(_obj.value)){
		return false;
	}
	
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(_obj.value)){
		return true;
	}
	_obj.focus();
	return false;
}
/*shows message alert or information in a page..
	@params
		_id = Element id
		_value = innerhtml value
		_display = style of display: 'block' if message is shown otherwise 'none'; 
*/
function messageInfoDisplay(_id,_value,_display){
	if (_value!=null){
		document.getElementById(_id).innerHTML = _value;
	}
	document.getElementById(_id).style.display=_display;
}

function hideElement(_id) {
	document.getElementById(_id).style.display="none";
}

//returns true if obj is null;
function isElementNull(obj){
	if (obj == null || obj == 'undefined')
		return true;
	return false;
}

//returns true if obj value is null;
function isElementValueNull(_id){
	var _obj = document.getElementById(_id);
	if (_obj.value == null || _obj == 'undefined')
		return true;
	return false;
}

function validateObj(_obj){
	if (isElementNull(_obj)){
		alert("Element "+ _id +" cannot be found!");
		return false;
	}
	return true;
}

//shows element on the browser
function elementShow(_id){
	var _obj = document.getElementById(_id);
	if (!validateObj(_obj))
		return;
	_obj.style.display="block";
}
//hides elements on the browser
function elementHide(_id){
	var _obj = document.getElementById(_id);
	if (!validateObj(_obj))
		return;
	_obj.style.display="none";
}
//disabled or enabled elements
function elementMode(_id,isEnabled){
	var _obj = document.getElementById(_id);
	if (!validateObj(_obj))
		return;
	_obj.disabled = isEnabled;
}
//returns true if checkbox isChecked otherwise false
function isChecked(_id){
	var _obj = document.getElementById(_id);
	if (!validateObj(_obj))
		return;
	return _obj.checked;
}
//sets checked box as checked or not
function setChecked(_id,_isChecked){
	var _obj = document.getElementById(_id);
	if (!validateObj(_obj))
		return;
	_obj.checked = _isChecked;	 
}
//clearFields
function clearField(_id){
	var _obj = document.getElementById(_id);
	if (!validateObj(_obj))
		return;
	_obj.value = '';	
}
//sets Elements background color
function setElementBackgroundColor(_id,_color){
	var _obj = document.getElementById(_id);
	if (!validateObj(_obj))
		return;
		
	if(navigator.appName == "Microsoft Internet Explorer"){
		document.all[_id].style.backgroundColor = _color;
		return;
	}
	_obj.style.backgroundColor = _color;
}
//sets element style color
function setElementStyleColor(_id,_color){
	var _obj = document.getElementById(_id);
	if (!validateObj(_obj))
		return;
	_obj.style.color = "black";	
}
//return true if equal
function isEqualTo(_value1,_value2){
	return _value1 == _value2;
}

function disableHref(_id){
	var _obj = document.getElementById(_id);
	if (!validateObj(_obj))
		return;
	_obj.removeAttribute("href");
}

function enableHref(_id,_value){
	var _obj = document.getElementById(_id);
	if (!validateObj(_obj))
		return;
	_obj.setAttribute("href",_value);
}

/** DATE VALIDATION **/
//Date validation
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this;
}
//validates date
function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		return false;
	}
	if (strMonth.length<1 || month<1 || month>12){
		return false;
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		return false;
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		return false;
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		return false;
	}
	return true;
}

function showLoaderPopup(_text) {
	jQuery('#popup').css('display','none');
	jQuery('#popupEditProfile').css('display','none');
	jQuery('#popupLoader #title ').text(_text);
	showModal("popupLoader");
}
/** DATE VALIDATION **/

var urlPath = null;
function initializePath(path){
	urlPath = path;
}