String.prototype.trim = function() { return this.replace(/^\s*|\s*$/g,''); }
if (window.captureEvents){
}
else {
}
function clickHandler1(e){
	if (typeof event!=='undefined') {
		target=event.srcElement
	}
	else {
		target=e.target
	}

	if(target.className != "" || target.getAttribute("class") != null) {
		var cname = target.className ? target.className : target.getAttribute("class");
		cname = cname.trim();
		var spacePos = cname.indexOf(' ');
		if (spacePos >= 0) {
			cname = cname.substring(0,spacePos);
		}
		switch(cname) {
			case "ajax_subtab":
			case "ajax_tab_off":
			case "ajax_tab_on":
				showForm(target);
				break;

			default:
				return true;
				break;
		}
		return false;
	}
}
function unHighlightTabs()
{
	var tabs = Element.getElementsByClassName('tabs', 'ajax_tab_on');
	for(i=0;i<tabs.length;i++){
		Element.removeClassName(tabs[i], 'ajax_tab_on');
		Element.addClassName(tabs[i], 'ajax_tab_off');
		//tabs[i].set('ajax_tab_off');
	}
}
function highlightTab(t)
{
	Element.removeClassName(t, 'ajax_tab_off');
	Element.addClassName(t, 'ajax_tab_on');
}
function showForm(t) {
	new Ajax.Updater ('firstDiv', t, {method:'post',onComplete:function(){ showOtherDivs(t);}});
}
function showOtherDivs(t) {
	unHighlightTabs();
	highlightTab(t);
	switch (t.id) {
		case 'Coulomb_usagehistory':
			vcharge("");
			parking("");
	}
}
function reLoadDivByField(formName, fieldName) {
	innerdivCount=0;
	divCount=0;
	divName = $F($F(fieldName));
	url = $F($F(fieldName)+'_uri');
	post = $(formName).serialize();
	reLoadDiv1(divName, url, post);
}
function reLoadDiv1(divName, url, post) {
	new Ajax.Updater (divName, url, {method:'post',postBody:post});
}
function reLoadDivByField1(formName, fieldName) {
 
	divName = $F(fieldName);
	url = $F($F(fieldName)+'_uri');
	post = $(formName).serialize();
	reLoadDiv1(divName, url, post);
}
function reLoadDiv(divName, url) {
	new Ajax.Updater (divName, url, {method:'post'});
}
function vcharge(t) {
	sortColumn = "";
	if (t != "") {
		sortColumn = "/" + t.id;
	}
	new Ajax.Updater ('vchargehistory', base_url+'report/generate/vehicle_charge'+sortColumn, {method:'post'});
}
function parking(t) {
	sortColumn = "";
	if (t != "") {
		sortColumn = "/" + t.id;
	}
	new Ajax.Updater ('parkinghistory', base_url+'report/generate/parking'+sortColumn, {method:'post'});
}
function displayHide(idList, show){
	idArray = idList.split(",");
	for (var id=0; id<idArray.length; id++) {
	  if(show == 1) {
	    document.getElementById(idArray[id]).style.display="block";
	  }
	  else {
	    document.getElementById(idArray[id]).style.display="none";
	  }
	}
}
function addSubUser(divName, countDiv) {
	var div = document.getElementById(divName);
	count = document.getElementById(countDiv).value;
	count = eval(count) + 1;
   var YEMDiv = document.createElement("div");
	YEMDiv.setAttribute("id","YEM"+count);
	div.appendChild(YEMDiv);
	var mainDiv = document.createElement("tr");
      	mainDiv.setAttribute("id","request_secondcolumncontainer");
	YEMDiv.appendChild(mainDiv);
	var firstColDiv = document.createElement("td");
      	firstColDiv.setAttribute("id","right_firstcolumn");
	mainDiv.appendChild(firstColDiv);
	var ip = document.createElement("input");
	ip.setAttribute("name", "email"+count);
	ip.setAttribute("id", "email"+count);
	ip.setAttribute("value", '');
      	ip.setAttribute("class", "input_row");
	ip.setAttribute("type","text");
   ip.setAttribute("maxlength", "120");
   ip.setAttribute("size", "45");
	firstColDiv.appendChild(ip);
	firstColDiv = document.createElement("td");
      	firstColDiv.setAttribute("id", "right_firstcolumn");
	mainDiv.appendChild(firstColDiv);
	var ip = document.createElement("input");
	ip.setAttribute("name", "password"+count);
	ip.setAttribute("id", "password"+count);
      	ip.setAttribute("class", "input_row");
	ip.setAttribute("value", '');
	ip.setAttribute("type", "password");
   ip.setAttribute("maxlength", "16");
   ip.setAttribute("size", "16");
	firstColDiv.appendChild(ip);

	firstColDiv = document.createElement("td");
      	firstColDiv.setAttribute("id", "right_firstcolumn");
	mainDiv.appendChild(firstColDiv);

	var ip = document.createElement("input");
	ip.setAttribute("name", "password_confirm"+count);
	ip.setAttribute("id", "password_confirm"+count);
      	ip.setAttribute("class", "input_row");
	ip.setAttribute("value", '');
	ip.setAttribute("type", "password");
   ip.setAttribute("maxlength", "16");
   ip.setAttribute("size", "16");
	firstColDiv.appendChild(ip);
	document.getElementById(countDiv).value = count;
}
var newWin = null;
function popUp(strURL, strType, strHeight, strWidth) {
 if (newWin != null && !newWin.closed)
   newWin.close();
 var strOptions="";
 if (strType=="console")
   strOptions="resizable,height="+
     strHeight+",width="+strWidth;
 if (strType=="fixed")
   strOptions="status,height="+
     strHeight+",width="+strWidth;
 if (strType=="elastic")
   strOptions="toolbar,menubar,scrollbars,"+
     "resizable,location,height="+
     strHeight+",width="+strWidth;
 newWin = window.open(strURL, 'newWin', strOptions);
 newWin.focus();
}
function sendURLRequest(url, func) {
	new Ajax.Request (
				url, 
				{
					method:'post',
					onComplete: function(response){doIt(response, func)}
				});
}

function doIt(r, f)
{
	eval(f);
	return true;
}

function toggleDiv(divID)
{
	var display = document.getElementById(divID).style.display;
	if (display =='block') {
		newDisplay = 'none';
	}
	if (display =='none') {
		newDisplay = 'block';
	}
	
	document.getElementById(divID).style.display = newDisplay;
}

function sendURLRequestForm(formName, url, func) {
	post = $(formName).serialize();

	new Ajax.Request (
				url, 
				{
					method:'post',
					postBody:post,
					onComplete: function(response){doIt(response, func)}
				});
}

function sendURLFunctions(url, func, proceedFunc) {

	new Ajax.Request (
				url, 
				{
					method:'post',
					onComplete: function(response){doIt2(response, func, proceedFunc)}
				});
}

function sendURLFormFunctions(formName, url, proceedFunc, abortFunc) {
	post = $(formName).serialize();

	new Ajax.Request (
				url, 
				{
					method:'post',
					postBody:post,
					onComplete: function(response){doIt2(response, proceedFunc, abortFunc)}
				});
}

function doIt2(response, proceedFunc, abortFunc)
{
	if (response != "") {
		var jsonText = response.responseText;
		if (jsonText != "") {
			var jsonObj = jsonText.parseJSON();
		
			isError = jsonObj.error.status;
			if (isError == 1) {
				alert(jsonObj.error.message);
				eval(abortFunc);
				return false;
			}
		}
	}
	eval(proceedFunc);
	return true;
}

function getURLResponse(url, func) {
	new Ajax.Request (
				url, 
				{
					method:'post',
					onComplete: function(response){eval(func+'('+response.responseText+')')}
				});
}

function reLoadDivFunc(divName, url, post, func) {
	new Ajax.Updater (divName, 
											url, 
											{
												method:'post',
												postBody:post,
												onComplete: function(){eval(func())}
											});
}

function reLoadByForm(divName, url, formName)
{
	post = $(formName).serialize();
	new Ajax.Updater (
				divName, 
				url, 
				{
					method:'post',
					postBody:post
				});
}

function postURLFunc(url, post, func) {
	new Ajax.Request (
				url, 
				{
					method:'post',
					postBody:post,
					onComplete: function(response){eval(func+'('+response.responseText+')')}
				});
}

function indicateLoading(iContainer, iLMsgCnt, iLMsgTxt, reSize) {
	reSize = (reSize == null)? false : true;
	iLMsgCnt.style.display = 'block';
	iLMsgTxt.style.display = 'block';

	if (reSize) {
		iLMsgCnt.style.left = iContainer.offsetLeft + (iContainer.clientWidth / 2) - (iLMsgCnt.clientWidth / 2) + 'px';
		iLMsgCnt.style.top = iContainer.offsetTop + (iContainer.clientHeight / 2) - (iLMsgCnt.clientHeight / 2) + 'px';
	}
	else {
		iLMsgCnt.style.left = iContainer.offsetLeft + 'px';
		iLMsgCnt.style.top = iContainer.offsetTop + 'px';
		iLMsgCnt.style.height = iContainer.clientHeight + 'px';
		iLMsgCnt.style.width = iContainer.clientWidth + 'px';
		
		iLMsgTxt.style.left = ( (iContainer.clientWidth/2) - (iLMsgTxt.clientWidth/2)) + 'px';
		iLMsgTxt.style.top = ( (iContainer.clientHeight/2) - (iLMsgTxt.clientHeight/2)) + 'px';
	}

	iLMsgCnt.style.filter="alpha(opacity=70)";
}

function endLoading(iLMsgCnt, iLMsgTxt) {

	iLMsgCnt.style.display = 'none';
	iLMsgTxt.style.display = 'none';
	
}

function ajaxRequest
					(
						requestSettings, 
						loadingSettings, 
						postFunction,
						postAbortFunction
					)
{
	uri = (requestSettings['uri'] == null) ? false : requestSettings['uri'];
	formName = (requestSettings['formName'] == null) ? false : requestSettings['formName'];
	postVar = (requestSettings['postVar'] == null) ? false : requestSettings['postVar'];
	divName = (loadingSettings['divName'] == null) ? false : loadingSettings['divName'];
	postFunctionResponse = (postFunction['response'] == null) ? false : postFunction['response'];
	postAbortFunction = (postAbortFunction == null) ? false : postAbortFunction; // Riju - to be removed later
	postAbortFunctionResponse = (postAbortFunction['response'] == null) ? false : postAbortFunction['response'];
	if (!uri) {
		if (postAbortFunctionResponse)
			execPostFunction(postAbortFunction, response.responseText);
		else
			execPostFunction(postAbortFunction);
		return false;
	}
	post = "";
	if (formName) {
		post = $(formName).serialize();
	}
	if (postVar) {

		if (post != "")
			post = post + "&";

		post = postVar;
	}
	
	if (divName) {
		new Ajax.Updater 
				(
					divName,
					uri, 
					{
						method:'post',
						postBody:post,
						evalScripts:true,
						onSuccess: function(response)
													{
														if (postFunctionResponse)
															execPostFunction(postFunction, response.responseText);
														else
															execPostFunction(postFunction);
													}
					}
				);
			
	}
	else {
		new Ajax.Request 
				(
					uri, 
					{
						method:'post',
						parameters:post,
						onSuccess: function(response)
													{
														if (postFunctionResponse)
															execPostFunction(postFunction, response.responseText);
														else
															execPostFunction(postFunction);
													}
					}
				);

	}
}

function execPostFunction(postFunction, responseText)
{

	postFunctionName = (postFunction['name'] == null) ? false : postFunction['name'];
	postFunctionArgs = (postFunction['args'] == null) ? false : postFunction['args'];
	postFunctionResponse = (postFunction['response'] == null) ? false : postFunction['response'];

	responseText = (responseText == null) ? false : responseText;

	if (postFunctionName) {
		if (postFunctionArgs) {
			if (responseText)
				eval(postFunctionName)(responseText, postFunctionArgs);
			else
				eval(postFunctionName)(postFunctionArgs);
		}
		else {
			if (responseText)
				eval(postFunctionName)(responseText);
			else
				eval(postFunctionName)();
		}
	}

}
function setDropDown(responseText, ddObj)
{
	responseText = (responseText == null) ? false : responseText;

	lgth = ddObj.options.length;
	for (var count=0; count < lgth; count=count+1)
		ddObj.options[1] = null;

	if (!responseText)
		return;

	modelJSON = responseText.evalJSON();
	var modelCount = modelJSON.count;
  var modelRecords = modelJSON.record;

  var ctr = 0;
  for (idx in modelRecords) {
  
  	ctr = ctr + 1;
  	if (ctr > modelCount)
  		break;
  
  	anOption = document.createElement('option');

  	modelObj = modelRecords[idx];
		anOption.value = modelObj.ModelID;
		anOption.innerHTML = modelObj.ModelName;
		ddObj.appendChild(anOption);
	}

}
function inArray(needle, haystack)
{
	var found = false;
	for (var i=0; i<haystack.length; i++) {
		if (needle == haystack[i]) {
			found = true;
			break;
		}
	}
	return found;
}
function CurrencyFormatted(amount)
{
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
}
// end of function CurrencyFormatted()
