package com.oxylusflash.book { import com.oxylusflash.utils.StringUtil; import com.oxylusflash.utils.XMLUtil; import flash.display.Sprite; import flash.events.Event; import flash.events.FocusEvent; import flash.events.HTTPStatusEvent; import flash.events.IOErrorEvent; import flash.events.MouseEvent; import flash.events.SecurityErrorEvent; import flash.events.TimerEvent; import flash.geom.Rectangle; import flash.net.URLLoader; import flash.net.URLRequest; import flash.net.URLRequestMethod; import flash.net.URLVariables; import flash.text.TextField; import flash.text.TextFieldAutoSize; import flash.utils.Timer; public class ContactUs extends PopupWindowContent { private static const VALIDATION_OK:String = "validationOK"; public var infoTxt:TextField; public var yourNameInp:InputField; public var yourEmailInp:InputField; public var subjectInp:InputField; public var commentInp:InputField; public var submitBtn:DynamicButton; public var resetBtn:DynamicButton; private var yourEmailStr:String; private var yourNameStr:String; private var subjectStr:String; private var commentStr:String; private var formSendLdr:URLLoader; private var formSendReq:URLRequest; private var formSendVar:URLVariables; private var inpToGainFocus:InputField; /** * Send to friend content. */ public function ContactUs() { autoHideTimer.delay = 1000 * Global.xmlContactUs.messageTime; infoTxt.autoSize = TextFieldAutoSize.LEFT; infoTxt.styleSheet = Global.styleSheet; infoTxt.multiline = true; infoTxt.wordWrap = true; infoTxt.condenseWhite = true; infoTxt.selectable = true; infoTxt.mouseWheelEnabled = false; infoTxt.htmlText = Global.xmlContactUs.contactDetails; infoTxt.addEventListener(Event.SCROLL, infoTxt_scrollHandler, false, 0, true); formSendReq = new URLRequest(Global.xmlContactUs.script); formSendReq.method = URLRequestMethod.POST; formSendVar = new URLVariables(); //formSendVar.recipientEmail = Global.xmlContactUs.recipientEmail; yourNameInp.nextFocus = yourEmailInp; yourEmailInp.nextFocus = subjectInp; subjectInp.nextFocus = commentInp; commentInp.nextFocus = yourNameInp; yourNameInp.textField.restrict = "^0-9~!@#$%\\^&*()_+={}[]:;\\\\|<>?/"; yourNameInp.width = Global.xmlContactUs.formWidth; yourEmailInp.width = Global.xmlContactUs.formWidth; subjectInp.width = Global.xmlContactUs.formWidth; commentInp.width = Global.xmlContactUs.formWidth; infoTxt.width = Global.xmlContactUs.width - Global.xmlContactUs.formWidth - Global.xmlContactUs.formMarginRight; infoTxt.x = Global.xmlContactUs.width - infoTxt.width; infoTxt.scrollRect = new Rectangle(0, 0, infoTxt.width, Global.xmlContactUs.height); yourNameInp.y = 0; yourEmailInp.y = yourNameInp.y + yourNameInp.height + Global.xmlContactUs.fieldsYSpacing; subjectInp.y = yourEmailInp.y + yourEmailInp.height + Global.xmlContactUs.fieldsYSpacing; commentInp.y = subjectInp.y + subjectInp.height + Global.xmlContactUs.fieldsYSpacing; submitBtn.y = resetBtn.y = Global.xmlContactUs.height - submitBtn.height; commentInp.height = Global.xmlContactUs.height - commentInp.y - submitBtn.height - Global.xmlContactUs.fieldsYSpacing; yourNameInp.addEventListener(InputField.FOCUS, input_focusHandler, false, 0, true); yourEmailInp.addEventListener(InputField.FOCUS, input_focusHandler, false, 0, true); subjectInp.addEventListener(InputField.FOCUS, input_focusHandler, false, 0, true); commentInp.addEventListener(InputField.FOCUS, input_focusHandler, false, 0, true); yourNameInp.addEventListener(InputField.BLUR, input_blurHandler, false, 0, true); yourEmailInp.addEventListener(InputField.BLUR, input_blurHandler, false, 0, true); subjectInp.addEventListener(InputField.BLUR, input_blurHandler, false, 0, true); commentInp.addEventListener(InputField.BLUR, input_blurHandler, false, 0, true); yourNameInp.addEventListener(InputField.SUBMIT, input_submitHandler, false, 0, true); yourEmailInp.addEventListener(InputField.SUBMIT, input_submitHandler, false, 0, true); subjectInp.addEventListener(InputField.SUBMIT, input_submitHandler, false, 0, true); yourNameInp.addEventListener(InputField.CANCEL, input_cancelHandler, false, 0, true); yourEmailInp.addEventListener(InputField.CANCEL, input_cancelHandler, false, 0, true); subjectInp.addEventListener(InputField.CANCEL, input_cancelHandler, false, 0, true); submitBtn.label = Global.xmlTranslate.str68; resetBtn.label = Global.xmlTranslate.str69; resetBtn.x = submitBtn.width + Global.xmlContactUs.buttonsXSpacing; submitBtn.addEventListener(MouseEvent.CLICK, submitBtn_clickHandler, false, 0, true); resetBtn.addEventListener(MouseEvent.CLICK, resetBtn_clickHandler, false, 0, true); resetForm(); } private function infoTxt_scrollHandler(e:Event):void { infoTxt.scrollV = 1; } /** * Init. */ override public function init():void { } // Buttons events handlers. private function submitBtn_clickHandler(e:MouseEvent):void { submitForm(); } private function resetBtn_clickHandler(e:MouseEvent):void { resetForm(); } /** * Create new URL loader for submiting the form data. */ private function createNewFormSendLdr():void { if (formSendLdr) { formSendLdr.removeEventListener(IOErrorEvent.IO_ERROR, formSendLdr_eventHandler); formSendLdr.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, formSendLdr_eventHandler); formSendLdr.removeEventListener(Event.COMPLETE, formSendLdr_eventHandler); formSendLdr = null; } formSendLdr = new URLLoader(); formSendLdr.addEventListener(IOErrorEvent.IO_ERROR, formSendLdr_eventHandler, false, 0, true); formSendLdr.addEventListener(SecurityErrorEvent.SECURITY_ERROR, formSendLdr_eventHandler, false, 0, true); formSendLdr.addEventListener(Event.COMPLETE, formSendLdr_eventHandler, false, 0, true); } /** * Pressing the ESC key when an input has focus. * @param e */ private function input_cancelHandler(e:Event):void { if (e.currentTarget.parent is InputField) { switch(e.currentTarget.parent) { case yourNameInp: yourNameStr = yourNameInp.label = Global.xmlTranslate.str58; break; case yourEmailInp: yourEmailStr = yourEmailInp.label = Global.xmlTranslate.str59; break; case subjectInp: subjectStr = subjectInp.label = Global.xmlTranslate.str60; break; case commentInp: commentStr = commentInp.label = Global.xmlTranslate.str61; break; } } } /** * Pressing the ENTER key when an input has focus. * @param e */ private function input_submitHandler(e:Event):void { submitForm(); } /** * Input focus and blur handlers. * @param e */ private function input_focusHandler(e:Event):void { if (e.currentTarget is InputField) { switch(e.currentTarget) { case yourNameInp: if (yourNameStr == Global.xmlTranslate.str58) yourNameInp.label = ""; break; case yourEmailInp: if (yourEmailStr == Global.xmlTranslate.str59) yourEmailInp.label = ""; break; case subjectInp: if (subjectStr == Global.xmlTranslate.str60) subjectInp.label = ""; break; case commentInp: if (commentStr == Global.xmlTranslate.str61) commentInp.label = ""; break; } } } private function input_blurHandler(e:Event):void { if (e.currentTarget is InputField) { switch(e.currentTarget) { case yourNameInp: if (!StringUtil.isBlank(yourNameInp.label)) yourNameStr = StringUtil.squeeze(yourNameInp.label); else yourNameStr = Global.xmlTranslate.str58; yourNameInp.label = yourNameStr; break; case yourEmailInp: if (!StringUtil.isBlank(yourEmailInp.label)) yourEmailStr = StringUtil.squeeze(yourEmailInp.label); else yourEmailStr = Global.xmlTranslate.str59; yourEmailInp.label = yourEmailStr; break; case subjectInp: if (!StringUtil.isBlank(subjectInp.label)) subjectStr = StringUtil.squeeze(subjectInp.label); else subjectStr = Global.xmlTranslate.str60; subjectInp.label = subjectStr; break; case commentInp: if (!StringUtil.isBlank(commentInp.label)) commentStr = commentInp.textField.htmlText; else commentStr = Global.xmlTranslate.str61; commentInp.label = commentStr; break; } } } /** * Get message after form validation. * @return Validation message. */ private function formValidationMsg():String { if (yourNameStr == Global.xmlTranslate.str58) { inpToGainFocus = yourNameInp; return Global.xmlTranslate.str62; } if (yourEmailStr == Global.xmlTranslate.str59) { inpToGainFocus = yourEmailInp; return Global.xmlTranslate.str63; } if (!StringUtil.isEmail(yourEmailStr)) { inpToGainFocus = yourEmailInp; return Global.xmlTranslate.str64; } if (subjectStr == Global.xmlTranslate.str60) { inpToGainFocus = subjectInp; return Global.xmlTranslate.str65; } if (commentStr == Global.xmlTranslate.str61) { inpToGainFocus = commentInp; return Global.xmlTranslate.str66; } return VALIDATION_OK; } /** * Sends the from data to a server side script. */ private function submitForm():void { var msg:String = formValidationMsg(); if (msg == VALIDATION_OK) { createNewFormSendLdr(); formSendVar.senderName = yourNameStr; formSendVar.senderEmail = yourEmailStr; formSendVar.subject = subjectStr; formSendVar.senderComment = commentStr; formSendReq.data = formSendVar; formSendLdr.load(formSendReq); popupWindow.disable(); alertAbout(Global.xmlTranslate.str67, false); } else { alertAbout(msg); } } /** * URL loader events handler. * @param e */ private function formSendLdr_eventHandler(e:Event):void { switch (e.type) { case IOErrorEvent.IO_ERROR: alertAbout(IOErrorEvent(e).text); break; case SecurityErrorEvent.SECURITY_ERROR: alertAbout(SecurityErrorEvent(e).text); break; case Event.COMPLETE: var responseData:Object = com.oxylusflash.utils.XMLUtil.getParams(new XML(formSendLdr.data)); if (responseData.success) { resetForm(); alertAbout(Global.xmlTranslate.str70); } else { alertAbout(responseData.message); } break; } } /** * On timer complete. */ override protected function autoHideTimer_timerCompleteHandler(e:TimerEvent):void { super.autoHideTimer_timerCompleteHandler(e); if (inpToGainFocus) { inpToGainFocus.gainFocus(); inpToGainFocus = null; } } /** * Reset form. */ private function resetForm():void { yourNameStr = yourNameInp.label = Global.xmlTranslate.str58; yourEmailStr = yourEmailInp.label = Global.xmlTranslate.str59; subjectStr = subjectInp.label = Global.xmlTranslate.str60; commentStr = commentInp.label = Global.xmlTranslate.str61; } override public function reset():void { resetForm(); } } }