//Function - To submit the page.


	
var count=0;
var lenCount = 0;
function fnSubmit(objForm,hAction) {
	var theForm = eval("document."+objForm);	
	if(lenCount==0 || lenCount<0){
		alert('Please add the OD pair in the list');
		return;			
	}else{
		theForm.hAction.value=hAction;
		//startBlink();
		//fnShowProcessingMessage();
		theForm.submit();
		showLoader();
		
	}	
}/*
function doBlink() {

        //var blink = document.all.tags("BLINK");
		//var blink =document.getElementsByTagName("BLINK")[0];
		var blink=document.getElementById("blinkProcessing");
		blink.style.visibility = blink.style.visibility == "hidden" ? "visible" : "hidden";
	
	
	//for (var i=0; i<blink.length; i++)
//		blink[i].style.visibility = blink[i].style.visibility == "hidden" ? "visible" : "hidden"

		
		
}

function startBlink() {
	//if (document.all)
		setInterval("doBlink()",750)
}
*/
function fnShowProcessingMessage(){
var divtag=document.getElementById("divProcessingMsg");
divtag.style.display='block';


}
//Function - To submit the page .
function fnSubmitPage(objForm,hAction) {

      var theForm = eval("document."+objForm);
      theForm.hAction.value=hAction;
      if(hAction == 'addImport'){
            var theFile = theForm.fileName.value;
            var extn = theFile.substring(theFile.length - 4, theFile.length);
            if(trim(theFile) == '' ){
                  alert("Please select the file to be uploaded");
                  return;
            }else if(extn != '.xls'){
                  alert("Please submit the OD pair only in Excel format");
                  return;
            }else{
            	 // startBlink();
            	// fnShowProcessingMessage();
                  theForm.submit();  
                  showLoader();
            }
      }else{
            theForm.submit();
            showLoader();
      }
}

//Function - To add a particular ODpair to the list
function addRecord() {
	var theForm = document.frmRailMiles;
	var origin  = trim(theForm.txtOrigin.value).toUpperCase();
	var destination  = trim(theForm.txtDestination.value).toUpperCase();
	
	var arrayOrigin = new Array();
	var arrayDestination = new Array();
	var station = '';
	var state = '';
	
	var regExStn = /^[0-9A-Za-z,&()/.''\- ]*$/;
	var regExSt = /^[A-Za-z,'' ]*$/;	
	
	if(fnValid()){
		if (origin == '' || destination == '') {
			alert('Origin and Destination should be entered');
			return;
		} else {	
			if(origin.lastIndexOf(',') != -1){
		
			    var stnValue = origin.substring(0,origin.lastIndexOf(','));
			    stnValue = trim(stnValue);
		
				if(!regExStn.test(stnValue)){
					alert("Please enter a valid Origin Station name");
					return false;
				}
		
				if(stnValue.length == 0){
					alert("Origin Station cannot be blanks");
					return false;
				}
		
			    var stValue = origin.substring(origin.lastIndexOf(',')+1);
			    stValue = trim(stValue);
		
				if(stValue.length == 0){
					alert("Origin State cannot be blanks");
					return false;
				}
		
				if(!regExSt.test(stValue)){
					alert("Please enter a valid State name.");
					return false;
				}
		
				if(stValue.length != 2){
					alert("Origin State should be two characters.");
					return false;
				}
		
		
			}else{
				alert('Origin Station should be entered in the format "Station,State".');
				return false;
			}
			if(destination.lastIndexOf(',') != -1){
		
			    var stnValue = destination.substring(0,destination.lastIndexOf(','));
			    stnValue = trim(stnValue);
		
				if(!regExStn.test(stnValue)){
					alert("Please enter a valid Destination Station name");
					return false;
				}
		
				if(stnValue.length == 0){
					alert("Destination Station cannot be blanks");
					return false;
				}
		
			    var stValue = destination.substring(destination.lastIndexOf(',')+1);
			    stValue = trim(stValue);
		
				if(stValue.length == 0){
					alert("Destination State cannot be blanks");
					return false;
				}
		
				if(!regExSt.test(stValue)){
					alert("Please enter a valid Destination State name.");
					return false;
				}
		
				if(stValue.length != 2){
					alert("Destination State should be two characters.");
					return false;
				}
		
	
			}else{
				alert('Destination Station should be entered in the format "Station,State".');
				return false;
			}				

			//var rowObj = eval("document.all.NumODPairs");
			var rowObj = document.getElementById("NumODPairs");
			if(null != rowObj){
				rowObj.style.display = "none";
			}
			var tbody = document.getElementById("tableadd").getElementsByTagName("tbody")[0];
			var row   = document.createElement("tr");
			row.setAttribute("id","tr"+count);
			row.bgColor = "#DEDEDE";

			var cell = document.createElement("td");
			cell.setAttribute("vAlign","top");
			cell.setAttribute("align","center");
			var control = document.createElement("input");
			control.setAttribute("type","checkbox");
			control.setAttribute("id","chkInd"+count);
			control.setAttribute("name","chkInd"+count);
			control.setAttribute("value",count);			
			control.onclick = new Function("","fnDeselect('frmRailMiles',document.frmRailMiles.chkAll)");

			cell.appendChild(control);
			row.appendChild(cell);

			var cell = document.createElement("td");
			cell.setAttribute("vAlign","top");			
			var control = document.createElement("input");
			control.setAttribute("type","hidden");
			control.setAttribute("id","hidOrigin");
			control.setAttribute("name","hidOrigin");
			control.setAttribute("value",origin);
			cell.appendChild(control);

			var control = document.createElement("DIV");
			//control.setAttribute("innerHTML",origin);		
			control.innerHTML=origin;
			cell.appendChild(control);
			row.appendChild(cell);

			var cell = document.createElement("td");
			cell.setAttribute("vAlign","top");

			var control = document.createElement("input");
			control.setAttribute("type","hidden");
			control.setAttribute("id","hidDestination");
			control.setAttribute("name","hidDestination");
			control.setAttribute("value",destination);
			cell.appendChild(control);
			var control = document.createElement("DIV");
			//control.setAttribute("innerHTML",destination);	
			control.innerHTML=destination;		
			cell.appendChild(control);
			row.appendChild(cell);
			tbody.appendChild(row);
			count++;
			lenCount++;
			theForm.btnDelete.disabled = false;
			theForm.chkAll.disabled = false;
		}
	}else{
		return;
	}
}
//Function - To check and uncheck select all check box when all the check boxes are checked/unchecked
function fnDeselect(objForm,checkBoxAll){
	var flag = 0;	
	var isNotChecked = false;
	var len = 0;	
	if('frmRailMiles' == objForm){
		len = count;
	}else{
		len = document.frmRailImport.hidCount.value;	
	}	
	for (i =0; i < len; i++) {
		///var checkBoxInd = eval("document."+objForm+".chkInd"+i);
		var checkBoxInd = document.getElementById("chkInd"+i);
		if(checkBoxInd != undefined){			
			if (!checkBoxInd.checked) {
				isNotChecked = true;				
			}
		}
	}
	if(isNotChecked){
		checkBoxAll.checked = false;
	}else{
		checkBoxAll.checked = true;
	}
}

//Function - To check and uncheck all check box when select all  check box is checked/unchecked
function fnSelectAll(objForm) {
	var theForm = eval("document."+objForm);
	if('frmRailMiles' == objForm){
		len = count;
	}else{
		len = document.frmRailImport.hidCount.value;	
	}
	if (theForm.chkAll.checked) {
		for (var i =0; i < len ; i++) {
			var checkBoxInd = eval("document."+objForm+".chkInd"+i);
			if(checkBoxInd != undefined){
				checkBoxInd.checked = true
			}
		}
	} else {
		for (var i =0; i < len ; i++) {
			var checkBoxInd = eval("document."+objForm+".chkInd"+i);
			if(checkBoxInd != undefined){
				checkBoxInd.checked = false
			}
		}
	}
}
//Function - To check which of the rows are checked to be deleted
function deleteRecord(objForm) {
	var isChecked = false;	
	var theForm = eval("document."+objForm);
	var len = count;		
	for (var i =0; i < len ; i++) {
		//var chkBox = eval("document."+objForm+".chkInd"+i);	
		var chkBox =document.getElementById("chkInd"+i);
		if(chkBox != undefined){				
			if (chkBox.checked) {
				isChecked = true;					
				remChild(chkBox.value);
				//theForm.hidCount.value = parseInt(theForm.hidCount.value)-1;								
				lenCount--;
			}
		}
	}	
	if(lenCount <= 0){	   
		//document.all.NumODPairs.style.display= '';
		document.getElementById("NumODPairs").style.display= '';
		theForm.chkAll.checked = false;
		theForm.chkAll.disabled = true;
		theForm.btnDelete.disabled = true;		
	}	
	if(!isChecked){
		alert('Please select any row to delete.');
		return;
	}
	
}

function deleteImport() {
	var lenCheck = 0;
	var isChecked = false;	
	var theForm = document.frmRailImport;
	var len = theForm.hidCount.value;		
	for (var i =0; i < len ; i++) {
		var chkBox = eval("document.frmRailImport.chkInd"+i);			
		if(chkBox != undefined){				
			if (chkBox.checked) {
				lenCheck++;
				isChecked = true;					
				remChild(chkBox.value);
				theForm.hidSelectedCount.value = parseInt(theForm.hidSelectedCount.value)-1;
			}
		}
	}	
	if(lenCheck == len){
		document.all.NumODPairs.style.display= '';
		theForm.chkAll.checked = false;
		theForm.chkAll.disabled = true;	
		theForm.btnDelete.disabled = true;		
	}	
	if(!isChecked){
		alert('Please select any row to delete.');
		return;
	}
	
}

//Function - To delete a selected row
function remChild(n) {	


	if (document.getElementById) {
		var tbl = document.getElementById('tableadd');
	    if (tbl.hasChildNodes()) {
	    	var tbdy;
	        for (var i = 0; i < tbl.childNodes.length; i++) {
	        	if (tbl.childNodes[i].nodeName == 'TBODY') {
	          		tbdy = tbl.childNodes[i];
	          		break;
	        	}
	      	}
	      	if (tbdy.hasChildNodes()) {
	      		var row, rowCounter = 0;
				for (var i = 0; i < tbdy.childNodes.length; i++) {								
					if ('undefined'!=tbdy.childNodes[i].id && tbdy.childNodes[i].id == 'tr'+n ) {
					//if (tbdy.childNodes[i].getAttribute("id") == 'tr'+n ) {
						row = tbdy.childNodes[i];							
						tbdy.removeChild(row);							
						break;						
					}
				}
	    	}
		}
	}	
}


//Function - To check if the entered city state combination is valid or not.
/*function fnValid(){
	var theForm = document.frmRailMiles;
	var isValidOrg = false;
	var isValidDes = false;	
	if(lenCount > 1000){
		alert('Please enter only upto 1000 OD pairs at a time.');
		return;	
	}else if(!isAlpha(theForm.txtOrigin,'Origin')){
		return;
	}else if(!isAlpha(theForm.txtDestination,'Destination')){
		return;
	}
    for(a=0;a<divOrgCount;a++){
    	var divOrg = eval("document.frmRailMiles.hidOrgDiv"+a);
    	if(divOrg != undefined && trim(theForm.txtOrigin.value).toUpperCase() == divOrg.value){
        	isValidOrg = true;
            break;
        }
    }
    	
	for(b=0;b<divDesCount;b++){
		var divDest = eval("document.frmRailMiles.hidDesDiv"+b);				
  		if(divDest != undefined && trim(theForm.txtDestination.value).toUpperCase() == divDest.value){
        	isValidDes = true;
        	break;
  		}
	}
		
  	if (divOrgCount == 0 && divDesCount == 0){
  		return true;
  	}else if(!isValidOrg){
  		alert('Please enter a valid City,State for Origin');
  		return false;
  	}else if(!isValidDes){
  		alert('Please enter a valid City,State for Destination');
  		return false;
  	}else{
  		return true;
  	}
}*/

//Function - To check if the entered city state combination is valid or not.
function fnValid(){
	var theForm = document.frmRailMiles;
	var isValidOrg = false;
	var isValidDes = false;	
	if(lenCount > 1000){
		alert('Only 1000 Origin / Destination pairs can be entered at one time.');
		return;	
	}/*else if(!isAlpha(theForm.txtOrigin,'Origin')){
		return;
	}else if(!isAlpha(theForm.txtDestination,'Destination')){
		return;
	}*/
	/*else if(null != arrSyn){
	    for(a=0;a<arrSyn.length;a++){	    	
	    	if(trim(theForm.txtOrigin.value).toUpperCase() == arrSyn[a]){
	        	isValidOrg = true;
	            break;
	        }
	    }
	    for(b=0;b<arrSyn.length;b++){	    	
	    	if(trim(theForm.txtDestination.value).toUpperCase() == arrSyn[b]){
	        	isValidDes = true;
	            break;
	        }
	    }
	}   
	if(!isValidOrg){
  		alert('Please enter a valid City,State for Origin');
  		return false;
  	}else if(!isValidDes){
  		alert('Please enter a valid City,State for Destination');
  		return false;
  	}else{
  		return true;
  	}*/
  	return true;
}

/***1.0 Start***/
function fnAlphaAndSpace(event) {
	//var e = event || evt; // for trans-browser compatibility
var charCode =(window.Event) ? event.which : event.keyCode;

	//var charCode = e.which || e.keyCode;
	if ((charCode >=65 && charCode <=90)      	// A to Z
		|| (charCode >=97 && charCode <=122)  	// a to z
		|| (charCode ==32)					  	// Space
		|| (charCode ==44) || (charCode ==8) || (charCode ==0)) {				  	// ,(Comma)
		return true;
	} else {
		return false;
	}
}
/***1.0 End***/


