var FreeConnect=function(){var customInvitationText=null;var friendsName=null;var callerNumber;var calleeNumber;var callerCountryDialCode;var calleeCountryDialCode;var callerCountryISOCode;var calleeCountryISOCode;var callerName;var callerEMail;var callerPhoneWithoutCountryCode;var calleePhoneWithoutCountryCode;var implicitUserId=null;var connectValidateEmail=function(input){if(input.split("@").length>2){throw new Error("Enter one email address")}if(!input.match(/[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/)){throw new Error("Enter a valid email address")}if(input=="friend@email.com"){throw new Error("Enter a valid email address")}return true};var handleLoginClick=function(){recordClickStream("LINK_CLICK","LOGIN_FC","INDEX_FC_LOGIN");var screenName=$("#emailField").val();var password=$("#passwordField").val();if(screenName==""){errorMessage("Please enter your Email");$("#emailField").addClass("jx_field_error").focus();return }if(password==""){errorMessage("Please enter your password");$("#passwordField").addClass("jx_field_error").focus();return }jQuery.ajax({type:"POST",url:"callcontrol",cache:false,data:{act:"login",wtype:"js-call-element",screenName:screenName,password:password},success:function(msg){var jsonMsg=eval("("+msg+")");if(!jsonMsg.callOptions.error){document.location.href="home.jsp?friendsPhoneNumber="+calleeNumber}else{if(jsonMsg.callOptions.errorCode=="INVALID_NAME_PASSWORD"){errorMessage("Please verify your email/password")}else{errorMessage("Unknown error, please retry")}}}})};var initializeGetNumberPage=function(implicitUuid){implicitUserId=implicitUuid;recordClickStream("VISIT","NONE","INDEX_FC_SUCCESS");$("#signUpLink").click(function(){var targetUrl="registration.jsp?";if(implicitUserId!=null){targetUrl+="imuid="+implicitUserId}recordClickStream("LINK_CLICK","SIGNUP_FC","INDEX_FC_SUCCESS");document.location.href=targetUrl})};var initializeErrorPage=function(){recordClickStream("VISIT","NONE","INDEX_FC_ERROR");$("#signUpLink").click(function(){var targetUrl="registration.jsp?";if(implicitUserId!=null){targetUrl+="imuid="+implicitUserId}recordClickStream("LINK_CLICK","SIGNUP_FC","INDEX_FC_ERROR");document.location.href=targetUrl});$("#tryAgainLink").click(function(){recordClickStream("LINK_CLICK","TRY_AGAIN_FC","INDEX_FC_ERROR");document.location.href="index.jsp"})};var initializeLoginPage=function(){recordClickStream("VISIT","NONE","INDEX_FC_LOGIN");if(callerEMail){$("#emailField").attr("value",callerEMail);$("#passwordField").focus()}else{$("#emailField").focus()}$("#loginButton").click(handleLoginClick)};var initializeInfoPage=function(){$("#fullNameField").focus();recordClickStream("VISIT","NONE","INDEX_FC_USER_INFO");$("#emailField").keypress(function(e){if((e.which&&e.which==13)||(e.keyCode&&e.keyCode==13)){$("#getJaxtrNumberButton").trigger("click");return false}else{return true}});$("#fullNameField").keypress(function(e){if((e.which&&e.which==13)||(e.keyCode&&e.keyCode==13)){$("#getJaxtrNumberButton").trigger("click");return false}else{return true}});$("#getJaxtrNumberButton").click(function(){recordClickStream("LINK_CLICK","GET_NUMBER_FC","INDEX_FC_USER_INFO");var fullname=$("#fullNameField").val();var email=$("#emailField").val();if(fullname==""){errorMessage("Please enter your Full Name");$("#fullNameField").addClass("jx_field_error").focus()}else{if(email==""){errorMessage("Please enter your Email");$("#emailField").addClass("jx_field_error").focus()}else{$.ajax({type:"POST",url:"FreeConnect.action?UserInfo=true",dataType:"json",data:{caller:callerNumber,callee:calleeNumber,fullname:fullname,email:email},success:function(json){implicitUserId=json.implicitUserUUID;if(json.error==true){handleUserInfoErrors(json)}else{$.ajax({type:"POST",url:"FreeConnect.action?GetNumber=true",dataType:"html",data:{caller:callerNumber,callee:calleeNumber},success:function(html){$("#free_connect_div").html(html)}})}}})}}})};var handleUserInfoErrors=function(json){switch(json.errorCode){case"CALLER_NULL":$("#free_connect_div").load("FreeConnect.action?init=true&errorCode=CALLER_NULL",{},initializeFreeConnectScreen);break;case"CALLEE_NULL":$("#free_connect_div").load("FreeConnect.action?init=true&errorCode=CALLEE_NULL",{},initializeFreeConnectScreen);break;case"CALLER_ERROR":$("#free_connect_div").load("FreeConnect.action?init=true&errorCode=CALLER_ERROR",{},initializeFreeConnectScreen);break;case"CALLEE_ERROR":$("#free_connect_div").load("FreeConnect.action?init=true&errorCode=CALLEE_ERROR",{},initializeFreeConnectScreen);break;case"NO_CALLER_DID":$.ajax({type:"POST",url:"FreeConnect.action?ErrorPage=true",dataType:"html",data:{caller:callerNumber,callee:calleeNumber,errorCode:"NO_CALLER_DID"},success:function(html){$("#free_connect_div").html(html)}});break;case"NO_CALLEE_DID":$.ajax({type:"POST",url:"FreeConnect.action?ErrorPage=true",dataType:"html",data:{caller:callerNumber,callee:calleeNumber,errorCode:"NO_CALLEE_DID"},success:function(html){$("#free_connect_div").html(html)}});break;case"FULL_NAME_NULL":errorMessage("Please enter your name");$("#fullNameField").focus();$("#fullNameField").addClass("jx_field_error");break;case"EMAIL_NULL":errorMessage("Please enter your Email");$("#emailField").focus();$("#emailField").addClass("jx_field_error");break;case"FULL_NAME_ERROR":errorMessage("Please enter a valid name");$("#fullNameField").focus();$("#fullNameField").addClass("jx_field_error");break;case"EMAIL_ERROR":errorMessage("Please enter a valid Email");$("#emailField").focus();$("#emailField").addClass("jx_field_error");break;case"EXPLICIT_MEMBER":callerEMail=json.userEmail;$.ajax({type:"POST",url:"FreeConnect.action?LoginPage=true",dataType:"html",success:function(html){$("#free_connect_div").html(html);initializeLoginPage()}});break;case"CREATE_USER_FAILED":errorMessage("Please enter valid information");$("#fullNameField").focus();break;default:alert("Unknown error. Report this with error code: 1986");break}};var handlePreCheckErrors=function(json){switch(json.errorCode){case"CALLER_NULL":errorMessage("Please enter your mobile number");$("#phone_number_caller").focus();$("#phone_number_caller").addClass("jx_field_error");break;case"CALLEE_NULL":errorMessage("Please enter your friend's mobile number");$("#phone_number_callee").focus();$("#phone_number_callee").addClass("jx_field_error");break;case"CALLER_ERROR":errorMessage("Your mobile number is invalid, please enter a valid number.");$("#phone_number_caller").focus();$("#phone_number_caller").addClass("jx_field_error");break;case"CALLEE_ERROR":errorMessage("Your friend's mobile number is invalid, please enter a valid number");$("#phone_number_callee").focus();$("#phone_number_callee").addClass("jx_field_error");break;case"NO_CALLER_DID":$.ajax({type:"POST",url:"FreeConnect.action?ErrorPage=true",dataType:"html",data:{caller:callerNumber,callee:calleeNumber,errorCode:"NO_CALLER_DID"},success:function(html){$("#free_connect_div").html(html)}});break;case"NO_CALLEE_DID":$.ajax({type:"POST",url:"FreeConnect.action?ErrorPage=true",dataType:"html",data:{caller:callerNumber,callee:calleeNumber,errorCode:"NO_CALLEE_DID"},success:function(html){$("#free_connect_div").html(html)}});break;case"IMPLICIT_CALLER_DID_EXHAUSTED":$.ajax({type:"POST",url:"FreeConnect.action?ErrorPage=true",dataType:"html",data:{caller:callerNumber,callee:calleeNumber,errorCode:"IMPLICIT_CALLER_DID_EXHAUSTED"},success:function(html){$("#free_connect_div").html(html)}});break;case"CALLEE_DID_EXHAUSTED":$.ajax({type:"POST",url:"FreeConnect.action?ErrorPage=true",dataType:"html",data:{caller:callerNumber,callee:calleeNumber,errorCode:"CALLEE_DID_EXHAUSTED"},success:function(html){$("#free_connect_div").html(html)}});break;case"CALLER_DID_EXHAUSTED":callerEMail="";$.ajax({type:"POST",url:"FreeConnect.action?LoginPage=true",dataType:"html",success:function(html){$("#free_connect_div").html(html)}});break;case"EXPLICIT_MEMBER":callerEMail="";$.ajax({type:"POST",url:"FreeConnect.action?LoginPage=true",dataType:"html",success:function(html){$("#free_connect_div").html(html)}});break;case"IMPLICIT_CALLEE_MEMBER":$.ajax({type:"POST",url:"FreeConnect.action?InfoPage=true",dataType:"html",success:function(html){$("#free_connect_div").html(html)}});break;case"CALLER_NEW_MEMBER":$.ajax({type:"POST",url:"FreeConnect.action?InfoPage=true",dataType:"html",success:function(html){$("#free_connect_div").html(html)}});break;case"CALL_POLICY_ERROR":$.ajax({type:"POST",url:"FreeConnect.action?ErrorPage=true",dataType:"html",data:{caller:callerNumber,callee:calleeNumber,errorCode:"CALL_POLICY_ERROR"},success:function(html){$("#free_connect_div").html(html)}});break;default:alert("Unknown error. Report this with error code: 1986");break}};var select_country_code=function(elmt,iso){$(elmt).find(".countries option").removeAttr("selected");$(elmt).find(".countries option[value="+iso.toUpperCase()+"]").attr("selected","selected");var country=countries[iso.toLowerCase()];if(country){var code=countries[iso.toLowerCase()].code;$(elmt).find(".country_code").val(code)}else{$(elmt).find(".country_code").val("")}};var predict_country_code=function(elmt,phone_number){var stripped=phone_number.replace(/[\(\)\.\-\ \,\/]/g,"");if(stripped.indexOf("+")===0){var match="";for(var i in country_codes){if(stripped.indexOf(i)===0){match=country_codes[i]}}if(match){select_country_code(elmt,match);var number=$.trim(phone_number);$(elmt).find(".phone").val($.trim(number.substring(countries[match].code.length)))}}};var valid_phone_number=function(phone_number){if(!phone_number){return false}var stripped=phone_number.replace(/[\(\)\.\-\ \+\,\/]/g,"");if(isNaN(Number(stripped))){return false}if(stripped.length<6||stripped.length>20){return false}var status=$.ajax({url:"/user/ValidatePhoneNumber.action",data:"phoneNumber=+"+stripped,timeout:2,async:false}).responseText;status=eval("("+status+")");if(status.valid==="false"){return false}return true};var resetMessages=function(){$(".jx_notification_error").remove();$(".jx_notification_success").remove();$(".jx_field_error").removeClass("jx_field_error")};var errorMessage=function(message){resetMessages();message=message.replace(/&lt;/g,"<");message=message.replace(/&gt;/g,">");var error=document.createElement("div");$(error).addClass("jx_notification_error");$(error).html("<p>"+message+"</p>");$(error).insertAfter(".dialog h2")};var validate_phone_number=function(elmt,user_entered_phone){if(user_entered_phone){var phone_number=$(elmt).find(".country_code").val()+$(elmt).find(".phone").val();if(!valid_phone_number(phone_number)){errorMessage("Please enter a valid phone number");$(elmt).find(".phone").addClass("jx_field_error").focus()}else{resetMessages();$(elmt).find(".phone").removeClass("jx_field_error")}}};return{setCountryPhoneCode:function(valu){if(valu==""){$("#error_message").html('<div class="jx_notification_error"><p>Please select a country</p></div>')}else{$("#error_message").html("");var dialCode=valu.split(" ");$("#connect_country_code_1").val(dialCode[1])}},setCountryPhoneCode2:function(valu){if(valu==""){$("#error_message").html('<div class="jx_notification_error"><p>Please select a country</p></div>')}else{$("#error_message").html("");var dialCode=valu.split(" ");$("#connect_country_code_2").val(dialCode[1])}},init:function(){$().ajaxStop(function(){$("#fc_loading").hide()});$().ajaxSend(function(){$("#fc_loading").show()});var prevent_close=false;$(".questions .link").click(function(e){prevent_close=true;setTimeout(function(){prevent_close=false},1000);e.preventDefault();$("#questions").fadeIn();var pos={sTop:function(){return window.pageYOffset||document.documentElement&&document.documentElement.scrollTop||document.body.scrollTop},wHeight:function(){return window.innerHeight||document.documentElement&&document.documentElement.clientHeight||document.body.clientHeight},sLeft:function(){return window.pageXOffset||document.documentElement&&document.documentElement.scrollLeft||document.body.scrollLeft},wWidth:function(){return window.innerWidth||document.documentElement&&document.documentElement.clientWidth||document.body.clientWidth}};var elHeight=$("#questions").height();var elWidth=$("#questions").width();var elTop=pos.sTop()+(pos.wHeight()/2)-(elHeight/2);var elLeft=pos.sLeft()+(pos.wWidth()/2)-(elWidth/2);$("#questions").css({position:"absolute",marginTop:"0",top:elTop,left:elLeft});$("#questions").bgiframe()});$("body").click(function(e){if(!prevent_close){setTimeout(function(){$("#questions").fadeOut()},50)}});$(".phone_input").attachPhoneBehaviors();$(".phone_input").each(function(){var parent=this;$(parent).find(".phone").change(function(){validate_phone_number(parent,true)});$(parent).find(".countries, .country_code, .phone").keypress(function(e){if((e.which&&e.which==13)||(e.keyCode&&e.keyCode==13)){$("#fc_start_button").trigger("click");return false}else{return true}})});var callerCountryDialCodeFromCookie=jQuery.cookie("callerCountryDialCode");var countryDropDownValueFromCookie=jQuery.cookie("countryDropDownValue");var callerPhoneWithoutCountryCodeFromCookie=jQuery.cookie("callerPhoneWithoutCountryCode");if(callerCountryDialCodeFromCookie&&countryDropDownValueFromCookie&&callerPhoneWithoutCountryCodeFromCookie){$("#caller_country option").each(function(){if($(this).val()==countryDropDownValueFromCookie){$(this).attr("selected","selected")}});$("#caller_country_code").val(callerCountryDialCodeFromCookie);$("#caller_phone").val(callerPhoneWithoutCountryCodeFromCookie);$("#callee_phone").focus()}else{$("#caller_phone").focus()}recordClickStream("VISIT","NONE","INDEX_FC_MAIN");$("#fc_start_button").click(function(){resetMessages();recordClickStream("LINK_CLICK","GET_NUMBER_FC","INDEX_FC_MAIN");callerCountryDialCode=$("#caller_country_code").val();callerCountryISOCode=$("#caller_country").val();callerPhoneWithoutCountryCode=$("#caller_phone").val();jQuery.cookie("callerCountryDialCode",callerCountryDialCode);jQuery.cookie("countryDropDownValue",callerCountryISOCode);jQuery.cookie("callerPhoneWithoutCountryCode",callerPhoneWithoutCountryCode);callerNumber=$("#phone_input_caller").find(".country_code").val()+$("#phone_input_caller").find(".phone").val();if(!valid_phone_number(callerNumber)){errorMessage("Please enter a valid phone number.");$("#phone_input_caller").find(".phone").addClass("jx_field_error").focus();return }else{resetMessages();$("#phone_input_caller").find(".phone").removeClass("jx_field_error")}calleeCountryISOCode=$("#callee_country_code").val().split(" ")[0];calleeCountryDialCode=$("#callee_country").val();calleePhoneWithoutCountryCode=$("#callee_phone").val();calleeNumber=$("#phone_input_callee").find(".country_code").val()+$("#phone_input_callee").find(".phone").val();if(!valid_phone_number(calleeNumber)){errorMessage("Please enter a valid phone number.");$("#phone_input_callee").find(".phone").addClass("jx_field_error").focus();return }else{resetMessages();$("#phone_input_callee").find(".phone").removeClass("jx_field_error")}if(callerNumber==calleeNumber){errorMessage("Your friend's number cannot not be the same as your number.");$("#phone_input_callee").find(".phone").addClass("jx_field_error").select();return }$.ajax({type:"POST",url:"FreeConnect.action?PreCheck=true",dataType:"json",data:{caller:callerNumber,callee:calleeNumber},success:function(json){implicitUserId=json.implicitUserUUID;if(json.error==true){handlePreCheckErrors(json)}else{$.ajax({type:"POST",url:"FreeConnect.action?GetNumber=true",dataType:"html",data:{caller:callerNumber,callee:calleeNumber},success:function(html){$("#free_connect_div").html(html)}})}}})})},initLogin:function(){initializeLoginPage()},initError:function(){initializeErrorPage()},initInfo:function(){initializeInfoPage()},initGetNumber:function(imuid){initializeGetNumberPage(imuid)}}}();