/*
   You can set timerlen and slideAniLen as ur wish.
*/
var timerlen =100;
var slideAniLen = 250;

var timerID = new Array();
var startTime = new Array();
var obj = new Array();
var endHeight = new Array();
var moving = new Array();
var dir = new Array();

function slidedown(did){
        
        if(moving[did])
                return;

        if(document.getElementById(did).style.display != "none")
                return; // cannot slide down something that is already visible
        moving[did] = true;
        dir[did] = "down";
        startslide(did);
        
}

function slideup(did){
        if(moving[did])
                return;

        if(document.getElementById(did).style.display == "none")
                return; // cannot slide up something that is already hidden

        moving[did] = true;
        dir[did] = "up";
        startslide(did);
}

function startslide(did){
              
        obj[did] = document.getElementById(did);
        endHeight[did] = parseInt(obj[did].style.height);
        startTime[did] = (new Date()).getTime();

        if(dir[did] == "down"){
               obj[did].style.height = "1px";
        }
        obj[did].style.display = "block";
        timerID[did] = setInterval('slidetick(\'' + did + '\');',timerlen);
  }

function slidetick(did){
        var elapsed = (new Date()).getTime() - startTime[did];
                
        if (elapsed > slideAniLen)
                endSlide(did)
        else {
                var d =Math.round(elapsed / slideAniLen * endHeight[did]);
                if(dir[did] == "up")
                        d = endHeight[did] - d;
                    
                obj[did].style.height = d + "px";
        }

        return;
}

function endSlide(did){
        clearInterval(timerID[did]);

        if(dir[did] == "up")
                obj[did].style.display = "none";
                

        obj[did].style.height = endHeight[did] + "px";

        delete(moving[did]);
        delete(timerID[did]);
        delete(startTime[did]);
        delete(endHeight[did]);
        delete(obj[did]);
        delete(dir[did]);

        return;
}


function toggleSlide(did, checkbox)
{
   
    if(checkbox.checked == false)
    {
	    // div is hidden, so let's slide down
	    slidedown(did);                   
    }
    else
    {
	    // div is not hidden, so slide up
	    if(CheckValidate())
        {
	        slideup(did);
	        checkbox.checked = true;
	    }     
	    else
	    {	       
	        checkbox.checked = false;    
	    }            
    }
		
}

function toggleSlideRegister(did, checkbox)
{     

    var txtBilling_Email = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucBillingInfo1_TextBoxEmail');
    if(document.getElementById(did).style.display != "none")
    {
	    // div is hidden, so let's slide down
	    slideup(did);                  
    }
    else
    {
	    // div is not hidden, so slide up	   
	    if(CheckValidate())
        {
	        slidedown(did); 
	        checkbox.checked = true;
	    }     
	    else
	    {	        
	        checkbox.checked = false;    
	    }     
	}		
}

/*===== Check Validate for Billing and Shipping Information =====*/
    
    function CheckValidate()
    {
        var txtBilling_FirstName = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucBillingInfo1_TextBoxFirstName');
        var txtBilling_LastName = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucBillingInfo1_TextBoxLastName');
        var txtBilling_Address1 = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucBillingInfo1_TextBoxAddr1');
        var txtBilling_Address2 = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucBillingInfo1_TextBoxAddr2');
        var txtBilling_City = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucBillingInfo1_TextBoxCity');
        var ddlBilling_Country = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucBillingInfo1_DropDownListCountry');        
        var ddlBilling_State = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucBillingInfo1_DropDownListState');
        var txtBilling_State = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucBillingInfo1_TextBoxState');
        var txtBilling_ZipCode = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucBillingInfo1_TextBoxZipCode');
        var txtBilling_Email = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucBillingInfo1_TextBoxEmail');
        var txtBilling_DayPhone = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucBillingInfo1_TextBoxDayPhone');
        var txtBilling_NightPhone = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucBillingInfo1_TextBoxNightPhone');
        
        var txtShipping_FirstName = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucShippingInfo1_TextBoxFirstName');
        var txtShipping_LastName = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucShippingInfo1_TextBoxLastName');
        var txtShipping_Address1 = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucShippingInfo1_TextBoxAddr1');
        var txtShipping_Address2 = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucShippingInfo1_TextBoxAddr2');
        var txtShipping_City = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucShippingInfo1_TextBoxCity');
        var ddlShipping_Country = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucShippingInfo1_DropDownListCountry');        
        var ddlShipping_State = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucShippingInfo1_DropDownListState');
        var txtShipping_State = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucShippingInfo1_TextBoxState');
        var txtShipping_ZipCode = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucShippingInfo1_TextBoxZipCode'); 
        var txtShipping_DayPhone = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucShippingInfo1_TextBoxDayPhone');
        var txtShipping_NightPhone = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucShippingInfo1_TextBoxNightPhone');      
        var txtShipping_ProfileName = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucShippingInfo1_TextBoxShipProfileName');      
        
        var div_DropDownListState = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucShippingInfo1_div_DropDownListState');
        var div_TextBoxState = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucShippingInfo1_div_TextBoxState');
        var div_StarState = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucShippingInfo1_div_StarState');
               
        var LabelUserName = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucUserRegister1_LabelUserNameValue');
        var message = "";
        var returnvalue = false;
        if(txtBilling_FirstName.value.length == 0)
            message = "First Name cannot be blank. \n"; 
        if(txtBilling_LastName.value.length == 0)
            message += "Last Name cannot be blank. \n";  
        if(txtBilling_Address1.value.length == 0)
            message += "Address 1 cannot be blank. \n";   
        if(txtBilling_City.value.length == 0)
            message += "City cannot be blank. \n";
        if(ddlBilling_Country.selectedIndex == 0)       
            message += "Country cannot be blank. \n";    
        if(ddlBilling_Country.value == "US" && ddlBilling_State.selectedIndex == 0)       
            message += "State cannot be blank. \n";
        if(txtBilling_ZipCode.value.length == 0)       
            message += "Zip Code cannot be blank. \n";        
        if(txtBilling_Email.value.length == 0)        
            message += "Email Address cannot be blank. \n";   
          
        if(txtBilling_Email.value != "" && CheckEmail(txtBilling_Email) == false)
        {              
            message += "Email Address is invalid. \n";                  
        }
        else
        {
            LabelUserName.innerText = txtBilling_Email.value;
        }
        if(message != "")
        {
            alert(message);            
        }
        else
        {
            if(ddlBilling_Country.value == "US")
            {
                div_DropDownListState.style.display = "inline";
                div_TextBoxState.style.display = "none";
                div_StarState.style.display = "inline";                    
                ddlShipping_State.selectedIndex = ddlBilling_State.selectedIndex;                    
            }
            else
            {
                div_DropDownListState.style.display = "none";
                div_TextBoxState.style.display = "inline";
                div_StarState.style.display = "none";                
                txtShipping_State.value = txtBilling_State.value;                
            }
            
            txtShipping_FirstName.value = txtBilling_FirstName.value;
            txtShipping_LastName.value = txtBilling_LastName.value;
            txtShipping_Address1.value = txtBilling_Address1.value;
            txtShipping_Address2.value = txtBilling_Address2.value;
            txtShipping_City.value = txtBilling_City.value;
            ddlShipping_Country.selectedIndex = ddlBilling_Country.selectedIndex;
            txtShipping_ZipCode.value = txtBilling_ZipCode.value;
            txtShipping_DayPhone.value = txtBilling_DayPhone.value;
            txtShipping_NightPhone.value = txtBilling_NightPhone.value;          
            txtShipping_ProfileName.value = "Default Shipping Address";
            
            returnvalue = true;
        
        }
        return returnvalue;
    } 

    function CheckEmail(textbox)
    {       
        var filter=/^.+@.+\..{2,3}$/;
        if (filter.test(textbox.value))
           return true;
        else            
           return false;
                
    }

    function UpdateDropDownListValue()
    {
//        var ddlBilling_Country = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucBillingInfo1_DropDownListCountry');        
//        var ddlBilling_State = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucBillingInfo1_DropDownListState');
//        var txtBilling_State = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucBillingInfo1_TextBoxState');
//        
//        var ddlShipping_Country = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucShippingInfo1_DropDownListCountry');        
//        var ddlShipping_State = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucShippingInfo1_DropDownListState');
//        var txtShipping_State = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucShippingInfo1_TextBoxState');
//        
//        var div_DropDownListState = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucShippingInfo1_div_DropDownListState');
//        var div_TextBoxState = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucShippingInfo1_div_TextBoxState');
//        var div_StarState = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucShippingInfo1_div_StarState');
        var txtBilling_FirstName = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucBillingInfo1_TextBoxFirstName');
        var txtBilling_LastName = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucBillingInfo1_TextBoxLastName');
        var txtBilling_Address1 = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucBillingInfo1_TextBoxAddr1');
        var txtBilling_Address2 = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucBillingInfo1_TextBoxAddr2');
        var txtBilling_City = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucBillingInfo1_TextBoxCity');
        var ddlBilling_Country = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucBillingInfo1_DropDownListCountry');        
        var ddlBilling_State = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucBillingInfo1_DropDownListState');
        var txtBilling_State = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucBillingInfo1_TextBoxState');
        var txtBilling_ZipCode = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucBillingInfo1_TextBoxZipCode');
        var txtBilling_Email = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucBillingInfo1_TextBoxEmail');
        var txtBilling_DayPhone = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucBillingInfo1_TextBoxDayPhone');
        var txtBilling_NightPhone = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucBillingInfo1_TextBoxNightPhone');
        
        var txtShipping_FirstName = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucShippingInfo1_TextBoxFirstName');
        var txtShipping_LastName = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucShippingInfo1_TextBoxLastName');
        var txtShipping_Address1 = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucShippingInfo1_TextBoxAddr1');
        var txtShipping_Address2 = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucShippingInfo1_TextBoxAddr2');
        var txtShipping_City = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucShippingInfo1_TextBoxCity');
        var ddlShipping_Country = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucShippingInfo1_DropDownListCountry');        
        var ddlShipping_State = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucShippingInfo1_DropDownListState');
        var txtShipping_State = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucShippingInfo1_TextBoxState');
        var txtShipping_ZipCode = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucShippingInfo1_TextBoxZipCode'); 
        var txtShipping_DayPhone = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucShippingInfo1_TextBoxDayPhone');
        var txtShipping_NightPhone = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucShippingInfo1_TextBoxNightPhone');      
        var txtShipping_ProfileName = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucShippingInfo1_TextBoxShipProfileName');      
        
        var div_DropDownListState = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucShippingInfo1_div_DropDownListState');
        var div_TextBoxState = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucShippingInfo1_div_TextBoxState');
        var div_StarState = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucShippingInfo1_div_StarState');
        
        var checkBox = document.getElementById('ctl00_ContentPlaceHolderMainContent_ucShippingInfo1_CheckBoxSame');
        if(ddlBilling_Country.value == "US")
        {
            div_DropDownListState.style.display = "inline";
            div_TextBoxState.style.display = "none";
            div_StarState.style.display = "inline"; 
        }
        else
        {
            div_DropDownListState.style.display = "none";
            div_TextBoxState.style.display = "inline";
            div_StarState.style.display = "none";
        }
        if(checkBox.checked)
        {
            ddlShipping_Country.selectedIndex = ddlBilling_Country.selectedIndex;
            ddlShipping_State.selectedIndex = ddlBilling_State.selectedIndex; 
            txtShipping_State.value = txtBilling_State.value;
            txtShipping_FirstName.value = txtBilling_FirstName.value;
            txtShipping_LastName.value = txtBilling_LastName.value;
            txtShipping_Address1.value = txtBilling_Address1.value;
            txtShipping_Address2.value = txtBilling_Address2.value;
            txtShipping_City.value = txtBilling_City.value;
            ddlShipping_Country.selectedIndex = ddlBilling_Country.selectedIndex;
            txtShipping_ZipCode.value = txtBilling_ZipCode.value;
            txtShipping_DayPhone.value = txtBilling_DayPhone.value;
            txtShipping_NightPhone.value = txtBilling_NightPhone.value;          
            txtShipping_ProfileName.value = "Default Shipping Address";
        }
    }


function DisplayShippingInfo(did, checkbox)
{
    var check = document.getElementById(checkbox);
    if(check.checked == true)
    {
	    // div is hidden, so let's slide down
	    slideup(did);	                       
    }
    else
    {
	   // div is not hidden, so slide up	   
	   slidedown(did);
    }
}

function DisplayRegisterInfo(did, checkbox)
{
    var check = document.getElementById(checkbox);
    if(check.checked == true)
    {
	    // div is hidden, so let's slide down	    
	   slidedown(did);                      
    }
    else
    {
	   // div is not hidden, so slide up	   
	   slideup(did);	 
    }
}
