/**********************************************************************************************
	              function.js Created by Websites Pundit
**********************************************************************************************/
function checkme() {
	var len=document.getElementsByName('checkbx[]').length;
	if(document.getElementById('check_n').checked==true) {
		for(i=1;i<=len;i++)	{
			var chk='checkbx'+i;
			document.getElementById(chk).checked=true;
			var trElm = document.getElementById(chk).parentNode.parentNode;
			trElm.className="selectedRow";
		}
	}
	else {
		for(i=1;i<=len;i++)	{
			var chk='checkbx'+i;
			document.getElementById(chk).checked=false;
			var trElm=document.getElementById(chk).parentNode.parentNode;
			//var cnt;
			if(i%2==1) {
				trElm.className="alterClass1";
			} else {
				trElm.className="alterClass2";
			}
		}
	}
}

function chngTrClass() {
	var chk1 = document.getElementById("check_n").value;
	var len=document.getElementsByName('checkbx[]').length;
	for(i=1;i<=len;i++)	{
		var chk='checkbx'+i;
		if(document.getElementById(chk).checked==true) {
			var trElm=document.getElementById(chk).parentNode.parentNode;
			trElm.className="selectedRow";
		}
		else {
			var trElm=document.getElementById(chk).parentNode.parentNode;
			document.getElementById("check_n").checked=false;
			if(i%2==1) {
				trElm.className="alterClass1";
			} else {
				trElm.className="alterClass2";
			}
		}
	}
}
	
function deleteme(pageUrl,frmName) {
	var len=document.getElementsByName('checkbx[]').length;
	for(i=1;i<=len;i++)	{
		var chk='checkbx'+i;
		if(document.getElementById(chk).checked==true) {
			var flag=1;
		}
	}
	if(flag=="1") {
		eval('document.'+frmName+'.action = "'+pageUrl+'?index=List&action=Active"');
		eval('document.'+frmName+'.submit()');
	}
	else {
		document.getElementById("msgCntnr").innerHTML='<div id="msgId" class="msgError" style="width:300px">Please select an item to change the status!!</div>';
	}
}

function deleteme_vn(pageUrl,frmName) {
	var len=document.getElementsByName('checkbx[]').length;
	for(i=1;i<=len;i++)	{
		var chk='checkbx'+i;
		if(document.getElementById(chk).checked==true) {
			var flag=1;
		}
	}
	if(flag=="1") {
		eval('document.'+frmName+'.action = "'+pageUrl+'?index=List&action=Delete"');
		eval('document.'+frmName+'.submit()');
	}
	else {
		document.getElementById("msgCntnr").innerHTML='<div id="msgId" class="msgError" style="position:absolute;top:145px;margin-left:30px;margin-right:100px;width:300px">Please select an item to delete!!</div>';
	}
}

function deleteme_n(pageUrl,frmName) {
	var len=document.getElementsByName('checkbx[]').length;
	for(i=1;i<=len;i++)	{
		var chk='checkbx'+i;
		if(document.getElementById(chk).checked==true) {
			var flag=1;
		}
	}
	if(flag=="1") {
		eval('document.'+frmName+'.action = "'+pageUrl+'?index=List&action=Inactive"');
		eval('document.'+frmName+'.submit()');
	}
	else {
		document.getElementById("msgCntnr").innerHTML='<div id="msgId" class="msgError" style="width:300px">Please select an item to change the status!!</div>';
	}
}

function setCheckBox(theForm,theStatus) {
	var sus=eval("window.document."+theForm+".elements.length");
	for(j=0;j<sus;j++) {
		if(eval("window.document."+theForm+".elements["+j+"].type")=="checkbox") {
			eval("window.document."+theForm+".elements["+j+"].checked="+theStatus);
		}
	}
}

function getDeleteConfirm(theURL,theValue,theMsg) {
	var str="Are you sure you want to delete this "+theMsg+" : "+theValue+" ?    ";
	document.getElementById("confrmDel").innerHTML=str;
	choice=confirm(str);
	if(choice) {
		window.location.href=theURL;
	} else {
		return false;
	}
}

function CheckFileField(theForm,theElement,theValue) {
	var fileVal=eval("window.document."+theForm+"."+theElement+".value");
	if(fileVal=="")	{
		alert("Please provide a "+theValue+" file    ");
		eval("window.document."+theForm+"."+theElement+".focus()");
		return false;
	}

	var ext=fileVal.substr(fileVal.lastIndexOf(".")).toLowerCase();
	if(ext!=".csv") {
		alert("Upload file should be .csv format    ");
		eval("window.document."+theForm+"."+theElement+".focus()");
		eval("window.document."+theForm+"."+theElement+".select()");
		return false;
	}

}

function isEmpty(theForm,theElement,theMess) {
	var str=eval("window.document."+theForm+"."+theElement+".value");
	if(str=="") {
		alert(""+theMess+" should not be empty    ");
		eval("window.document."+theForm+"."+theElement+".focus()");
		return false;
	}
}

function IsNumeric(theForm,theElement,theMess) {
	var str=eval("window.document."+theForm+"."+theElement+".value");
	if(str=="") {
		alert(""+theMess+" should not be empty    ");
		eval("window.document."+theForm+"."+theElement+".focus()");
		return false;
	}

	if(isNaN(str)==true || parseInt(str) <= 0) {
		alert(""+theMess+" should not be alpha numeric and always greater than Zero    ");
		eval("window.document."+theForm+"."+theElement+".focus()");
		eval("window.document."+theForm+"."+theElement+".select()");
		return false;
	}
}

function isEmail(theForm,theElement,theMess) {
	var str=eval("window.document."+theForm+"."+theElement+".value");
	if(str=="") {
		alert(""+theMess+" should not be empty !!! Please provide a valid email address    ");
		eval("window.document."+theForm+"."+theElement+".focus()");
		return false;
	}

	var re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,})+$/;
	if(re.test(str)==false) {
		alert("That is not a valid Email address. Please enter again.    ");
		eval("window.document."+theForm+"."+theElement+".focus()");
		eval("window.document."+theForm+"."+theElement+".select()");
		return false;
	}

}

function isSpecialChar(theForm,theElement,theMsg) {
	var iChars = "!@#$%^&*~`()+=-[]\\\';,/{}|\":<>?";
	for(var i=0;i<eval("window.document."+theForm+"."+theElement+".value.length");i++) {
		if(iChars.indexOf(eval("window.document."+theForm+"."+theElement+".value.charAt("+i+")"))!=-1) {
			alert(theMsg);
			eval("window.document."+theForm+"."+theElement+".focus()");
			eval("window.document."+theForm+"."+theElement+".select()");
			return false;
		}
	}
}

function setCombo(theForm,theElement,theMess) {
	var str=eval("window.document."+theForm+"."+theElement+".selectedIndex");
	if(str==0) {
		alert("Please select a value for "+theMess+"    ");
		eval("window.document."+theForm+"."+theElement+".focus()");
		return false;
	}
}

function _doPagination(page,query) {
	var d = window.document;
	var str = d.getElementById('cmbPage')[d.getElementById('cmbPage').selectedIndex].value;
	if(query == "") {
		window.location.href = page + "?page=" + str;
	} else {
		window.location.href = page + "?page=" + str + "&" + query;
	}
}

function LoadPopup(getFileName,getWindowName,getHeight,getWidth) {
	var _file = getFileName;
	var _window = getWindowName;
	var _toolbar = 0;
	var _menubar = 0;
	var _status = 1;
	var _resizable = 1;
	var _width = getWidth;
	var _height = getHeight;
	var _top = (screen.height - _height) / 2;
	var _left = (screen.width - _width) / 2;
	var _scrollbars = 1;
	var _condition = "toolbar=" + _toolbar + ",menubar=" + _menubar + ",status=" + _status + ",resizable=" + _resizable;
	_condition+=",width=" + _width + ",height=" + _height + ",left=" + _left + ",top=" + _top + ",scrollbars=" + _scrollbars + "";
	window.open(_file,_window,_condition);
}

function _doInputNumberInit(param) {
	if(navigator.appName.indexOf('Microsoft')!=-1) {
		document.getElementById(param).onkeypress = _doInputNumberOnly;
	} else {
		document.getElementById(param).addEventListener("keypress",_doInputNumberOnly, true);
	}
}

function _doInputNumberOnly(evt) {
	if(navigator.appName.indexOf('Microsoft')!=-1) {
		if(event.keyCode < 46 || event.keyCode > 57) {
			event.returnValue = false;
		}
	} else {
		if(evt.charCode && (evt.charCode < 46 || evt.charCode > 57)) {
			if(evt.preventDefault) {
				evt.preventDefault();
			}
		}
	}
}

function _doMoveCursor(presentElement,futureElement,targetLength) {
	var d = window.document;
	var p = eval("d.getElementById('" + presentElement + "').value.length");
	//alert(p);
	if(p == targetLength) {
		eval("d.getElementById('" + futureElement + "').focus()");
	}
}

function modelessDialogShow(url,width,height) {
	window.showModelessDialog(url,window,"dialogWidth:"+width+"px;dialogHeight:"+height+"px;edge:Raised;center:1;help:0;resizable:1;");
}

function modalDialogShow(url,width,height) {
	window.showModalDialog(url,window,"dialogWidth:"+width+"px;dialogHeight:"+height+"px;edge:Raised;center:1;help:0;resizable:1;maximize:1");
}

function move(fbox, tbox) {
	var arrFbox = new Array();
	var arrTbox = new Array();
	var arrLookup = new Array();
	var i;
	for (i = 0; i < tbox.options.length; i++) {
		arrLookup[tbox.options[i].text] = tbox.options[i].value;
		arrTbox[i] = tbox.options[i].text;
	}

	var fLength = 0;
	var tLength = arrTbox.length;
	for(i = 0; i < fbox.options.length; i++) {
		arrLookup[fbox.options[i].text] = fbox.options[i].value;
		if (fbox.options[i].selected && fbox.options[i].value != "") {
			arrTbox[tLength] = fbox.options[i].text;
			tLength++;
		} else {
			arrFbox[fLength] = fbox.options[i].text;
			fLength++;
   		}
	}

	arrFbox.sort();
	arrTbox.sort();
	fbox.length = 0;
	tbox.length = 0;
	var c;
	for(c = 0; c < arrFbox.length; c++) {
		var no = new Option();
		no.value = arrLookup[arrFbox[c]];
		no.text = arrFbox[c];
		fbox[c] = no;
	}

	for(c = 0; c < arrTbox.length; c++) {
		var no = new Option();
		no.value = arrLookup[arrTbox[c]];
		no.text = arrTbox[c];
		tbox[c] = no;
   	}
}

/**** for timer ****/
var secs
var timerID = null
var timerRunning = false
var delay = 1000

function InitializeTimer() {
    // Set the length of the timer, in seconds
    secs = 4
    StopTheClock()
    StartTheTimer()
}

function StopTheClock() {
    if(timerRunning)
        clearTimeout(timerID)
    timerRunning = false
}

function StartTheTimer() {
    if (secs==0) {
        StopTheClock()
        // Put something that is supposed to happen after the allotted time.
        // For example, displaying a message:
		if(document.getElementById("msgId")) {
			document.getElementById("msgId").style.display='none';
			InitializeTimer();
		}
    }
    else {
        //self.status = secs
        secs = secs - 1
        timerRunning = true
        timerID = self.setTimeout("StartTheTimer()", delay)
    }
}

function Filter4Characters(evt) {
	if(navigator.appName.indexOf('Microsoft')!=-1) {
		if(event.keyCode < 46 || event.keyCode > 57) {
			event.returnValue = false;
		}
	} else {
		if(evt.charCode && (evt.charCode < 46 || evt.charCode > 57)) {
			if(evt.preventDefault) {
				evt.preventDefault();
			}
		}
	}
}

