$(document).ready(function(){

	//recognized
	if ($("input[name=_recognized]").val() == 1) {
		$("#usercontainer, #welcome1").hide();
		$("#userinfo, #welcome2").show();
	}
	else if ($("input[name=_recognized]").val() == 2) {
		$("#welcome1").hide();
		$("#welcome2").show();
	}

	//switch views
	$("#switch, #updatereg").click(function(e) {
		e.preventDefault();
		$("#userinfo").hide();
		$("#usercontainer").show();
                //InField plugin for textbox - prafulla 
                $(function(){ $("label").inFieldLabels(); });
	});

	//not me
	$("#notme").click(function(e) {
		e.preventDefault();
		$("#userinfo, #welcome2").hide();
		$("#usercontainer, #welcome1").show();
                   
                // set all the select fields the first option (index 0)
	         $("#usercontainer select").each(function() {
			$(this).attr("selectedIndex", 0);
		});

                // Set the hidden field values to blank
                $('input[name=ocpcode1]').val('');
		$('input[name=ocpcode2]').val('');
		$('input[name=ocpcode3]').val('');
		$('input[name=indcode1]').val('');
		$('input[name=indcode2]').val('');

                // prafulla: Set the opcode and industry field values to default 
                $('#ocpcode1_dt :first-child').attr('selected', true)
                //alert($('#ocpcode1_dt :first-child').attr('selected'));
                $('#indcode1_dt :first-child').attr('selected', true)
                $('#indcode2_dt :first-child').attr('selected', true)

              // reset recognized for page back later - sure it should be this way?
		$("input[name=_recognized]").val(0);    

//		$("#usercontainer input[type='text'],#usercontainer input[type='hidden'],#usercontainer textarea").each(function() {
        	$("#usercontainer input[type='text'],#usercontainer textarea").each(function() {
                    $(this).val($(this).attr('title'));
        	    $(this).blur();
		});

                /*
		$("#usercontainer input[type=hidden]").each(function() {
			$(this).val('');
		});
                */

		$("#usercontainer :checked").removeAttr('checked');

		// leftover prefill fields
		$("input[name=fax],input[name=url],input[name=website]").val('');
		$("#ocpcode_secondary,#ocpcode_other,#indcode_secondary,#indcode_other").hide();
	});

        //InField plugin for textbox
        $(function(){ $("label").inFieldLabels(); });


	//clearInput plugin for textbox
	$(".clearinput").clearInput();

	//clearValue
	$("#tp_submit").click(function(){
		$("#tp_qual .clearinput").each(function(){
			if ($(this).attr('title') == $(this).val()) { $(this).val(''); }
		});
	});

	//modal window
	$("#modal-qlogin").jqm();
	$(".qualform.login").click(function() {
		$("#modal-qlogin").jqm({overlayClass: "qloginOverlay", modal: true});
		$("#modal-qlogin").jqmShow();
		$("#email-login").blur();
		return false;
	});

	// ajax prefill
	var aj_pfill = function(data) {
		var items = [];
		var fields = [];
		$.each(data, function(key, val) {
			var code = /ocpcode|indcode/;	//industry+occupation code need to be sorted
			if (key.match(code)) {
				//no sort by key, so set value to key=val, then sort on value
				items.push(key+'='+val);
			}
			if (val!='') {
				if ($("input[name=" + key + "]:radio").length > 0) {
					$("input[name=" + key + "]:radio[value=" + val +"]").attr("checked", true);
				}
				else if ($("input[name=" + key + "]").length > 0) {
					$("input[name=" + key + "]").val(val);
				}
				else if ($("select[name=" + key + "]").length > 0) {
					$("select[name=" + key + "]").val(val);
				}
			}
			fields[key] = val;
		});

		//sort array so questions are in order
		var items2 = items.sort();

		//loop through sorted ocp/ind array.
		for (i=0; i< items2.length; i++){
			//split on '=' and set the dropdown values.
			//s[0] is key, s[1] is value
			var s = items2[i].split("=");
			$("select[name=" + s[0] + "_dt]").val(s[1]);
			if (s[0] == 'indcode1') { checkValInd(); }
			else if (s[0] == 'ocpcode1') { checkValOcp(); }
		}

		if (fields._recognized == 1) {
                      //alert("i am here 0 ");
			for (var i in fields) {
				if ($("#pf_" +i)) { $("#pf_" +i).html(fields[i]); }
			}
			$("#usercontainer").hide();
			$("#userinfo").show();
		}

                //InField plugin for textbox
                $(function(){ $("label").inFieldLabels(); });

                //alert("i am here 1");
		$("#welcome1").html("Welcome Back, " + data.first + ". Please review the information for <u>accuracy and completeness</u>.");
		$("#modal-qlogin").jqmHide();
	}; // end aj_pfill;

	// tp login
	$("#submit-login").click(function(e) {
		e.preventDefault();
		var em = $("#email-login").val();
		var ph = $("#phone-login").val();
		if (em == '' || em == $("#email-login").attr('title') || ph == '' || ph == $("#phone-login").attr('title')) {
			$("#error-login").css("display","block");
			return false;
		}

		$.ajax({
			type: "GET",
			url: "/c/tplogin.mpl",
			data: {email:em, phone:ph},
				dataType: "json",
				contentType: "application/json; charset=utf-8",
				success: aj_pfill,
				error: function(XMLHttpRequest, textStatus, errorThrown){
					$("#error-login").css("display","block");
				}
		}); // end ajax
	}); // end submit click

        // prafulla - Fix for linkedin login close button
        // Source: http://forum.jquery.com/topic/trigger-from-iframe-to-parent-window
        // Trigger from iframe to parent window
        var domainvar;
        domainvar = document.domain;
        pathArray = (domainvar).split("\.");
        var newdomain = pathArray[1] + "." + pathArray[2]
        //alert(newdomain);
        document.domain = newdomain;       
        //document.domain = 'tradepub.com';
        $(document).bind('myCustomTrigger', function(e) {
           $(".jqmClose").click();
        });

	// linkedin login
	$(".jqmClose").click(function(e) {
		var tid;
		if ($.cookie("tpid")) { tid = $.cookie("tpid"); }

		$.ajax({
			type: "GET",
			url: "/c/tplogin.mpl",
			data: {tpid:tid},
				dataType: "json",
				contentType: "application/json; charset=utf-8",
				success: aj_pfill,
				error: function(XMLHttpRequest, textStatus, errorThrown){
				}
		}); // end ajax
	}); // end jqmClose click

}); // end ready

(function($){$.InFieldLabels=function(b,c,d){var f=this;f.$label=$(b);f.label=b;f.$field=$(c);f.field=c;f.$label.data("InFieldLabels",f);f.showing=true;f.init=function(){f.options=$.extend({},$.InFieldLabels.defaultOptions,d);if(f.$field.val()!=""){f.$label.hide();f.showing=false};f.$field.focus(function(){f.fadeOnFocus()}).blur(function(){f.checkForEmpty(true)}).bind('keydown.infieldlabel',function(e){f.hideOnChange(e)}).change(function(e){f.checkForEmpty()}).bind('onPropertyChange',function(){f.checkForEmpty()})};f.fadeOnFocus=function(){if(f.showing){f.setOpacity(f.options.fadeOpacity)}};f.setOpacity=function(a){f.$label.stop().animate({opacity:a},f.options.fadeDuration);f.showing=(a>0.0)};f.checkForEmpty=function(a){if(f.$field.val()==""){f.prepForShow();f.setOpacity(a?1.0:f.options.fadeOpacity)}else{f.setOpacity(0.0)}};f.prepForShow=function(e){if(!f.showing){f.$label.css({opacity:0.0}).show();f.$field.bind('keydown.infieldlabel',function(e){f.hideOnChange(e)})}};f.hideOnChange=function(e){if((e.keyCode==16)||(e.keyCode==9))return;if(f.showing){f.$label.hide();f.showing=false};f.$field.unbind('keydown.infieldlabel')};f.init()};$.InFieldLabels.defaultOptions={fadeOpacity:0.5,fadeDuration:300};$.fn.inFieldLabels=function(c){return this.each(function(){var a=$(this).attr('for');if(!a)return;var b=$("input#"+a+"[type='text'],"+"input#"+a+"[type='password'],"+"textarea#"+a);if(b.length==0)return;(new $.InFieldLabels(this,b[0],c))})}})(jQuery);






