function isExplorer() {
	if (navigator.appName.toUpperCase().match(/MICROSOFT INTERNET EXPLORER/) != null)
		return true;
	else 
		return false;
}

// Focus is set on first form elemnt on page
function setCursor(formName){
	if(!formName){
		if(document.forms.length > 0){
			document.forms[0].elements[0].focus();
		}
	}
	else{
		 document.forms[formName].elements[0].focus();	
	}
}


function selectAll(checkBoxName) {

	checkBoxes = document.getElementsByName(checkBoxName);

	for(var i = 0; i < checkBoxes.length; i++) {
		checkBoxes[i].checked = true;	
	}

}


// Assign function lockEvent to event handlers
function lockBrowser(){
	if (window.event){
				document.onmousedown = lockEvent;
			}
	document.onclick = lockEvent;
	document.onkeydown = lockEvent;	
	document.onkeypress = lockEvent;
}

function unLock(){			
			document.onmousedown = releaseEvent;
			document.onclick = releaseEvent;
			document.onkeydown = releaseEvent;	
			document.onkeypress = releaseEvent;		
		}


// Disable all form elements on page
function disableFormElements(){
	for(var i = 0; i < document.forms.length; i++)
		for (var j = 0; j < document.forms[i].elements.length; j++)
			document.forms[i].elements[j].disabled = true;	
}

// Enaable all form elements on page
function enableFormElements(){
	for(var i = 0; i < document.forms.length; i++)
		for (var j = 0; j < document.forms[i].elements.length; j++)
			document.forms[i].elements[j].disabled = false;	
}


// Discard event
function lockEvent(e){
	var msg = "no right click";
	if(window.event){
				// Capture all keys
				event.keyCode = 0;
				// Right mouse click
				if(event.button > 1){
					alert(msg);
				}		
				event.returnValue = false;
			}	
		else if(e){
			if(e.which == 2 || e.which == 3){
				alert(msg);
			}
			return(false);
		}
}

// Release event
function releaseEvent(){
			return true;
}	


function clearSelect(sel) {
	if (sel.options.lentgh < 1)
		return;
	for (var i=(sel.options.length-1); i>=0; i--) {
		sel.options[i] = null;
	}
}

function copyObjectToSelect(objects, target) {

	clearSelect(target);
	
	var isExplorerFlag = isExplorer();
	
	for (var i = 0; i < objects.length; i++) {
		var oOption = document.createElement("OPTION");
		oOption.text=objects[i].text;
		oOption.value=objects[i].value;
		
		if (isExplorerFlag)
			target.add(oOption);
		else
			target.add(oOption, null);
	}
}


function ParserNumeric(myfield, e){
		var key;
		var keychar;
		if (window.event)
   			key = window.event.keyCode;
		else if (e)
   			key = e.which;
		else
   			return true;
		keychar = String.fromCharCode(key);
		// control keys: 8=back, 9=tab, 13=return, 27=ESC
		if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27) ){
   			return true;
		}
		// numbers
		else if ((("0123456789").indexOf(keychar) > -1)){
   			return true;
		}
		else
   			return false;
	}

function ParserNonNumeric(myfield, e){
		var key;
		var keychar;
		if (window.event)
   			key = window.event.keyCode;
		else if (e)
   			key = e.which;
		else
   			return true;
		keychar = String.fromCharCode(key);
		
		if ((("0123456789").indexOf(keychar) > -1)){
   			return false;
		}
		else
   			return true;
	}


function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }
   
function IsInteger(sText)
{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }
   
   
function resizeIframeHeight(oIframe){
				var oBody;
				var scroll_height;
				var new_height;
				if(isExplorer()){
				   oBody = oIframe.contentWindow.document.body;
				   scroll_height= oBody.scrollHeight;//find the height of the internal page
				   new_height = scroll_height*1.12;
				}
				else{
							oBody = oIframe.contentDocument;
							scroll_height= oBody.height;//find the height of the internal page
							new_height = scroll_height*1.2;
				}
 			oIframe.style.height=new_height;//change the height of the iframe
			}
			
function resizeIframeHeightBaseOLD(oIframe){
		var oBody;
		var scroll_height;
		var new_height;
		if(isExplorer()){
		   oBody = oIframe.contentWindow.document.body;
		   scroll_height = oBody.scrollHeight;
		   new_height = scroll_height*1;
		}
		else{
			oBody = oIframe.contentDocument;
			scroll_height = oBody.height;
			new_height = scroll_height*1 + 30;
		}
 		oIframe.style.height=new_height;
	}

function resetIframeHeight(frameName){
	if(document.getElementById && (document.getElementById(frameName).contentDocument || document.getElementById(frameName).contentWindow)) {
		var objFrame = document.getElementById(frameName);
 		objFrame.style.height = "0px";
	}
	else if(document.all){
		document.all[frameName].style.height = "0px";
	}
}

function resizeIframeHeightBase(frameName) {
	resizeIframeHeightBaseWithPadding(frameName, 25);
}
	
function resizeIframeHeightBaseWithPadding(frameName, heightPadding) {
	if(document.getElementById && (document.getElementById(frameName).contentDocument || document.getElementById(frameName).contentWindow)) {
		var objFrame = document.getElementById(frameName);
		var objDoc; 
		//IE5.5+, Mozilla 1+, Opera
		if(objFrame.contentDocument){
		   objDoc = objFrame.contentDocument; 
		}
		//IE5.5
		else if(objFrame.contentWindow){
		   objDoc = objFrame.contentWindow.document;
		}
		//IE5, Safari
		else if(window.frames && window.frames[frameName]){
		   objDoc = window.frames[frameName].document;
		}
		else if(objFrame.document){
		   objDoc = objFrame.document;
		}
		else{
				 objDoc = null;
		}
 	var height = (objDoc.body.scrollHeight ? objDoc.body.scrollHeight : objDoc.body.offsetHeight); // find height of internal page
 	objFrame.style.height = (height*1 + heightPadding*1) + "px";
	}
	else if(document.all){
		var objFrame = document.frames(frameName);
		var height = document.frames(frameName).document.body.scrollHeight;
		document.all[frameName].style.height = (height*1 + heightPadding*1) + "px";
	}
}
			
			
function isValidDate(day, month, year) {
    if (month < 1 || month > 12) {
            return false;
        }
        if (day < 1 || day > 31) {
            return false;
        }
        if ((month == 4 || month == 6 || month == 9 || month == 11) &&
            (day == 31)) {
            return false;
        }
        if (month == 2) {
            var leap = (year % 4 == 0 &&
                       (year % 100 != 0 || year % 400 == 0));
            if (day>29 || (day == 29 && !leap)) {
                return false;
            }
        }
        return true;
		}
		
function lastDayOfTheMonth(day, month, year) {
        if ((month == 4 || month == 6 || month == 9 || month == 11) &&
            (day == 31)) {
            return 30;
        }
        if (month == 2) {
            var leap = (year % 4 == 0 &&
                       (year % 100 != 0 || year % 400 == 0));
            if (day > 28 && leap) {
                return 29;
            }else{
            				return 28;
            }
        }
       	return 31;
		}		
		
function checkDate(oForm, dayFieldName, yearMonthFieldName) {

	eDay = oForm.elements[dayFieldName];
	siDay = eDay.selectedIndex;
	day = eDay.options[siDay].value;
	eYearMonth = oForm.elements[yearMonthFieldName];
	siYearMonth = eYearMonth.selectedIndex;
	yearMonth = eYearMonth.options[siYearMonth].value;
	year=yearMonth.substring(0,4);
	month=yearMonth.substring(4,6);

	if (!isValidDate(day, month, year)){
		lastDay = lastDayOfTheMonth(day, month, year);
		eDay.selectedIndex = lastDay-1;
	}
}		

function checkDateWithCalendar(oForm, dayFieldName, yearMonthFieldName, calendarFieldName) {

	eDay = oForm.elements[dayFieldName];
	siDay = eDay.selectedIndex;
	day = eDay.options[siDay].value;
	eYearMonth = oForm.elements[yearMonthFieldName];
	siYearMonth = eYearMonth.selectedIndex;
	yearMonth = eYearMonth.options[siYearMonth].value;
	year=yearMonth.substring(0,4);
	month=yearMonth.substring(4,6);

	if (!isValidDate(day, month, year)){
		lastDay = lastDayOfTheMonth(day, month, year);
		eDay.selectedIndex = lastDay-1;
	}
	
	synchronizeCalendar(oForm, yearMonthFieldName, dayFieldName, calendarFieldName);
}
				
function correctDate(oForm, dayFieldName1, yearMonthFieldName1, dayFieldName2, yearMonthFieldName2) {

	checkDate(oForm,dayFieldName1, yearMonthFieldName1);
	checkDate(oForm,dayFieldName2, yearMonthFieldName2);

	eDay1 = oForm.elements[dayFieldName1];
	siDay1 = eDay1.selectedIndex;
	day1 = eDay1.options[siDay1].value;
	eYearMonth1 = oForm.elements[yearMonthFieldName1];
	siYearMonth1 = eYearMonth1.selectedIndex;
	yearMonth1 = eYearMonth1.options[siYearMonth1].value;
	year1=yearMonth1.substring(0,4);
	month1=yearMonth1.substring(4,6);
	date1= new Date(year1,month1-1,day1);
				
	eDay2 = oForm.elements[dayFieldName2];
	siDay2 = eDay2.selectedIndex;
	day2 = eDay2.options[siDay2].value;
	eYearMonth2 = oForm.elements[yearMonthFieldName2];
	siYearMonth2 = eYearMonth2.selectedIndex;
	yearMonth2 = eYearMonth2.options[siYearMonth2].value;
	year2=yearMonth2.substring(0,4);
	month2=yearMonth2.substring(4,6);
	date2= new Date(year2,month2-1,day2);
				
	if (date1.getTime() > date2.getTime()) {
		eDay2.selectedIndex = eDay1.selectedIndex;
		eYearMonth2.selectedIndex = eYearMonth1.selectedIndex;
	}
}

function correctDateWithCalendar(oForm, dayFieldName1, yearMonthFieldName1, calendarFieldName1, dayFieldName2, yearMonthFieldName2, calendarFieldName2) {

	checkDateWithCalendar(oForm, dayFieldName1, yearMonthFieldName1, calendarFieldName1);
	checkDateWithCalendar(oForm, dayFieldName2, yearMonthFieldName2, calendarFieldName2);

	eDay1 = oForm.elements[dayFieldName1];
	siDay1 = eDay1.selectedIndex;
	day1 = eDay1.options[siDay1].value;
	eYearMonth1 = oForm.elements[yearMonthFieldName1];
	siYearMonth1 = eYearMonth1.selectedIndex;
	yearMonth1 = eYearMonth1.options[siYearMonth1].value;
	year1=yearMonth1.substring(0,4);
	month1=yearMonth1.substring(4,6);
	date1= new Date(year1,month1-1,day1);
				
	eDay2 = oForm.elements[dayFieldName2];
	siDay2 = eDay2.selectedIndex;
	day2 = eDay2.options[siDay2].value;
	eYearMonth2 = oForm.elements[yearMonthFieldName2];
	siYearMonth2 = eYearMonth2.selectedIndex;
	yearMonth2 = eYearMonth2.options[siYearMonth2].value;
	year2=yearMonth2.substring(0,4);
	month2=yearMonth2.substring(4,6);
	date2= new Date(year2,month2-1,day2);
				
	if (date1.getTime() > date2.getTime()) {
		eDay2.selectedIndex = eDay1.selectedIndex;
		eYearMonth2.selectedIndex = eYearMonth1.selectedIndex;
		
		synchronizeCalendar(oForm, yearMonthFieldName2, dayFieldName2, calendarFieldName2);
	}
}

function synchronizeCalendar(oForm, yearMonthFieldName, dayFieldName, calendarFieldName) {

	eYearMonth = oForm.elements[yearMonthFieldName];
	siYearMonth = eYearMonth.selectedIndex;
	yearMonth = eYearMonth.options[siYearMonth].value;

	eDay = oForm.elements[dayFieldName];
	siDay = eDay.selectedIndex;	
	day = eDay.options[siDay].value;

	eCalendarField = oForm.elements[calendarFieldName];
	eCalendarField.value = day + "-" + yearMonth.substring(4, 6) + "-" + yearMonth.substring(0, 4);
}
			
	function setIfBlank(obj, str){
		obj.value = removeBlanks(obj.value);
		if(obj.value == ""){
			obj.value = str;
		}
	}

//Trim all text fields in form
function trimFormFields(formObj){
	for(var i = 0; i < formObj.elements.length; i++){
		if(formObj.elements[i].type == "text" && formObj.elements[i].value != ""){
			formObj.elements[i].value = strTrim(formObj.elements[i].value);
		}	
	}
}

// Removes all blanks in string
function removeBlanks(str){
	var re = / /g;
	return str.replace(re, '');
}

// Removes starting whitespaces 
function strTrimLeft(str) {
	return str.replace(/^\s+/,'');
}

// Removes trailing whitespaces 
function strTrimRight(str) {
 return str.replace(/\s+$/,'');
}

// Removes starting and trailing whitespaces 
function strTrim(str) {
  return str.replace(/^\s+/,'').replace(/\s+$/,'');
}

// Sets display property of object to "none"
function displayNone(id){
	var obj = document.getElementById(id);
	if(obj){
		obj.style.display="none";
	}
}

// Sets display property of object to "block"		
function displayBlock(id){
	var obj = document.getElementById(id);
	if(obj){
		obj.style.display="block";
		}
}


function enableImageButton(checkBox, enabledDiv, disabledDiv){
		var obj1 = document.getElementById(enabledDiv);
		var obj2 = document.getElementById(disabledDiv);
		
		if(checkBox.checked){
			if(obj1 && obj2){
				displayNone(enabledDiv);
				displayBlock(disabledDiv);					
			}
		}
		else{
			displayNone(disabledDiv);
			displayBlock(enabledDiv);
		}
}


function openPrintWindow(){
		window.open("print.jsp","printWindow","height=500,width=700,resizable=no,menubar=yes,status=yes,scrollbars=yes").focus();	
}


function printContent(){
	//copy content from opener to pop up window
	document.getElementById('contentId').innerHTML = window.opener.document.getElementById('printId').innerHTML;
	
	//hide buttons and extra spacing
	
	if(document.getElementById('buttons')){
		document.getElementById('buttons').style.display = "none";		
	}
	if(document.getElementById('spacing')){
		document.getElementById('spacing').style.display = "none";		
	}
	
	//print
	window.print();
}

function openAdditionalService(url){
	var strSize;
	var strProps	
	// Check screen resolution
	if ((screen.availWidth>=800) && (screen.availHeight>=600)){
			strSize="width=850,height=600";
	}
	else{
			strSize="width=750,height=500";
	}
	strProps = strSize + ",resizable=yes,menubar=no,status=no,scrollbars=yes";
	
	window.open(url,"additionalService",strProps).focus();	
}

// Check maxlength of textarea
function textCounter(field, maxLength){
	if(field.value.length > maxLength-1){
    field.value = field.value.substring(0, maxLength-1);
    return false;
 }
}


function replaceDivInnerHTML(source,target){
   eDiv = document.getElementById(source);
   if(eDiv)
   {
      ePaymentDiv = document.getElementById(target);
      if(ePaymentDiv)
      {
         ePaymentDiv.innerHTML = eDiv.innerHTML;
         ePaymentDiv.style.visibility = "visible";
      }
   }
}


function loadBufferedContent(targetId, containerName, bufferUrl){
			var content = window.frames[containerName].document.body.innerHTML;
			var regexp1 = /src="\//gi;
			var replaceStr1 = 'src="' + bufferUrl + "/";
			var regexp2 = /href="\//g;
			var replaceStr2 = 'href="' + bufferUrl + "/";
			
			content = content.replace(regexp1, replaceStr1);
			content = content.replace(regexp2, replaceStr2);  
   
   document.getElementById(targetId).innerHTML = content;
}

function copyToClipboard(text2copy) {
  if (window.clipboardData) {
    window.clipboardData.setData("Text",text2copy);
  } else {
    var flashcopier = 'flashcopier';
    if(!document.getElementById(flashcopier)) {
      var divholder = document.createElement('div');
      divholder.id = flashcopier;
      document.body.appendChild(divholder);
    }
    document.getElementById(flashcopier).innerHTML = '';
    var divinfo = '<embed src="scripts/_clipboard.swf" FlashVars="clipboard='+escape(text2copy)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
    document.getElementById(flashcopier).innerHTML = divinfo;
  }
}