
var request = false;

try
{
  request = new XMLHttpRequest();
} 
catch (trymicrosoft)
{
	try
	{
		request = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (othermicrosoft)
	{
		try
		{
			request = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (failed)
		{
			request = false;
		}
	}
}

if (!request) alert("Error initializing XMLHttpRequest!");


/***********************************************
* Hide / show block
***********************************************/

function toggle_visibility(id) 
{
	var e = document.getElementById(id);
   	if(e.style.display == 'block')
		e.style.display = 'none';
   	else
    	e.style.display = 'block';
}

/***********************************************
* Validation - User Registration
***********************************************/
function validateUser(login,mode) 
{
	var msg='';
	
	if ( isblank('txtFirstName') || isblank('txtLastName') || isblank('txtLogin') ||
	 	isblank('txtEmail') )
	{
		msg ='<font color=\'red\'>- Please enter User Name, Email, '
		msg += 'First Name, Last Name !</font><br/>';
	}
	
	if (mode == 'new')
	{
		if ( isblank('txtPassword') || isblank('txtConfirmPassword') )
		{
			msg += '<font color=\'red\'>- Please enter Password and Confirm Password!</font><br/>';
		}	
		else
		{
			if (!confirmpassword('txtPassword','txtConfirmPassword','')) 
			{
				msg += '<font color=\'red\'>- Passwords entered do not match!</font><br/>';
			}
		}
	
		isvalidlogin('txtLogin');
	
		if (document.getElementById('txtInvalid').value == "1") 
		{
				msg += '<font color=\'red\'>- User Name is already in use!</font><br/>';
		} 
	}
	
	if (!checkmail('txtEmail'))
 	{
		msg += '<font color=\'red\'>- Please enter a valid email!</font><br>';
	}
	
	if (msg != '')
	{
		document.getElementById('divError').innerHTML = '<font color=\'red\'><b>Error:</b></font><br>' + msg;
		document.getElementById('divError').style.display='Block';
		return false;
	}
	else
	{
		return true;
	}
}

/***********************************************
* Validation -Event Registration
******************************************/
function validateEvent() 
{
	var msg='';
	
	if ( isblank('txtTitle') )
	{
		msg ='<font color=\'red\'>- Please enter Title!</font><br/>';
	}	
	
	if (!isblank('txtEmail1') && !isblank('txtEmail2'))
	{
		if (!checkmail('txtEmail1') || !checkmail('txtEmail2'))
	 	{
			msg += '<font color=\'red\'>- Please enter a valid email!</font><br>';
		}
	}
	
	if (!isselected('ddlCategory'))
	{
		msg += '<font color=\'red\'>- Please select a Category!</font><br>';
	}

	if (msg != '')
	{
		document.getElementById('divError').innerHTML = '<font color=\'red\'><b>Error:</b></font><br>' + msg;
		document.getElementById('divError').style.display='Block';
		return false;
	}
	else
	{
		return true;
	}
}

/***********************************************
* Validation -Content Registration
******************************************/
function validateContent() 
{
	var msg='';
	
	if ( isblank('txtTitle') )
	{
		msg ='<font color=\'red\'>- Please enter Title!</font><br/>';
	}	
	if (!isselected('ddlCategory'))
	{
		msg += '<font color=\'red\'>- Please select a Category!</font><br>';
	}
	if (msg != '')
	{
		document.getElementById('divError').innerHTML = '<font color=\'red\'><b>Error:</b></font><br>' + msg;
		document.getElementById('divError').style.display='Block';
		return false;
	}
	else
	{
		return true;
	}

}
/***********************************************
* Validation - dropdown selected
***********************************************/
function isselected(obj)
{
	var objvalue = document.getElementById(obj).value;
	if (objvalue != "0")
	{
		
		return true;
	}
	
	return false;
}

/***********************************************
* Validation - empty
***********************************************/
function isblank(obj)
{
	var objvalue = document.getElementById(obj).value;
	if (objvalue == '')
	{
		
		return true;
	}
	
	return false;
}

/***********************************************
* Validation - Password and Confirm Password
***********************************************/
function confirmpassword(obj1, obj2, err)
{
	if (err !=''){var err = document.getElementById(err);}

	if ( isblank(obj1) || isblank(obj2) )
	{
		if (err !='')
		{
			err.innerHTML="<p>Passwords are blank!  Please enter new and confirm passwords.</p>";
			err.style.display='Block';
			err.style.color ='red';
		}
		return false;
	}
	
	if	(document.getElementById(obj1).value != document.getElementById(obj2).value)
	{
		if (err !='')
		{
			err.innerHTML="<p>Passwords entered do not match.</p>";
			err.style.display='Block';
			err.style.color ='red';
		}
		return false;
	}
	return true;
}

/***********************************************
* Email Validation script- © Dynamic Drive (www.dynamicdrive.com)
* This notice must stay intact for legal use.
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/
var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i

function checkmail(obj)
{
	var objvalue = document.getElementById(obj).value;
	var returnval=emailfilter.test(objvalue)
	return returnval
}

/***********************************************
can Delete object?
***********************************************/
function confirmDelete(prompt,delUrl)
{
	if (prompt != "Password")
	{
		msg = "Are you sure you want to delete this " + prompt + " ?";
	}
	else
	{
		msg = "Are you sure you want to reset this " + prompt + " ?";
	}
	
	if (confirm(msg)) 
	{
    	document.location = delUrl;
  	}
}

/***********************************************
* AJAX functions
***********************************************/

function isvalidlogin(obj)
{
	var login = document.getElementById(obj).value;
	var url = "../../includes/checklogin.php?login=" + login;
	request.open("GET", url, true);
	request.onreadystatechange = updatePage;
	request.send(null);
}

function updatePage()
{
	if (request.readyState == 4)
	{
		if(request.status == 200)
		{
			if ( request.responseText != 0) 
			{
				document.getElementById('txtInvalid').value = "1";
			}
			else
			{
				document.getElementById('txtInvalid').value = "0";
			}
		}
	}
}

var ajax = new Array();

function getSubCategories(sel)
{
	var category = sel.options[sel.selectedIndex].value;
	document.getElementById('ddlSubCategory').options.length = 0;	
	if(category.length>0)
	{
		var index = ajax.length;
		ajax[index] = new sack();
		
		ajax[index].requestFile = '../../includes/getSubCategories.php?category='+category;	
		ajax[index].onCompletion = function(){ createSubCategories(index) };	
		ajax[index].runAJAX();	
	}
}

function createSubCategories(index)
{
	var obj = document.getElementById('ddlSubCategory');
	eval(ajax[index].response);		
}	
