// Common JS functions.

function setInput( field, action )
{
	switch ( action )
	{
		case "clear":
			if ( field.defaultValue == field.value )
				field.value = "";
			break;
		case "reset":
			if ( field.defaultValue == "" )
				field.value = field.defaultValue;
			break;
		default:
			// No action.
	}
}

function popup( url, name, height, width, menubar )
{
	var config = "height=" + height + ", width=" + width + ", toolbar=no, menubar=" + menubar + ", location=no, directories=no, status=yes, resizable=yes, scrollbars=yes";
	var win = window.open( url, name, config );
}

function closeWindow( url, name )
{
	window.close();
	if ( url != "" )
	{
		if ( !window.opener || window.opener.closed )
		{
			if ( name == "" )
				var name = "Opener";
			var win = window.open( url, name, "height=600, width=600, toolbar=yes, menubar=yes, location=yes, directories=yes, status=yes, resizable=yes, scrollbars=yes" );
		}
	}
}

function changeField( selectedField, changingField, changingValue )
{
	var selectedValue = selectedField.options[selectedField.selectedIndex].value;
	var selfCall = false;
	var firstChangingField = changingField;
	var firstChangingValue = changingValue;
	if ( changingField.length > 1 )
	{
		selfCall = true;
		var firstChangingField = changingField.splice( 1, 1 );
		var firstChangingValue = changingValue.splice( 1, 1 );
	}
	if ( selectedValue == "" )
	{
		while ( firstChangingField[0].options.length > 0 )
			firstChangingField[0].options[( firstChangingField[0].options.length - 1 )] = null;
		firstChangingField[0].options[0] = new Option( "-", "" );
	}
	else
	{
		var changingArray = eval( selectedField.name + "_" + selectedValue );
		while ( changingArray.length < firstChangingField[0].options.length )
			firstChangingField[0].options[( firstChangingField[0].options.length - 1 )] = null;
		for ( var i = 0; i < changingArray.length; i++ )
		{
			firstChangingField[0].options[i] = new Option( changingArray[i][0], changingArray[i][1] );
			if ( firstChangingValue[0] == changingArray[i][1] )
				firstChangingField[0].options[i].selected = true;
		}
	}
	if ( selfCall )
		changeField( firstChangingField[0], changingField, changingValue );
}

function checkValueBeforeSubmit( form, checkingField, alertText )
{
	if ( checkingField.value == "" )
	{
		 alert( alertText );
		 return false;
	}
	else
	{
		form.submit();
		return true;
	}
}

// Font switchers functions.

function setCookie( name, value, days, path, domain, secure )
{
	var expires, date;
	if ( typeof days == "number")
	{
		date = new Date();
		date.setTime( date.getTime() + ( days * 24 * 60 * 60 * 1000 ) );
		expires = date.toGMTString();
	}
	document.cookie = name + "=" + escape( value ) + ( ( expires ) ? "; expires=" + expires : "" ) +
		( ( path ) ? "; path=" + path : "" ) + ( ( domain ) ? "; domain=" + domain : "" ) + ( ( secure ) ? "; secure" : "" );
}

function getCookie( name )
{
	var nameq = name + "=";
	var c_ar = document.cookie.split( ';' );
	for ( var i = 0; i < c_ar.length; i++ )
	{
		var c = c_ar[i];
		while ( c.charAt( 0 ) == ' ' )
			c = c.substring( 1, c.length );
		if ( c.indexOf( nameq ) == 0 )
			return unescape( c.substring( nameq.length, c.length ) );
	}
	return '';
}

function deleteCookie( name, path, domain )
{
	if ( getCookie( name ) )
		document.cookie = name + "=" + ( ( path ) ? "; path=" + path : "") +
			( ( domain ) ? "; domain=" + domain : "" ) + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
}

var bodySize = 1;

function fixSize( fontSize )
{
 	setCookie( "myFontSize", fontSize, 100, "/" );
	document.body.style.fontSize = fontSize + "em";
}

function increaseSize()
{
	var fontSize = getCookie( "myFontSize" );
	if ( fontSize == "" )
		fontSize = bodySize + 0.2;
	else if ( fontSize < 1.4 )
		fontSize = eval( fontSize ) + 0.2;
	fixSize( fontSize );
}

function decreaseSize()
{
	var fontSize = getCookie( "myFontSize" );
 	if ( fontSize == "" )
		fontSize = bodySize - 0.2;
	else if ( fontSize > 0.8 )
		fontSize = eval( fontSize ) - 0.2;
	fixSize( fontSize );
}

function restartSize()
{
	var fontSize = bodySize;
	fixSize( fontSize );
}

function startSize()
{
	var fontSize = getCookie( "myFontSize" );
	if ( fontSize == "" )
		fontSize = bodySize;
	fixSize( fontSize );
}

var contentSize = 1;

function fixContentSize( fontSize )
{
 	setCookie( "contentFontSize", fontSize, 100, "/" );
	document.getElementById('main-content').style.fontSize = fontSize + "em";
}

function increaseContentSize()
{
	var fontSize = getCookie( "contentFontSize" );
	if ( fontSize == "" )
		fontSize = contentSize + 0.2;
	else if ( fontSize < 1.4 )
		fontSize = eval( fontSize ) + 0.2;
	fixContentSize( fontSize );
}

function decreaseContentSize()
{
	var fontSize = getCookie( "contentFontSize" );
 	if ( fontSize == "" )
		fontSize = contentSize - 0.2;
	else if ( fontSize > 0.8 )
		fontSize = eval( fontSize ) - 0.2;
	fixContentSize( fontSize );
}

// follows method onchange of attribute 'residence_country' select options( class 'user' ); attribute
// 'state' dropdown only appears if method below change its style property when conditions are true;

var AttributeBase='ContentObjectAttribute';
var postal_code_country_id = '95';

function onCategoryChange(id) {
	var category_select = document.getElementById( id );
	var category_select_options = category_select.options;

	for ( var j = 0; j < category_select_options.length; j++ ) {
		if ( category_select_options[j].selected ) {
			if ( category_select_options[j].value == postal_code_country_id ) {
				document.getElementById( 'html_container_user_postal_code' ).style.display = 'inline';
			} else {
				document.getElementById( 'html_container_user_postal_code' ).style.display = 'none';
				document.getElementById( AttributeBase + '_user_postal_code' ).value = '';
			}
			break;
		}
	}
}

var OA_p=location.protocol=='https:'?'https:':'http:';
var OA_r=Math.floor(Math.random()*99999999);
if (!document.OA_used) document.OA_used = ',';
function OA_adjs(z,n)
{
  if (z>-1) {
    var az="<"+"script language='JavaScript' type='text/javascript' ";
    az+="src='"+OA_p+"//openx.iguanait.com/www/delivery/ajs.php?n="+n+"&zoneid="+z;
    az+="&source="+OA_channel+"&exclude="+document.OA_used+"&r="+OA_r;
    az+="&mmm_fo="+(document.mmm_fo)?'1':'0';
    if (document.context) az+= "&context=" + escape(document.context);
    if (window.location) az+="&loc="+escape(window.location);
    if (document.referrer) az+="&referer="+escape(document.referrer);
    az+="'><"+"/script>";
    document.write(az);
  }
}
function OA_adpop(z,n)
{
  if (z>-1) {
    var az="<"+"script language='JavaScript' type='text/javascript' ";
    az+="src='"+OA_p+"//openx.iguanait.com/www/delivery/apu.php?n="+n+"&zoneid="+z;
    az+="&source="+OA_channel+"&exclude="+document.OA_used+"&r="+OA_r;
    if (window.location) az+="&loc="+escape(window.location);
    if (document.referrer) az+="&referer="+escape(document.referrer);
    az+="'><"+"/script>";
    document.write(az);
  }
}