/*
|	SMS manager
|
|	Manages SMS functionality and popups
|	Requires: core.js,connection.js
*/

		//SMS function
		{hp.sms=function(){
			return {
				attach:function(oText){
					//attach to sms links
					var oTextCache=oText.sms,nTextLen=oTextCache.length;
					for(var i=0;i<nTextLen;i++){
						oLinkTarget=hp.utils.get('oTextSend'+oTextCache[i].id);
						if(oLinkTarget){
							oLinkTarget.smsid=oTextCache[i].id;
							hp.utils.addEvent(oLinkTarget,'click',function(){hp.sms.show(this.smsid,this);});
						}
					}
					//close button and send button
					hp.utils.addEvent(hp.utils.get('oSmsClose'),'click',function(){hp.sms.close();});
				},
				show:function(nId,oP){
					//position and show
					var nOT=0,nOL=0;
					oT=hp.utils.get('oSms_o');
					while(oP.offsetParent!=null){nOT+=oP.offsetTop;nOL+=oP.offsetLeft;oP=oP.offsetParent;}
					oT.style.top=(nOT)+"px",oT.style.left=(nOL-260)+"px";
					oT.style.display='block';
					//show and resize underlay
					hp.content.toggle('oSms_u');
					hp.utils.maximise('oSms_u');//initial resize incase no DOM resize ocurrs
					//set body and button
					hp.utils.get('oSms_body').innerHTML='Please confirm you would like to send a text message to your account manager, to get them to contact you.<div class="right" style="margin-top: 10px;"><input type="button" name="bSendMessage" id="oSmsSend" value="Send Message" /></div><div class="clear"></div>';
					hp.utils.get('oSmsSend').intid=nId;
					hp.utils.addEvent(hp.utils.get('oSmsSend'),'click',function(){hp.sms.send(this.intid);});
				},
				close:function(){
					hp.content.toggle('oSms_o');
					hp.content.toggle('oSms_u');
				},
				send:function(nId){
					var oT=hp.utils.get('oSms_body');
					oT.className='',oT.innerHTML='<div><div id="oSmsLoading" class="left"></div><div style="padding-left: 0;" class="left padded10">Sending text message... Please wait!</div><div class="clear"></div></div>'
					hp.http.loading(hp.utils.get('oSmsLoading'));
					
					//send
					var oSMSR=hp.http.conn(),oResponse;
					oSMSR.open("get","/get/sms/?id="+nId,true);
					oSMSR.onreadystatechange=function() {
						if(oSMSR.readyState==4){
							if (oSMSR.status==200) {
								oResponse=eval("("+oSMSR.responseText+")");
								oT.className='padded10';
								if(oResponse.text=='success'){
									oT.innerHTML = '<h3>Thanks, </h3><p>Message Sent!</p>';
								} else if(oResponse.text=='failed'){
									oT.innerHTML = '<h3>Sorry, </h3><p>Message send failed, '+oResponse.reason+'</p><p>Please try again later.</p>';
								}
							}
						}
					}
					oSMSR.send(null);
				}
			}
		}()};