/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

jQuery(document).ready(function(){          
    updateBar();
});
/*
jQuery(document).ready(function(){          
    updateFields();
    updateBar();
    
    jQuery("#name").DefaultValue("");
    jQuery("#email").DefaultValue("");
    jQuery("#street").DefaultValue("");
    jQuery("#city").DefaultValue("");
    jQuery("#zip").DefaultValue("");
    jQuery("#telephone").DefaultValue("");
    jQuery("#security_code").DefaultValue("");	
    jQuery("#aboutus").DefaultValue("");	
    
    var options = {         
        beforeSubmit:  showRequest,  // pre-submit callback 
        success:       showResponse  // post-submit callback 
    }; 
    //jQuery("#sample-request-form").ajaxForm(options);    
    
});
*/
    
jQuery.fn.DefaultValue = function(text){
    return this.each(function(){                
        //Make sure we're dealing with text-based form fields
        if(this.type != 'text' && this.type != 'password' && this.type != 'textarea')
            return;
		
        //Store field reference
        var fld_current=this;
		
        //Set value initially if none are specified
        if(this.value=='') {
            this.value=this.title;
        } else {
            //Other value exists - ignore
            return;
        }
		
        //Remove values on focus
        jQuery(this).focus(function() {
            if(this.value==this.title || this.value=='')
                this.value='';
        });
		
        //Place values back on blur
        jQuery(this).blur(function() {
            if(this.value==this.title || this.value=='')
                this.value=this.title;
        });		
    });
};
function showRequest(){
    
    if (jQuery('#sr-agreement-cb').attr('checked')){
        return true;
    }else{
        alert("You must agree to the terms and agreement")
        return false;
    }
    

    return false;
}
function showResponse(){
    jQuery.unblockUI();
    updateFields();
}

function updateFields(){
    jQuery.ajax({
        url:'/catalogrequest/index/getdata?url='+window.location.href,
        dataType: 'json',
        success: function(data){                
            if(data.states)                    
                jQuery("#state").html(data.states);                                
            if(data.securitycode)
                jQuery(".security_code_box").html(data.securitycode);   
            if(data.error){                    
                alert(data.error);
                movetoform();
            }else{
                if(data.success){                    
                    jQuery(".sr-body").html(data.success);
                }
            }
        },
        error: function(XMLHttpRequest,textStatus,errorThrown){
            movetoform();
        }
    });
}

function validate_samplerequest_form(thisform){
    
    with(thisform){
        var fields = new Array();
        fields[0] = new field("","email","",new Array("0","2"));        
        fields[1] = new field("","name","",new Array("0"));        
        fields[2] = new field("","street","",new Array("0"));        
        fields[3] = new field("","city","",new Array("0"));
        fields[4] = new field("","state","",new Array("0"));        
        fields[5] = new field("","zip","",new Array("0","1"));        
        fields[6] = new field("","telephone","",new Array("0"));        
        fields[7] = new field("","interested-in","",new Array("0"));        
        fields[8] = new field("","security_code","",new Array("0"));   
        fields[9] = new field("custom","10");
        return validatefields1(thisform,fields);        
        }
    
}

function custom_10(){    
    if(jQuery(".security_code_box").html() != jQuery("#security_code").val()){
        jQuery("#security_code").addClass("errorfield");
        return false;
    }else{
        jQuery("#security_code").removeClass("errorfield");
        return true;
    }
}
function ajaxLoad(parent_id,msg){
    jQuery('.ajax_loading').remove();
    jQuery('#'+parent_id).prepend('<div class="ajax_loading">'+msg+'</div>');
    jQuery('.ajax_loading').show();
}

function ajaxUnload(){
    jQuery('.ajax_loading').remove();
}

function displayForm(){
    jQuery('#sample-form').dialog('open');
}

function movetoAgreement(){
    var valid = validate_samplerequest_form(jQuery("#sample-request-form"));
    if(valid){
        jQuery('#sr-form').hide();
        jQuery('#sr-agreement').show();
    }
}
function movetoMain(){
    jQuery('#sr-form').show();
    jQuery('#sr-agreement').hide();
}

function movetoform(){
    jQuery('#sr-form').show();
    jQuery('#sr-agreement').hide();
}


function updateBar(){
    var bar_loaded = false;
    var bar_html = jQuery("#consultant-bar").html();
    if(bar_html && bar_html.trim().length > 0){
        bar_loaded = true;
    }
    
    if(!bar_loaded){
        jQuery.ajax({           
            url:'/parties/index/consultantbar',
            dataType: 'json',
            success: function(data){                            
                if(data.html){
                    if(jQuery("#consultant-bar").html().length < 1)
                        jQuery("#consultant-bar").html(data.html);
                }
            },
            error: function(XMLHttpRequest,textStatus,errorThrown){            
            }
        });
    }
    
}
//getUrlVars()["id"];
function getUrlVars(key1) {
    var vars = {};
    var ret_val = "";
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
        vars[key] = value;
        if(key1 == key){            
            ret_val = value;
        }
    });
    return ret_val;
            
}
