/*
NAME:		ltrim
PURPOSE:	Remove white spaces from beginning of a string
RETURNs:	String without preceeding white spaces
*/
function ltrim(str){
	var i;

	if(str.length==0)return str;
	
	for(i=0; str.charAt(i)==" "; i++);
	if(i>0){
		rvalue = str.substring(i,str.length);
	}
	else{
		rvalue = str;
	}
	
	return rvalue;
}//end ltrim


/*
NAME:		rtrim
PURPOSE:	remove white spaces from end of a string
RETURNs:	string without trailing white spaces
*/ 
function rtrim(str){
	if(str.length==0)return str;
	
	for(i=str.length-1; str.charAt(i)==' '; i--);
	
	if(i!=str.length){
		rvalue = str.substring(0,i+1);
	}
	else{
		rvalue = str;
	}
	
	return rvalue;
}//rtrim


/*
NAME:		trim
PURPOSE:	remove white spaces from beginning and end of a string
RETURNs:	string without leading or trailing white spaces
*/
function trim(str){
	return rtrim(ltrim(str));
}//trim


/*
NAME:		displaytextcount
PURPOSE:	displays the amount of chracters within a textarea and its limit
RETURNs:	no return value
*/
function displaytextcount(viewobj,obj,max,viewtext){
	if(window.event.keyCode>=37 && window.event.keyCode<=40)return;
	
	if(obj.value.length>=max){
		window.event.keyCode=0;
		obj.value = obj.value.substring(0,max);
		
	}
	
	if(obj.value.length==0){
		viewobj.innerHTML = viewtext + " (" + max + " Characters)";
	}
	else{
		viewobj.innerHTML = viewtext + " (" + obj.value.length + " of " + max + ")";
	}
}

/*
NAME:		inlist
PURPOSE:	Checks if similar text is already in a listbox
RETURNs:	true if text was found or false otherwise
*/
function inlist(listobj,text){
		var rvalue = false;

		for(i=0; i<listobj.options.length;i++){

			if(listobj.options(i).text == text){
				rvalue = true;
				break;
			}
		}

		return rvalue;
	}//inlist

/*
NAME:		addsingleitem
PURPOSE:	Adds a single selected item from one listbox to another
RETURNs:	no return value
*/
	function addsingleitem(obj,tolist,limit,frlist){
		text = obj.value;

		if(text==""){
			if(addlistitem(frlist,tolist,limit)==false){
				alert("Enter an item to add or select an item from the list above.")
				obj.focus();
			}
			return;
		}
		//check if tolist is full
		if (tolist.options.length >= limit){
			alert("The selected list is full. Remove an item from the list and retry");
			return;
		}

		//check if the item is already added to list
		canadd = true;
		for(j=0; j<tolist.options.length; j++){
			if(tolist.options(j).text.toLowerCase() == text.toLowerCase()){
				canadd = false;
			}
		}

		if(canadd == false) {
			alert("The following item was not added because it was previously selected:\n" + text);
			return;
		}

		//if reach this far you can add item to list
		optn = document.createElement("OPTION");
		tolist.appendChild(optn);
		optn.value = text;
		optn.text = text;
		optn.selected = true;

		obj.value="";


	}

/*
NAME:		addlistitem
PURPOSE:	Adds multiple selected items from one listbox to another
RETURNs:	
*/
function addlistitem(frlist,tolist,limit){
		var count = 0;
		var optn;
		var inlist = new String();
		var rvalue = 0;

		inlist = "";
		isselected = false;
		
		for(i=0; i<frlist.options.length; i++){
			if(frlist.options(i).selected==true){
				isselected =true;
				rvalue = 1;
				frlist.options(i).selected=false;
				//check if tolist is full
				if (tolist.options.length >= limit){
					alert("The selected list is full. Remove an item from the list and retry");
					break;
				}

				//check if the item is already added to list
				canadd = true;
				for(j=0; j<tolist.options.length; j++){
					if(tolist.options(j).text == frlist.options(i).text){
						canadd = false;
					}
				}

				if(canadd == false) {
					inlist = inlist + frlist.options(i).text + "\n"
					continue;
				}

				//if reach this far you can add item to list
				optn = document.createElement("OPTION");
				tolist.appendChild(optn);
				optn.value = frlist.options(i).text;
				optn.text = frlist.options(i).text;
				optn.selected = true;
				rvalue =2;
			}

		}

		if(inlist!=""){
			alert("The following items were not added because they were previously selected:\n" + inlist);
		}

		return rvalue;

	}//end addListItem
	
	
	function addlistitemsArtist(frlist,tolist,limit){
		var count = 0;
		var optn;
		var inlist = new String();
		var rvalue = 0;

		inlist = "";
		isselected = false;
		
		for(i=0; i<frlist.options.length; i++){
			if(frlist.options(i).selected==true){
				isselected =true;
				rvalue = 1;
				frlist.options(i).selected=false;
				//check if tolist is full
				if (tolist.options.length >= limit){
					alert("The selected list is full. Remove an item from the list and retry");
					break;
				}

				//check if the item is already added to list
				canadd = true;
				for(j=0; j<tolist.options.length; j++){
					if(tolist.options(j).text == frlist.options(i).text){
						canadd = false;
					}
				}

				if(canadd == false) {
					inlist = inlist + frlist.options(i).text + "\n"
					continue;
				}

				//if reach this far you can add item to list
				optn = document.createElement("OPTION");
				tolist.appendChild(optn);
				optn.value = frlist.options(i).value;
				optn.text = frlist.options(i).text;
				optn.selected = true;
				rvalue =2;
			}

		}

		if(inlist!=""){
			alert("The following items were not added because they were previously selected:\n" + inlist);
		}

		return rvalue;

	}//end addListItem

	function removeListItem(listobj){
		for(i=listobj.options.length-1; i>=0; i--){
			if(listobj.options(i).selected == true){
				listobj.options(i).removeNode();
			}
		}
	}//removeListItem
	
	
	
	function checkemail(email)
	{
	//*************************************************************************
	 
	 
	//----Ensuring There is A @ sign----
	//----Ensuring There is Only One(1) @ sign----
	//----Ensuring That @ sign is Not At Beginning of Address----
	//----Ensuring That There is No Dot(.) Just Before @ Sign----
	//----Ensuring That There is No Dot(.) Just After @ Sign----
	//----Ensuring There is a Dot(.)----
	//----Ensuring There is No @ sign At The End Of Address----
	//----Ensuring There Is No Dot(.) At The Beginning Of Address----
		
		var mychar=new Array('!','#','$','%','&','*','=','+',';','?','/','^','|','(',')','[',']','{','}',':',';','"','<','>',',','~');
	   var mynum = new Array("0","1","2","3","4","5","6","7","8","9");
	
	if(email=="")
	{
	      alert("Invalid Email Address !");
	       return false;
	}
	
	if(email.indexOf("@")==-1 || email.indexOf("@")!=email.lastIndexOf("@") || email.indexOf("@")==0 ||email.indexOf(".")==email.indexOf("@")-1 
	||email.lastIndexOf(".")==email.indexOf("@")+1 || email.indexOf(".")==-1 || email.indexOf("@")==email.length-1)
	{
	  alert("Invalid Email Address !");
	  return false;
	}
	
	//---------Eliminating Spaces Among Characters---------
	if(email.indexOf(" ") < email.length-1)
	{
	 if(email.indexOf(" ")!=-1)
	 {
	   alert("Invalid Email Address !")
	   return false;
	 }
	}
	
	//--------Eliminating Numbers From The Beginning of Email Address--------
	/*for(i=0; i<mynum.length; i=i+1)
	{
	 if(email.indexOf(mynum[i])==0)
	 {
	    alert("Invalid Email Address !")
	    return false;
	 }
	}*///End of for
	//Eliminating Characters That May Not Be Used
	for(i=0;i<mychar.length;i=i+1)
	{
	  if(email.indexOf(mychar[i])!=-1)
	   {
	       alert("Invalid Email Address !")
	       return false;
	   }
	
	}
	
	
	//Eliminating Two Cosecutive Dots
	
	for(i=email.indexOf(".");i<email.length;i++)
	{
	  if(email.charAt(i)==".")
	  {
	   if (email.charAt(i+1)==".")
		 {
	              alert("Invalid Email Address !")
	              return false;
		 } 
		
	//Eliminating Dots At The Beginning And End
	
		 if(i==0)
		  {
		       alert("Invalid Email Address !")
	               return false;
		  }
		  
		  if(i==email.length-1)
		  {
	             alert("Invalid Email Address !")
	             return false;
		  }
		  
	    }//End of outer if 
	 }// End of for 
	
	
 /*	//Checking Number of Characters After last Dot in Email
	
	
	   var str = email.substring(email.lastIndexOf("."),email.length-1 )
	   if(str.length <2 || str.length>3)
	   {
	       alert("Invalid Email Address !")
	      	return false;
	   } 
	     
	
	 if(charAt(email.length-1)==' ')
	   {
	        alert("Invalid Email Address")
	        return false;  
	   } */
	 
	//************************************************************************************
	//if reach this far then everything was ok
	return true;
}

function dataok(obj, msg){
	if(trim(obj.value)==""){
		alert(msg);
		obj.focus();
		
		if(obj.tagName.toUpperCase() == "INPUT")
			obj.select();
		return false;
	}
		
	//if reach this far then everything is ok
	return true;
			
}//end dataok

function selectlistitems(listbox){
	for(i = 0; i<listbox.options.length; i++){
		listbox.options(i).selected = true;
	}//end for
}

function isURL(str){
	var s = new String()
	s = str
	
	//check if space is in url
	if(s.search(" ")>=0){
		return false;
	}
	
	//check for http:// or www.
	if(s.substr(0,7).toLowerCase() != "http://" && s.substr(0,4).toLowerCase() != "www."){
		return false;
	}
	
	//check for '.'
	if(s.indexOf(".",7)<1){
		alert(s.substr(7,s.length).search("."))
		return false;
	}
	
	//if reach this far then url is valid
	return true;
}

function checkusername(username)
	{
			
	var mychar=new Array('!','#','$','@','%','&','*','=','+',';','?','/','^','|','(',')','[',']','{','}',':',';','"','<','>',',','~','.');
	var mynum = new Array("0","1","2","3","4","5","6","7","8","9");
		
	
	//---------Eliminating Spaces Among Characters---------
	if(username.indexOf(" ") < username.length-1)
	{
	 if(username.indexOf(" ")!=-1)
	 {
	   alert("Invalid username!")
	   return false;
	 }
	}
		
	
	//Eliminating Characters That May Not Be Used
	for(i=0;i<mychar.length;i=i+1)
	{
	  if(username.indexOf(mychar[i])!=-1)
	   {
	       alert("Invalid username !")
	       return false;
	   }
	
	}
				
	//Eliminating Dots At The Beginning And End
	
		 if(i==0)
		  {
		       alert("Invalid username !")
	               return false;
		  }
		  
		  if(i==username.length-1)
		  {
	             alert("Invalid username !")
	             return false;
		  }
		  
		 
	return true;
}

