﻿// JavaScript methods for the Phone user control.
var selIdPhoneType = 0;
var txtIdPhone = 0;
var selIdPhone = 0;
var btnIdPhone = 0;
var chxIdPhone = 0;

var isPhoneTypeVisible = true;
var isPhoneNumberVisible = true;
var isCountryVisible = true;
var isTelephoneNumberVisible = true;
var isPrimaryVisible= true;

var phoneTypeRequired = true;
var isCountryRequired = true;
var isTelephoneNumberRequired = true;
var isPrimaryRequired = true;

var phoneTypeRequiredError = '';
var countryRequiredError = '';
var telephoneNumberRequiredError= '';
var primaryRequiredError = '';

var primaryRadioButton;
var controlsEditPhone;
var controlsAddPhone;
var phoneList = new phoneListContainer();

function phone(phoneNumber, phoneType, countryId, isPrimary) {
	this.Number = phoneNumber;
	this.PhoneType = phoneType;
	this.CountryId = countryId;
	this.IsPrimary = isPrimary;
}

function phoneControls(PhoneInformationJson) {
	this.PhoneInformationJson = PhoneInformationJson;
	
}

function phoneListContainer() {
	
	this.AddPhoneObject = function(phoneObject, phoneHiddenField) {
		var actualPhone;
		var temp = $("#" + phoneHiddenField).val();
		if (($("#" + phoneHiddenField).val() != "" || $("#" + phoneHiddenField).val() == "{}") &&
			$("#" + phoneHiddenField).val() != undefined)
			actualPhone = JSON.parse($("#" + phoneHiddenField).val());
		else
			actualPhone = new Array();

		actualPhone.push(phoneObject);

		$("#" + phoneHiddenField).val(JSON.stringify(actualPhone));

		return actualPhone;
	}
}


//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(phoneTypeDropDownId, countryDropDownId, telephoneNumberTextBoxId,
        primaryBoxId, selectedCountry,removePhoneButtonLabel,selectedPhoneType,
        selectedTelephoneNumber, selectedPrimary) {
    
	var container = document.getElementById("tablePhoneContainer");	
	var phoneRow = container.insertRow(-1);
	
	var dupePhoneTypeBox;
    var dupeCountryBox;
    var dupeDoNotCallResonBox;
    var dupeTelephoneNumberBox;
    var dupePrimaryBox;

    
    //Phone Type field
	dupePhoneTypeBox = document.getElementById(phoneTypeDropDownId).cloneNode(true);
	dupePhoneTypeBox.id = "_PhoneType" + eval(selIdPhoneType += 1);
	dupePhoneTypeBox.value = selectedPhoneType;
    container.rows[container.rows.length - 1].insertCell(0);
    container.rows[container.rows.length - 1].cells[0].appendChild(dupePhoneTypeBox);
    
    //Telephone field
	dupeTelephoneNumberBox = document.getElementById(telephoneNumberTextBoxId).cloneNode(true);
	dupeTelephoneNumberBox.id = "_TelephoneNumber" + eval(txtIdPhone += 1);
	dupeTelephoneNumberBox.value = selectedTelephoneNumber;
    container.rows[container.rows.length - 1].insertCell(1);
    container.rows[container.rows.length - 1].cells[1].appendChild(dupeTelephoneNumberBox);	
    
    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();
    reInitializeRadio();
    
   }

   function addSavedPhoneNumber(phoneTypeDropDownId, countryDropDownId, telephoneNumberTextBoxId,
        primaryBoxId, selectedCountry, removePhoneButtonLabel,selectedPhoneType,
        selectedTelephoneNumber, selectedPrimary) {

   		var container = document.getElementById("tablePhoneContainer");
   		var phoneRow = container.insertRow(-1);

        var dupePhoneTypeBox;
   		var dupeCountryBox;
   		var dupeDoNotCallResonBox;
   		var dupeTelephoneNumberBox;
   		var dupePrimaryBox;

        //Phone Type field
	    dupePhoneTypeBox = document.getElementById(phoneTypeDropDownId).cloneNode(true);
	    dupePhoneTypeBox.id = "_PhoneType" + eval(selIdPhoneType += 1);
	    dupePhoneTypeBox.value = selectedPhoneType;
        container.rows[container.rows.length - 1].insertCell(0);
        container.rows[container.rows.length - 1].cells[0].appendChild(dupePhoneTypeBox);
    
   		//Telephone field
   		dupeTelephoneNumberBox = document.getElementById(telephoneNumberTextBoxId).cloneNode(true);
   		dupeTelephoneNumberBox.id = "_TelephoneNumber" + eval(txtIdPhone += 1);
   		dupeTelephoneNumberBox.value = selectedTelephoneNumber;
   		container.rows[container.rows.length - 1].insertCell(1);
   		container.rows[container.rows.length - 1].cells[1].appendChild(dupeTelephoneNumberBox);

   		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");
	var radios = 0;
	for (var i = 0; i < radioList.length; i++) {
		if (radioList[i].type == "radio") {
			radios = radios + 1;
   		}
	}

	if (radios < 2) {
		radioList[1].style.display = "none";
	}

	if (radios >= 2) {
		radioList[1].style.display = "block";
	}
}

// Process the written phones with Json
function processPhoneNumber(phoneTypeID, countryID, telephoneID, primaryID) {
	var phone_str = "";
	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");
			var phoneNumber = (collectionText[0].value == "") ? "" : collectionText[0].value;
			var phoneType = (collectionList[0].value == "") ? "" : collectionList[0].value;
			var newPhone = new phone(phoneNumber, phoneType, parseInt(collectionList[1].value),
				collectionText[1].checked);
			phoneList.AddPhoneObject(newPhone, controlsAddPhone.PhoneInformationJson);			
		}
	}

	return true;
}

// Handle the initial state for phone container
function initializeGridPhone(phoneTypeDropDownId, countryDropDownId, telephoneNumberTextBoxId,
    primaryCheckBox, defaultValues, removeButtonLabelText,defaultCountryValue){ 
      
  	primaryRadioButton = primaryCheckBox;
  	if (removeButtonLabelText != null)
        removeButtonLabel = removeButtonLabelText;
    var i;
    //First row is always number 0
    if (defaultValues.length > 0)
    {
        var phType;

        var country;
        var number;
        var doNotCallReason;
        var primary;
        
        for(i=0;i<document.forms[0].elements.length;i++)
        {	       
            var element = document.forms[0].elements[i];

	        if(element.id.substring(element.id.length-18,element.id.length) == '_PhoneTypeDropDown')
	        {
	            phType = document.getElementById(element.id);
	            if (phType != null) selectDropDownOption(phType, 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 = defaultValues[0][3];    
	            }
	        }
	   }
    }
    else
    {
        country = document.getElementById(countryDropDownId);	
        if  (country         != null)   selectDropDownOption(country, defaultCountryValue);     
    }
    for(i=1;i<defaultValues.length;i++) {       
	    addSavedPhoneNumber(phoneTypeDropDownId, 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 radioList = document.getElementById("tablePhoneContainer").getElementsByTagName("input");
		var radios = 0;
		var allCheck = false;
		for (var i = 0; i < radioList.length; i++) {
			if (radioList[i].type == "radio") {
				radios = radios + 1;
				if (radioList[i].checked)
					allCheck = true;
			}
		}
	           
		if(!allCheck) {
    		radioList[1].checked = true;
		}
    }
