// PluroWeb JavaScript
//calendar
var DHTML_SUITE_THEME = 'blue';
var DHTML_SUITE_THEME_FOLDER = '/core/javascript/calendar/themes/';
var DHTML_SUITE_JS_FOLDER = '/core/javascript/calendar/js/';


// Form Processing functions
/**************************************************************************************************/

// change input field CSS class on focus
function process_input_focus(objMe)
{
	objFlag = document.getElementById("inputflag_" + objMe.id);
	if ( objFlag != undefined )
	{
		if(!objFlag.origClassName) 
			objFlag.origClassName = objFlag.className;
		objFlag.className="inputflag_act";
	}
}

// change input field CSS class on blurr
function process_input_blur(objMe)
{
	objFlag = document.getElementById("inputflag_" + objMe.id);
	if ( objFlag != undefined )
	{
		if(objMe.value.length > 0)
		{
			objFlag.className = 'inputflag_chng';
		}
		else
		{
			if ( (objFlag.origClassName == 'inputflag_err') || (objFlag.origClassName == 'inputflag_req') || (objFlag.origClassName == 'inputflag_ok') )
				objFlag.className = 'inputflag_err';
			else
				objFlag.className = objFlag.origClassName;
		}
	}
}

// Form Country object
function Country(state_label, zip_label)
{
	this.state_label = state_label;
	this.zip_label = zip_label;
}

// populate state drop down depending on the country
function populate_states(country, field, state_label, zip_label)
{
	optionList = document.getElementById(field).options;
	for (i = optionList.length; i >= 0; i--){
		optionList[i] = null;
	}

	if(countries[country])
	{
		document.getElementById(field).disabled=false;
		if(countries[country].zip_label != "")
			document.getElementById(zip_label).innerHTML  = countries[country].zip_label+":";
		else document.getElementById(zip_label).innerHTML = "Zip:";
		if(countries[country].state_label != "")
			document.getElementById(state_label).innerHTML = countries[country].state_label+":";
		else document.getElementById(state_label).innerHTML = "State:";
		optionList[0] = new Option("-- Select "+countries[country].state_label+" --","");

		for(i=0; i<countries[country].states.length; i++)
		{
			optionList[i+1] = new Option(countries[country].states[i], countries[country].abbrv[i]);
		}

		if(document.getElementById(field).className != "input_err" && document.getElementById(field).className != "input_ok") document.getElementById(field).className = "input_req";
	}
	else
	{
		document.getElementById(field).options[0] = new Option("N/A","N/A");
		document.getElementById(field).disabled=true;
		document.getElementById(zip_label).innerHTML = "Zip:";
		document.getElementById(state_label).innerHTML = "State:";
		document.getElementById(field).className = "input_opt";
	}
}

// select dropdown value
function select_dropdown_value(objDropdownField, strValue)
{
	if ( strValue != '' )
	{
		for(i=0; i<objDropdownField.length; i++)
			if(objDropdownField[i].value == strValue) 
				objDropdownField[i].selected = true;
	}
}

// get checked radio button value
function get_checked_radio_value(objRadioButton) 
{
	if(!objRadioButton)
		return "";
	var intRadioGroupLength = objRadioButton.length;
	
	if(intRadioGroupLength == undefined)
		if(objRadioButton.checked)
			return objRadioButton.value;
		else
			return "";
	for(var i = 0; i < intRadioGroupLength; i++) 
	{
		if(objRadioButton[i].checked) 
			return objRadioButton[i].value;
	}
	return "";
}

// set checked radio button value
function set_checked_radio_value(objRadioButton, strNewValue) 
{
	if(!objRadioButton)
		return;
		
	var intRadioGroupLength = objRadioButton.length;
	if(intRadioGroupLength == undefined) 
	{
		objRadioButton.checked = (objRadioButton.value == strNewValue.toString());
		return;
	}
	for(var i = 0; i < intRadioGroupLength; i++) 
	{
		objRadioButton[i].checked = false;
		if(objRadioButton[i].value == strNewValue.toString())
			objRadioButton[i].checked = true;
	}
}

function format_decimal(num)
{
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + num + '.' + cents);
}

function format_decimal4(num)
{
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*10000+0.5000000000001);
	cents = num%10000;
	num = Math.floor(num/10000).toString();
	if (cents<10) {
		cents="000"+cents;
	} else if (cents<100) {
		cents="00"+cents;
	} else if(cents<1000) {
		cents = "0" + cents;
	}
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++) num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + num + '.' + cents);
}

// toggle display property of a <div>
function toggle_div(strDivId)
{
	alert(strDivId);
	objDiv = document.getElementById(strDivId);

	if(objDiv.style.display=='block')
	{
		objDiv.style.display='none';
	}
	else
	{
		objDiv.style.display='block';
	}
}


// Browser Detection functions
/**************************************************************************************************/
function is_in_string(strNeedle, strHaystack)
{
//	alert('test');
	if(strHaystack.indexOf(strNeedle) >= 0)
	{return true;}
	else{return false;}
}

function detect_browser()
{
	var strBrowser;
	strUserAgent = navigator.userAgent.toLowerCase();

	if (is_in_string('konqueror', strUserAgent)) strBrowser = "konqueror";
	else if (is_in_string('safari', strUserAgent)) strBrowser = "safari";
	else if (is_in_string('omniweb', strUserAgent)) strBrowser = "omniweb";
	else if (is_in_string('opera', strUserAgent)) strBrowser = "opera";
	else if (is_in_string('webtv', strUserAgent)) strBrowser = "webtv";
	else if (is_in_string('icab', strUserAgent)) strBrowser = "icab";
	else if (is_in_string('msie', strUserAgent)) strBrowser = "msie";
	else if (!is_in_string('compatible', strUserAgent))	strBrowser = "netscape";
	else strBrowser = "undefined";

//	if (!version) version = detect.charAt(place + thestring.length);
/*	if (!OS)
	{
		if (checkIt('linux')) OS = "Linux";
		else if (checkIt('x11')) OS = "Unix";
		else if (checkIt('mac')) OS = "Mac"
		else if (checkIt('win')) OS = "Windows"
		else OS = "an unknown operating system";
	}*/

	return strBrowser;
}



// Pop-up and window.open functions
/**************************************************************************************************/
function show_window_centered( link, title, w, h )
{
	var top		= (screen.height - h)/2;
	var left	= (screen.width - w)/2;

	window.open(link, title, 'toolbar=no,titlebar=no,width=' + w + ',height=' + h + ',resizable,left=' + left + ',top=' + top); 	
}

function show_window_centered_with_options( link, title, w, h, user_options )
{
	var top		= (screen.height - h)/2;
	var left	= (screen.width - w)/2;
	
	if ( user_options )
		user_options = ',' + user_options;

	window.open(link, title, 'width=' + w + ',height=' + h + ',resizable,left=' + left + ',top=' + top + user_options); 	
}

function in_array(strNeedle, arrHaystack)
{
	if ( arrHaystack.length == 0 )
		return false;

	if ( in_array_binary(strNeedle, arrHaystack, 0, arrHaystack.length-1) > -1 )
		return true;
	else 
		return false;
}

function in_array_linear(strNeedle, arrHaystack)
{
	for ( i=0; i < arrHaystack.length;  i++ )
		if ( arrHaystack[i].value == strNeedle )
			return true;
	
	return false;
}

function in_array_binary(strNeedle, arrHaystack, intLeft, intRight)
{
	if ( intRight < intLeft )
		return -1;

	intMiddle = Math.floor((intRight-intLeft)/2) + intLeft;
	if ( arrHaystack[intMiddle].text == strNeedle )
		return intMiddle;
		
	if ( strNeedle < arrHaystack[intMiddle].text )
		return in_array_binary(strNeedle, arrHaystack, intLeft, intMiddle-1);
	else
		return in_array_binary(strNeedle, arrHaystack, intMiddle+1, intRight);	
}

function display_array(arrArray)
{
	var strElements = '';
	for(i=0; i<arrArray.length; i++)
	{
		if ( strElements != '' )
			strElements = strElements + ', ';
		strElements = strElements + arrArray[i].text;
	}
	
	alert(strElements);
}

function array_search(strNeedle, arrHaystack)
{
	for (i=0; i<arrHaystack.length; i++)
		if ( strNeedle == arrHaystack[i] )
			return i;
			
	return -1;
}

function toggle_div(objDiv)
{
	if(objDiv.style.display == 'none')
		objDiv.style.display = 'block';
	else objDiv.style.display = 'none';
}

function debug_object(obj, parent)
{
	var msg;
	for (var i in obj)
	{
		if (parent) 
		{
			msg = parent + '.' + i + '\n' + obj[i]; 
		}
		else
		{
			msg = i + '\n' + obj[i];
		}

		if (!confirm(msg)) { return; }
		//document.write(msg);
		if (typeof obj[i] == 'object')
		{ 
			if (parent) { debug_object(obj[i], parent + '.' + i); } else { debug_object(obj[i], i); }
		}
	}
}


function displayCalendar(strId, objCal)
{
	objInput 		= document.getElementById(strId+'_date');
	objInputHidden 	= document.getElementById(strId);
	objCal.setCalendarPositionByHTMLElement(strId+'_date',0,objInput.offsetHeight+2);	// Position the calendar right below the form input
	objCal.setInitialDateFromInput(objInputHidden,'yyyymmddhhii');	// Specify that the calendar should set it's initial date from the value of the input field.
	if(objCal.isVisible()){
		objCal.hide();
	}else{
		objCal.resetViewDisplayedMonth();	// This line resets the view back to the inital display, i.e. it displays the inital month and not the month it displayed the last time it was open.
		objCal.display();
	}		
}	
/* arrInput is an associative array with the properties
year
month
day
hour
minute
calendarRef - Reference to the DHTMLSuite.calendar object.
*/
function getDateFromCalendar(arrInput)
{
	objInput = document.getElementById(arrInput.calendarRef.id+'_date');
	objInputHidden = document.getElementById(arrInput.calendarRef.id);
			
	objInput.value = arrInput.month + '-' + arrInput.day + '-' + arrInput.year;
	objInputHidden.value = arrInput.year + arrInput.month + arrInput.day;	
	
	if(objInput.onchange)
		objInput.onchange();
	if(objInputHidden.onchange)
		objInputHidden.onchange();
	
	arrInput.calendarRef.hide();			
}	


function findPosX(obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }

  function findPosY(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }
