/*------------------ global js (Front End) ----------------------------*/

/*--------------- Change style css ----------------------*/
function celOver(obj) {
  var s = obj.className;
  s = s.replace(/-over/g, "");
  obj.className = s+"-over";
}

function celOut(obj) {
  var s = obj.className;
  s = s.replace(/-over/g, "");
  obj.className = s;
}

function verifEmail(chp) {
	
  var returnString = true;
  regex = new RegExp("^[a-z0-9_]([.-]?[a-z0-9_]+)+@[a-z0-9_]([.-]?[a-z0-9_]+)+\.([a-z]{2,4}|[a-z]{6})$", "gi");
  if (!regex.exec(chp)) {
     returnString = false;
  }
  return returnString
}

function chiffreok(valeur) {
	if (isNaN(valeur)) {
		return false;
	} else {
		return true;
	}
}

/*------------- Search field ----------------*/
function searchinfo() {
  var errors = '';
  var form = document.SearchFrm
  if (form.searchfield.value == "Search" || form.searchfield.value == "") {
    errors = "True";
  }
  if (errors) {
    //alert('The following error(s) occurred:\n'+errors);
  }
  document.MM_returnValue = (errors == '');
}

function searchfocus(val) {
  if (val.value == "Search") {
    val.value = "";
  }
}

function searchblur(val) {
  if (val.value == "") {
    val.value = "Search";
  }
}


/*-------gallery system (item)----------*/
function popup(idimg,width,height) { 
  xp = (screen.width-width)/2;
  if (xp <= 0) {
    xp = 0;
  }
  yp = 0;

  fen = window.open(idimg,'','toolbar=0,location=0,directories=0,status=0,scrollbars=0,resizable=0,copyhistory=0,menuBar=0,top='+yp+',left='+xp+',width=' + width + ',height=' + height);
  fen.focus(); 
}

/*-------gallery system (itempopup)----------*/
function checksize() {
 if (document.images[0].complete) {
  window.resizeTo(document.images[0].width+12,document.images[0].height+30);
  window.focus();
 }
}

/*------------------end global js----------------------------*/


/*------------------Validate Form Upload----------------------------*/
function VerifyUpload() {

  var form = document.form1;
  var errors = "";
  
  if (form.RespFile.value.length == 0) {
	errors += "- A File is Required\n";
  }
  
  if (errors) {
    alert('The following error(s) occurred:\n'+errors);
  }
  document.MM_returnValue = (errors == '');
	
}

/*------------------Validate Publication----------------------------*/
function VerifyPublication() {

  var form = document.form1;
  var errors = "";
  
  if (form.Title.value.length == 0) {
	errors += "- Title is required\n";
  }
  
  if (form.ShortDesc.value.length == 0) {
	errors += "- Short Desc. is required\n";
  }
  
  if (errors) {
    alert('The following error(s) occurred:\n'+errors);
  }
  document.MM_returnValue = (errors == '');
	
}
/*----------Validate Publication------------*/
function MM_validatePublication() {
	var form = document.form1;
	var errors = "";
  
    if (form.Title.value.length == 0) {
	  errors += "- Title is required\n";
    }
  
    if (form.ShortDesc.value.length == 0) {
	  errors += "- Short Desc. is required\n";
    }
  
    if (errors) {
      alert('The following error(s) occurred:\n'+errors);
    }
    document.MM_returnValue = (errors == '');
	
}

/*------------------Validate NoticeBoard Object----------------------------*/
function VerifyNoticeBoard() {

  var form = document.form1;
  var errors = "";
  
  if (form.Subject.value.length == 0) {
	errors += "- Subject is required\n";
  }

  if (form.code.value.length == 0) {
	errors += "- Word Verification is required\n";
  }

  if (errors) {
    alert('The following error(s) occurred:\n'+errors);
  }
  document.MM_returnValue = (errors == '');
	
}

/*--------------------- MemberADD -------------------*/
function MM_validateMemberAdd() {

  var form = document.form1;
  var errors = "";
  var coche;
 
  if (form["Name"]) {
    if (form.Name.value.length == 0) {
	  errors += "- Screen Name is required\n";
    }
  }

  if (form.ContactFirstName.value.length == 0) {
	errors += "- First Name is required\n";
  }
  
  if (form.ContactLastName.value.length == 0) {
	errors += "- Last Name is required\n";
  }

  if (form.ContactAddress.value.length == 0) {
	errors += "- Address is required\n";
  }

  if (form.CountyID.selectedIndex == 0) {
	errors += "- County is required\n";
  }

  if (form.ContactTel.value.length == 0) {
    errors += "- Phone Number is required\n";
  } else {
    /*
    if ((checkInternationalPhone(form.ContactTel.value)==false)){
      errors += "-Please Enter a Valid Phone Number\n";
    }
  */
  }

  if (form.ContactEmail.value.length == 0) {
    errors += "- Email is required\n";
  } else {
    if(form.ContactEmail.value.indexOf(".") ==-1 || form.ContactEmail.value.indexOf("@") ==-1){
	  errors += "- Email is invalid\n";
    }
  }  


  if (form.ContactEmail2) {
     if (form.ContactEmail2.value.length == 0) {
       errors += "- Repeat Email is required\n";
     } else {
       if (form.ContactEmail.value != form.ContactEmail2.value) {
	     errors += "- Email do not match\n";
       }
     }
  }

  if (form.list1ID.selectedIndex == 0) {
	errors += "- Hospital is required\n";
  }

  if (form.list2ID.selectedIndex == 0 && form.Other.value.length == 0) {
	errors += "- Grade is required\n";
  }

  coche=0;
  totalRadio = document.getElementsByName("headDept").length;
  for (i=0; i< totalRadio; i++){
    if (document.getElementsByName("headDept")[i].checked == true) {
      coche++;
      break;
    }
  }

  if (coche == 0) {
	errors += "- \"Are you the Head of the Department?\" is required\n";
  }

  if (form.Username.value.length == 0) {
	errors += "- Username is required\n";
  }

  var flgpass = true;
  if (form.Password.value.length < 6) {
	errors += "- Password must be 6 or more characters long\n";
    flgpass = false;
  }

  if (form.Password2) {
    if (form.Password2.value.length == 0) {
	  errors += "- Repeat Password is required\n";
      flgpass = false;
    }

  if (form.code) {
    if (form.code.value.length == 0) {
	  errors += "- Word Verification is required\n";
    }
  }

    if (form.Password.value != form.Password2.value && flgpass) {
	  errors += "- Passwords do not match\n";
    }
  }

  if (form.Impact) {
    if (!form.Impact.checked) {
      errors += "- I am a member of impact is required\n"; 
    }
  }

  if (form.PartA) {
    if (!form.PartA.checked) {
      errors += "- I Agree to the Terms & Conditions is required\n"; 
    }
  }

  if (form.list3ID) {
    var coche=0;
    totalRadio = document.getElementsByName("list3ID").length;
    for (i=0; i< totalRadio; i++){
      if (document.getElementsByName("list3ID")[i].checked == true) {
        coche++;
        break;
      }
    }

    if (coche == 0) {
	  errors += "- Fee is required\n";
    }
  }


  if (form.ExpireDate) {
    if (form.ExpireDate.value.length == 0) {
      errors += "- Expire Date is required\n";
    }
  }


  if (errors) {
    alert('The following error(s) occurred:\n'+errors);
  }
  document.MM_returnValue = (errors == '');
	
}

/*------------------------------Member Edit-------------------------------------------------------------------------*/

function MM_validateMemberEditAccount() {

  var form = document.form1;
  var errors = "";
  var coche;
 
  if (form.ContactFirstName.value.length == 0) {
	errors += "- First Name is required\n";
  }
  
  if (form.ContactLastName.value.length == 0) {
	errors += "- Last Name is required\n";
  }

  if (form.ContactAddress.value.length == 0) {
	errors += "- Address is required\n";
  }

  if (form.CountyID.selectedIndex == 0) {
	errors += "- County is required\n";
  }

  if (form.ContactTel.value.length == 0) {
    errors += "- Phone Number is required\n";
  } else {
    /*
    if ((checkInternationalPhone(form.ContactTel.value)==false)){
      errors += "-Please Enter a Valid Phone Number\n";
    }
  */
  }

  if (form.ContactEmail.value.length == 0) {
    errors += "- Email is required\n";
  } else {
    if(form.ContactEmail.value.indexOf(".") ==-1 || form.ContactEmail.value.indexOf("@") ==-1){
	  errors += "- Email is invalid\n";
    }
  }  


  if (form.ContactEmail2) {
     if (form.ContactEmail2.value.length == 0) {
       errors += "- Repeat Email is required\n";
     } else {
       if (form.ContactEmail.value != form.ContactEmail2.value) {
	     errors += "- Email do not match\n";
       }
     }
  }

  if (form.list1ID.selectedIndex == 0) {
	errors += "- Hospital is required\n";
  }

  if (form.list2ID.selectedIndex == 0 && form.Other.value.length == 0) {
	errors += "- Grade is required\n";
  }

  if (errors) {
    alert('The following error(s) occurred:\n'+errors);
  }
  document.MM_returnValue = (errors == '');

}


/*---------------------------Verify Phone Number and Internation, Min of 5 digits accepted--------------------------*/
var digits = "0123456789";
var phoneNumberDelimiters = "()- ";
var validWorldPhoneChars = phoneNumberDelimiters + "+";
var minDigitsInIPhoneNumber = 5;

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone){
  s=stripCharsInBag(strPhone,validWorldPhoneChars);
  return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

/*--------------------- MemberADD -------------------*/
function MM_validateMemberEdit() {

  var form = document.form1;
  var errors = "";
  
  if (form.Name.value.length == 0) {
	errors += "- Name is required\n";
  }

  if (form.ContactFirstName.value == 0) {
	errors += "- First Name is required\n";
  }
  
  if (form.ContactLastName.value == 0) {
	errors += "- Last Name is required\n";
  }

  if (form.ContactAddress.value == 0) {
	errors += "- Address is required\n";
  }


  if ((form.CountyID.value==1)||(form.CountyID.value=="")) {
	errors += "- County is required\n";
  }
  
  if ((form.ContactTel.value==null)||(form.ContactTel.value=="")){
    errors += "-Please Enter your Phone Number\n";
  }
  
  if ((checkInternationalPhone(form.ContactTel.value)==false)){
    errors += "-Please Enter a Valid Phone Number\n";
  }
  
  if ((form.ContactEmail.value==null)||(form.ContactEmail.value=="")){
    errors += "-Please Enter your Email address\n";
  }
	
  if(form.ContactEmail.value.indexOf(".") ==-1 || form.ContactEmail.value.indexOf("@") ==-1){
	errors += "-Email is invalid\n";
  }
  
  if (errors) {
    alert('The following error(s) occurred:\n'+errors);
  }
  document.MM_returnValue = (errors == '');
	
}


// Function to alternate the colours on rows of a table
function alternate(id){
 if(document.getElementsByTagName){  
   var table = document.getElementById(id);  
   var rows = table.getElementsByTagName("tr");  
   for(i = 0; i < rows.length; i++){          
 //manipulate rows
     if(i % 2 == 0){
       rows[i].className = "even";
     }else{
       rows[i].className = "odd";
     }
   }
 }
}



/*-----------------verify Conference Registration---------------------*/
function verifReg() {
  var form = document.form1;
  var errors = "";

  var coche=0; 
  var totalRadio;
  coche=0;
  totalRadio = document.getElementsByName("radiochoice").length;
  for (i=0; i< totalRadio; i++){
    if (document.getElementsByName("radiochoice")[i].checked == true) {
      coche++;
      break;
    }
  }

  if (coche == 0) { 
    errors += "- 'Tick as appropriate' is required\n";
  } 

  var i=1;
  var elem;
  var flagElem = true;
  var flagLoop = false;
  while (elem = document.getElementById("Workshop"+i)) {
    flagLoop = true;
    if (elem.value > 0) {
      flagElem = false;
      break;
    }
    i++;
  }

  if (flagElem && flagLoop) {
    errors += "- Workshop session is required\n";
  }

  if (!form.impact.checked) {
    errors += "- Member of Impact is required\n";
  }

  if (errors) {
    alert('The following error(s) occurred:\n'+errors);
  }
  document.MM_returnValue = (errors == '');
}

function verifDoublons(val,elem) {
  var form = document.form1;
  var tab=val.split("-");
  var flag = false;
  for (i=tab[0]; i<=tab[1]; i++) {
     //alert(form.elements["Workshop"+i].value);
     for (j=tab[0]; j<=tab[1]; j++) {
       if (i != j && (form.elements["Workshop"+i].value == form.elements["Workshop"+j].value && form.elements["Workshop"+i].value != 0)) {
         flag = true;
         break;
       }
     }
  }  

  if (flag) {
    alert("The following error occurred:\n- You have already selected this workshop for this session, please enter another selection");
    elem.options[0].selected = true;
  }
}

function verifNumber(elem) {
  var val = elem.value;
  if (val != "") {
      if (!chiffreok(val)) {
         elem.value = "";
         alert("The following error occurred:\n- The value must be a Number");
         elem.focus();
      } else {
         var n = val.indexOf(".");
         if (n != -1) {
           elem.value = "";
           alert("The following error occurred:\n- The value must be an Interger");
           elem.focus();
         }
      }
  }
  calcTotal();
}

function calcTotal() {
  var form = document.form1;
  var totalVal = 0;
  var show = false;
  var bouton = document.getElementsByName("radiochoice");
  for (var i=0; i<bouton.length;i++) {
    if (bouton[i].checked) {
      if (form.elements["radioval"+eval(bouton[i].value)]) {
        totalVal +=  eval(form.elements["radioval"+eval(bouton[i].value)].value);
      }
      break;
    }
  }

  var v = 1;
  while (form.elements["ExtraVal"+v]) {
    if (form.elements["ExtraVal"+v].value != "") {
	   if (form.elements["ExtraPriceStart"+v].value < form.elements["ExtraVal"+v].value) {
          totalVal +=  ((form.elements["ExtraVal"+v].value - form.elements["ExtraPriceStart"+v].value)*form.elements["ExtraPrice"+v].value);
       }
    }
    v += 1;
  }

  form.Total.value = totalVal;
}


function MM_validatePeriodFee() {

  var form = document.form1;
  var errors = "";

  var coche=0;
  totalRadio = document.getElementsByName("list3ID").length;
  for (i=0; i< totalRadio; i++){
    if (document.getElementsByName("list3ID")[i].checked == true) {
      coche++;
      break;
    }
  }

  if (coche == 0) {
	errors += "- Fee is required\n";
  }

  if (errors) {
    alert('The following error(s) occurred:\n'+errors);
  }
  document.MM_returnValue = (errors == '');
}