﻿// JavaScript methods for the Phone user control.

var txtIdPhone = 0;
var selIdPhone = 0;
var btnIdPhone = 0;
var chxIdPhone = 0;

var isPhoneNumberVisible = true;
var isCountryVisible = true;
var isDescriptionVisible= true;
var isTelephoneNumberVisible = true;
var isPrimaryVisible= true;

var isCountryRequired = true;
var isDescriptionRequired = true;
var isTelephoneNumberRequired = true;
var isPrimaryRequired = true;

var countryRequiredError = '';
var descriptionRequiredError = '';
var telephoneNumberRequiredError= '';
var primaryRequiredError = '';

function filterText(tbValue) {
    // Remove commas, pipes and underscores from the text since they are being used as array delimiters 
    tbValue.value = tbValue.value.replace(/,/g, "");
    tbValue.value = tbValue.value.replace(/_/g, "");
    tbValue.value = tbValue.value.replace(/\174/g, "");
}

// Add a phone number to the container
function addPhoneNumber(descriptionTextBoxId, countryDropDownId, telephoneNumberTextBoxId,
        primaryBoxId, selectedCountry,removePhoneButtonLabel, selectedDescription,
        selectedTelephoneNumber, selectedPrimary ) {
    
	var container = document.getElementById("tablePhoneContainer");	
	var phoneRow = container.insertRow(-1);
	
    var dupeCountryBox;
    var dupeDoNotCallResonBox;
    var dupeDescriptionBox;
    var dupeTelephoneNumberBox;
    var dupePrimaryBox;

    //Telephone field
	dupeTelephoneNumberBox = document.getElementById(telephoneNumberTextBoxId).cloneNode(true);
	dupeTelephoneNumberBox.id = "_TelephoneNumber" + eval(txtIdPhone += 1);
	dupeTelephoneNumberBox.value = selectedTelephoneNumber;
    container.rows[container.rows.length - 1].insertCell(0);
    container.rows[container.rows.length - 1].cells[0].appendChild(dupeTelephoneNumberBox);	 	
    
    // Description field
	dupeDescriptionBox = document.getElementById(descriptionTextBoxId).cloneNode(true);
	dupeDescriptionBox.id = "_Description" + eval(txtIdPhone += 1);
	dupeDescriptionBox.value = selectedDescription;
    container.rows[container.rows.length - 1].insertCell(1);
    container.rows[container.rows.length - 1].cells[1].appendChild(dupeDescriptionBox);	    
   
	dupeCountryBox = document.getElementById(countryDropDownId).cloneNode(true);
	dupeCountryBox.id = "_Country" + eval(selIdPhone += 1);
	dupeCountryBox.selectIndex = selectedCountry;
	selectDropDownOption(dupeCountryBox, selectedCountry)
    container.rows[container.rows.length - 1].insertCell(2);
    container.rows[container.rows.length - 1].cells[2].appendChild(dupeCountryBox);	 	
	
	dupePrimaryBox = document.getElementById(primaryBoxId).cloneNode(false);
	dupePrimaryBox.id = "_Primary" + eval(chxIdPhone += 1);
	dupePrimaryBox.style.display = "block";

	if (selectedPrimary =='1' || selectedPrimary == true || selectedPrimary == 'true')
	    dupePrimaryBox.checked = true;	
	else dupePrimaryBox.checked = false;
    
    container.rows[container.rows.length - 1].insertCell(3);
    container.rows[container.rows.length - 1].cells[3].align = "center";
    container.rows[container.rows.length - 1].cells[3].appendChild(dupePrimaryBox);	 	

	if (selectedPrimary =='1' || selectedPrimary == true || selectedPrimary == 'true')
	    dupePrimaryBox.checked = true;	
	else dupePrimaryBox.checked = false;
	
	// Add remove button
	var btnRemove = document.createElement("input");
	btnRemove.type = "button";
	btnRemove.value = removePhoneButtonLabel;	
	btnRemove.id = "remove_" + eval(btnIdPhone += 1);
	btnRemove.className = "btnRemove";	
    container.rows[container.rows.length - 1].insertCell(4);
    container.rows[container.rows.length - 1].cells[4].appendChild(btnRemove);	 		
	
    // Add the row to the DOM       
	reInitializePhone();    
}
// Add event for delete a phone
function reInitializePhone() {
	var removeButtons = document.getElementById("tablePhoneContainer").getElementsByTagName("input");
	for (i = 0; i < removeButtons.length; i ++) {		
		if((removeButtons[i].type == "button") && (removeButtons[i].className != "glassFormButtonMed") ){		    		    
			removeButtons[i].onclick = function () {
    			this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode);     			
    			validateFirstItem();
    			reInitializeRadio();
			 }
			removeButtons[i].className = "btnRemove";
		}
	}	
	validateFirstItem();
}
// Handle the state for first phone row.
function validateFirstItem()
{
	if(document.getElementById("tablePhoneContainer").rows.length == 2)
	    document.getElementById("defaultPrimaryLabel").style.display = "none";
    else
        document.getElementById("defaultPrimaryLabel").style.display = "block";
	    
    var radioList = document.getElementById("tablePhoneContainer").getElementsByTagName("input");	                  

    if(radioList.length == 3)
        radioList[2].style.display = "none";
        
    if(radioList.length > 3)
        radioList[2].style.display = "block";          
}
// Process the written phones
function processPhoneNumber(descriptionID, countryID, telephoneID, primaryID) {
	
    var phone = "";
    var contentPrincipal = document.getElementById("tablePhoneContainer");
    var phoneRows = contentPrincipal.getElementsByTagName("tr");
    var phoneInfoHidden; 
    for (var i = 0; i < phoneRows.length; i = i + 1) 
    {
         
        if (phoneRows[i].getElementsByTagName("input").length > 0) {

            var collectionText = phoneRows[i].getElementsByTagName("input");
            var collectionList = phoneRows[i].getElementsByTagName("select");
            phone = phone + collectionText[0].value + "_Description,";      
            phone = phone + collectionList[0].value + "_Country,";                        
            phone = phone + collectionText[1].value + "_TelephoneNumber,";
            phone = phone + collectionText[2].checked + "_Primary|" ;
        }
    }
    
   
    for(var i=0;i<document.forms[0].elements.length;i++)
    {	       
        var element = document.forms[0].elements[i];
        if(element.id.substring(element.id.length-17,element.id.length) == '_PhoneInformation')
	    {
	        phoneInfoHidden = document.getElementById(element.id);		   
	        phoneInfoHidden.value =phone;
	    }
    }    
   
     phoneInfoHidden.value = phone;
    
     return true;
}
// Handle the initial state for phone container
function initializeGridPhone(descriptionTextBoxId, countryDropDownId, telephoneNumberTextBoxId,
    primaryCheckBox, defaultValues, removeButtonLabelText,defaultCountryValue){ 
      
  	if (removeButtonLabelText != null)
        removeButtonLabel = removeButtonLabelText;
    
    //First row is always number 0
    if (defaultValues.length > 0)
    {
        var description;
        var country;
        var number;
        var doNotCallReason;
        var primary;
        
        for(var i=0;i<document.forms[0].elements.length;i++)
        {	       
            var element = document.forms[0].elements[i];
            if(element.id.substring(element.id.length-19,element.id.length) == '_DescriptionTextBox')
	        {
	            description = document.getElementById(element.id);		   
	            if  (description     != null)   description.value = defaultValues[0][0];     
	        }
	        else if(element.id.substring(element.id.length-16,element.id.length) == '_CountryDropDown')
	        {
	            country = document.getElementById(element.id);	
	            if  (country         != null)   selectDropDownOption(country, defaultValues[0][1]); 
	        }
	        else if(element.id.substring(element.id.length-23,element.id.length) == '_TelephoneNumberTextBox')
	        {
	            number  = document.getElementById(element.id);
	            if  (number          != null)   number.value = defaultValues[0][2]; 
	        }
	        else if(element.id.substring(element.id.length-16,element.id.length) == '_PrimaryCheckBox')
	        {
	            primary = document.getElementById(element.id);
	            if  (primary         != null)   primary.checked = Boolean(defaultValues[0][3]);     
	        }
	   }
    }
    else
    {
        country = document.getElementById(countryDropDownId);	
        if  (country         != null)   selectDropDownOption(country, defaultCountryValue);     
    }
    for(var i=1;i<defaultValues.length;i++) {       
	    addPhoneNumber(descriptionTextBoxId,countryDropDownId, telephoneNumberTextBoxId, primaryCheckBox, defaultValues[i][1], removeButtonLabel, defaultValues[i][0],
			defaultValues[i][2], defaultValues[i][3]);
	}
    validateFirstItem();
    reInitializeRadio();
}
//Check fisrt radio if none is checked
function reInitializeRadio(){
	var removeRadioButton = document.getElementById("tablePhoneContainer").getElementsByTagName("input");	                  
            for(var i = 0; i < removeRadioButton.length; i++){
                var allCheck = false;            
                if(removeRadioButton[i].type == "radio") {
                     if(removeRadioButton[i].checked && !allCheck)
                        allCheck = removeRadioButton[i].checked;
					}                
                }            
    if(!allCheck)
    {
        var radioList = document.getElementById("tablePhoneContainer").getElementsByTagName("input");	                      
        if(radioList.length == 3)    
        radioList[2].checked = true;        
    }
}
