// ************************************************************************************
// Disable select controls
// ************************************************************************************

//Attach the Change eventhandler to the OnChange event of each select control.
//When a select control's selecteditem is changed, all other select controls are
//disabled, until the page is returned. The eventhandler is only attached when
//the select control already contains an eventhandler for the onChange event, which
//is in most cases the postback client-side script.
function DisableSelectsUponSelection()
{
    selects = document.getElementsByTagName( 'select' ); 
    for(i=0; i < selects.length; i++)
    {
	    if(selects[i] != null && selects[i].onchange != null)
	    {
	  	    selects[i].attachEvent( 'onchange', Change );
	  	    
	  	   /* if(window.addEventListener) 
	  	    {
				selects[i].onchange = Change; 		
			}
			else if(window.attachEvent) 
			{
				selects[i].attachEvent('onchange',Change);			
			}*/
	    }
    }
}

//Eventhandler for the OnChange event of select controls 
function Change()
{
	selects = document.getElementsByTagName( 'select' ); 
	for(i=0; i < selects.length; i++)
	{
		if ( selects[i] != null )
		{
			selects[i].disabled = "disabled";
		}
	}
}


// ************************************************************************************
// Show/Hide buttons when printing page	
// ************************************************************************************

//Attach the BeforePrint & AfterPrint eventhandlers to the OnBeforePrint and OnAfterPrint
//events of the window. Before the page is printed, hide all buttons. After the page has been
//printed, show them all again. 
//window.attachEvent( 'onbeforeprint', BeforePrint );
//window.attachEvent( 'onafterprint', AfterPrint );


if (window.addEventListener)
 {
    //alert('Before, After print!');
  	//window.onbeforeprint = BeforePrint; 
	//window.onafterprint = AfterPrint; 
 }
 else
 {
	window.attachEvent('onbeforeprint',BeforePrint);
	window.attachEvent('onafterprint',AfterPrint); 	 
 }	

//Eventhandler for the OnBeforePrint event of the window 
function BeforePrint()
{
	inputs = document.getElementsByTagName( 'input' ); 
	for(i=0; i < inputs.length; i++)
	{
		if ( inputs[i] != null )
		{
			inputs[i].style.display = 'none';
		}
	}
}

//Eventhandler for the OnAfterPrint event of the window 
function AfterPrint()
{
	inputs = document.getElementsByTagName( 'input' ); 
	for(i=0; i < inputs.length; i++)
	{
		if ( inputs[i] != null )
		{
			inputs[i].style.display = "";
		}
	}
}


// ************************************************************************************
// Show the div element containing the information for the passed rmaNumber, and hide
// all other div elements containing status information. Used in the Track & Trace form. 
// ************************************************************************************

function ShowStatus( rmaNumber )
{
	divs = document.getElementsByTagName( 'div' );
	firstRmaDiv = null;
	selectedRmaDiv = null;
	countRmaDiv = 0
	for(i=0; i < divs.length; i++)
	{
		rma = new String(divs[i].id);
		if ( rma.indexOf( 'iRMA' ) > -1 )
		{
			countRmaDiv++;
			if ( firstRmaDiv == null ) firstRmaDiv = divs[i];
			if ( rma == rmaNumber )
			{
				divs[i].style.display = "";
				selectedRmaDiv = divs[i];
			}
			else
			{
			    divs[i].style.display = "none";
			}
		}
	}

	if ( countRmaDiv == 1 && firstRmaDiv != null )
	{
		firstRmaDiv.style.display = "";
		firstRmaDiv.scrollIntoView(false);
	}
	
	if ( selectedRmaDiv != null )
	{
		selectedRmaDiv.scrollIntoView(false);
	}
}


// ************************************************************************************
// Add the passed newOption (string) to the passed select element.
// ************************************************************************************

function AddOtherAccessory( availableList, includedList, newOption )
{
	if ( newOption != null && newOption.length > 0 && includedList != null && includedList.options != null && availableList != null && availableList.options != null )
	{
		for(i=0; i < includedList.length; i++)
		{
			if ( includedList.options[i].text.toLowerCase() == newOption.toLowerCase() ) return;
		}
		for(i=0; i < availableList.length; i++)
		{
			if ( availableList.options[i].text.toLowerCase() == newOption.toLowerCase() ) 
			{	
				availableList.options[i].selected = "true";
				LT_Transfer( availableList.id, includedList.id, true, true, true, false);
				LT_StoreListState( includedList );
				return;
			}
		}
		includedList.options[includedList.length] = new Option( newOption, newOption, false, false );
		LT_StoreListState( includedList );
	}
}


// ************************************************************************************
// Show/Hide (or Enable/Disable when showHide is false) the controls passed in the
// parameter controls, based on the value of the parameter of checked.
// ************************************************************************************

function UpdateControls( checked, controls, showHide )
{
  value = "";
  if ( checked != null && checked == false )
  { 
    if ( showHide == null || showHide == false ) { value = "disabled"; } else { value = "none"; } 
  } 
  
  controlString = new String( controls );
  controlArray = controlString.split(";");
  for( i=0; i < controlArray.length; i++ )
  {
	control = document.getElementById( controlArray[i] );
	if ( controlArray[i].length > 0 && control != null )
	{  
	  if ( showHide == null || showHide == false ) { control.disabled = value; } else { control.style.display = value; }	
	}
  }
}


// ************************************************************************************
// Show the div element containing the information for the passed claimid, and hide
// all other div elements containing claim information. Used in the Find Marsh claim form. 
// ************************************************************************************

function ShowMarshClaim( claimID )
{
	divs = document.getElementsByTagName( 'div' );
	firstClaimDiv = null;
	countClaimDiv = 0
	for(i=0; i < divs.length; i++)
	{
		claim = new String(divs[i].id);
		if ( claim.indexOf( 'CLAIM_' ) > -1 )
		{
			countClaimDiv++;
			if ( firstClaimDiv == null ) firstClaimDiv = divs[i];
			if ( claim == claimID )
			{
				divs[i].style.display = "";
			}
			else
			{
			    divs[i].style.display = "none";
			}
		}
	}
	if ( countClaimDiv == 1 && firstClaimDiv != null )
		firstClaimDiv.style.display = "";
}


// ************************************************************************************
// Show the date selector form. The initial values of the controls are passed to the
// dialogs and when the dialog is closed again the new values are stored back into the
// controls. 
// ************************************************************************************

function ShowDateSelector( dateFromControlID, dateUntilControlID, dateFromStorageID, dateUntilStorageID, rangeTypeStorageID )
{
	var dateFromControl = document.getElementById( dateFromControlID );
	var dateUntilControl = document.getElementById( dateUntilControlID );
	var dateFromStorage = document.getElementById( dateFromStorageID );
	var dateUntilStorage = document.getElementById( dateUntilStorageID );	
	var rangeTypeStorage = document.getElementById( rangeTypeStorageID );
	var dateObject = new Object();
	if ( dateFromControl.value == "" )
	{
		dateObject = null;
	}
	else
	{
		if ( dateFromStorage.value == "" )
		{
			dateFromArr = dateFromControl.value.split("-");
			dateObject.dateFrom = new Date( dateFromArr[2], dateFromArr[1]-1, dateFromArr[0]);
			dateUntilArr = dateUntilControl.value.split("-");
			dateObject.dateUntil = new Date( dateUntilArr[2], dateUntilArr[1]-1, dateUntilArr[0] );
			dateObject.rangeType = "4";
		}
		else
		{
			dateObject.dateFrom = dateFromStorage.value;
			dateObject.dateUntil = dateUntilStorage.value;
			dateObject.rangeType = rangeTypeStorage.value;
		}
	}
	dateObject = window.showModalDialog( "frm_dateselector.aspx", dateObject, "dialogWidth:550px;dialogHeight:375px;status:no;help:no" );			
	if ( dateObject != null )
	{
		if ( dateObject.rangeType != "0" )
		{
			dateFromControl.value = dateObject.dateFrom.getDate() + "-" + ( dateObject.dateFrom.getMonth() + 1 ) + "-" + dateObject.dateFrom.getFullYear();
			dateUntilControl.value = dateObject.dateUntil.getDate() + "-" + ( dateObject.dateUntil.getMonth() + 1 ) + "-" + dateObject.dateUntil.getFullYear();
			dateFromStorage.value = dateObject.dateFrom;
			dateUntilStorage.value = dateObject.dateUntil;
			rangeTypeStorage.value = dateObject.rangeType;
		}
		else
		{
			dateFromControl.value = "";
			dateUntilControl.value = "";
			dateFromStorage.value = "";
			dateUntilStorage.value = "";
			rangeTypeStorage.value = "0";
		}
	}
}

// ************************************************************************************
// Show a popup dialog containing a message. The DialogMode is automatically set
// to pdmAuto, meaning the dialog is automatically constructed using the properties of
// the form.
// ************************************************************************************

function ShowPopupMessage( dialogID, dialogMessageTranslationLanguageID, dialogMessageTranslationTable, dialogMessageTranslationKey, dialogType, dialogButtons, dialogWidth, dialogHeight, showModal, dialogResultStorageID, dontShowDialogAgainSessionSuffix, dialogResultSessionSuffix )
{
	if ( showModal == true )
	{
		var dialogResultObject = window.showModalDialog( "frm_popupdialog.aspx?dialogid=" + dialogID + "&dialogmessagetranslationlanguageid=" + dialogMessageTranslationLanguageID + "&dialogmessagetranslationtable=" + dialogMessageTranslationTable + "&dialogmessagetranslationkey=" + dialogMessageTranslationKey + "&dialogtype=" + dialogType + "&dialogbuttons=" + dialogButtons, null, "dialogWidth:" + dialogWidth + ";dialogHeight:" + dialogHeight + ";status:no;help:no;scroll:no;" );			
	}
	else
	{
		var dialogResultObject = window.showModelessDialog( "frm_popupdialog.aspx?dialogid=" + dialogID + "&dialogmessagetranslationlanguageid=" + dialogMessageTranslationLanguageID + "&dialogmessagetranslationtable=" + dialogMessageTranslationTable + "&dialogmessagetranslationkey=" + dialogMessageTranslationKey + "&dialogtype=" + dialogType + "&dialogbuttons=" + dialogButtons, null, "dialogWidth:" + dialogWidth + ";dialogHeight:" + dialogHeight + ";status:no;help:no;scroll:no;" );			
	}
	
	var dialogResultStorageControl = document.getElementById( dialogResultStorageID );
	if ( dialogResultStorageControl != null && dialogResultObject != null )
	{
		dialogResultStorageValue = new String( dialogResultStorageControl.value );
		dialogResultStorageControl.value = dialogResultStorageValue + dialogID + dontShowDialogAgainSessionSuffix + "=" + dialogResultObject.dontShowDialogAgain + ";" + dialogID + dialogResultSessionSuffix + "=" + dialogResultObject.dialogResult + ";";
	}
}

// ************************************************************************************
// Client-side functionality to check whether the dialog should be shown again. This is
// necessary when - after the user viewed the popup message the first time - the popup
// message has to be shown again and no postback has been performed by any control on the
// form yet. Therefor, the client-side selected value of "DontShowPopupMessageAgain" was
// not saved to the session yet, so the value should be retrieved from the hidden control
// on the form directly, client-side. See frm_gsmrepairorder_mutate.aspx, used in the
// accessories dual-list box.
// ************************************************************************************

function DontShowPopupMessageAgain( dialogID, dialogResultStorageID )
{
	var dialogResultStorageControl = document.getElementById( dialogResultStorageID );
	if ( dialogResultStorageControl != null )
	{
		dialogResultStorageValue = new String( dialogResultStorageControl.value );
		dialogResultStorageArr = dialogResultStorageValue.split(";");
		if ( dialogResultStorageArr.length > 0 )
		{
			for(i=0; i < dialogResultStorageArr.length; i++)
			{
				if ( dialogResultStorageArr[i].indexOf( dialogID ) > -1 )
				{
					dialogResults = dialogResultStorageArr[i].split("=");
					if ( dialogResults.length == 2 )
						if ( dialogResults[1] == "true" || dialogResults[1] == "1" )
							return true;
				}
			}
		}
	}
	return false;
}

// ************************************************************************************
// Trim leading and trailing spaces from the passed textbox's value.
// ************************************************************************************

function TrimTextBoxValue()
{
	String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, '').replace(/^\s+|\s+$/, ''); };
	enteredValue = new String( event.srcElement.value );
	event.srcElement.value = enteredValue.trim();
}

function TrimTextBoxValues()
{
	inputs = document.getElementsByTagName('input'); 
	for(i=0; i < inputs.length; i++)
	{
		if(inputs[i] != null && inputs[i].value != null)
			if(window.attachEvent)
				inputs[i].attachEvent("onblur",TrimTextBoxValue);
			else if(window.addEventListener)
				inputs[i].addEventListener('onblur',TrimTextBoxValue,false);
			else
				inputs[i]['onblur'] = TrimTextBoxValue;
					 	
	}
}

// ************************************************************************************
// Set the valid input chars.
// ************************************************************************************

function CSValidation_OnlyAllow(iType)
{
	// iType holds the dataType to allow
	// - 0 means only digits
	// - 1 means only characters + dash and single-quote and dot
	// - any other value means digits, characters, underscore, dash and single-quote

	var sKey=String.fromCharCode(event.keyCode);

 	switch (iType)
 	{
		case 0:
			var REValidChars=/[\d\r]/gi;
			break;
		case 1:
			var REValidChars=/[a-z\-\'\s\r\.]/gi;
			break;
		default:
			var REValidChars=/\w/gi;
			break;
	}

	event.returnValue=REValidChars.test(sKey);
}

function CSValidation_RemoveProhibitedChars(iType, oCaller)
{
	// iType holds the dataType to allow
	// - 0 means only digits
	// - 1 means only characters + dash and single-quote
	// - any other value means digits, characters, underscore, dash and single-quote

	switch (iType) {
		case 0:
			var REInvalidChars=/[^\d\r]/gi;
			break;
		case 1:
			var REInvalidChars=/[^a-z\-\'\s\r\.]/gi;
			break;
		default:
			var REInvalidChars=/\W/gi;
			break;
	}

	if (oCaller.value!="undefined" && oCaller.value!=null && REInvalidChars.test(oCaller.value))
	{
		oCaller.value=oCaller.value.replace(REInvalidChars, "");
	}
}

// ************************************************************************************
// Update the zipcode validation control, based on the selected country value.
// ************************************************************************************

function UpdateZipcodeValidator( countryValue, validatorControl )
{
	if ( countryValue == null || validatorControl == null )
		return false;
		
	var errormessage = new String( validatorControl.errormessage );
	if ( errormessage.indexOf( ' (' ) > 0 )
		errormessage = errormessage.substr( 0, errormessage.indexOf( ' (' ) ) ;
		
	switch( countryValue )
	{
		case "1": validatorControl.validationexpression = '[0-9]{4}( )?[a-zA-Z]{2}'; errormessage += ' (0000 XX)'; break;
		case "2": validatorControl.validationexpression = '([bB]-)?[0-9]{4}'; errormessage += ' (B-0000)'; break;
		case "3": validatorControl.validationexpression = '[0-9]{5}'; errormessage += ' (00000)'; break;
		case "5": validatorControl.validationexpression = '[0-9]{5}'; errormessage += ' (00000)'; break;
		case "6": validatorControl.validationexpression = '([lL]-)?[0-9]{4}'; errormessage += ' (L-0000)'; break;
	}
	
	validatorControl.errormessage = errormessage;
	return true;
}

// ************************************************************************************
// Format the zipcode value entered in the textBoxControl based on the selected country value.
// ************************************************************************************

function FormatZipcode( countryValue, textBoxControl )
{
	if ( countryValue == null || textBoxControl == null )
		return false;			
		
	enteredValue = textBoxControl.value;
	switch( countryValue )
	{
		case "1": textBoxControl.value = enteredValue.replace( /([0-9]{4})([a-zA-Z]{2})/, "$1 $2" ); textBoxControl.value = textBoxControl.value.toUpperCase(); break;
		case "2": textBoxControl.value = enteredValue.replace( /([bB]-)?([0-9]{4})/, "B-$2" ); break;
		case "6": textBoxControl.value = enteredValue.replace( /([lL]-)?([0-9]{4})/, "L-$2" ); break;					
	}					
}

// ************************************************************************************
// Update the zipcode information button and the related controls.
// ************************************************************************************

function UpdateGetZipcodeInformationButton( buttonControl, countryValue, streetControl, cityControl )
{
	if ( buttonControl == null || countryValue == null )
		return false;
		
	if ( countryValue == "1" )
		buttonControl.style.display = '';
	else
	{
		streetControl.disabled = '';
		cityControl.disabled = '';					
		buttonControl.style.display = 'none';
	}
		
	return true;
}



