// Javascript Document

// Start Section for AJAX functions imported from Bids Application
function clearcontent(loc)
		{
		document.getElementById(loc).innerHTML='&nbsp;';
		}


/*********** Start Section for AJAX functions ***********/

// Section Start AJAX URL Requests
	var request = false;
	   try {
		 request = new XMLHttpRequest();
	   } catch (trymicrosoft) {
		 try {
		   request = new ActiveXObject("Msxml2.XMLHTTP");
		 } catch (othermicrosoft) {
		   try {
			 request = new ActiveXObject("Microsoft.XMLHTTP");
		   } catch (failed) {
			 request = false;
		   }  
		 }
	   }
	
	   if (!request)
		 alert("Error initializing XMLHttpRequest!");
	 
	 var loc = ""; //the shared variable to store the divid
	 var jcall = "" //the shared variable to store the javascript function to call after the response comes back
	 
function getInfo(fa, valdiv, tocall) {

		 loc = valdiv;
		 
		 if (tocall != undefined)
			{
			jcall = tocall;
			}

		// var customerval = document.getElementById("customer").value;
		
		var url = fa + "&ajax=yes&timestamp=" + new Date().getTime(); //adding a timestamp makes each request unique, preventing ie from using the cached page

		request.open("POST", url, true);

		 request.onreadystatechange = updatePage;
		 request.send(null);
//alert('5');
	 
	   }

function updatePage() {

		if(request.readyState == 1)
		{
		document.getElementById(loc).innerHTML='';
		document.getElementById(loc).innerHTML = 'Loading...';
		}
		if (request.readyState == 4)
		{
		if (request.status == 200) 
		{ 
		var answer = request.responseText;
		document.getElementById(loc).innerHTML='';
		document.getElementById(loc).innerHTML = answer;
		
		  if (jcall != "")
			{
			eval(jcall);
			jcall = "";
			}

		}
		}
		} 
// Section End AJAX URL Requests


// Section Start AJAX FORM POST Requests
		var http_request = false;
		var ReturnTo = "";
function makePOSTRequest(url, parameters) {
		  http_request = false;
		  if (window.XMLHttpRequest) { // Mozilla, Safari,...
			 http_request = new XMLHttpRequest();
			 if (http_request.overrideMimeType) {
				http_request.overrideMimeType('text/xml');
			 }
		  } else if (window.ActiveXObject) { // IE
			 try {
				http_request = new ActiveXObject("Msxml2.XMLHTTP");
			 } catch (e) {
				try {
				   http_request = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {}
			 }
		  }
		  if (!http_request) {
			 alert('Cannot create XMLHTTP instance');
			 return false;
		  }
		 
		  http_request.onreadystatechange = alertContents;
		  http_request.open('POST', url, true);
		  http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		  http_request.setRequestHeader("Content-length", parameters.length);
		  http_request.setRequestHeader("Connection", "close");
		  http_request.send(parameters);
		  }

function alertContents() {
		  if (http_request.readyState == 4) {
			 if (http_request.status == 200) {
				 //alert(http_request.responseText);
				result = http_request.responseText;
				
				document.getElementById(ReturnTo).innerHTML = result;  
				 
			  if (jcall != "")
					{
						eval(jcall);
						jcall = "";
					}

			 } else {
				alert('There was a problem with the request.');
			 }
		  }
	   }
	   
//Use this function to post forms to ajax
function ajaxpost(theurl, theparams, retdiv, tocall) {
		
		  newurl = theurl + "&ajax=yes"
		  ReturnTo = retdiv; //the name of the div to return the results to
		  
		  if (document.getElementById("ChangedFields") != null && document.getElementById("ChangedFields").value != "")
		  {
		  	var poststr = theparams + "&cfields=" + document.getElementById("ChangedFields").value; // sets the url parameters and adds the changed fields value to the parameters
		 	document.getElementById("ChangedFields").value = ""; // clear the changed fields value
		 }
		  else
		  {
			  var poststr = theparams 
		  }
		  
		  
		  	if (tocall != undefined)
			{
			jcall = tocall;
			}
			
		  makePOSTRequest(newurl, poststr);
   		}
   
	   /*
	   theurl = the template to post form fields to
	   theparams = all the formfields to be posted, see example below:
	   retdiv = the results come back to the div with this id
	   
	   An example of how to construct the 'theparams' variable for the ajaxpost function:
	   
	   "mytextarea1=" + encodeURI( document.getElementById("mytextarea1").value ) +
	   "&mytextarea2=" + encodeURI( document.getElementById("mytextarea2").value );
   */
// Section End AJAX FORM POST Requests

/*********** End Section for AJAX functions ***********/

/*********** Start Section for User-Defined functions ***********/
function processAdminOnClick() {
	//alert('1');
	
	//clearcontent('MainContent');	
	//clearcontent('content2');	
	var bUseAjax = false;
	//bUseAjax = true;
	
	var strUrl;
	if (bUseAjax == true)
	{
		// Use Ajax (call getInfo function)		
		var valdiv;
		strUrl='index.cfm?fa=v.dspAdminUserList&SRT=AUID';
		//valdiv = 'content2';
		valdiv = 'MainContent';
		getInfo(strUrl, valdiv);
		////getInfo(strUrl, valdiv, tocall) 
	}
	else
	{
		// Use regular location.href to redirect	
		strUrl='index.cfm?fa=c.AdminUserList&SRT=AUID';
		location.href=strUrl;
	}
}


function processNeighborCDClick() {
	// Purpose: Process  Button click for "Neighborhood Council"
	var bUseAjax = false;
	//bUseAjax = true;

	var strUrl;
	if (bUseAjax == true)
	{
		// Use Ajax (call getInfo function)		
		var valdiv;
		strUrl='index.cfm?fa=v.dspAdminNCList&SRT=ANCI';
		valdiv = 'MainContent';
		getInfo(strUrl, valdiv);
	}
	else
	{
		// Use regular location.href to redirect	
		strUrl='index.cfm?fa=c.AdminNCList&SRT=ANCI';
		location.href=strUrl;
	}
}


function processCommitteeClick() {
	// Purpose: Process  Button click for "Committee"
	var bUseAjax = false;
	//bUseAjax = true;

	var strUrl;
	if (bUseAjax == true)
	{
		// Use Ajax (call getInfo function)		
		var valdiv;
		strUrl='index.cfm?fa=v.dspAdminCommList&SRT=ACMN';
		valdiv = 'MainContent';
		getInfo(strUrl, valdiv);
	}
	else
	{
		// Use regular location.href to redirect	
		strUrl='index.cfm?fa=c.AdminCommList&SRT=ACMN';
		location.href=strUrl;
	}
}




/*********** Start Section for Formatting Functions (Imported from Bids) ***********/
function filterNum(str) {
re = /^\$|,/g;
// remove "$" and ","
return str.replace(re, "");
}

function filterInvalidChars(str)
{
re = /'|"/g;

return str.replace(re, "");
}

function filterampersands(str)
{
re = /&/g;
return str.replace(re, "and");
	
}

function Trim(TRIM_VALUE, obj){
		if(TRIM_VALUE.length < 1){
		obj.value = "";
		}
		
		TRIM_VALUE = filterInvalidChars(TRIM_VALUE); //removes restricted characters
		TRIM_VALUE = filterampersands(TRIM_VALUE); //replaces ampersand with 'and'
		
		TRIM_VALUE = RTrim(TRIM_VALUE);
		TRIM_VALUE = LTrim(TRIM_VALUE);
		
		if(TRIM_VALUE==""){
		obj.value = "";
		}
		else{
		
		obj.value = TRIM_VALUE;
		return TRIM_VALUE;
		}
		} //End Function

function RTrim(VALUE){
		var w_space = String.fromCharCode(32);
		var v_length = VALUE.length;
		var strTemp = "";
		if(v_length < 0){
		return"";
		}
		var iTemp = v_length -1;
		
		while(iTemp > -1){
		if(VALUE.charAt(iTemp) == w_space){
		}
		else{
		strTemp = VALUE.substring(0,iTemp +1);
		break;
		}
		iTemp = iTemp-1;
		
		} //End While
		return strTemp;
		
		} //End Function

function LTrim(VALUE){
		var w_space = String.fromCharCode(32);
		if(v_length < 1){
		return"";
		}
		var v_length = VALUE.length;
		var strTemp = "";
		
		var iTemp = 0;
		
		while(iTemp < v_length){
		if(VALUE.charAt(iTemp) == w_space){
		}
		else{
		strTemp = VALUE.substring(iTemp,v_length);
		break;
		}
		iTemp = iTemp + 1;
		} //End While
		return strTemp;
		} //End Function

function phoneValidate(str)
		{
		var strng = str.value;
		var stripped = strng.replace(/[\(\)\.\-\ ]/g, '');
		var pass = 0;
		
		if (strng.length == 0)
			{return true;}
			
		if (strng.charAt(0) != '(')
			{
			pass = 1;
			}
		if (strng.charAt(4) != ')')
			{
			pass = 1;
			}
		if (strng.charAt(9) != '-')
			{
			pass = 1;
			}
		if (strng.charAt(5) != ' ')
			{
			pass = 1;
			}
		
			
		//strip out acceptable non-numeric characters
		if (isNaN(stripped))
		 {
		  pass = 1;
		 }
		if (!(stripped.length == 10))
		 {
			pass = 1;
		 }
		 if (pass == 1)
			{
			str.value = '';
			str.focus();
			alert('Phone number does not appear to be in the right format. \n Please enter it in the format (xxx) xxx-xxxx');
			return false;
			}
		}

function _isInteger(val) {
		var digits="1234567890";
		for (var i=0; i < val.length; i++) {
			if (digits.indexOf(val.charAt(i))==-1) { return false; }
			}
		return true;
		}

function formatPhoneFax2(strField) {
		 var oLen = strField.value.length; 
		 var Key = window.event.keyCode;
		 if ((Key < 48) || (Key > 57)) {
			return false;
			}
		 else {
			 if(oLen == 3) {
				strField.value = "(" + strField.value;
				strField.value += ") ";
			}
			if(oLen == 9) {
				strField.value += "-";
			}
		}
		}


function formatCouncilFileNumber(strField) 
{
	/* Purpose: Automatically Add "-" for Council file 12-3456 */
	var oLen = strField.value.length; 
	var Key = window.event.keyCode;
	//alert('Key'+ Key);
	// Need to research how to convert 'S' or 's' to a code

	if ((Key == 115) || (Key == 83)) {  // If user Type 'S" after 7-th Char, append '-' to the previous strring
		if(oLen == 7) {
			strField.value += "-";
			return true;
		}
	}

	
	if ((Key < 48) || (Key > 57)) {
		return false;
	}
	else {
		if(oLen == 2) {
			strField.value += "-";
		}
	/*
	if(oLen == 7) {
		strField.value += "-S";
	}
	*/
	}
}




function isValidNumber(obj){
	
	if (obj.value.length == "")
	 {
		return true;		 
	 }
	 
	if (obj.value.charAt(0) == "$")
		{
		val = obj.value.replace("$","");
		}
	else
		{
		val = obj.value;
		}
	 
	 cur  = /^-?\d{1,3}(,\d{3})*(\.\d{1,2})?$/;
	 anum=/(^-?\d+$)|(^-?\d+\.\d+$)/;
	 ret = false;

	 if(val.indexOf(",")>-1)
	 	ret = cur.test(val);
	 else
	 	ret = anum.test(val);

	 if(!ret){
	 	alert("The value you entered is not in a valid format.");
		obj.value = '';
		obj.focus();
	 	return false;
	 }
	 else
	 	return true;
 }
/*********** End Section for Formatting Functions ***********/



/*********** Start Section for New function add for CIS ***********/
function isIntegerAtIndex(val, iBeg, iEnd) {
// Purpose: Check if the sub string are integers
// e.g., isIntegerAtIndex('12345', 0,3) will check if the 1st to 3rd char '123' is integer
	if ((iBeg < 0) || (iBeg >  val.length))
		return false; 

	if ((iEnd < 0) || (iEnd >  val.length))
		return false; 

	if ((iEnd < iBeg))
		return false; 

	var digits="1234567890";
	for (var i=iBeg; i < iEnd; i++) {
		if (digits.indexOf(val.charAt(i))==-1) { return false; }
		}
	return true;
	}


function CouncilFileNoValidate(TRIM_VALUE, obj)
// Purpose: Check the Council File Number, Make sure it's xx-xxxx format or xx-xxxx-Sxx format
//
{
		Trim(TRIM_VALUE, obj);

		var bValid = false;		
		
		var strVal; 
		
		var strMsg;
		strMsg = 'Council File Number does not appear to be in the right format. \n Please enter it in the format 99-9999 or 99-9999-S99 '

		var strMsg1 = '';

		
		// String Value for the Council File Number
		// 08-0010-S9
		// 07-0938 
		strVal = obj.value;
		
		// Let empty string pass
		if(strVal.length < 1){
			bValid = true;
			return bValid;
		}

		// 07-0938 (Length is 7)
		if (strVal.length == 7){
			bValid = true;
		}
		// 08-0010-S9 to 08-0010-S99 (Length between 10 and 12
		else if ( (strVal.length >= 10) && (strVal.length <= 12) )
		{
			bValid = true;
		}
		// Does not fit in any pattern
		else
		{
			strMsg1 = ' \n The length of File name should be 7 for regular or 10-11 for Special Council File';
			bValid = false;obj.focus(); alert(strMsg + strMsg1); return bValid;
		}

		//alert('len:' + strVal.length );
		
		if (strVal.charAt(2) != '-')
		{
			strMsg1 = ' \n (The 3-th char should be -)';
			bValid = false;obj.focus(); alert(strMsg + strMsg1); return bValid;
		}

		// This is the special council file
		// 08-0010-S9
		// 0123456789
		// 0-1 char must be Numeric
		//alert('strVal:'+strVal);
		var strTmp1;
		strTmp1 = strVal.substring(0,2); // Start and End index, 0, 2 will give char 1-2
		//alert('str 0,2:' + strTmp1);

		if (isNaN(strTmp1))
		{
			strMsg1 = ' \n The 1-2 th char:' + strTmp1 +  ' should be integer';
			bValid = false;obj.focus(); alert(strMsg + strMsg1); return bValid;
		}

		strTmp1 = strVal.substring(3,7);
		//alert('str 3,7:' + strTmp1);
		if (isNaN(strTmp1))
		{
			strMsg1 = ' \n The 4-7 th char:' + strTmp1 +  ' should be integer';
			bValid = false;obj.focus(); alert(strMsg + strMsg1); return bValid;
		}

		if (! isIntegerAtIndex(strVal, 0, 2) )
		{
			strTmp1 = strVal.substring(0,2); // Start and End index, 0, 3 will give char 1-3

			strMsg1 = ' \n The 1-2 th char:' + strTmp1 +  ' should be integer';
			bValid = false;obj.focus(); alert(strMsg + strMsg1); return bValid;
		}
		
		if (! isIntegerAtIndex(strVal, 3, 7 ) )
		{
			strTmp1 = strVal.substring(3,7); // Start and End index, 0, 3 will give char 1-3

			strMsg1 = ' \n The 4-7 th char:' + strTmp1 +  ' should be integer';
			bValid = false;obj.focus(); alert(strMsg + strMsg1); return bValid;
		}
		
		// Strip out - and S, then the rest should be number
		var stripped;
		stripped = strVal;
		stripped = stripped.replace(/S/g, '');
		stripped = stripped.replace(/-/g, '');

		//alert('10');			
		if ( (strVal.length >= 10) )
		{
			// 7-th char must be - (0-baased)
			if (strVal.charAt(7) != '-')
			{
				strMsg1 = ' \n The 8-th char should be - for Special Council File';
				bValid = false;obj.focus(); alert(strMsg + strMsg1); return bValid;
			}
			
			// 8-th char must be "S" (0-based)
			if ( strVal.charAt(8).toUpperCase() != 'S' )
			{
				strMsg1 = ' \n The 9-th char should be S';
				bValid = false;obj.focus(); alert(strMsg + strMsg1); return bValid;
			}
			
			//alert('10.1');			
			// 9-10 char must be Numeric
			if (! isIntegerAtIndex(strVal, 9, strVal.length ) )
			{

				strTmp1 = strVal.substring(9);

				strMsg1 = ' \n The 10-12 th char:' + strTmp1 + ' should be integer';
				bValid = false;obj.focus(); alert(strMsg + strMsg1); return bValid;
			}

			//alert('10.2');			
		}
		//alert('11');			
		
} //End Function


/*********** End Section for New function add for CIS ***********/


