
var _NONE_BGCOLOR = '#FFFFFF';
var _TWICE_BGCOLOR = '#BCCF92';
var _ONCE_BGCOLOR = '#B5D0DC';

var cbos = document.getElementsByTagName('SELECT');
var divs = document.getElementsByTagName('DIV');
var inputs = document.getElementsByTagName('INPUT');

var radArr=Array();
var cboData=Array();

Array.prototype.indexOf = 
function(value)
{
	for(var i=0;i<this.length;i++)if(this[i]==value)return i;
	return -1;
}

function fillPriorityCbos(szClass,iNumber,objCbo)
{

	if(!szClass || szClass == '') return;
	
	cboData[szClass]=iNumber;
	
	var optList = Array();
	for(var i=1;i<=iNumber;i++){optList.push(i);} // fill array


	
	for(var i=0;i<cbos.length;i++)
	{
		
		if(cbos[i].className==szClass && (objCbo==null || objCbo.id!=cbos[i].id) )
		{
			var tmp = cbos[i].value;
			cbos[i].options.length=0;
			cbos[i].options[0] = new Option('','');
			for(var j=1;j<=iNumber;j++)
			{
				var add=true;
				var k=0;
				while(k<cbos.length&&add)
				{
					//
				//	alert(cbos[k].className);
					if(cbos[k].className==szClass && j==cbos[k].value && cbos[k].value!=tmp)
					{
						
						add=false;
					}
					k++;
				}

				if( add )
				{
				//alert(szClass+" "+ selectedvalue);
				//alert('add ' + j + " to " +cbos[i].name + " selected " + selectedvalue);
					cbos[i].options[cbos[i].options.length] = new Option(j,j);
				}
			}
			//cbos[i].selectedIndex = selectedvalue;
			//alert(szClass+" "+selectedvalue);
			cbos[i].value = tmp;
			
			if(iNumber==1)
			{
				//cbos[i].value=1;
			}
		}
	}
}

function resetPriorityCbos(obj)
{
	var szClass = obj.className;
	var iNumber = cboData[szClass];
	fillPriorityCbos(szClass,iNumber,obj);
}

function toggleDiv(theClass,theId)
{
	for(var i=0;i<divs.length;i++)
	{
		if(divs[i].className==theClass)
		{
			if(divs[i].id==theId)
			{
				divs[i].style.display='block';
			}
			else
			{
				divs[i].style.display='none';
			}
		}
	}
}

function initDivs()
{
	for(var i=0;i<cbos.length;i++)
	{
		if(cbos[i].name.substr(0,16)=='participant_item')
		{
			toggleDiv('items','item'+cbos[i].value);
		}
	}
}

function autoFillParticipant(chk,i,fromLoad)
{
	
	fromLoad = (typeof (fromLoad) != 'undefined');

	var cfn = document.getElementById('contact_firstname');
	var cln = document.getElementById('contact_lastname');
	var cea = document.getElementById('contact_email');
	var ct = document.getElementById('contact_phone');
	var ct1 = document.getElementById('contact_phone1');
	var ct2 = document.getElementById('contact_phone2');
	var ct3 = document.getElementById('contact_phone3');
	var ct4 = document.getElementById('contact_phone4');
	var pfn = document.getElementById('firstname'+i);
	var pln = document.getElementById('lastname'+i);
	var pea = document.getElementById('email'+i);
	var pt = document.getElementById('participant_phone'+i+'_');
	var pt1 = document.getElementById('participant_phone'+i+'_1');
	var pt2 = document.getElementById('participant_phone'+i+'_2');
	var pt3 = document.getElementById('participant_phone'+i+'_3');
	var pt4 = document.getElementById('participant_phone'+i+'_4');
	
	
	if(fromLoad)
	{
		if(chk.checked)cea.disabled=cfn.disabled=cln.disabled=ct1.disabled=ct2.disabled=ct3.disabled=ct4.disabled=true;
	}
	else
	{
		cea.disabled=cfn.disabled=cln.disabled=ct1.disabled=ct2.disabled=ct3.disabled=ct4.disabled=chk.checked;
		cfn.value=(chk.checked?pfn.value:'');
		cln.value=(chk.checked?pln.value:'');
		cea.value=(chk.checked?pea.value:'');
		ct1.value=(chk.checked?pt1.value:'');
		ct2.value=(chk.checked?pt2.value:'');
		ct3.value=(chk.checked?pt3.value:'');
		ct4.value=(chk.checked?pt4.value:'');
	ct.value=(chk.checked?pt.value:'');

		for(var i=0;i<inputs.length;i++)
		{
			if(inputs[i].type == chk.type && inputs[i].className == chk.className && inputs[i].name!=chk.name)
			{
				inputs[i].disabled = chk.checked;
			}
		}
	}
}

function countRegs()
{
	for(var i=0;i<radArr.length;i++)
	{
		var theClass = radArr[i];
		var total=0;
		var chosen=0;
		var theDiv = document.getElementById(theClass);
		for(var j=0;j<inputs.length;j++)
		{
			if(inputs[j].type=='radio' && inputs[j].className==theClass)
			{
				++total;
				if(inputs[j].checked)++chosen;
			}
		}
		theDiv.innerHTML = chosen + ' / ' + total;
	}
}

function countPartSubelems()
{
	var filter = /^sets([0-9]+)_([0-9]+)$/;
	var assigns = Array();
	for(var i=0;i<inputs.length;i++)
	{
		if(inputs[i].type=='radio' && inputs[i].className.match(filter))
		{
			var theId = inputs[i].id.substring(inputs[i].id.indexOf('[')+1,inputs[i].id.length-1);			
			var pid = theId.split('_')[0];
			var tsid = theId.split('_')[1];
			var seid = inputs[i].value;
			if(!assigns[pid])assigns[pid]=Array();
			if(inputs[i].checked)assigns[pid][seid] = (isNaN(assigns[pid][seid])?1:assigns[pid][seid]+1);
		}
	}

	var twiced = Array();

	for(i in assigns)
	{
		if(!isNaN(i))
		{
			var col = document.getElementById('partCol' + i);
			col.style.backgroundColor = _NONE_BGCOLOR;

			for(j in assigns[i])
			{
	
				var twice = (assigns[i][j]>1);
	
				if(twice)
				{
					col.style.backgroundColor = _TWICE_BGCOLOR;
					twiced.push(i);
				}
				else if(twiced.indexOf(i) == -1 && assigns[i][j] == 1)
				{
					col.style.backgroundColor = _ONCE_BGCOLOR;
				}
			}
		}
	}
}

function updateSchedule()
{
	countRegs();
	countPartSubelems();
}

function validatePriorities(msg)
{
	var ok = true;
	var i  = 0;
	while(ok==true&&i<cbos.length)
	{
		if(cbos[i].className.indexOf('cboPriority')==0)
		{
			if(cbos[i].style.display == 'none')
			{
				cbos[i].options.length=0;
				cbos[i].options[0] = new Option('','A');
				cbos[i].value = 'A';
			}
			else
			{
				if(cbos[i].value=='' && !cbos[i].disabled)
				{
					var optArr = Array();
					for(var j=0;j<cbos[i].options.length;j++){
					
					optArr.push(cbos[i].options[j].value);
					}
					if(optArr.indexOf(1)>-1||optArr.indexOf(2)>-1||optArr.indexOf(3)>-1)
					{
						ok=false;
					}
				}
			}
		}
		++i;
	}
	if(!ok)alert(msg);
	return ok;
}

function autoTab(fromObj,toId,maxlength)
{
	if(fromObj.value.length==maxlength)
	{
		var toObj = document.getElementById(toId);
		toObj.focus();
		toObj.select();
	}
}

function goodKey()
{
	var code;
	var e = window.event;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
//	alert(code);
	return (code>32 && code<127);
}

function updatePhone(hidId)
{
	document.getElementById(hidId).value =
	document.getElementById(hidId+'1'+'').value +
	document.getElementById(hidId+'2'+'').value +
	document.getElementById(hidId+'3'+'').value +
	(document.getElementById(hidId+'4'+'')?document.getElementById(hidId+'4'+'').value:'');
//	alert(hidId + ' : ' + document.getElementById(hidId).value);
}

function stripslashes(str) {
str=str.replace(/\\'/g,'\'');
str=str.replace(/\\"/g,'"');
str=str.replace(/\\\\/g,'\\');
str=str.replace(/\\0/g,'\0');
return str;
}
// OFFICIAL ADDRESS
function loadAddress()
{
	var index = document.getElementById('company').selectedIndex ;

	document.getElementById('companyOther').style.display = document.getElementById('companyOtherLabel').style.display = (document.getElementById('company').value=='-2'?'block':'block');
	document.getElementById('companyOther').value    = stripslashes(cieInfo[index][1]);document.getElementById('companyOther').readOnly = (document.getElementById('company').value!='-2');  
	document.getElementById('address').value    = stripslashes(cieInfo[index][2]);document.getElementById('address').readOnly = (document.getElementById('company').value!='-2');  
	document.getElementById('city').value       = stripslashes(cieInfo[index][3]);document.getElementById('city').readOnly = (document.getElementById('company').value!='-2');  
	document.getElementById('state').value      = stripslashes(cieInfo[index][4]);document.getElementById('state').disabled = (document.getElementById('company').value!='-2');  
	document.getElementById('postalcode').value = stripslashes(cieInfo[index][5]);document.getElementById('postalcode').readOnly = (document.getElementById('company').value!='-2');  
	document.getElementById('country').value    = stripslashes(cieInfo[index][6]);document.getElementById('country').disabled = (document.getElementById('company').value!='-2');  
	document.getElementById('web').value        = stripslashes(cieInfo[index][7]);document.getElementById('web').readOnly = (document.getElementById('company').value!='-2');  
	var phone = cieInfo[index][8];
	document.getElementById('phone').value      = phone;
	document.getElementById('phone1').value      = phone.substr(0,3);document.getElementById('phone1').readOnly = (document.getElementById('company').value!='-2');
	document.getElementById('phone2').value      = phone.substr(3,3);document.getElementById('phone2').readOnly = (document.getElementById('company').value!='-2');
	document.getElementById('phone3').value      = phone.substr(6,4);document.getElementById('phone3').readOnly = (document.getElementById('company').value!='-2');
	document.getElementById('phone4').value      = phone.substr(10);document.getElementById('phone4').readOnly = (document.getElementById('company').value!='-2');
	var fax = cieInfo[index][9];
	document.getElementById('fax').value        = fax;
	document.getElementById('fax1').value        = fax.substr(0,3);document.getElementById('fax1').readOnly = (document.getElementById('company').value!='-2');
	document.getElementById('fax2').value        = fax.substr(3,3);document.getElementById('fax2').readOnly = (document.getElementById('company').value!='-2');
	document.getElementById('fax3').value        = fax.substr(6,4);document.getElementById('fax3').readOnly = (document.getElementById('company').value!='-2');
	
	
	document.getElementById('cie_accepted').value = Math.min(index,1);
}

// BILLING ADDRESS
function loadAddress2()
{
	var index = document.getElementById('billing_company').selectedIndex;
	document.getElementById('billing_companyOther').style.display = document.getElementById('billing_companyOtherLabel').style.display = ( document.getElementById('billing_company').value=='-2'?'block':'block');
	document.getElementById('billing_companyOther').value    = stripslashes(cieInfo[index][1]);document.getElementById('billing_companyOther').readOnly = (document.getElementById('billing_company').value!='-2');  
	document.getElementById('billing_address').value    = cieInfo[index][2];document.getElementById('billing_address').readOnly = (document.getElementById('billing_company').value!='-2');
	document.getElementById('billing_city').value       = cieInfo[index][3];document.getElementById('billing_city').readOnly = (document.getElementById('billing_company').value!='-2');
	document.getElementById('billing_state').value      = cieInfo[index][4];document.getElementById('billing_state').disabled = (document.getElementById('billing_company').value!='-2');
	document.getElementById('billing_postalcode').value = cieInfo[index][5];document.getElementById('billing_postalcode').readOnly = (document.getElementById('billing_company').value!='-2');
	document.getElementById('billing_country').value    = cieInfo[index][6];document.getElementById('billing_country').disabled = (document.getElementById('billing_company').value!='-2');
	document.getElementById('billing_web').value        = cieInfo[index][7];document.getElementById('billing_web').readOnly = (document.getElementById('billing_company').value!='-2');

	var billing_phone = cieInfo[index][8];
	document.getElementById('billing_phone').value       = billing_phone;
	document.getElementById('billing_phone1').value      = billing_phone.substr(0,3);document.getElementById('billing_phone1').readOnly = (document.getElementById('billing_company').value!='-2');
	document.getElementById('billing_phone2').value      = billing_phone.substr(3,3);document.getElementById('billing_phone2').readOnly = (document.getElementById('billing_company').value!='-2');
	document.getElementById('billing_phone3').value      = billing_phone.substr(6,4);document.getElementById('billing_phone3').readOnly = (document.getElementById('billing_company').value!='-2');
	document.getElementById('billing_phone4').value      = billing_phone.substr(10);document.getElementById('billing_phone4').readOnly = (document.getElementById('billing_company').value!='-2');

	var billing_fax = cieInfo[index][9];
	document.getElementById('billing_fax').value         = billing_fax;
	document.getElementById('billing_fax1').value        = billing_fax.substr(0,3);document.getElementById('billing_fax1').readOnly = (document.getElementById('billing_company').value!='-2');
	document.getElementById('billing_fax2').value        = billing_fax.substr(3,3);document.getElementById('billing_fax2').readOnly = (document.getElementById('billing_company').value!='-2');
	document.getElementById('billing_fax3').value        = billing_fax.substr(6,4);document.getElementById('billing_fax3').readOnly = (document.getElementById('billing_company').value!='-2');
}


function _ById(theId){return document.getElementById(theId);}
function same_address(checked)
{
	try
	{
		var index = _ById('billing_company').selectedIndex = (checked?_ById('company').selectedIndex:0);
		_ById('billing_companyOther').style.display = _ById('billing_companyOtherLabel').style.display = (checked?(_ById('billing_company').value=='-2' ?'block':'block'):'block');
		_ById('billing_companyOther').value = (checked?_ById('companyOther').value:'');
		_ById('billing_address').value    = (checked?_ById('address').value:'');
		_ById('billing_city').value       = (checked?_ById('city').value:'');
		_ById('billing_state').value      = (checked?_ById('state').value:'QC');
		_ById('billing_postalcode').value = (checked?_ById('postalcode').value:'');
		_ById('billing_country').value    = (checked?_ById('country').value:'CA');
		_ById('billing_web').value        = (checked?_ById('web').value:'');
		_ById('billing_phone').value      = (checked?_ById('phone').value:'');
		_ById('billing_phone1').value     = (checked?_ById('phone1').value:'');
		_ById('billing_phone2').value     = (checked?_ById('phone2').value:'');
		_ById('billing_phone3').value     = (checked?_ById('phone3').value:'');
		_ById('billing_phone4').value     = (checked?_ById('phone4').value:'');
		_ById('billing_fax').value        = (checked?_ById('fax').value:'');
		_ById('billing_fax1').value       = (checked?_ById('fax1').value:'');
		_ById('billing_fax2').value       = (checked?_ById('fax2').value:'');
		_ById('billing_fax3').value       = (checked?_ById('fax3').value:'');
		
		_ById('billing_company').readOnly      = // _ById('billing_company').disabled =
		_ById('billing_companyOther').readOnly = // _ById('billing_companyOther').disabled =
		_ById('billing_address').readOnly      = // _ById('billing_address').disabled =
		_ById('billing_city').readOnly         = // _ById('billing_city').disabled =
		_ById('billing_postalcode').readOnly   = // _ById('billing_postalcode').disabled =
		_ById('billing_web').readOnly          = // _ById('billing_web').disabled =
		_ById('billing_phone').readOnly        = // _ById('billing_phone').disabled =
		_ById('billing_phone1').readOnly       = // _ById('billing_phone1').disabled =
		_ById('billing_phone2').readOnly       = // _ById('billing_phone2').disabled =
		_ById('billing_phone3').readOnly       = // _ById('billing_phone3').disabled =
		_ById('billing_phone4').readOnly       = // _ById('billing_phone4').disabled =
		_ById('billing_fax').readOnly          = // _ById('billing_fax').disabled =
		_ById('billing_fax1').readOnly         = // _ById('billing_fax1').disabled =
		_ById('billing_fax2').readOnly         = // _ById('billing_fax2').disabled =
		_ById('billing_fax3').readOnly         = // _ById('billing_fax3').disabled =

		_ById('billing_state').disabled =
		_ById('billing_country').disabled = checked;

		return true;
	}
	catch(e)
	{
//		alert(e.message);
		return false;
	}
}

function follow_address(obj)
{
	try
	{
		if(_ById('copy_address').checked == true)
		{
			if(obj.id=='company')
			{
				_ById('billing_company').selectedIndex = obj.selectedIndex;
			}
			else
			{
				_ById('billing_'+obj.id).value=obj.value;
			}
			updatePhone('billing_phone');
			updatePhone('billing_fax');
		}
		return true;
	}
	catch(e)
	{
		alert(e.message);
		return false;
	}
}

function updateParticipantsBlock(active,maximum,fromLoad)
{
	fromLoad = (typeof (fromLoad) != 'undefined');
	var contactDisplay = (active>1?'block':'none');
//	document.getElementById('contactBlock').style.display=contactDisplay;
	document.getElementById('contactBlock').style.display='block';
	for(var i=0;i<maximum;i++)
	{
		var theBlock = document.getElementById('participantBlock'+i);
		document.getElementById('isContactRow'+i).style.display='block';
		theBlock.style.display = (i<active?'block':'none');
		if(i>=active)
		{
			document.getElementById('prename'+i).value = '';
			document.getElementById('firstname'+i).value = '';
			document.getElementById('lastname'+i).value = '';
			document.getElementById('title'+i).value = '';
			document.getElementById('email'+i).value = '';
//			document.getElementById('conference_title'+i).value = '';
			document.getElementById('is_contact'+i).checked = false;
			document.getElementById('subscribe'+i).checked = false;
			autoFillParticipant(document.getElementById('is_contact'+i),i,fromLoad);
		}
		else
		{
			if(document.getElementById('is_contact'+i).checked && fromLoad)
			{
				autoFillParticipant(document.getElementById('is_contact'+i),i,fromLoad);
			}
			else
			{
				if(fromLoad)
				{
					var oneIsChecked = false;
					var j = 0;
					while(j<active && !oneIsChecked)
					{
						if(j!=i&&document.getElementById('is_contact'+j).checked)
						{
							oneIsChecked = true;
						}
						j++;
					}
					document.getElementById('is_contact'+i).disabled = oneIsChecked;
				}
			}
		}
	}
	document.form1.participants_number.value = active;
}

function setAbsence(obj)
{

	var nums = obj.id.substr(11).split('_');
	var part = nums[0];
	var slot = nums[1];
//	alert(part+' '+slot);
	for(var i=0;i<cbos.length;i++)
	{
		
		if(cbos[i].className.substr(12).indexOf('pt'+part+'_'+slot)==0)
		{
			if(obj.checked)
			{
			
//				cbos[i].options.length=0;
//				cbos[i].options[0] = new Option('---','0');
				cbos[i].style.display = 'none';
			}
			else
			{
				cbos[i].style.display = 'block';
			}
//			if(cbos[i].value=='' && !cbos[i].disabled)ok=false;
		}
	}
}

function replaceAction(theForm,replaceThat,replaceWith,fullNew)
{
	var act = theForm.action;
	var qss = act.indexOf('?');
	act = (qss>-1?act.replace(replaceThat,replaceWith):fullNew);
	theForm.action = act;
}
var MatrixPopup;
function popupMatrix(event_id)
{
	if(MatrixPopup && !MatrixPopup.closed)
	{
		MatrixPopup.focus();
	}
	else
	{
		MatrixPopup = window.open('index.php?module=Events&type=user&func=view_popupMatrix&event_id='+event_id,'popupMatrix','width=680,height=450');
	}
}

function sendAjaxMsg(str)
{
	var xmlhttp = false;
	if(typeof XMLHttpRequest != 'undefined'){try{xmlhttp = new XMLHttpRequest();}catch(e){/**/}}
	else if(ActiveXObject){xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
	
	if(xmlhttp)
	{
		xmlhttp.open('GET', str, true);
		xmlhttp.onreadystatechange=
		function()
		{
			if(xmlhttp.readyState==4)
			{
				--pCount;
				if(pCount==0){redirect(szRedir);}
			}
		}
		xmlhttp.send(null);
	}
}

function redirect(str)
{
	if(str.substr(0,4)=='str:')
	{
		document.write(str.substr(4));
	}
	else
	{
		top.location.href = str;
	}
}

function changeDiv(the_div,the_change)
{
	ids = document.getElementById('organizations').value.split(',');
	for (var x = 0; x < ids.length; x++){
		if('div'+ids[x] == the_div){
			document.getElementById('div'+ids[x]).style.backgroundColor = '#FF0000';
			document.getElementById('save'+ids[x]).style.display = 'block';
			
		}else{
			document.getElementById('div'+ids[x]).style.backgroundColor = '#FFF';
			document.getElementById('save'+ids[x]).style.display = 'none';
		}
   	}
}



function debug_mode(){ return (location.search.toLowerCase().indexOf('debug')>-1); }



