<?php
	
	$class_etamine_com->UseClass("user");

	$db = new etamineDB();
	$db = new etamineDB('t155', 'alam@273', '180.92.168.223', 'cvcdata');
	$dropDown = new DropDownOptions();
	$dt = new etamineDate();
	$class_etamine_com->UseClass("docmanager");
	$listBox = new ListBox();
	$listBox1 = new ListBox();	
	$showFile = "cwgreports/stdmprojects.html";	
	$ajax = new etamineAjax();
	$ajax->setRequestType("POST");	
	$ajax->exportFunction("loginuser", "theform");
	$ajax->process();

	if ($p_action == "logout") {
		$user = new etamineUser();
		$userInfo = $user->Logout($_SESSION['userid']);
		// for Temparay folder file removing purpose added by sithes on 05.10.09
		$userInfo = $user->RemoveTempFile(USERFILES . "tmp/");		
		$message = "Logout Sucessfull";
		$_SESSION = array();
		session_destroy();
		header("location:?");
	}

	$ajax = new etamineAjax();
	$ajax->exportFunction("loginuser", "theform");
	$ajax->process();
	
	
	$lanusername = $lan['USERNAME'];
	$lanpassword = $lan['PASSWORD'];
	$forgotpassword = $lan["FORGOTPASSWORD"];
	$login = $lan["LOGIN"];
	
	function loginuser($formData) {
		$formDataArray = etamineAjax::getPostData($formData);
		$tab = new etamineAjaxBehavior();
		$user = new etamineUser();
	//	print_r($tab);
		$userInfo = $user->LoginValidation($formDataArray['username'], md5($formDataArray['upassword']));
		
		if (!empty($userInfo['USERID'])) {
			
			$dtArray = split ('%Y', $userInfo[DATEFORMAT]);
			$_SESSION['userid'] = $userInfo[USERID];
			$_SESSION['username'] = $userInfo[FIRSTNAME];
			$_SESSION['name'] = $userInfo[FIRSTNAME];
			$_SESSION['email'] = $userInfo[EMAIL];
			$_SESSION['lastip'] = $userInfo[LASTIP];
			$_SESSION['TIMEZONE'] = $userInfo[TIMEZONE];
			$_SESSION['DATEFORMAT'] = $userInfo[DATEFORMAT];
			$_SESSION['DTFORMAT'] = $dtArray[0] . "%Y"; //This has to be changed.
			$_SESSION['ICSTPROJECTID'] = $userInfo[ICSTPROJECTID];
			$_SESSION['PROJECTCODE'] = $userInfo[PROJECTCODE];
			
			$_SESSION['LEVELSID']	 = $userInfo[LEVELID];
			$_SESSION['COUNTRYID']	 = $userInfo[COUNTRY];
			$_SESSION['STATEID']	 = $userInfo[STATE];
			$_SESSION['DISTRICTID']	 = $userInfo[DISTRICTID];
			$_SESSION['TALUKID']	 = $userInfo[TALUKID];
			$_SESSION['PANCHYATHID'] = $userInfo[PANCHYATHID];
			$_SESSION['VILLAGEID'] = $userInfo[VILLAGEID];

			$_SESSION['GROUPID'] = $userInfo[GROUPID];
			$_SESSION['MGROUPID'] = $userInfo[MGROUPID];
			$_SESSION['LABID'] = $userInfo[LABID];
			$_SESSION['PATINENTINWARDID'] = $userInfo[INWARDID];// ADDED ON 13/11/09 3.52PM
			$_SESSION['sessionid'] = session_id();
			$_SESSION['ACCESSINFO'] = $userInfo['ACCESSINFO'];

			// added by sithes for login user identification 23/10/09
			$db = new etamineDb();
			
			//checking expiry time if they didn't logout properly next time who ever login dat time am making all expiry time empty 
			$sql = "UPDATE etaminev2.tbluser SET expirytime = '0000-00-00 00:00:00', islogin = 'No' where expirytime < subtime(now(),'24:00:00') and expirytime <> now() ";
			$db->DbExecute($sql);	
			// expiry time end
			$sqlupdate = "UPDATE etaminev2.tbluser SET expirytime = ADDTIME(NOW(),'00:05:00'), islogin = 'Yes'  where userid = '".$userInfo[USERID]."'";
			
			$db->DbExecute($sqlupdate);				
				if($_SESSION['GROUPID'] == 1 || $_SESSION['GROUPID'] == 2)
				{
					$tab->add(TabRedirect::getBehavior("index.php?module=dashboard&action=demo"));
				}
				if(!empty($_SESSION['LEVELSID']))
				{
					$tab->add(TabRedirect::getBehavior("index.php?module=dashboard&action=$level"));
				}
				if(empty($_SESSION['LEVELSID']) and ($_SESSION['GROUPID'] != 1))
				{
					$tab->add(TabRedirect::getBehavior("index.php?module=admin&action=view"));
				}
		} else {
		//	print_r($user->ErrorInfo);
			$tab->add(TabAlert::getBehavior($user->ErrorInfo));
		}
		return $tab->getString();
	}

		
	$organisationName	= $db->DbGetField("select  venue from cvcdata.tblvenues where venunameid = '$p_venueid'");
	$organisationCode	= $db->DbGetField("select  venuecode from cvcdata.tblvenues where venunameid = '$p_venueid'");
	$totalOrgProjects	= $db->DbGetField("select  count(projectid) from cvcdata.tbladdproject where stadium = '$p_venueid' and isactive='Yes' ");
//	print_r($organisationCode);
	
	$sqlQuery2 ="select projectid,stadium,workcodeno, nameofwork, estimatedcost, awardedcontract from cvcdata.tbladdproject where stadium = '$p_venueid' and isactive='Yes' ";
	

	$dbValues2	= $db->DbGetMultipleRecords($sqlQuery2,"");

	
	for($i=0;$i<count($dbValues2);$i++)
	{
		$dbValues2[$i]['SLNO']=$i+1;
	}
	
	$listBox->ValueOnly = False;
	
	$middleValues[] = array(

		"AJAXCONTENT"		=>	$ajax->getJavaScript(false, true),
		//"STARTNO"			=>	(($currentpage - 1) * $pagelength),
		"GROUPID"			=>	$_SESSION['GROUPID'],
		"FORMACTION"		=>	$p_action,
		"MODULE"			=>	$p_module,
		//"PAGECONTENTS"	=>	$pageContents,
		"JSDIR"				=>	$GLOBALS['javascript'],
		"SKINS"				=>	$GLOBALS['skins'],

		"LOGINPASSWORD"		=>	$lanpassword,
		"FORGOTPASSWORD"	=>	$forgotpassword,
		"LOGIN"				=>	$login,
		"USERNAME"			=>	$username,
		"UIAPPSUITE"		=>	$uisuitehtml,
		"LOGINBUTTON"		=>	$loginButton,
		"MENUS"				=>	$menucontent,

		"ORGANIZATIONID"	=>	$p_organizationid,
		"ORGANISATIONNAME"	=>	$organisationName,
		"ORGANISATIONCODE"	=>	$organisationCode,
		"TOTALORGPROJECTS"	=>	$totalOrgProjects,
		"VENUEID"		=>	$p_venueid,
	);	
		
	$topValues = array(
		"USERNAME"		=>	$_SESSION['username'],
		"GROUPID"		=>	$_SESSION['GROUPID'],
		"SKINS"			=>	$GLOBALS['skins'],
	);

	$etamineParser->assign("dbValues2", $dbValues2);

	$topContents = $etamineParser->Parse($topFile,$topValues);
	$etamineParser->assign($_SESSION['ACCESSINFO']['Front Contractor']);
	$middleContents = $etamineParser->Parse(TEMPLATE . $showFile , $middleValues);
?>

