// PluroWeb JavasScript

// 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";
	}
}

// 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;
	}
}

// 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); 	
}