function mSelectTab(oSender){
	var oTbs = document.getElementById('uptbs'), aTbs = oIThelp.getAllElementsByClassName(oTbs, 'userprofile_tab'), oActBody = document.getElementById(oIThelp.mGetAttribute(oSender, 'tabbody'));
	for(var o = 0; o < aTbs.length; o++){
		if(aTbs[o].className.contains('active')){
			oIThelp.mRemoveClass(aTbs[o], 'active');
			oIThelp.mRemoveClass(document.getElementById(oIThelp.mGetAttribute(aTbs[o], 'tabbody')), 'ith_visible');
			oIThelp.mAddClass(document.getElementById(oIThelp.mGetAttribute(aTbs[o], 'tabbody')), 'ith_not_visible');
		}
	}
	oIThelp.mAddClass(oSender, 'active');
	oIThelp.mRemoveClass(oActBody, 'ith_not_visible');
	oIThelp.mAddClass(oActBody, 'ith_visible');
}

function mSetVisibility(oSender){
	var sFieldId = oIThelp.mGetAttribute(oSender, 'visiblefield'), sClassName = oSender.className == 'lock_on' ? 'lock_off' : 'lock_on', sValue = sClassName == 'lock_on' ? 'no' : 'yes';
	oSender.className = sClassName;
	document.getElementById(sFieldId).value = sValue;
}

function mUserProfileValidate(){
	var aFields = oIThelp.getAllElementsByClassName(document.getElementById('up_basic'), 'field'), sMessage='';
	for(var f = 0; f < aFields.length; f++){
		var oField = aFields[f], validateStr = oIThelp.mGetAttribute(oField, 'validate');
		if(validateStr){
			var oInput = oField.getElementsByTagName('INPUT')[0], oTitle = oIThelp.getAllElementsByClassName(oField, 'fieldtitle')[0];
			if(oTitle.className.contains('invalid')) oIThelp.mRemoveClass(oTitle, 'invalid');
			var aValidation = validateStr.split(' ');
			for(var v = 0; v < aValidation.length; v++){
				var retVal = '';
				switch ( aValidation[v].trim() ) {
					case "valid_variable"	: if (oInput.value!='' && !/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(oInput.value)) retVal=oIThelp.mGetLocalName('VALIDATION_VARIABLE'); break;
					case "email"			: if (oInput.value!='' && !/^([a-zA-Z0-9_\-])+(\.([a-zA-Z0-9_\-])+)*@((\[(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5]))\]))|((([a-zA-Z0-9])+(([\-])+([a-zA-Z0-9])+)*\.)+([a-zA-Z])+(([\-])+([a-zA-Z0-9])+)*))$/.test(oInput.value)) retVal=oIThelp.mGetLocalName('VALIDATION_EMAIL'); break;
					case "int"			: if (oInput.value!='' && !/^[0-9]+$/.test( oInput.value )) retVal=oIThelp.mGetLocalName('VALIDATION_INT'); break;
					case "required"		: if (oInput.value=='') retVal=oIThelp.mGetLocalName('VALIDATION_REQUIRED'); break;
				}
				if(retVal != ''){
					oIThelp.mAddClass(oTitle, 'invalid');
					sMessage += retVal + ': ' + (oClient.bIE ? oTitle.innerText : oTitle.textContent) + '\n';
				}
			}
		}
	}
	return sMessage;
}

function mChangePassword(sMessage){
	var oPw = document.getElementById('basic_password'), oOpw = document.getElementById('previous_password'), oNpw = document.getElementById('new_password'), oNcpw = document.getElementById('new_password_confirm');
	if( oPw.value != oOpw.value ){
		alert(sMessage);
		return false;
	}
	else if( oNpw.value != oNcpw.value ){
		alert(oIThelp.mGetLocalName('PW_DOES_NOT_MATCH'));
		return false;
	}
	var sPassword = oNpw.value, retVal = '';
	if( ! /^\w*(?=\w*\d)\w*$/.test(sPassword)) {retVal = oIThelp.mGetLocalName('VALIDATION_PASSWORD'); };
	if( ! /^\w*(?=\w*[a-z])\w*$/.test(sPassword)) {retVal = oIThelp.mGetLocalName('VALIDATION_PASSWORD'); };
	if( ! /^\w*(?=\w*[A-Z])\w*$/.test(sPassword)) {retVal = oIThelp.mGetLocalName('VALIDATION_PASSWORD'); };
	if( ! /^.{6,50}$/.test(sPassword)) {retVal = oIThelp.mGetLocalName('VALIDATION_PASSWORD'); };
	if(retVal != ''){alert(retVal); return false;}
	document.getElementById('user_action').value = 'changepassword';
	oIThelp.mPlainLoading();
	var oF = theForm ? theForm : document.forms[0];
	oF.target = 'communicateframe';
	oF.submit();
}

function changePasswordCallBack(sMessage){
	oIThelp.mStopPlainLoading();
	var oPw = document.getElementById('basic_password');
	if(sMessage.contains('CHANGED_')) oPw.value = sMessage.substring(8);
	else alert(sMessage);
	
}

function mNewsLetterClick(oSender){
	document.getElementById('portal_newsletter').value = oSender.checked ? 'on' : 'off';
}

function mUserSave(){
	var sMessage = mUserProfileValidate();
	if(sMessage != ''){
		alert(sMessage);
		return false;
	}
	document.getElementById('user_action').value = 'save';
	var oF = theForm ? theForm : document.forms[0];
	oF.target = '';
	oF.submit();
}

function mUserImageSave(sMessage){
	var oFileInput = document.getElementById('user_image');
	if(oFileInput.value == '') return false;
	var ext = oFileInput.value.substring(oFileInput.value.lastIndexOf('.') + 1).toLowerCase();
    if(ext != "bmp" && ext != "gif" && ext != "tiff" && ext != "png" && ext != "jpeg" && ext != "jpg"){
		alert(sMessage);
		return false;
	}
	document.getElementById('user_action').value = 'uploaduserimage';
	var oF = theForm ? theForm : document.forms[0];
	oF.target = '';
	oF.submit();
}

function mUserImageDelete(oSender){
	document.getElementById('userimage_meta').value = '';
	var oImg = document.getElementById('user_image_frame');
	oImg.src = oIThelp.mGetAttribute(oSender, 'blankimage');
	oImg.style.top = '0px';
	oImg.style.left = '0px';
	oSender.parentNode.removeChild(oSender);
}

function mShowCalendar(oSender) {
	//oInput, oImg, format, showsTime, showsOtherMonths, oAttachElement
  var oInput = document.getElementById(oIThelp.mGetAttribute(oSender, 'input'));
  var oImg = oSender;
  var oAttachElement = document.getElementById(oIThelp.mGetAttribute(oSender, 'attachinput'));
  oAttachElement.uniqueFormat = oIThelp.mGetAttribute(oAttachElement, 'format');
  var format = oIThelp.mGetAttribute(oSender, 'format');
  var showsTime = null;
  var showsOtherMonths = true;
  
  if (_dynarch_popupCalendar != null) {
	// we already have some calendar created
	_dynarch_popupCalendar.hide();                 // so we hide it first.
  } else {
	// first-time call, create the calendar.
	var cal = new ith_Calendar(1, null, 
		function(cal, date) {
			cal.sel.value=date; 
			if (cal.selAE!=null) cal.selAE.value=cal.date.print(cal.selAE.uniqueFormat); 
			if (cal.dateClicked) cal.callCloseHandler();
		},
		function(cal) {cal.hide(); _dynarch_popupCalendar = null;} 
	);
	// uncomment the following line to hide the week numbers
	// cal.weekNumbers = false;
	if (typeof showsTime == "string") {
	  cal.showsTime = true;
	  cal.time24 = (showsTime == "24");
	}
	if (showsOtherMonths) {
	  cal.showsOtherMonths = true;
	}
	_dynarch_popupCalendar = cal;                  // remember it in the global var
	cal.setRange(1900, 2070);        // min/max year allowed.
	cal.create();
  }
  _dynarch_popupCalendar.setDateFormat(format);    // set the specified date format
  if(oInput.value.trim() != '')
	_dynarch_popupCalendar.parseDate(oInput.value);      // try to parse the text in field
  _dynarch_popupCalendar.sel = oInput;
  _dynarch_popupCalendar.selAE = oAttachElement;
  
  // the reference element that we pass to showAtElement is the button that
  // triggers the calendar.  In this example we align the calendar bottom-right
  // to the button.
  _dynarch_popupCalendar.showAtElement(oImg, 'br');        // show the calendar

  return false;
}


