var _flag_copy_shipping_info = 0;

var ctrl_order = {
	version:'0.1'
	
	,hghgh:function(hjhu){
		
	}
	
	,get_prev_step:function(){
		if( current_step - 1 > 1 /*0*/ )
		{
			$('step' + current_step ).hide();
			$('step' + ( --current_step ) ).show();
			$('btn_next').value = 'Next';
		}
	}
	
	,get_next_step:function(){
		if( ctrl_order._check_value() == true )
		{
			$('step' + current_step ).hide();
			++current_step;
			try {
	      		$('step' + current_step ).show();
		    } catch (e) {
		      	$('create_order').submit();
		    }
		}
	}
	
	,_check_value:function(){
		switch( current_step )
		{
		case 1:
			if( ctrl_order._check_email( $('data[email]') ) ){
				$('btn_next').value = 'Next';
		  		return ctrl_order._login();	
			}else{
				alert('Error: wrong email address.');
			}
		  break    
		case 2:
            //email
            if( !ctrl_order._check_email( $('data[email]') ) ){
                $('data[email]').style.border = '3px solid red';
                $('data[email]').focus();
                return false;
            }else{
                $('data[email]').style.border = '1px solid #789838';
            }
            //fname
            if( $F('data[ship_fname]').empty() == true ){
                $('data[ship_fname]').style.border = '3px solid red';
                $('data[ship_fname]').focus();
                return false;
            }else{
                $('data[ship_fname]').style.border = '1px solid #789838';
            }
            //lname
            if( $F('data[ship_lname]').empty() == true ){
                $('data[ship_lname]').style.border = '3px solid red';
                $('data[ship_lname]').focus();
                return false;
            }else{
                $('data[ship_lname]').style.border = '1px solid #789838';
            }
            //Street Address
            if( $F('data[ship_address]').empty() == true ){
                $('data[ship_address]').style.border = '3px solid red';
                $('data[ship_address]').focus();
                return false;
            }else{
                $('data[ship_address]').style.border = '1px solid #789838';
            }
            //City
            if( $F('data[ship_city]').empty() == true ){
                $('data[ship_city]').style.border = '3px solid red';
                $('data[ship_city]').focus();
                return false;
            }else{
                $('data[ship_city]').style.border = '1px solid #789838';
            }
            //Zip Code
            if( $F('data[ship_zip]').empty() == true ){
                $('data[ship_zip]').style.border = '3px solid red';
                $('data[ship_zip]').focus();
                return false;
            }else{
                $('data[ship_zip]').style.border = '1px solid #789838';
            }
            //Country
            if( $F('data[ship_country]').empty() == true ){
                $('data[ship_country]').style.border = '3px solid red';
                $('data[ship_country]').focus();
                return false;
            }else{
                $('data[ship_country]').style.border = '1px solid #789838';
                //State
                if( $('ship_state').visible() == true && $F('data[ship_state]').empty() == true ){
                    $('data[ship_state]').style.border = '3px solid red';
                    $('data[ship_state]').focus();
                    return false;
                }else{
                    $('data[ship_state]').style.border = '1px solid #789838';
                }
            }
            //Phone
            if( $F('data[ship_phone]').empty() == true ){
                $('data[ship_phone]').style.border = '3px solid red';
                $('data[ship_phone]').focus();
                return false;
            }else{
                $('data[ship_phone]').style.border = '1px solid #789838';
            }
            //Shipping method
            if( $F('data[shipping_methos]').empty() == true ){
                $('data[shipping_methos]').style.border = '3px solid red';
                $('data[shipping_methos]').focus();
                return false;
            }else{
                $('data[shipping_methos]').style.border = '1px solid #789838';
            }
            

			if( _flag_copy_shipping_info == 0 ){
				ctrl_order.copy_shipping_info(); //copy shipping info to billing info
				_flag_copy_shipping_info = 1;
			}
            if( $F('data[bill_cc_owner]').empty() == true ){
                $('data[bill_cc_owner]').value = $F('data[ship_fname]') + ' ' + $F('data[ship_lname]');
            }
            
            
            $('btn_next').value = 'Checkout';
		  	return true;
		  break
		case 3:
            //Payment Method
            if( $F('data[cc_type]').empty() == true ){
                $('data[cc_type]').style.border = '3px solid red';
                $('data[cc_type]').focus();
                return false;
            }else{
                $('data[cc_type]').style.border = '1px solid #789838';
            }
            
            //Street Address
            if( $F('data[bill_address]').empty() == true ){
                $('data[bill_address]').style.border = '3px solid red';
                $('data[bill_address]').focus();
                return false;
            }else{
                $('data[bill_address]').style.border = '1px solid #789838';
            }
            //City
            if( $F('data[bill_city]').empty() == true ){
                $('data[bill_city]').style.border = '3px solid red';
                $('data[bill_city]').focus();
                return false;
            }else{
                $('data[bill_city]').style.border = '1px solid #789838';
            }
            //Zip Code
            if( $F('data[bill_zip]').empty() == true ){
                $('data[bill_zip]').style.border = '3px solid red';
                $('data[bill_zip]').focus();
                return false;
            }else{
                $('data[bill_zip]').style.border = '1px solid #789838';
            }
            //Country
            if( $F('data[bill_country]').empty() == true ){
                $('data[bill_country]').style.border = '3px solid red';
                $('data[bill_country]').focus();
                return false;
            }else{
                $('data[bill_country]').style.border = '1px solid #789838';
                //State
                if( $('bill_state').visible() == true && $F('data[bill_state]').empty() == true ){
                    $('data[bill_state]').style.border = '3px solid red';
                    $('data[bill_state]').focus();
                    return false;
                }else{
                    $('data[bill_state]').style.border = '1px solid #789838';
                }
            }
            
            
            
			//paymethod
			switch( $F('data[cc_type]') )
			{
				case 'paypal':
					$('create_order').action = '/Cart-PayPal';
				break;
				default:
					$('create_order').action = '/Cart-PayOrder';
			}
		
			//$('btn_next').value = 'Checkout';
		  	return true;
		  break
		/*case 4:
		  //policy
            if( $F('data[policy]') == null ){
                $('data[policy]').style.border = '3px solid red';
                $('data[policy]').focus();
                return false;
            }else{
                $('data[policy]').style.border = '1px solid #789838';
            }
			
		  return true;
		  break*/
		}
		return false;
	}
	
	,_login:function(){
		var email = $F('data[email]');
		var password = $F('data[password]');
		
		var myAjax = new Ajax.Request('member-AjaxLogin-' + email + '-' + password , 
						{
						method: 'get',
						onSuccess: function(transport) {
							
							var response = transport.responseText || "no response text";
							if( response == 'OK'){
								$('step' + current_step ).hide();
								$('step' + (++current_step) ).show();
								return true;
							}else{
								alert( response );
								return false;
							}
						},
		    			onFailure: function(){ alert('Something went wrong...') }
		    		}
		    	);
	},
	
	/* Check eMail address */
	_check_email:function( email ){
		var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z]{2,4})+$/;
		return filter.test(email.value);
	},
		
	
	/*Copy shipping info to billing*/
	copy_shipping_info: function(){
		if( $('billing_info_like_shipping').checked )
		{
			$('data[bill_address]').value 	= $F('data[ship_address]');
			$('data[bill_city]').value 		= $F('data[ship_city]');
			$('data[bill_zip]').value 		= $F('data[ship_zip]');
			$('data[bill_country]').value 	= $F('data[ship_country]');
			ctrl_order.set_states_list_bill( $F('data[ship_state]') );
            //$('data[bill_state]').value     = $F('data[ship_state]') ;
			$('step3_billing_info').hide();
		}else{
			ctrl_order.clear_shipping_info();
			$('step3_billing_info').show();
		}
	},
	
	/*Clear shipping info to billing*/
	clear_shipping_info: function(){
		$('data[bill_address]').value 	= '';
		$('data[bill_city]').value 		= '';
		$('data[bill_zip]').value 		= '';
		$('data[bill_country]').value 	= '';
		$('data[bill_state]').value 	= '';

	},
    
    set_shipping_method: function(){
        var shipping = $F('data[shipping_methos]');
        var url = '/Cart-AjaxOrderAmmount-' + shipping;
        var myAjax = new Ajax.Updater(
            'order_ammount', 
            url, 
            {
                method: 'get' 
            });
        
        if( $F('data[shipping_methos]') ){
            var nodes = $A( $('data[shipping_methos]') );
            nodes.each(function(node){
                //alert(node.nodeName + ': ' + node.innerHTML + ': ' + node.value + ': ' + node.index);
                if( $F('data[shipping_methos]') == node.value ){
                    $('shipping_method_name').update( nodes[ node.index ].innerHTML );
                }
                
            });
        }else{
            $('shipping_method_name').update( '' );
        }
        
    },
    
    set_states_list: function( selected ){
       var country = $F('data[ship_country]');
       var myAjax =  new Ajax.Request('/ajax-state_list-' + country, {
          method:'get',
          onSuccess: function(transport){
             //data[ship_state]
             $('data[ship_state]').length = 0;
             if( transport.responseText.empty() == false ){
                $('ship_state').show();
                eval( transport.responseText ); // var json;
                $('data[ship_state]')[0]=new Option('Select state', '', false) ;
                for(i = 1 ; json.states[i - 1] ; i++ ){
                     $('data[ship_state]')[i]=new Option(json.states[i].name, json.states[i].iso, false, (selected == json.states[i].iso ? true : false)) ;
                }
             }else{
                $('ship_state').hide();
             }
           }
        });
        //billing info
        //$('data[bill_country]').value     = $F('data[ship_country]');
        //ctrl_order.set_states_list_bill();
        //$('data[bill_state]').value     = $F('data[ship_state]') ;
    },
    
    set_states_list_bill: function( selected ){
       var country = $F('data[bill_country]');
       var myAjax =  new Ajax.Request('/ajax-state_list-' + country, {
          method:'get',
          onSuccess: function(transport){
             //data[ship_state]
             $('data[bill_state]').length = 0;
             if( transport.responseText.empty() == false ){
                $('bill_state').show();
                eval( transport.responseText ); // var json;
                $('data[bill_state]')[0]=new Option('Select state', '', false) ;
                for(i = 1 ; json.states[i - 1] ; i++ ){
                     $('data[bill_state]')[i]=new Option(json.states[i].name, json.states[i].iso, false, (selected == json.states[i].iso ? true : false) ) ;
                }
             }else{
                $('bill_state').hide();
             }
           }
        });
    },

    
    set_billing_form: function(){
        var billing = $F('data[cc_type]');
        //hide
        if( current_billing_form != '' ){
            $(current_billing_form).hide(); 
        }
        //show
        if( !billing.empty() ){
            current_billing_form = 'step3_billing_' + billing;
            $(current_billing_form ).show();
        }
        
        
    },
    
    /* retrive email/password */
    forgotemail: function(){
        var url = '/Member-Password-';
        var myAjax = new Ajax.Request(url, {
          method: 'get',
          parameters: $F('forgotemail'),
          onSuccess: function(transport) {
            alert( transport.responseText  );
            $('forgot').hide();
          }
        });
            
            

            
    }
    
	/*,
	submit_form: function()
	{
		$('create_order').submit();	
	}*/
	
	
};


//function dump_obj(obj_name,obj) {
//    var result = "";
//    for (var i in obj) {
//        try {
//          result += obj_name + "." + i + " = " + obj[i] + " (" + typeof(obj[i]) + ")\n";
//        } catch (err) {
//          result += obj_name + "." + i + " = <undefined>\n";
//        }
//    }
    //alert(result);
//    $('debug_js').innerHTML = result;
//} 