var origHref;

$.ajaxSetup({
    type: "POST",
    cache: false,
    dataType: "json",
    scriptCharset: "utf-8"
});

function checkLogin(obj, onSuccess) {
	try {
		origHref = obj.href;
		$.get('/zorbax/session/is-recognized', '', function(text) {
			if(text=='1') {
				onSuccess(origHref, obj, true);
			} else {
				tb_show('', '#TB_inline?height=550&width=400&inlineId=register');

				$('#loginform')[0].onsubmit = function() {
					try {
						if(this.purl.value) {
                            var channel = (this.URLprofile.value) ? this.URLprofile.value : 'nochannel' ;
							$.post('/zorbax/'+channel+'/login', $(this).serialize(), function(response) {
								if(response.data.errors == "0"){
									if(typeof _pclick != "undefined") {
                                        _pclick.init(_pclick.pclick_url,response.data.purlId,response.data.channelId);
                                        _pclick.trackEvent('Login','login',true);
                                    }
									onSuccess(origHref, obj, false);
								}else{
									alert('Invalid Purl! Please try again, or click "Register" if you do not have an account.');
								}
							}, "json");
						}
					} catch(e) { console.debug(e); }
					return false;
				}

				$('#registerform')[0].onsubmit = function() {
					$('#register_submit')[0].disabled = true;
					try {
						if(this.firstName.value && this.lastName.value && this.business.value && this.address1.value && this.city.value && this.state.value && this.postal.value && this.country.selectedIndex && this.email.value && this.market.selectedIndex) {
							var channel = (this.URLprofile.value) ? this.URLprofile.value : 'nochannel' ;
                            var data = {
                                firstName: this.firstName.value,
                                lastName: this.lastName.value,
                                business: this.business.value,
                                section: this.section.value,
                                address1: this.address1.value,
                                city: this.city.value,
                                state: this.state.value,
                                postal: this.postal.value,
                                country: this.country.value,
                                phone: this.phone.value,
                                email: this.email.value,
                                market: this.market.value,
                                URLprofile: this.URLprofile.value
                            };
                            $.ajax({
                                url: '/zorbax/'+channel+'/create-purl',
                                data: data,
                                success: function(response) {
                                    if(response.data.errors == "0"){
                                        if(typeof _pclick != "undefined"){
                                            var lbl = 'Registered for PURL';
                                            // check for a global _pageName.
                                            if(typeof _pageName != 'undefined' && _pageName){
                                                lbl += '_' + _pageName;
                                            }
                                            _pclick.trackEvent(lbl,false,false,response.data.purlId,response.data.channelId);
                                        }
                                        onSuccess(origHref, obj, true);
                                    }else{
                                        alert('A server error occurred...\nSettings not saved...\n');
                                        $('#register_submit')[0].disabled = false;
                                    }
                                }
							});
						}
						else
						{
							alert('Please complete all the required fields.');
							$('#register_submit')[0].disabled = false;
						}
					} catch(e) { console.debug(e); }
					return false;
				}
			}
		});
	} catch(e) { console.debug(e); }
	return false;
}
