if(typeof jQuery != "function"){ alert("jQuery missing"); } if(window.location.hostname != "www.toonfootball.com"){ alert("You are not authorized to use this api"); } //initialse the cnhq namespace jQuery(function(){ (function($) { var _$ = window.cnhq = function() { return new cnhq.fn.init(); }, udf = undefined; $.cnhq = _$; _$.jq = $; _$.minSwfVer = 8; _$.version = "1.0.0"; _$.fn = cnhq.prototype = { init:function(){ return _$.version; }, getApi:function(api){ var c = _$.apiconfig; var m = _$.apimapper; if(typeof c == typeof udf){ throw "API_CONIG_NOT_FOUND"; } if(typeof eval("m." + api) != typeof udf){ return "http://" + c.dn + "/" + c.lang + eval("m." + api); } }, checkCrossXhr:function(){ if(window.swfobject != udf && window.CrossXHR != udf){ var obj = window.swfobject.getFlashPlayerVersion(); if(obj.major >= _$.minSwfVer){ return true; } } return false; }, getParameterByName:function( name ){ name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]"); var regexS = "[\\?&]"+name+"=([^]*)"; var regex = new RegExp( regexS ); var results = regex.exec( window.location.href ); if( results == null ) return ""; else return results[1]; }, getRequestMethod:function(){ if(_$.fn.checkCrossXhr()){ return _$.fn.crossxhr; }else{ return _$.fn.jsonp; } }, profileParse:function(d){ if(d.status == "0"){ var profile = d.response.PROFILE, role = d.response.ROLE, cf = d.response.CUSTOM_FIELD; profile.getValue = function(field){ return (typeof profile[field] != typeof udf)?profile[field].VALUE:""; }; var dt = { userId: profile.getValue("ID"), userName: profile.getValue("USER"), firstName: profile.getValue("FIRST_NAME"), lastName: profile.getValue("LAST_NAME"), email: profile.getValue("EMAIL"), dateOfBirth: profile.getValue("DATE_OF_BIRTH"), gender: profile.getValue("GENDER"), country: profile.getValue("COUNTRY"), city: profile.getValue("CITY"), state: profile.getValue("STATE"), hintQuestion: profile.getValue("HINT_Q"), hintAnswer: profile.getValue("HINT_A"), newsletter: (profile.getValue("NEWSLETTER") === null || profile.getValue("NEWSLETTER") == "0")?false:true, isAdult: (typeof role["R.ADULTCOMPLETE"] != typeof udf || typeof role["R.ADULTINCOMPLETE"] != typeof udf)?true:false, isParent: (typeof role["R.PARENTCOMPLETE"] != typeof udf || typeof role["R.PARENTINCOMPLETE"] != typeof udf)?true:false, isConfirmed: (typeof role["R.ADULTCOMPLETE"] != typeof udf || typeof role["R.KIDSCOMPLETE"] != typeof udf || typeof role["R.PARENTCOMPLETE"] != typeof udf)?true:false, parentEmail: profile.getValue("PARENT_EMAIL") }; for(key in cf){ dt[key] = cf[key]; } d.response = dt; } return d; }, updateProfileParse:function(p){ var map = { parentFirstName: "a_fn", parentLastName: "a_ln", parentEmail: "a_em", parentAddress: "a_address", parentPhone: "a_phone", city : "city", country : "cnty", email : "em", firstName : "fn", gender: "gr", lastName : "ln", newsletter : "nl", hintQuestion : "hq", hintAnswer : "ha", state : "state" }; var par = {}; for (var key in p ){ switch (true){ case !!(typeof map[key] != typeof udf): par[map[key]] = p[key]; break; case !!(key.match(/my\:.*/)): par[key] = p[key]; break; } } return par; }, crossxhr:function(api,par,cb){ function interCB(){ if ( this.readyState == 4) { res = eval("("+this.responseText+")"); res = _$.fn.translateResponse(res); }else{ /* * force error */ res = _$.fn.translateResponse({data:{},status:-1}); } cb(res); } par = $.param(par); par = (par)?"?"+par:""; api = _$.fn.getApi(api); req = new CrossXHR(); req.onreadystatechange = interCB; req.open('GET', api+par); req.send(); }, jsonp:function(api,par,cb){ api = _$.fn.getApi(api); function interCB(d){ res = _$.fn.translateResponse(d); cb(res); } $.ajax({ type: "GET", cache:false, data:par, jsonp:"jsonp_callback", url: api, dataType: "jsonp", success: interCB }); }, translateResponse:function(d){ if(status!="0"){ /* * Error code map. */ switch(d.msg_code){ case "ERR_USER_NOT_FOUND": case "ERR_INCORRECT_PASSWORD": e = "ERR_BAD_CREDENTIALS"; break; case "ERR_NO_PASSPORT_SESSION": e = "ERR_NOT_LOGGED_IN"; break; case "SUCCESS": e = null; break; case "ERR_FIELD_IS_MANDATORY": e = "ERR_CONSTRAINT_VIOLATION"; break; case "ERR_MISSING_ARGUMENT": e = "ERR_PARAMETER_REQUIRED"; break; case "ERR_INVALID_TOKEN": e = "ERR_SESSION_EXPIRED"; break; case "ERR_INVALID_USERNAME": case "ERR_EMAIL_ALREADY_EXISTS": case "ERR_MISSING_ARGUMENT": case "ERR_INVALID_INPUT": case "ERR_INVALID_PASSWORD": case "ERR_USER_ALREADY_EXISTS": case "ERR_USER_NOT_RESERVED": e = d.msg_code; break; default: e = "ERR_SERVER_ERROR"; break; } } return {response:d.data,error:e,status:d.status}; }, dateFormat:function(date,del){ del = del || "-"; function twodigit(str){ return (str.length == 1)? "0"+str:str; } if ( Object.prototype.toString.call(d) !== "[object Date]" && !isNaN(date.getTime())){ var d = {year:date.getFullYear()+"",month:twodigit((date.getMonth()+1)+""),date:twodigit(date.getDate()+"")}; d.full = d.year + del + d.month + del + d.date; }else{ var d = {year:"",month:"",date:"",full:""}; } return d; } }; _$.fn.call = _$.fn.getRequestMethod(); })(jQuery); //passport api path mapping (function(_$){ _$.apimapper = { login: "/auth/login", logout: "/auth/logout", getProfile:"/profile/fullprofile", updateProfile:"/jsapi/updateprofile", getLoginStatus:"/user/getloginstatus", signUp:"/signup/basic/update", requestParentalConsent:"/jsapi/requestparentalconsent", getChildProfileForParentalConsent:"/jsapi/getchildprofileforparentalconsent", parentalConsent:"/jsapi/parentalconsent", suggestUserName:"/jsapi/suggestusername", sendResetPasswordEmail:"/jsapi/sendresetpasswordemail", resetPassword:"/jsapi/resetpassword", requestEmailVerify:"/jsapi/requestemailverify", emailVerify:"/jsapi/emailverify" }; _$.token = _$.fn.getParameterByName("token"); })(cnhq); //initilise passport namespace and api functions (function(_$){ _$.passport = { //login fucnctions login:function(username,password,callback){ var par = {"username":username,"password":password,"gen_auth":1}; _$.fn.call("login",par,callback); }, /* logout functions * creates a invisible iframe apendding to the last child element of body * interCB calculates how many iframe finshed loading */ logout: function(callback){ function interCB(d){ if(d.status !== 0){ callback(d); }else{ v = d.response.logout_urls; l = v.length - 1; if (_$.fn.checkCrossXhr()){ que = []; function swf_called(){ if(this.readyState == 4){ l --; if(l === 0){ callback(d); } }else{ //force error d = _$.fn.translateResponse({data:{},status:-1}); callback(d); } } for (var i = 0; i <= l; i++){ que[i] = new CrossXHR(); que[i].onreadystatechange = swf_called; que[i].open('GET', v[1]); que[i].send(); } }else{ function if_loaded(){ l --; $(this).remove(); if(l === 0){ callback(d); $("#logoutContainer").remove(); } } $(document.body).append("
"); $("#logoutContainer").hide(); for (var i = 0; i <= l; i++){ $("#logoutContainer").append(""); ifr = $("#logoutif_"+i).attr("src",v[i]); ifr.load(if_loaded); }; } } } _$.fn.call("logout",{},interCB); }, getLoginStatus:function(callback){ _$.fn.call("getLoginStatus",{},callback); }, getProfile:function(callback){ function interCB(d){ d = _$.fn.profileParse(d); callback(d); } _$.fn.call("getProfile",{},interCB); }, updateProfile:function(profile,callback){ var par = _$.fn.updateProfileParse(profile); _$.fn.call("updateProfile",par,callback); }, signUp:function(username,password,dateOfBirth,country,hintQuestion,hintAnswer,callback){ var dob = _$.fn.dateFormat(dateOfBirth); var par = {username:username,password:password,confirm_password:password,dob:dob.full,dob_year:dob.year,dob_month:dob.month,dob_day:dob.date,country:country,token:"",question:hintQuestion,answer:hintAnswer}; _$.fn.call("signUp",par,callback); }, requestParentalConsent:function(email,cid,callback){ var par = {parent_email:email || "",cid:cid || ""}; _$.fn.call("requestParentalConsent",par,callback); }, parentalConsent:function(childProfile,callback){ var par = _$.fn.updateProfileParse(childProfile); par.token = _$.token; _$.fn.call("parentalConsent",par,callback); }, getChildProfileForParentalConsent:function(callback){ var par = {token: _$.token || ""}; function interCB(d){ d = _$.fn.profileParse(d); callback(d); } _$.fn.call("getChildProfileForParentalConsent",par,interCB); }, suggestUserName:function(username,firstname,lastname,callback){ var par = {username:username || "",fn: firstname || "", ln:lastname || ""}; _$.fn.call("suggestUserName",par,callback); }, sendResetPasswordEmail:function(username,callback){ var par = {username:username}; _$.fn.call("sendResetPasswordEmail",par,callback); }, resetPassword:function(password,callback){ var par = { token: _$.token || "", password: password, confirm_password: password }; _$.fn.call("resetPassword",par,callback); }, requestEmailVerify:function(email,cid,callback){ var par = {email:email || "",cid:cid || ""}; _$.fn.call("requestEmailVerify",par,callback); }, emailVerify:function(token,callback){ var par = {token:token || ""}; _$.fn.call("emailVerify",par,callback); } }; })(cnhq); }); jQuery(function(){ if(typeof window.cnhq != "function"){ alert("passport library missing"); } (function(_$){ _$.apiconfig = { dn: "www.cartoonnetworkhq.com", lang: "us" }; })(cnhq); });