var error_bgColor = 'cornsilk';

function roundDollar(value)
{
	value = parseFloat(value);
	value += "";
	
	if(value.indexOf(".") == -1)
	{
		value += ".00";
	}
	else if(value.length - value.indexOf(".") > 3)
	{
		value = value.substring(0,value.indexOf(".")+3);
	}
	else if(value.length - value.indexOf(".") == 2)
	{
		value += "0";
	}

	return value;
}

function ensureDigits(obj)
{
	var val = obj.value;
	obj.value = val.replace(/[^\d]/g,"");
	obj.focus();
}

function checkValid(obj, type, min, max)
{
	if(type == 'int' || type == 'float')
	{
		var val = obj.value;
		if(type == 'int')
			val = val.replace(/[^\d]/g,"");
			
		val = type=='int'?parseInt(val):parseFloat(val);
		
		if(!(val >= min && val <= max))
		{
			obj.value = min;
		}
		else
		{
			obj.value = val;
		}
		
		return true;
	}
	else if(type == "text")
	{
		var val = obj.value;
		val = val.replace(/\s/g,"");
		
		if(val.length < min || val.length > max)
		{
			obj.style.backgroundColor = error_bgColor;
			obj.focus();
			alert("Please enter a value with a minimum length of " + min);
			return false;		
		}
		else
		{
			obj.style.backgroundColor = 'white';
		}
	}
	else if(type == "email")
	{
		var val = obj.value;
		val = val.match(/^.{1,}@.{1,}\..{2,}/);
		
		if(val == null)		
		{
			obj.style.backgroundColor = error_bgColor;
			obj.focus();
			alert("Please enter a valid e-mail address");
			return false;
		}
		else
		{
			obj.style.backgroundColor = 'white';
		}
	}
	else if(type == "password")
	{
		var val = obj.value;
		val = val.replace(/\s/g,"");
		
		if(val.length < min || val.length > max)
		{
			obj.style.backgroundColor = error_bgColor;
			obj.focus();
			alert("Please enter a value with a minimum length of " + min);
			return false;		
		}
		else if(document.forms[0]['password1'] && (obj.value != document.forms[0]['password1'].value))
		{
			obj.style.backgroundColor = error_bgColor;
			document.forms[0]['password1'].style.backgroundColor = error_bgColor;
			obj.focus();
			alert("Please make sure you re-enter the password properly");
			return false;			
		}
		else
		{
			obj.style.backgroundColor = 'white';
		}
	}
	else if(type == "creditcard")
	{
		var number = obj.value;
		number = number.replace(/[^\d]/g,"");
		
		if(number.match(/.{16}/) != null)
		{
			var total = 0;
			var flag = 0;
			for (var i=(number.length - 1);i>=0; i--) 
			{
				if (flag == 1) 
				{
					var digits = number.charAt(i) * 2;
					if (digits > 9) digits -= 9;
					total += digits;
					flag = 0;
				} 
				else 
				{
					total = total + parseInt(number.charAt(i));
					flag = 1;
				}
			}

			if ((total%10) == 0) 
			{
				return true;
			} 
			else 
			{
				alert('Please enter a valid credit card number');
				return false;
			}
		}
		else
		{
			alert('Please enter a valid credit card number');
			return false;		
		}
	}
	
	return true;
}

function request()
{
	this.script = arguments[0];
	this.runmode = arguments[1];
	this.request = arguments[2];
	this.func = arguments[3];
	this.arguments = new Array();
	
	for(x=4;x<arguments.length;x++)
	{
		this.arguments[x] = arguments[x];
	}
		
	return this;
}

function send_request(req)
{
	var xmlhttp = false;
	

	try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");} 
	catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { xmlhttp = false; } }

	if (!xmlhttp && typeof XMLHttpRequest!='undefined') 
	{
		xmlhttp = new XMLHttpRequest();
	}
	
	if(xmlhttp)
	{
		var url = req.script + "?rm=" + req.runmode + "&" + req.request;
		xmlhttp.open("GET", url, true); 
		xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		
		xmlhttp.onreadystatechange = function() {
														if(xmlhttp.readyState==4 && xmlhttp.status==200) 
														{
															req.func(xmlhttp);
														}
														else if(xmlhttp.readyState==4)
														{
															alert("Error with server response: " + xmlhttp.responseText);
														}
													};
		xmlhttp.send(null);
	}
	else
	{
		alert("We're sorry, but we do not support your browser.\nPlease upgrade to a more recent browser.");
	}
}

function set_select(select_obj, value)
{
	for(x=0;x<select_obj.options.length;x++)
	{
		if(select_obj.options[x].value == value)
		{
			select_obj.options[x].selected = true;
			break;
		}
	}
}

function updateState(country_sel)
{
	var MSIE = (navigator.userAgent.indexOf('MSIE')!=-1);

	while(frm_state_id.options.length > 0)
	{
		frm_state_id.remove(0);
	}

	var country = country_sel.options[country_sel.selectedIndex].value;

	if(states[country] != null)
	{
		for(x=0;x<states[country].length;x++)
		{
			var option = document.createElement("OPTION");

			option.value = states[country][x].state_id;
			option.text = states[country][x].name;


			MSIE?frm_state_id.add(option):frm_state_id.appendChild(option);
		}		
	}
	else
	{
		var option = document.createElement("OPTION");
		option.value = "0";
		option.text = "Not applicable";
		MSIE?frm_state_id.add(option):frm_state_id.appendChild(option);
	}
}

function CState(state_id, name)
{
	this.state_id = state_id;
	this.name = name;
	return this;
}

function openSlideShow(dir, photo_id)
{
	var slideshow_window = window.open("/scripts/expman.pl?rm=slideshow&dir=" + dir + "&photo_id=" + photo_id, 'slides', "height=400, width=500, status=no, toolbar=no, menubar=no, location=no, scrollbars=no, resizable=yes");
	slideshow_window.moveTo( (screen.availWidth-600)/2, 200 )
	slideshow_window.focus();	
}

