var defaultSize		= 12;
var resizeCounter	= 0;
var increment		= 2;
var maxsize			= 18;
var minsize			= 8;
var currentID		= "";
var oldText			= "";
///////////////////// Beyond here be functions ////////////////
function toggle(ID)
{
	if(ID == currentID)
	{
		min(ID);
	}
	else
	{
		if(currentID != "")
		{
			min(currentID);
		}
		max(ID);
	}
	return false;
}

function min(categoryID)
{
	var obj		= $(categoryID);
	obj.style.display = 'none';
	currentID	= "";
}

function max (categoryID)
{
	var obj		= $(categoryID);
	obj.style.display = 'block';
	currentID	= categoryID;
}

function flagAd(category, reason, itemID, ulID)
{
	flagRequest	= new xmlRequest();
	var serverScript	= 'http://'+location.host+'/flag.php?type='+category+'&itemID='+itemID+'&reason='+reason;
	
	flagRequest.open("GET", serverScript, true);
	flagRequest.onreadystatechange = function()
	{
		if((flagRequest.readyState == 4)&&(flagRequest.status == 200))
		{
			alert(flagRequest.responseText);
		}
	}
	flagRequest.send(null);
	toggle(ulID);
	return false;
}

function verifyForm(pgForm)
{
	var errMsgs		= new Array();
	var reqMsgSet	= false;
	for(var i = 0; i < pgForm.elements.length; i++)
	{
		if(pgForm.elements[i].name == 'user_name')
		{
			if((pgForm.elements[i].value.length < 6)||(pgForm.elements[i].value.length > 12))
			{
				if(!alertMsg){var alertMsg	= "User names must be 6 - 12 characters long.";}
				else{alertMsg = alertMsg+"\nUser names must be 6 - 12 characters long.";}
				errMsgs.push(pgForm.elements[i].name);
			}
		}
		if(pgForm.elements[i].name == 'email')
		{
			if(isValidEmail(pgForm.elements[i].value) == false)
			{
				if(!alertMsg){var alertMsg	= "A valid Email address is required!";}
				else{alertMsg = alertMsg+"\nA valid Email address is required!";}
				errMsgs.push(pgForm.elements[i].name);
			}
			var priEmail	= pgForm.elements[i].value;
		}
		if(pgForm.elements[i].name == 'ver_email')
		{
			if(pgForm.elements[i].value != priEmail)
			{
				if(!alertMsg){var alertMsg	= "Email addresses do not match!";}
				else{alertMsg = alertMsg+"\nEmail addresses do not match!";}
				errMsgs.push(pgForm.elements[i].name);
			}
			if(pgForm.elements[i].value ==''){errMsgs.push(pgForm.elements[i].name);}
		}
		if(pgForm.elements[i].name == 'user_pass')
		{
			if(isValidPass(pgForm.elements[i].value) == false)
			{
				if(!alertMsg){var alertMsg	= "A password is required and may contain only Numbers, letters and these characters; @#$!%&";}
				else{alertMsg = alertMsg+"\nA password is required may contain only Numbers, letters and these characters; @#$!%&";}
				errMsgs.push(pgForm.elements[i].name);
				
			}else{
				var userPass	= pgForm.elements[i].value;
			}
		}
		if(pgForm.elements[i].name == 'verify_pass')
		{
			if(pgForm.elements[i].value != userPass)
			{
				if(!alertMsg){var alertMsg	= "Passwords do not match!";}
				else{alertMsg = alertMsg+"\nPasswords do not match!";}
				errMsgs.push(pgForm.elements[i].name);
			}
		}
		if(pgForm.elements[i].getAttribute('number'))
		{
			var numVal		= pgForm.elements[i].getAttribute('number');
			var	shoVal		= numVal.replace(/,/, " - ");
			var regExpObj	= new RegExp("^\\d{"+numVal+"}$");
			if(pgForm.elements[i].getAttribute('required') == '1') 
			{
				if(regExpObj.exec(pgForm.elements[i].value) == null)
				{
					if(!alertMsg){var alertMsg	= "Field "+pgForm.elements[i].name+" must be "+shoVal+" digits only";}
					else{alertMsg += "\nField "+pgForm.elements[i].name+" must be "+shoVal+" digits only";}
					errMsgs.push(pgForm.elements[i].name);
				}
			}else{
				if((pgForm.elements[i].value.search(regExpObj) == -1) && (pgForm.elements[i].value.length > 0))
				{
					if(!alertMsg){var alertMsg	= "Field "+pgForm.elements[i].name+" must be "+shoVal+" digits only";}
					else{alertMsg += "\nField "+pgForm.elements[i].name+" must be "+shoVal+" digits only";}
					errMsgs.push(pgForm.elements[i].name);
				}
			}
		}
		if(pgForm.elements[i].getAttribute('required') == '1')
		{
			if(pgForm.elements[i].value == '')
			{
				if(!reqMsgSet)
				{
					if(!alertMsg){var alertMsg	= "Required Field(s) blank.";}
					else{alertMsg = alertMsg+"\nRequired Field(s) blank.";};
					reqMsgSet	= 1
				}
				if(pgForm.elements[i].type != 'radio')
				{
					errMsgs.push(pgForm.elements[i].name);
				}
			}
		}
	}
	if(errMsgs.length > 0)
	{
		var errSpan	= $('error');
		alert(alertMsg+'\nPlease correct highlighted fields.');
		for(var j = 0; j < errMsgs.length; j++)
		{
			var fieldName	= errMsgs[j];
			pgForm.elements[fieldName].style.background = '#fff3c8';
		}
		return false;
	}
	return true;
}

function postComment(commentForm, serverScript, targetDiv)
{
	var errMsgs		= new Array();
	var reqMsgSet	= false;
	commentReq		= new xmlRequest();
	var formSubmit	= null;
	targetObj		= $(targetDiv);
	for(var i = 0; i < commentForm.elements.length; i++)
	{
		if(!formSubmit)
		{
			if((commentForm.elements[i].type == 'checkbox')||(commentForm.elements[i].type == 'radio'))
			{
				if(commentForm.elements[i].checked){formSubmit = commentForm.elements[i].name+'='+escape(commentForm.elements[i].value);}
			}else{
				var fieldData	= commentForm.elements[i].value.replace("&", "AMP");
				formSubmit		= commentForm.elements[i].name+'='+escape(fieldData);
			}
			if((commentForm.elements[i].value == '')&&(commentForm.elements[i].type != 'select-one'))
			{
				errMsgs.push(commentForm.elements[i].name);
			}
		}else{
			if((commentForm.elements[i].type == 'checkbox')||(commentForm.elements[i].type == 'radio'))
			{
				if(commentForm.elements[i].checked){formSubmit += '&'+ commentForm.elements[i].name+'='+escape(commentForm.elements[i].value);}
			}else{
				var fieldData	= commentForm.elements[i].value.replace("&", "AMP");
				formSubmit		+= '&'+commentForm.elements[i].name+'='+escape(fieldData);
			}
			if((commentForm.elements[i].value == '')&&(commentForm.elements[i].type != 'select-one'))
			{
				errMsgs.push(commentForm.elements[i].name);
			}
		}
		if(commentForm.elements[i].name == 'email')
		{
			if(isValidEmail(commentForm.elements[i].value) == false)
			{
				errMsgs.push(commentForm.elements[i].name);
			}
		}
		if(commentForm.elements[i].getAttribute('required') == '1')
		{
			if(commentForm.elements[i].value == '')
			{
				errMsgs.push(commentForm.elements[i].name);
			}
		}
	}
	if(errMsgs.length > 0)
	{
		alert('ERROR: All fields are required');
		for(var i = 0; i < errMsgs.length; i++)
		{
			var fieldName	= errMsgs[i];
			commentForm.elements[fieldName].style.background = '#fff3c8';
		}
		return false;
	}
	
	commentReq.open("POST", serverScript, true);
	commentReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	commentReq.onreadystatechange = function()
	{
		if (commentReq.readyState == 4 && commentReq.status == 200)
		{
			if(commentReq.responseText.substr(0, 6) == "ERROR:")
			{
				alert(commentReq.responseText);
			}
			else if(commentReq.responseText.substr(0,2) == "OK")
			{
				targetObj.innerHTML = commentReq.responseText.substr(3);
			}
		}
	}
	commentReq.send(formSubmit);
	return false;
}

function commentForm(serverScript, targetID)
{
	
	formReq		= new xmlRequest();
	targetObj	= $(targetID);
	formReq.open("GET", serverScript, true);
	formReq.onreadystatechange = function()
	{
		if (formReq.readyState == 4 && formReq.status == 200)
		{
			oldText	= targetObj.innerHTML;
			targetObj.innerHTML = formReq.responseText;
		}
	}
	formReq.send(null);
	return false;
	
}
function closeForm(targetID)
{
	targetObj	= $(targetID);
	targetObj.innerHTML	= oldText;
	return false;
}
function isValidEmail(email)
{
	email	= email.trim();
	if(email.search(/^[^@\s]+@([-a-z0-9]+\.)+[a-z]{2,}$/i) == -1)
	{
		return false;
	}else{
		return true;
	}
}

function isValidPass(pass)
{
	pass	= pass.trim();
	if(pass.search(/[-a-zA-Z0-9]/) == -1)
	{
		return false;
	}else{
		return true;
	}
}
function $(id) 
{
	return document.getElementById(id);
}

String.prototype.trim = function()
{
	return this.replace(/^\s+|\s+$/g,"");
}

String.prototype.ltrim = function()
{
	return this.replace(/^\s+/,"");
}

String.prototype.rtrim = function()
{
	return this.replace(/\s+$/,"");
}

function xmlRequest()
{
	try
	{
		xmlHttp=new XMLHttpRequest();
	}catch(e){
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
			try
			{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}catch (e){
				alert("Your browser is quite out-dated and does not support AJAX!");
			}
		}
	}
	return xmlHttp;
}

function imgkeep(id) 
{
	$('imgFile-' + id).disabled = true;
}

function imgdelete(id) 
{
	$('imgFile-' + id).disabled = true;
}

function imgchange(id) 
{
	$('imgFile-' + id).disabled = false;
}

function Cookie(name)
{
	this.$name = name;  // Remember the name of this cookie
	var allcookies = document.cookie;
	
	if (allcookies == ""){return;}
	
	var cookies = allcookies.split(';');
	var cookie = null;
	
	for(var i = 0; i < cookies.length; i++)
	{
		cookies[i]	= cookies[i].trim();

		 // Does this cookie string begin with the name we want?
		if(cookies[i].substring(0, name.length+1) == (name + "="))
        	{
			cookie = cookies[i];
			break;
		}
	}

	if (cookie == null){return;}
	var cookieval = cookie.substring(name.length+1);

	var a = cookieval.split('&'); // Break it into an array of name/value pairs
	for(var i=0; i < a.length; i++)
	{
		a[i] = a[i].split(':');
	}

	for(var i = 0; i < a.length; i++)
	{
		this[a[i][0]] = decodeURIComponent(a[i][1]);
	}
}

Cookie.prototype.store = function(daysToLive, path, domain, secure)
{
	var cookieval = "";
	for(var prop in this)
	{
        
		if ((prop.charAt(0) == '$') || ((typeof this[prop]) == 'function')){continue;}
		if (cookieval != ""){cookieval += '&';}
		cookieval += prop + ':' + encodeURIComponent(this[prop]);
	}

	var cookie = this.$name + '=' + cookieval;
	if (daysToLive || daysToLive == 0)
	{
		var date = new Date();
		date.setTime(date.getTime()+(daysToLive*24*60*60*1000));
		cookie += ";expires="+date.toGMTString();
	}

	if (path){cookie += ";path=" + path;}
	if (domain){cookie += ";domain=" + domain;}
	if (secure){cookie += ";secure";}
	
	document.cookie = cookie;
}

Cookie.prototype.remove = function(path, domain, secure)
{
    // Delete the properties of the cookie
    for(var prop in this)
	{
		if (prop.charAt(0) != '$' && typeof this[prop] != 'function')
		delete this[prop];
	}

	// Then, store the cookie with a lifetime of 0
	this.store(0, path, domain, secure);
}

Cookie.enabled = function( )
{

	if (navigator.cookieEnabled != undefined) return navigator.cookieEnabled;
	if (Cookie.enabled.cache != undefined) return Cookie.enabled.cache;

	document.cookie = "testcookie=test; max-age=10000";  // Set cookie

    // Now see if that cookie was saved
	var cookies = document.cookie;
	if(cookies.indexOf("testcookie=test") == -1)
	{
        // The cookie was not saved
        	return Cookie.enabled.cache = false;
	}else{
		// Cookie was saved, so we've got to delete it before returning
		document.cookie = "testcookie=test; max-age=0";  // Delete cookie
		return Cookie.enabled.cache = true;
	}
}

function currFormSubmit(pgForm) 
{
	var curr_form	= pgForm;
	var from_idx	= curr_form.from.selectedIndex;
	var from_value	= curr_form.from.options[from_idx].value;
	var to_idx		= curr_form.to.selectedIndex;
	var to_value	= curr_form.to.options[to_idx].value
	var amount		= curr_form.amount.value;
	if (amount.length == 0)
	{
		alert("Currency Converter:\nPlease input an amount value!");
		return false;
	}
	/*some debugging stuff
	alert("From "+from_value);
	alert("To "+to_value);
	alert("Amount "+amount);
	*/
	res_win	=	window.open('/convert_curr.php?from='+from_value+"&to="+to_value+"&amount="+amount,'conversion_window','status = 1, height = 150, width = 500, resizable = 0');
	if(window.focus){res_win.focus();}
	return false;
}
