/**
 * Formal Forms - Covidien Standard Code for Ajax Form Submission with Formalized UI
 *
 */

$.fn.activateLocator = function(target, options, hideForm, spotLight) {

    var thisDiv = this;
    var parms = $('form', thisDiv).serialize();


    /*
    for(key in options){
    parms = parms + '&' + key + '=' + options[key];
    }
    */

    $(".search").click(function() {

        var langVal = options['languageID'];

     
        if ($("#address").val() == '') {

            if (langVal == '155419') {
                alert("Proporcionar una direcci\u00F3n completa o parcial");
            }
            else if (langVal == '185125') {
                alert("Forne\u00E7a um endere\u00E7o completo ou parcial");
            }
            else {
                alert("Please provide a complete or partial address");
            }
            $("#address").focus();
        } else {
            var radParam = '&rad=' + $("#radius").val();
            var qParam = '&q=' + $("#address").val();
            var qCategory;
            var qProfilePage;
            var qLanguageID;
            var qType;


            if (options['specialty'])
                qCategory = '&specialty=' + options['specialty'];
            else if (options['context'])
                qCategory = '&specialty=' + options['context'];

            if (options['profilePage'])
                qProfilePage = '&profilePage=' + options['profilePage'];

            if (options['languageID'])
                qLanguageID = '&languageCode=' + options['languageID'];
            else
                qLanguageID = '&languageCode=0';

            var orderByParam;
            if ($("#orderBy").val() == '') {
                orderByParam = '';
            } else {
                orderByParam = '&orderby=' + $("#orderBy").val();
            }
            
            if (options['type'])
                qType = '&type=' + options['type'];

            var queryURL = '../SurgeonLocator/results.aspx?contentType=html' + orderByParam + radParam + qParam + qCategory + qProfilePage + qLanguageID + qType;

            //var queryURL = '../SurgeonLocator/results.aspx?contentType=html' + orderByParam + radParam + qParam;	

            $('.formError').hide();
            $('.formResults').hide();
            $('.formInProcess').show();
            //alert(queryURL);    
            $.ajax({
                type: 'GET',
                url: queryURL,
                //data: parms,
                complete: function() { },
                error: function(request, textStatus, exception) {
                    $('.formInProcess').hide();
                    $('.formResults').hide();
                    $('.formError').show();
                },
                success: function(results) {
                    $('.formInProcess').hide();
                    $('.formError').hide();
                    $('.formResults').show();
                    $('.formResults').html(results);
                    
                    //var trackAddress = $("#address").val();
                    //var trackRadius = $("#radius").val();
                    //var trackLabel = "address - radius";
                    //var trackValue = trackAddress + " - " + trackRadius;
                    //pageTracker._trackEvent('Surgeon Locator', 'Search Click', trackLabel, trackValue);

                    pageTracker._trackEvent('Surgeon Locator', 'Search Click');

                    //alert('event tracking end. ' + trackLabel + ' : ' + trackValue);
                                     
                    pageTracker._addTrans(
                     '1234',           // order ID - required. Can assign a a unique id for each search.
                     'Surgeon Search', // affiliation or store name. In this case, term this the Surgeon Search.
                     '0',          	   // total - required. In this case, since doesn't apply, leave blank.
                     '',           	   // tax
                     '',               // shipping
                     '',       	       // city
                     '',     	       // state or province
                     ''                // country
      	            );
       
            		//loop through each profile link and extract params and values
            		$(".contactAddress a").each(function (i) {
			            var arrTemp = this.href.split('&');									   
		
            			//create empty object to store profile link properties
			            var oQueryProps = {};
		            	var surgeonName = $(this).parent().siblings(".contactName").text();
		
            			$(arrTemp).each(function(i){
			            	var arrParts = arrTemp[i].split('=');							
				            oQueryProps[arrParts[0]] = arrParts[1]; 
            			});
			
    		    	 pageTracker._addItem(
	    		    	 '1234',           // order ID - necessary to associate item with transaction. Can assign a a unique id for each search.
		    		     i+'',           // SKU/code - required. Not applicable, so just create some static string.
			    	    oQueryProps.surgeonID+'',           // product name. Can include physician ID here
				        surgeonName,   // category or variation. Not required. Can store name here perhaps - need to grab this dynamically
				        '1',          	   // unit price - required. Not applicable, so leave as 0.
				        '1'               // quantity - required Not applicable, so leave as 0.
			        );
      	        });
    		
    	    	//execute transaction (this sends info to google analytics)
	    	    pageTracker._trackTrans();
                    
                }//end success
            }); //end ajax call
        } //end else
    }); //end click();
};

function updateLocation(lat, lon, spec, profilePage, type) {
    
    var thisDiv = $(".formResults");
    
    $('.formError').hide();
    $('.formResults').hide();
    $('.formInProcess').show();
    
    var radParam = '&rad=' + $("#radius").val();
    var latParam = '&lat=' + lat;
    var longParam = '&long=' + lon;
    var orderByParam;
    var qCategory;
    var  qProfilePage;
    var qType;
    
    if(spec)
         qCategory= '&specialty='+spec;
          	
    if(profilePage)
          qProfilePage = '&profilePage='+profilePage; 
    
 
    if($("#orderBy").val() == '') {
        orderByParam = '';
    } else {
        orderByParam = '&orderby=' + $("#orderBy").val() ;
    }      

    if(type)
          qType = '&type='+type; 

    
    var queryURL = '../SurgeonLocator/results.aspx?contentType=html' + orderByParam + radParam + latParam + longParam+qCategory+qProfilePage+qType;   
  
        
    $.ajax({
        type: 'POST',
        url: queryURL,
		data: '',
		complete: function() {},
		error: function(request,textStatus,exception) {
		    $('.formInProcess').hide();
			$('.formResults').hide();
			$('.formError').show();
		},
		success: function(results) {
		    $('.formInProcess').hide();
			$('.formError').hide();
			$('.formResults').show();
			$('.formResults').html(results);
        }
    });	
}
		

function getSurgeonInfo(sid, langID, specialty) {
    
    var thisDiv = $(".formResults");
    
    $('.formError').hide();
    $('.formResults').hide();
    $('.formInProcess').show();



    var sidParam = '&surgeonID=0';
    var langParam = '&languageCode=0';
    var specialtyParam = '&specialty=all';
    
    //set surgeon ID
    if (!sid) {
        sidParam = '&surgeonID=0';
    } else {
        sidParam = '&surgeonID=' + sid;
    }
    
    //set language Code
    if (!langID) {
        langParam = '&languageCode=0';
    } else {
        langParam = '&languageCode=' + langID;
    }
    
    //set specialty
    if (!specialty) {
        specialtyParam = '&specialty=all';
    } else {
        specialtyParam = '&specialty=' + specialty;
    }
    
    var queryURL = '../SurgeonLocator/profile.aspx?contentType=html' + sidParam + langParam + specialtyParam;
        
    $.ajax({
        type: 'POST',
        url: queryURL,
		data: '',
		complete: function() {},
		error: function(request,textStatus,exception) {
		    $('.formInProcess').hide();
			$('.formResults').hide();
			$('.formError').show();
		},
		success: function(results) {
		    $('.formInProcess').hide();
			$('.formError').hide();
			$('.formResults').show();
			$('.formResults').html(results);
			
			$('.website').click(function(){
			    pageTracker._trackEvent('Surgeon Directory', 'website view', sidParam);	
			})
        }
    });	
}
	
