var forbiddenChars = "\\/:*?\"<>|";
var FILE_NAME_ERR = "File name cannot contain any of these characters : " + forbiddenChars;
var EMPTY_NAME_ERR = "File/Folder name cannot be empty";


// check if each character is between a-z (97-122), A-Z (65-90), 0-9 (48-57), .(46), _(95)
// check and return false if the first character is special character
function checkAlphanumeric(text) {
	i = 0;
	a = text.charCodeAt(0);
	
	if (a>122 || a<48 || (a>57&&a<65) || (a>90&&a<97)) {
			//alert("first character must be alphanumeric");
			return "First character must be in alphanumeric";
	} 

	while (i<text.length) {
		b = text.charCodeAt(i);
		
		if ( (b>122 || b<48 || (b>57&&b<65) || (b>90&&b<97)) && (b!=46) && (b!=95) ) {
			return "Only alphabertics, numbers, dot(.) and underscore(_) are allowed";
		}
		else {
			i++;
		}
	}
	return "";
}

// check if each character is between a-z (97-122), A-Z (65-90), 0-9 (48-57)
function checkAlphanumeric1(text) {
	i = 0;
	
	while (i<text.length) {
		b = text.charCodeAt(i);
		
		if (b>122 || b<48 || (b>57&&b<65) || (b>90&&b<97)) {
			return "Password may consist of a-z and 0-9.";
		}
		else {
			i++;
		}
	}
	return "";
}

function trim(text) {
	var isCharFound = false;
	// trim right
	while (!isCharFound&&text.length !=0) {
   		ch = text[text.length-1];
   		if (ch == " ")
      		text = text.substring(0, text.length-1);
   		else
      		isCharFound = true;
	}

	// trim left
	isCharFound = false;

	while (!isCharFound&&text.length!=0) {
   		ch = text[0];
		if (ch == " ")
      		text = text.substring(1, text.length);
		else
			isCharFound = true;
	}
	return text;
}

function getStyleObj(objId) {
	if (document.getElementById)	// W3C DOM
		return document.getElementById(objId).style;
	if (document.layers)		// NN 4
		return document.layers[objId];
	if (document.all)
		return document.all(objId).style;
	
	return null;
}

/* generic check/uncheck all checkboxes
	parameter
	@ chkall_id = id (not name) for check-all checkbox
	@ chkbox_id = id (not name) for all checkboxes
	try to give values to checkboxes, so can be retrieved when form is passed to servlet
*/
function chkAllBox(chkall_id, chkbox_id) {
	var chkboxObj = document.getElementById(chkbox_id);
	
	if (chkboxObj == null)
		return;
		
	chkBoxName = document.getElementById(chkbox_id).name; 
	chkList = document.getElementsByName(chkBoxName);
	
	if (chkall_id.checked == true) {
			
		for (i = 0 ; i < chkList.length; i++) {
			chkList[i].checked = true;
		}
	}
	else {
		for (i = 0 ; i < chkList.length; i++)
			chkList[i].checked = false;
	}
	
}
/*
	generic checkboxes checking, if all checkboxes is checked, the check-all checkbox is checked too.
*/
function check(chkall_id, chkbox_id) {
	chkList = document.getElementsByName(chkbox_id.name);
	len = chkList.length;
	k = 0;

	for (i = 0; i < len; i++) {
		if (chkList[i].checked == true)
			k++;
	}
	if (k == len)
		document.getElementById(chkall_id).checked = true;
	else
		document.getElementById(chkall_id).checked = false;
	return;
}

/*
	function to validate whether a value is float or not
*/
function isFloat(value) {
	var v = parseFloat(value);
	
	if (isNaN(v) == true)
		return false;
	
	return true;
}

/*
	function to validate whether a value is integer or not
*/
function isInteger(value) {
	var v = parseInt(value);
	
	if (isNaN(v) == true)
		return false;
	
	return true;
}

/* 
	function to validate total characters in textarea
*/
function limitTextarea(field, maxlimit) {
	if (field.value.length > maxlimit)
		field.value = field.value.substring(0, maxlimit);
}

/* 
	function to show current time
*/
function curTime()
{
	var now=new Date()
	var hrs=now.getHours()
	var min=now.getMinutes()
	var sec=now.getSeconds()
	var don="AM"
	if (hrs>=12){ don="PM" }
	if (hrs>12) { hrs-=12 }
	if (hrs==0) { hrs=12 }
	if (hrs<10) { hrs="0"+hrs }
	if (min<10) { min="0"+min }
	if (sec<10) { sec="0"+sec }
	document.getElementById("clock").innerHTML=hrs+":"+min+":"+sec+" "+don
	setTimeout("curTime()",1000)
}

/* 
	function to reset background color
*/
function resetToNormalColor()
{
	
	var the_inputs=document.getElementsByTagName("input");	
	for(var n=0;n<the_inputs.length;n++){
	
		if(the_inputs[n].type=="text" || the_inputs[n].type=="password"){			
			the_inputs[n].style.border = "1px solid #336699";	
			the_inputs[n].style.backgroundColor  = "#FFFFFF";
		}
	
	}			
	
	var the_select=document.getElementsByTagName("select");
	for(var n=0;n<the_select.length;n++){
		the_select[n].style.border = "1px solid #336699";	
		the_select[n].style.backgroundColor  = "#FFFFFF";	
	}
}

/* 
	function to change background color
*/

function warnChangeColor(theField)
{
	theField.style.border ="1px solid #c30";	
	theField.style.backgroundColor  ="#F0B1B3";
}

/*
	function to check if file/folder name consist of forbidden characters \/:*?"<>|;
*/

function isValidFileNameWin(inputString) {
	val = inputString;
	
	isFound = false;
	for (i = 0; i < val.length && !isFound; i++) {
		j = val.charCodeAt(i);
		if (j == 92 || j == 47 || j == 58 || j == 42 || j == 63 || j == 34 || j == 60 || j == 62 || j == 124) {
			isFound = true;
		}
	}
	if (isFound) {
		return false;
	}
	
	return true;
}

function opendoc(link) {
	var top = (window.screen.height - 800) / 2;
	var left = (window.screen.width - 850) / 2;
	window.open(link, '', 'scrollbars=yes,width=850,height=800,top=' + top + ',left=' + left);
}

function isNumber(digit) {
	var invalids = "0123456789";
	for(i=0; i<digit.length; i++) {
		if(invalids.indexOf(digit.charAt(i)) == -1 ) {
			return false;
		}
    }
	return true;
}