/*
|	Connection Manager
|
|	Creates and handles all connections and data returned by those connections
|	Requires: core.js
|
|	Change Log
	Date		Line (approx)	Description											Author
	-----------	---------------	---------------------------------------------------	------------------
	04/08/2008	219				Added locked account support						James Haley
	
*/
		
		//Connection
		{hp.http=function(){
			var oXMLResponse,oXML,bXMLOk=true;
			return {
				//Create connection
				conn:function(){
					if(typeof XMLHttpRequest!="undefined"){
						return new XMLHttpRequest();
					}else if(typeof ActiveXObject!="undefined"){
						return new ActiveXObject("Microsoft.XMLHTTP");
					}else{
						throw new Error("XMLHttpRequest not supported");
					};
				},
				//Create request
				request:function(vURI,oF,oT){
					var oR=hp.http.conn();
					hp.http.loading(hp.utils.get(oT));//show loading icon
					oR.open("GET",vURI+"?"+hp.utils.rnd(),true);
					oR.onreadystatechange=function() {
						if (oR.readyState==4) {
							oXMLResponse=oR.responseText;
							try {
								oXML=(new DOMParser()).parseFromString(oXMLResponse,"text/xml");
								oXML=oXML.getElementsByTagName('channel')[0].getElementsByTagName('item');
								oF.call(this,oT);
							} catch(o) {
								bXMLOk=false
								oF.call(this,oT);
							}
						};
					};
					oR.send(null);
				},
				//Parse response
				parsers:function(){
					var vOutput;
					return {
						//News List
						OutputNews:function(oT){
						if(bXMLOk) {
								var nNewsCount=(oXML.length),nShowCount=0,oT=hp.utils.get(oT);
								(nNewsCount>4)?nShowCount=5:nShowCount=nNewsCount;
								if(oT){
									vOutput='<div class="title"><div class="right"><a href="http://capgemini.dmflex.com/news.xml" target="_blank"><img src="/images/feed.gif" /></a></div>CAP Gemini News ('+nShowCount+' of '+nNewsCount+')</div><div class="list_body"><ul>'
										for(var i=0;i<nShowCount;i++){
											vOutput+='<li><div style="width:328px;display:block;border:1px SOLID #d5d5d5;"><a href="news.asp#'+i+'" class="listlink"><strong>'+oXML[i].childNodes[0].firstChild.data+'</a></strong></div></li>';
										}
									vOutput+='<li style="text-align: right;"><a href="/news.asp">&raquo; More CAP Gemini News</a></li>'
									vOutput+='</ul></div>';oT.innerHTML=vOutput;
								}
							} else {
								hp.utils.get(oT).innerHTML='<div class="title">CA News</div><div class="list_body"><ul><li>Sorry, CAP Gemini News feed currently unavailable</li></ul></div>'
							}
						},
						//News List
						OutputFullNewsList:function(oT){
							if(bXMLOk) {
								var nNewsCount=(oXML.length),oT=hp.utils.get(oT);
								if(oT){
									//output list
									vOutput='<div class="title">CAP Gemini News &ndash; All</div><div class="full_list_body">'
									vOutput+='<div class="list_options greybg gainlayout"><div class="left" style="margin-right: 5px;"><img src="/images/feed_big.gif" /></div><div class="padded5 left"><h4 class="light"><a href="http://capgemini.dmflex.com/news.xml" target="_blank">View RSS feed of this page</a></h4></div><div class="clear"></div></div><ul>'
									vOutput+='<li class="gainlayout"><p>CAP Gemini News Releases<br/>Listing of recent news releases from CAP Gemini.</p></li>'
									for(var i=0;i<nNewsCount;i++){
										vOutput+='<a name="'+i+'"></a><li id="oListItem'+i+'" class="gainlayout"><div class="grey"><h4>'+hp.utils.simpledate(oXML[i].childNodes[3].firstChild.data)+'</h4></div><h3><a href="'+oXML[i].childNodes[2].firstChild.data+'" target="_blank" class="listlink">'+oXML[i].childNodes[0].firstChild.data+'</a></h3><p>'+oXML[i].childNodes[1].firstChild.data+' <a href="'+oXML[i].childNodes[2].firstChild.data+'" target="_blank">&raquo; View Full Article</a></p></li>';
									}
									vOutput+='</ul></div>';oT.innerHTML=vOutput;
									//get id for hilight
									vHL=location.href,vHL=vHL.split("#");
									if(vHL[1]){
										oT=hp.utils.get('oListItem'+vHL[1]);
										window.scrollTo(0,oT.offsetTop);
										oT.className='highlighted gainlayout';
									}
								}
							}else{
								hp.utils.get(oT).innerHTML='<div class="title">CAP Gemini News</div><div class="list_body"><ul><li>Sorry, CAP Gemini feed currently unavailable</li></ul></div>'
							}
						}
					}
				}(),
				loading:function(oT){

					//oT.innerHTML='<div class="load"><img src="/images/l.gif" /></div>'
					oT.innerHTML='<div class="load">loading...</div>'
				},
				send:function(){
					return {
						register:function(){
							var oR=hp.http.conn();
							hp.utils.get('oWaitTitle').innerHTML='Please wait...';
							if(hp.utils.get('oLoginForm')){
								hp.utils.get('oWaitText').innerHTML='We are attempting to register your details.';
							}else{
								hp.utils.get('oWaitText').innerHTML='We are attempting to update your details.';
							}
							hp.content.swap('oRegister','oWait');//show final screen
							//check if profile pic is populated
							var bProfilePic;
							if(hp.utils.get('oProfileEdit')){
								if(hp.utils.get('oProfilePicture').value!=''){
									bProfilePic=1;
								}else{
									if(hp.utils.get('oProfilePictureRemove').checked){
										bProfilePic=0;
									}
								}
							}else{
								if(hp.utils.get('oProfilePicture').value!=''){
									bProfilePic=1;
								}else{
									bProfilePic=0;
								}
							}
							//check if to send profile pic stuff
							if(bProfilePic==undefined){
								var vURL="/get/register/?"+hp.utils.query.registration();
								bProfilePic=0;
							}else{
								var vURL="/get/register/?"+hp.utils.query.registration()+'&bProfilePic='+bProfilePic;
							}
							oR.open("get",vURL,true);
							oR.onreadystatechange=function() {
								if (oR.readyState==4) {
									
									try {
										var oResponse=eval("("+oR.responseText+")");
										if(oResponse.text=='userexists'){
											hp.utils.get('oWaitTitle').innerHTML='Sorry, it seems that email address has already been registered.';
											hp.utils.get('oWaitText').innerHTML='Please enter a <a href="javascript:void(0);" id="oEnterNewEmail">New email address</a>.';
											hp.utils.addEvent(hp.utils.get('oEnterNewEmail'),'click',function(){hp.content.swap('oWait','oRegister');});
										}else if(oResponse.text=='invalidcompany'){
											hp.utils.get('oWaitTitle').innerHTML='Sorry, an error has occurred (invalidcompany).'
											hp.utils.get('oWaitText').innerHTML='Please refresh the page and try again.';
										}
										else if(oResponse.text=='invaliddomain'){
											hp.utils.get('oWaitTitle').innerHTML='Sorry, an error has occurred (invalid domain).'
											hp.utils.get('oWaitText').innerHTML='Please refresh the page and try again.';
										}
										else if(oResponse.text=='successful'){
											if(bProfilePic==0){
												hp.utils.get('oWaitTitle').innerHTML='Registration complete.';
												hp.utils.get('oWaitText').innerHTML='';
												hp.content.showcompletedialog();
												hp.utils.get('oUploading').style.display='none';
											} else {
												hp.utils.get('oWaitText').innerHTML='Please wait while we attempt to upload your profile picture.';
												hp.utils.get('oUploading').style.display='block';
												//update user field with passed user id
												hp.utils.get('oUserId').value=oResponse.id;
												//prime iframe for onload actions
												hp.utils.addEvent(hp.utils.get('oUploadFrame'),'load',parent.hp.http.uploadcomplete);
												//set and submit form
												hp.utils.get('oRegisterForm').action="/get/upload/";
												hp.utils.get('oRegisterForm').target="_upload";
												hp.utils.get('oRegisterForm').submit();
											};
											hp.content.clearreg();

										}else if(oResponse.text=='updated'){
											if(bProfilePic==0){
												location.href="/?profile=updated";
											}else{																
												hp.utils.get('oWaitText').innerHTML='Please wait while we attempt to upload your new profile picture.';
												hp.utils.get('oUploading').style.display='block';
												//update user field with passed user id
												hp.utils.get('oUserId').value=oResponse.id;
												//prime iframe for onload actions
												hp.utils.addEvent(hp.utils.get('oUploadFrame'),'load',parent.hp.http.uploadcomplete);
												//set and submit form
												hp.utils.get('oRegisterForm').action="/get/upload/";
												hp.utils.get('oRegisterForm').target="_upload";
												hp.utils.get('oRegisterForm').submit();
											}
											
										}
									} catch(o) {
										alert('You have been registered, but due to a server error an email confirmation has not been sent.\n\nWe are currently working to resolve this issue.\n\nTo login, please reload the website and login using the details you supplied.\n\nSorry for any inconvenience caused.');
										location.href='/';
									}
								}
							}
							oR.send(null);
						},
						login:function(bM){
							if(bM) {
							//alert(something);
								hp.utils.get('oMainLoginFailed').style.display='none';
								hp.utils.get('oMainLoginTitle').style.display='block';
								hp.valid.email(hp.utils.get('oMainLoginEmail'),'oMainLoginEmailMsg');
								hp.valid.password(hp.utils.get('oMainLoginPassword'),'oMainLoginPasswordMsg');
							}else{
								hp.utils.get('oLoginFailed').style.display='none';
								hp.valid.email(hp.utils.get('oLoginEmail'),'oLoginEmailMsg');
								hp.valid.password(hp.utils.get('oLoginPassword'),'oLoginPasswordMsg');
							}
							var bRemember;
									if(hp.utils.get('oRemember').checked){
										bRemember=1;
									}else{
										bRemember=0;
									}
							

							//check login and set session in login file
							if(!bEmailError&&!bPasswordError){
								(bM)?hp.content.swap('oMainLogin','oMainLoggingIn',true):hp.content.swap('oLoginForm','oLoggingIn',true);
								var oR=hp.http.conn();
								
								var vURL=(bM)?"/get/login/?e="+encodeURIComponent(hp.utils.get('oMainLoginEmail').value)+"&r="+bRemember+"&p="+encodeURIComponent(hex_md5(hp.utils.get('oMainLoginPassword').value))+"&u="+encodeURIComponent(b64_sha1(hp.utils.get('oMainLoginPassword').value)):"/get/login/?e="+encodeURIComponent(hp.utils.get('oLoginEmail').value)+"&r="+bRemember+"&p="+encodeURIComponent(hex_md5(hp.utils.get('oLoginPassword').value))+"&u="+encodeURIComponent(b64_sha1(hp.utils.get('oLoginPassword').value));

								oR.open("get",vURL,true);
								oR.onreadystatechange=function() {
									if (oR.readyState==4) {
										var oResponse=eval("("+oR.responseText+")");
										if(oResponse.text=='success'){
											if(bM){
												location.href='/';
											} else {
												if(hp.utils.get('oRedirect').value==''){
													location.href='/';
												}else{
													location.href=hp.utils.get('oRedirect').value;
												}
											}
										}else if(oResponse.text=='locked'){ //Added 04/08/2008 JH
											location.href='/?locked=true';
										}else{
											if(bM) {
												hp.content.swap('oMainLoggingIn','oMainLogin',true);
												hp.utils.get('oMainLoginFailed').style.display='block';
												hp.utils.get('oMainLoginTitle').style.display='none';
											}else{
												hp.content.swap('oLoggingIn','oLoginForm',true);
												hp.utils.get('oLoginFailed').style.display='block';
											}
										}
									}
								}
								oR.send(null);
							}
						},
						getpassword:function(){
							var oR=hp.http.conn();
							hp.valid.email(hp.utils.get('oForgotPassEmail'),'oForgotPassEmailError','oForgotPassEmailAccept');
							if(!bEmailError&&hp.utils.get('oForgotPassEmail').value!=''){
								hp.utils.get('oForgotPasswordEmailErrorMsg').style.display='none';
								hp.utils.get('oForgotPassEmailLabel').className='';
								hp.utils.get('oForgotPassLoader').style.display='block';
								oR.open("get","/get/password/?e="+encodeURIComponent(hp.utils.get('oForgotPassEmail').value),true);
								oR.onreadystatechange=function() {
									if (oR.readyState==4) {									
										var oResponse=eval("("+oR.responseText+")");
										hp.utils.get('oForgotPassLoader').style.display='none';
										if(oResponse.text=='found'){
											hp.utils.get('oForgotPasswordErrorMsg').style.display='none';
											hp.utils.get('oForgotPasswordSent').style.display='block';
										} else {
											hp.utils.get('oForgotPasswordSent').style.display='none';
											hp.utils.get('oForgotPasswordErrorMsg').style.display='block';
										};
									}
								}
								oR.send(null);
							}else{
								hp.utils.get('oForgotPasswordEmailErrorMsg').style.display='block';
								hp.utils.get('oForgotPassEmailLabel').className='red';
								hp.utils.get('oForgotPasswordSent').style.display='none';
								hp.utils.get('oForgotPasswordErrorMsg').style.display='none';
							}
						},
						resetpassword:function(){
							hp.utils.get('oResetPassLoader').style.display='block';
							hp.valid.password(hp.utils.get('oResetPassword'),'oResetPasswordError','oResetPasswordAccept');
							hp.valid.confirmpass(hp.utils.get('oResetConfirmPassword'),'oResetConfirmPasswordError','oResetConfirmPasswordAccept','oResetPassword');
							
							if(!bPasswordError&&!bConfirmPasswordError&&hp.utils.get('oResetPassword').value!=''&&hp.utils.get('oResetConfirmPassword').value!=''){
								hp.utils.get('oResetPasswordErrorMsg').style.display='none';
								hp.utils.get('oResetPasswordLabel').className='';
								hp.utils.get('oResetConfirmPasswordErrorMsg').style.display='none';
								hp.utils.get('oResetConfirmPasswordLabel').className='';
								
								var oR=hp.http.conn(),vResetPassword=hex_md5(hp.utils.get('oResetPassword').value);
								//var oR=hp.http.conn(),vResetPassword=b64_sha1(hp.utils.get('oResetPassword').value);

								oR.open("get","/get/password/reset/?p="+encodeURIComponent(vResetPassword),true);
								oR.onreadystatechange=function() {
									if (oR.readyState==4) {	
										var oResponse=eval("("+oR.responseText+")");
										if (oResponse.text=='success'){
											location.href = '/?pwchanged=success'
										} else {
											location.href = '/?pwchanged=failed'
										}
										hp.utils.get('oResetPassLoader').style.display='none';
									}
								}
								oR.send(null);
							}else{
								
								if(bPasswordError||hp.utils.get('oResetPassword').value==''){
									hp.utils.get('oResetPasswordErrorMsg').style.display='block';
									hp.utils.get('oResetPasswordLabel').className='red';
								} else {
									hp.utils.get('oResetPasswordErrorMsg').style.display='none';
									hp.utils.get('oResetPasswordLabel').className='';
								}
								if(bConfirmPasswordError||hp.utils.get('oResetConfirmPassword').value==''){
									hp.utils.get('oResetConfirmPasswordErrorMsg').style.display='block';
									hp.utils.get('oResetConfirmPasswordLabel').className='red';
								} else {
									hp.utils.get('oResetConfirmPasswordErrorMsg').style.display='none';
									hp.utils.get('oResetConfirmPasswordLabel').className='';
								}
							}
						}
					}
				}(),
				uploadcomplete:function(){
					try {
						var oUploadResponse=eval("("+window.frames['_upload'].document.body.innerHTML+")");
						if(oUploadResponse.text=='uploaded'){
							hp.utils.get('oUploadingIcon').className="framedpic";
							hp.utils.get('oUploadingIcon').innerHTML='<img src="'+oUploadResponse.image+'?'+hp.utils.rnd()+'" id="oNewProfilePic" />';
							hp.utils.get('oUploadingStatus').innerHTML='<strong>Picture Uploaded!</strong>';
							hp.utils.addEvent(hp.utils.get('oNewProfilePic'),'load',function(){
								if(hp.utils.get('oLoginForm')){
									hp.utils.get('oWaitTitle').innerHTML='Registration complete.';
									hp.utils.get('oWaitText').innerHTML='';
									window.setTimeout(function(){hp.content.showcompletedialog();},500);
								}else{
									location.href='/?profile=updated';
								}
							});
						};
					} catch(o) {
						hp.utils.get('oUploadingIcon').innerHTML='<img src="/images/error.gif" alt="Error" />';
						hp.utils.get('oUploadingIcon').setAttribute("className","",0);
						hp.utils.get('oUploadingStatus').innerHTML='<strong>Sorry, upload failed!</strong>';
					}
				}
			}
		}()};
		
		//Custom Parsers
		