﻿function MIT(groupid,groupname){
 var GroupName =  getElementReference('GroupName');
 	GroupName.value = groupname;
}

function IsIDAdded(ID){
  var Recepients =  getElementReference('Recepients');
  var ret=false;
  for (var i=0;i<Recepients.length;i++){
    if (ID==Recepients.options[i].text)
      ret=true;
  }
  return ret;
}

function AddToTheList(){//1
	var MyContacts =  getElementReference('MyContacts');
	var Recepients =  getElementReference('Recepients');
	var GroupList  =  getElementReference('GroupIDList');
	var TextMessage=  getElementReference('TextMessage');

  if ( (MyContacts.selectedIndex < 0)  ){//2
    alert("Please select a group from the list first");
	alert("الرجاء اختيار المجموعة من القائمة");
  if (MyContacts.options[0] != null)
    MyContacts.options[0].selected = true;
    MyContacts.focus();
    return false;
  }//END OF 2
  else{ //3
    if (!IsIDAdded(MyContacts.options[MyContacts.selectedIndex].text))
	{ //4
      Recepients.options[Recepients.length] = new Option(MyContacts.options[MyContacts.selectedIndex].text,MyContacts.options[MyContacts.selectedIndex].value);
	}// END OF 4
	  /*if(GroupList.value=="")
	  	GroupList.value=MyContacts.options[MyContacts.selectedIndex].value;
	  else
	  	GroupList.value+=","+MyContacts.options[MyContacts.selectedIndex].value;*/
		//TextMessage.submit();

	} //END OF 3
}  //END OF 1

function RemoveFromTheList(){
var Recepients =  getElementReference('Recepients');
  if (Recepients.selectedIndex < 0){
	alert("الرجاء اختيار المستقبل من القائمة");
    if (Recepients.options[0] != null)
      Recepients.options[0].selected = true;
    Recepients.focus();
    return false;
  }
  else
    Recepients.options[Recepients.selectedIndex] = null;
}

function HandleSubmit(){
var Recepients =  getElementReference('Recepients');
var GroupList =  getElementReference('GroupIDList');
 	GroupList.value="";
  var SelectedList = "";
  for(var i = 0; i < Recepients.length; i++) {
	  if(GroupList.value=="")
	  	GroupList.value= (Recepients.options[i].value);
	 else{
		 if (Recepients.options[i].value!="")
		 	GroupList.value+= ","+(Recepients.options[i].value);
	 }
 }
  
  if(!validateExtraNumbers())
		{shomsg('خطأ في رقم/ارقام الخلوي');return false;}
	return true;
  //SelectedList = SelectedList.substring(0, SelectedList.length-1);
  //GroupIDList.value = SelectedList;
}



function validateExtraNumbers(){
var PhoneNumber =  getElementReference('PhoneNumber');
  var phoneNos = PhoneNumber.value.split(",");
  

    for(var i = 0; i < phoneNos.length; i++) {
    if(!validate(phoneNos[i]))
		return false;
		}
  return true;
}

function validate(n){
	
	if(n=="")
		return true;
	if(!isMobileNumber(n) ){
	//shomsg(n+' Invalid Mobile Number');
	//recipient.focus();
	return false;
	}
	
	return true;
}
