function fillrecipient1(elementtype,id,name1)
{

	var noduplication=false;

	noduplication = CheckForDuplication(elementtype,id);			// defined below



	if((elementtype==1) && (noduplication==true))			// user clicked on contact	
	{

		var celement = 'c' + id;
		name1 = name1 + "     <contact>";
		window.document.smsform1.recipientlist.options[window.document.smsform1.recipientlist.options.length]= new Option(name1,celement);
		var nr = parseInt(window.document.smsform1.numrecipients.value);
		nr=nr+1;
		window.document.smsform1.numrecipients.value = nr;
	}
	
	else if((elementtype==2) && (noduplication==true))			// user clicked on group	
	{

		var celement = 'g' + id;
		group_name = name1;
		name1 = name1 + "     <Group>";
		window.document.smsform1.recipientlist.options[window.document.smsform1.recipientlist.options.length]= new Option(name1,celement);
		var nr = parseInt(window.document.smsform1.numrecipients.value);
		nr=nr+1;
		window.document.smsform1.numrecipients.value = nr;
		var alert_msg = "Group Name: " + group_name + " added successfully to recipient list";
		alert(alert_msg);
	}
	else if((elementtype==4) && (noduplication==true))			// user clicked on contact present inside ZapitSMS Users	
	{

		var celement = 'u' + id;
		name1 = name1 + "     <ZapitSMS User>";
		window.document.smsform1.recipientlist.options[window.document.smsform1.recipientlist.options.length]= new Option(name1,celement);
		var nr = parseInt(window.document.smsform1.numrecipients.value);
		nr=nr+1;
		window.document.smsform1.numrecipients.value = nr;

	}





}




//----------------------------------------------------------------------------------------------

function CheckForDuplication(elementtype,id)
{

	var elementsinbox = window.document.smsform1.recipientlist.options.length;
	if(elementtype==1)
	{
		elementtosearch = 'c' + id;
	}
	else if(elementtype==2)
	{
		elementtosearch = 'g' + id;
	}
	else if(elementtype==3)
	{
		elementtosearch = 'm' + id;
	}
	else if(elementtype==4)
	{
		elementtosearch = 'u' + id;
	}
	else if(elementtype==5)
	{
		elementtosearch = 'z' + id;
	}



	for(var i=0;i<elementsinbox;i++)
	{
		if(window.document.smsform1.recipientlist.options[i].value==elementtosearch)
		{
			alert("Selected Recipient is Already in Recipient List");
			return false;
		}
		
	}

	
	return true;

}


//-------------------------------------------------------------------------------------------------

function RemoveFromRecipientList()
{
	//alert(window.document.smsform1.recipientlist.options[window.document.smsform1.recipientlist.selectedIndex].value);
	var index = window.document.smsform1.recipientlist.selectedIndex;
	if(index>=0)
	{
		window.document.smsform1.recipientlist.remove(window.document.smsform1.recipientlist.selectedIndex);
		var nr = parseInt(window.document.smsform1.numrecipients.value);
		nr=nr-1;
		window.document.smsform1.numrecipients.value = nr;
	}

}


//------------------------------------------------------------------------------------------------


function AddMobileNumber()
{


	var tosmsL = window.document.smsform1.tosms.value.length;
	var tosms = window.document.smsform1.tosms.value;

	if(tosmsL<6)
	{
		alert("Please enter mobile number with atleast 6 chars");
		window.document.smsform1.tosms.focus();
		return false;
	}	

	var check1 = true;
	check1 = CheckForInvalidChars(tosms,tosmsL);					// defined below
	
	if(check1==false)
	{
		alert("You have an Invalid char in Mobile Number Field");
		window.document.smsform1.tosms.focus();
		return false;
	}


	var strarray = new Array();
	if(tosmsL>=8)
	{
		tosms = tosms.replace(/\s/g, "");						// remove empty spaces
		strarray = tosms.split(";");					// split string from , into array same as explode

	}
	
	for(var i=0;i<strarray.length;i++)
	{
		//alert(strarray[i]);
		
		noduplication = CheckForDuplication(3,strarray[i]);			// defined below
		
		if((noduplication==true)&& strarray[i].length>=8)			// user entered mobile number or list	
		{

			var celement = 'm' + strarray[i];
			window.document.smsform1.recipientlist.options[window.document.smsform1.recipientlist.options.length]= new Option(strarray[i],celement);
			var nr = parseInt(window.document.smsform1.numrecipients.value);
			nr=nr+1;
			window.document.smsform1.numrecipients.value = nr;

		}
		
	}	
	
	window.document.smsform1.tosms.value="";

}


//-----------------------------------------------------------------------------------------

function ValidateSendSMSForm()
{

	//inValidFlag = false;

	PopulateList();					// defined below - fill contact/groups/mobile numbers in hidden text box
	
	var fullrecipientL = window.document.smsform1.fullrecipientlist.value.length; 
	
	
	//--------- Check Hidden recipient List ------------------------------
	if(fullrecipientL<2)				// no contacts/groups/mobilenumber is in recipient list
	{
		alert("Recipient List Empty - Please Select contacts from contact list or Add Mobile number of Receiver");
		return false;
	}
	

	//-------------- Check For Message Length -----------------------------
	var smsmessageboxL = window.document.smsform1.smsmessagebox.value.length;
	if(smsmessageboxL<1)
	{
		alert("Please Enter SMS Text");
		window.document.smsform1.smsmessagebox.focus();
		return false;	
	}
	


	//--------- Orig Approved Chacking + Originator Settings ----------------------------
	
	var origapproved = window.document.smsform1.oa.value;
	
	if(origapproved=="2")				// if 2 then check originator settings
	{
		
		var origoption = "";
		for (var i=0; i < document.smsform1.originatoroptions.length; i++)
		 {
			 if (document.smsform1.originatoroptions[i].checked) 
			{
				 var origoption = document.smsform1.originatoroptions[i].value;
			 }
		 } 

		if(origoption=="custom")
		{
			
			var origtext = window.document.smsform1.originatortext.value;
			var origtextL = window.document.smsform1.originatortext.value.length;
			
			if(origtextL==0)
			{
				alert("Please Enter Custom Originator");
				window.document.smsform1.originatortext.focus();
				return false;
			}
		
		}


	}


	return true;
		


}



//-------------------------------------------------------------

function PopulateList()
{
 	var elementsinbox = window.document.smsform1.recipientlist.options.length;
 	window.document.smsform1.fullrecipientlist.value="";				// initialization 
	
	for(var i=0;i<elementsinbox;i++)
	{
		var listL = window.document.smsform1.fullrecipientlist.value.length;
		if(listL==0)
		{
			window.document.smsform1.fullrecipientlist.value = window.document.smsform1.recipientlist.options[i].value;			
		}
		else
		{
			window.document.smsform1.fullrecipientlist.value = window.document.smsform1.fullrecipientlist.value + "," + window.document.smsform1.recipientlist.options[i].value;	
		}
		
	}

	
}





//----------------------------------------------------------------------

function CheckForInvalidChars(value,len_value)
{

	for(i=0;i<len_value;i++)
	{
		var c = value.charAt(i);
		if((c=='+')||(c==' ')||(c==';')||(c==1)||(c==2)||(c==3)||(c==4)||(c==5)||(c==6)||(c==7)||(c==8)||(c==9)||(c==0))
		{
			continue;
		}
		else
		{
			return false;
		}

	}	

	return true;

}