//////////////////HELPER FUNCTIONS ///////////////////////////////////////////
//http://www.somacon.com/p355.php
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 popup(a,width,height,menubar)	{
	if(typeof menubar == "undefined")
		window.open( a, "D1RMRC","status=1,height="+ height +",width="+ width +",resizable=1,scrollbars=1");
	else
		window.open( a, "D1RMRC","status=1,height="+ height +",width="+ width +",resizable=1,scrollbars=1,menubar=1");
}
function toggleButtonDiv(selectbox,divid)
{
	var divobj=document.getElementById(divid);
	var cid=document.getElementById('cid');
	if(selectbox.value !='')
	{
		divobj.style.display='';
		cid.value=selectbox.value;
	} else {
		divobj.style.display='none';
		cid.value='';
	}
}
function addOption(selectbox,text,value )
{
	var optn = document.createElement("OPTION");
	optn.text = text.trim();
	optn.value = value.trim();
	var alreadyAdded=false;
	for(var i=0;i<selectbox.options.length;i++)
	{
		if(selectbox.options[i].value==value && selectbox.options[i].text==text)
		{
			alreadyAdded=true;
			break;
		}
	}
	if(alreadyAdded==false)
		selectbox.options.add(optn);
}

function removeOption(selectbox)
{
	var i;
	for(var i=selectbox.options.length-1;i>=0;i--)
	{
		//if(selectbox.options[i].value != '')
			selectbox.remove(i);
	}
}
function fillOption(selectbox,data)
{
	var data_arr=Array();
	data_arr = data.split('~');
	for (x in data_arr)
	{
		temp = data_arr[x];
		temp_arr = temp.split('^');
		addOption(selectbox,temp_arr[1],temp_arr[0]);
	}
}
//check special characters
function checkSpecialChar(txtValue)  
{ 	
	var iChars = "!@#$%^&*()+=-[]\';,./{}|\":<>?";   
	for (var i = 0; i < txtValue.length; i++) 
	{   
		if (iChars.indexOf(txtValue.charAt(i)) != -1)    
		{   	
			return false;  
		}  
	}
	return true;
} 
//check space characters
function checkSpaceChar(txtValue)  
{ 	
	var iChars = " ";   
	for (var i = 0; i < txtValue.length; i++) 
	{   
		if (iChars.indexOf(txtValue.charAt(i)) != -1)    
		{   	
			return false;  
		}  
	}
	return true;
} 

function removeOneOption(selectbox,index)
{
	selectbox.remove(index);
}

function addAll(source,destination)
{
	var source=document.getElementById(source);
	if(source.options.length > 0)
	{
		var destination=document.getElementById(destination);
		for(var i=0;i<source.options.length;i++)
		{
			var option=source.options[i];
			addOption(destination,option.text,option.value);			
		}
		removeOption(source);
	}
}

function addOne(source,destination)
{
	var source=document.getElementById(source);
	if(source.selectedIndex != -1)
	{
		var destination=document.getElementById(destination);
		var option=source.options[source.selectedIndex];
		
		addOption(destination,option.text,option.value);
		removeOneOption(source,source.selectedIndex);
	}
}
function selectOptionAll(selectboxid)
{
	var obj=document.getElementById(selectboxid);
	for(i=0;i<obj.options.length;i++)
	{
		obj.options[i].selected=true;
	}
}

//////////////////////////////////////////////////////////////////////////
function checkLoginForm()
{
	var required = {
	"email": "Email cannot be left blank",
	"password": "Password cannot be left blank"
	};
	for (var i in required) 
	{
		var el = document.getElementById(i);
		if (!el.value) 
		{
			alert(required[i]);
			el.focus();	
			return false;
		}
	}
}
function checkAvailability()
{
	if(document.getElementById('username').value=='')
	{
		alert('Please enter your email first.');
		document.getElementById('username').focus();
		return false;
	}
	else
	{
		if(!checkSpaceChar(document.getElementById('username').value))
		{
			alert('Email contains space(s). Please remove them.');
			document.getElementById('username').focus();
			return false;	
		}
		popup("available.php?username="+document.getElementById('username').value.trim(),400,200);
		return false;
	}
}
function checkAvailabilityAdmin()
{
	if(document.getElementById('email').value=='')
	{
		alert('Please enter your email first.');
		document.getElementById('email').focus();
		return false;
	}
	else
	{
		if(!checkSpaceChar(document.getElementById('email').value))
		{
			alert('Email contains space(s). Please remove them.');
			document.getElementById('email').focus();
			return false;	
		}
		popup("../available.php?email="+document.getElementById('email').value.trim(),400,200);
		return false;
	}
}
function checkSignupForm()
{
	var required = {
	"fname": "First name cannot be left blank",
	"lname": "Last name cannot be left blank",
	"org": "Organization cannot be left blank",
	"position": "Position cannot be left blank",
	"address": "Address cannot be left blank",
	"city": "City cannot be left blank",
	"state": "Please select a state",
	"zip": "Zipcode cannot be left blank",
	"country": "Please select a country",
	"email": "Email cannot be left blank\nNOTE: This will be your username for login",
	"password": "Password cannot be left blank",
	"password2": "Please retype your password",
	"username": "User Name cannot be left blank"
	};
	for (var i in required) 
	{
		var el = document.getElementById(i);
		if (!el.value) 
		{
			alert(required[i]);
			el.focus();	
			return false;
		}
	}

	evalue=document.getElementById ('email').value;
    if(evalue.length &&(evalue.indexOf("@")==0||evalue.indexOf("@")==-1||evalue.indexOf(".")==-1||evalue.indexOf(".")==0))
    {
        alert("Invalid E-Mail address.\n"+"Format is: abc@domainname.com");
         document.getElementById('email').focus();
         return false;
    }
	
	if(document.getElementById('password').value != document.getElementById('password2').value)
	{
		alert('Passwords do not match. Please retype correct password.');
		document.getElementById('password2').value='';
		document.getElementById('password2').focus();
		return false;
	}
}
function checkSignupFormAdmin()
{
	if(document.getElementById('uid').value=='') //new user
	{
		var required = {
		"fname": "First name cannot be left blank",
		"lname": "Last name cannot be left blank",
		"org": "Organization cannot be left blank",
		"position": "Position cannot be left blank",
		"address": "Address cannot be left blank",
		"city": "City cannot be left blank",
		"state": "Please select a state",
		"zip": "Zipcode cannot be left blank",
		"country": "Please select a country",
		"email": "Email cannot be left blank\nNOTE: This will be your username for login",
		"password": "Password cannot be left blank",
		"password2": "Please retype your password"
		};
		for (var i in required) 
		{
			var el = document.getElementById(i);
			if (!el.value) 
			{
				alert(required[i]);
				el.focus();	
				return false;
			}
		}

		evalue=document.getElementById ('email').value;
		if(evalue.length &&(evalue.indexOf("@")==0||evalue.indexOf("@")==-1||evalue.indexOf(".")==-1||evalue.indexOf(".")==0))
		{
			alert("Invalid E-Mail address.\n"+"Format is: abc@domainname.com");
			 document.getElementById('email').focus();
			 return false;
		}
		
		if(document.getElementById('password').value != document.getElementById('password2').value)
		{
			alert('Passwords do not match. Please retype correct password.');
			document.getElementById('password2').value='';
			document.getElementById('password2').focus();
			return false;
		}

	}
	else //edit user
	{
		var required = {
		"fname": "First name cannot be left blank",
		"lname": "Last name cannot be left blank",
		"org": "Organization cannot be left blank",
		"position": "Position cannot be left blank",
		"address": "Address cannot be left blank",
		"city": "City cannot be left blank",
		"state": "Please select a state",
		"zip": "Zipcode cannot be left blank",
		"country": "Please select a country"
		};
		for (var i in required) 
		{
			var el = document.getElementById(i);
			if (!el.value) 
			{
				alert(required[i]);
				el.focus();	
				return false;
			}
		}

		if(document.getElementById('password').value != '')
		{
			if(document.getElementById('password2').value == '')
			{
				alert('Please retype your password.');
				document.getElementById('password2').focus();
				return false;
			}
			else
			{
				if(document.getElementById('password').value != document.getElementById('password2').value)
				{
					alert('Passwords do not match. Please retype correct password.');
					document.getElementById('password2').value='';
					document.getElementById('password2').focus();
					return false;
				}
			}
		}
		
	}
}
function checkContactForm()
{
	var required = {
	"fname": "First name cannot be left blank",
	"lname": "Last name cannot be left blank",
	"org": "Organization cannot be left blank",
	"position": "Position cannot be left blank",
	"address": "Address cannot be left blank",
	"city": "City cannot be left blank",
	"state": "Please select a state",
	"zip": "Zipcode cannot be left blank",
	"country": "Please select a country"
	};
	for (var i in required) 
	{
		var el = document.getElementById(i);
		if (!el.value.trim()) 
		{
			alert(required[i]);
			el.focus();	
			return false;
		}
	}
}

function checkLoginFormEdit()
{
	var required = {
	"oldpassword": "Old password cannot be left blank",
	"password": "New password cannot be left blank",
	"password2": "Please retype your new password"
	};
	for (var i in required) 
	{
		var el = document.getElementById(i);
		if (!el.value) 
		{
			alert(required[i]);
			el.focus();	
			return false;
		}
	}
	if(document.getElementById('password').value != document.getElementById('password2').value)
	{
		alert('New passwords do not match. Please retype new password correctly.');
		document.getElementById('password2').value='';
		document.getElementById('password2').focus();
		return false;
	}
	
}

function checkAccountForm()
{
	var required = {
	"txtFname": "First name cannot be left blank",
	"txtLname": "Last name cannot be left blank"
	};
	for (var i in required) 
	{
		var el = document.getElementById(i);
		if (!el.value) 
		{
			alert(required[i]);
			el.focus();	
			return false;
		}
	}
}

function checkCategoryForm()
{
	if(document.getElementById('txtCategory').value.trim()=='')
	{
		alert('Category cannot be left blank');
		document.getElementById('txtCategory').focus();
		return false;
	}
}

function checkNewsletterForm()
{
	if(document.getElementById('txtSubject').value.trim()=='')
	{
		alert('Newsletter subject cannot be left blank');
		document.getElementById('txtSubject').focus();
		return false;
	}
}

function checkMailForm()
{
	if(document.getElementById('email').value.trim()=='')
	{
		alert('Email cannot be left blank');
		document.getElementById('email').focus();
		return false;
	}

	evalue=document.getElementById ('email').value;
	if(evalue.length &&(evalue.indexOf("@")==0||evalue.indexOf("@")==-1||evalue.indexOf(".")==-1||evalue.indexOf(".")==0))
	{
		alert("Invalid E-Mail address.\n"+"Format is: abc@domainname.com");
		document.getElementById('email').focus();
		return false;
	}
}

function checkMailingGroupForm()
{
	if(document.getElementById('txtGroup').value.trim()=='')
	{
		alert('Mailing group cannot be left blank');
		document.getElementById('txtGroup').focus();
		return false;
	}
}

function showMailingList(value)
{
	document.location.href='mailing.php?op=list&gid='+value;
}

function checkSendNewsletterForm()
{
	if(document.getElementById('selMail').selectedIndex==0)
	{
		alert('Please select a group mailing list whom you want to sent this newsletter.');
		document.getElementById('selMail').focus();
		return false;
	}	
}