function service_request_toggle_csr(select) { 
	var option = select.options[select.selectedIndex];
	var comment_id = document.getElementById('comments');
	var csr_form_id = document.getElementById('csr_form');

	if(option.value == 35) {
		csr_form_id.style.display='';
		comment_id.style.display='none';
	}
	else {
		csr_form_id.style.display='none';
		comment_id.style.display='';
	}
}
function isValidIP (IPvalue) {
   errorString = "";

   var ipPattern = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
   var ipArray = IPvalue.match(ipPattern);
   var i;

   if (IPvalue == "0.0.0.0")
      errorString += IPvalue+' is a special IP address and cannot be used here.\n';
   else if (IPvalue == "255.255.255.255")
      errorString += IPvalue+' is a special IP address and cannot be used here.\n';
   if (ipArray == null)
      errorString += IPvalue+' is not a valid IP address.\n';
   else {
      for (i = 0; i < 4; i++) {
         thisSegment = ipArray[i];
         if (thisSegment > 255) {
            errorString += IPvalue+' is not a valid IP address.\n';
            break;
         }
      }
   }
   if (errorString == "") {
      return true;
   }
   else {
      alert (errorString);
      return false;
   }
}

function isValidDomain(domain) {
   errorString = "The Domain: '"+domain+"' ";
   var i,j;
   
   // codes for string.charCodeAt() which uses iso-latin-1
   // A-Z is 65 - 90
   // a-z is 97 - 122
   // - is 45
   // . is 46
   // 0-9 is 48 - 57
   
   if(domain.indexOf('.') == 0) { 
      // must not start with dot
      errorString += "must not start with a dot";
      alert(errorString);
      return false;
   } 

	// special case for splat record
	if(domain.indexOf('*') == 0 && domain.length == 1) {
		return true;
	}

//   if(domain.indexOf('.') < 0) { 
//      // must have atleast one dot separating two segments
//      errorString += "must have atleast one dot separating two segments";
//      alert(errorString);
//      return false;
//   }

   // split up into array by dots
   var segments = domain.split('.');

   // for each segment
   for(i=0;i<segments.length;i++) {
      segment = segments[i].toLowerCase(); // simplify checking

      if(segment == "") { 
         // segment must not be empty
         errorString += "must not have an empty label";
         alert(errorString);
         return false;
      }

      // for each character in the segment
      for(j=0;j<segment.length;j++) { 
         var character = segment.charAt(j);
         var code = segment.charCodeAt(j);

         if((code<97 || code>122) && (code<48 || code>57) && code!=45) { 
            // segment may only be a-z, 0-9 and -
            errorString += "must contain only characters 'a-z', '0-9', '.' and '-'";
            alert(errorString);
            return false;
         }

//         if(j==0 && (code<97 || code>122)) {
//            // first character of segment must be a-z
//            errorString += "must have a-z as the first character for each segment of the domain name";
//            alert(errorString);
//            return false;
//         }

         if(j==segment.length-1 && code==45) {
            // last character of segment must not be -
            errorString += "must not have '-' as the last character for a label";
            alert(errorString);
            return false;
         }
      }
   }
   return true
}

function validate(dom_num) {
   var tbody = document.getElementById('domain_'+dom_num);
   var rows = tbody.getElementsByTagName('tr');
   var rowtotal = 0;
   var i;

   // get the number of edit rows to go through
   for(i=0;i<rows.length;i++) {
      if(rows[i].id.indexOf("disp_entry_") >= 0) {
         rowtotal++;
      }
   }

   // go through each edit row
   for(i=0;i<rowtotal;i++) {
      // only look at it if it's being submitted for editting
      if(document.getElementById('editted_'+i).value == "1") {
         input_name = document.getElementById('name_'+i); // grab name
         input_content = document.getElementById('content_'+i); // grab content
         select_type = document.getElementById('select_'+i); // grab type
         input_type = select_type.options[select_type.selectedIndex];

         // No empty values
         if(input_content.value == "") {
            alert("The value column must not be left empty");
            input_content.focus();
            return false;
         }

         if(input_name.value != "" && !isValidDomain(input_name.value)) {
            input_name.select();
            input_name.focus();
            return false;
         }

         // if the type is mx
         if(input_type.value.toLowerCase() == "mx") {
            input_prio = document.getElementById('prio_'+i); // get the prio

            // prio needs to be an integer
            if(isNaN(input_prio.value) || input_prio.value.indexOf(".") >= 0) {
               alert("The priority of an MX record must be an integer");
               input_prio.select();
               input_prio.focus();
               return false;
            }
         }

         // if the type is a
         if(input_type.value.toLowerCase() == "a") {
            // value must look like an IP address
            if(!isValidIP(input_content.value)) {
               input_content.select();
               input_content.focus();
               return false;
            }
         } 
	 else if (input_type.value.toLowerCase() == "cname" || input_type.value.toLowerCase() == "mx") {
            // for other types (mx, cname, but not TXT) value must look like domain name
            if(!isValidDomain(input_content.value)) {
               input_content.select();
               input_content.focus();
               return false;
            }
         }
      }
   }

   if(document.getElementById('added_'+dom_num).value == "1") {
      input_name = document.getElementById('name_add_'+dom_num);
      input_content = document.getElementById('content_add_'+dom_num);
      select_type = document.getElementById('select_add_'+dom_num);
      input_type = select_type.options[select_type.selectedIndex];

      // No empty values
      if(input_content.value == "") {
         alert("The value column must not be left empty");
         input_content.focus();
         return false;
      }

      if(input_name.value != "" && !isValidDomain(input_name.value)) {
         input_name.select();
         input_name.focus();
         return false;
      }

      // if the type is mx
      if(input_type.value.toLowerCase() == "mx") {
         input_prio = document.getElementById('prio_'+i); // get the prio

         // prio needs to be an integer
         if(isNaN(input_prio.value) || input_prio.value.indexOf(".") >= 0) {
            alert("The priority of an MX record must be an integer");
            input_prio.select();
            input_prio.focus();
            return false;
         }
      }

      // if the type is a
      if(input_type.value.toLowerCase() == "a") {
         // value must look like an IP address
         if(!isValidIP(input_content.value)) {
            input_content.select();
            input_content.focus();
            return false;
         }
      } 
      else if (input_type.value.toLowerCase() == "cname" || input_type.value.toLowerCase() == "mx") {
         // for any other standard types (mx, cname) value must look like domain name
         if(!isValidDomain(input_content.value)) {
            input_content.select();
            input_content.focus();
            return false;
         }
      }
      // other type is TXT -- which basically can look like anything, but usually has an '=' somewhere
      // "All printable ASCII characters are permitted in the attribute value."
      // http://tools.ietf.org/html/rfc1464
   }
   return true;
}

function reset_all_rows(table, row_num, dom_num) {
   for(i=0;i<row_num;i++) {
      if(document.getElementById('disp_entry_'+i).className=='domain_'+dom_num) {
         document.getElementById('disp_entry_'+i).style.display='';
         document.getElementById('disp_entry_'+i).style.color='';
         document.getElementById('delete_entry_disp_'+i).checked=false;
         document.getElementById('edit_entry_'+i).style.display='none';
         document.getElementById('editted_'+i).value='0';
      }
   }
   document.getElementById('add_entry_'+dom_num).style.display='none';
   document.getElementById('added_'+dom_num).value='0';
}

function change_add_type(num) {
   var select = document.getElementById('select_add_'+num);
   if(select.options[select.selectedIndex].value.toLowerCase()=="mx") {
      document.getElementById('prio_add_'+num).style.display='';
   } else {
      document.getElementById('prio_add_'+num).style.display='none';
   }
}

function change_type(num) {
   var select = document.getElementById('select_'+num);
   if(select.options[select.selectedIndex].value.toLowerCase()=="mx") {
      document.getElementById('prio_'+num).style.display='';
   } else {
      document.getElementById('prio_'+num).style.display='none';
   }
}

function toggle_delete_row(num) {
   var disp_row = document.getElementById('disp_entry_'+num);
   var edit_row = document.getElementById('edit_entry_'+num);
   var editted = document.getElementById('editted_'+num);
   var checkbox = document.getElementById('delete_entry_disp_'+num);

   if(editted.value=='1') {
      disp_row.style.display='';
      disp_row.style.color="red";
      edit_row.style.display='none';
      editted.value='0';
      checkbox.checked='true';
      document.getElementById('delete_entry_edit_'+num).checked=false;
   }

   if(checkbox.checked) {
      disp_row.style.color="red";
   } else {
      disp_row.style.color="";
   }
}

function toggle_add_new(select_id) {
   if(select_id == '-1') {
      var added = document.getElementById('added_0');
      var add_entry = document.getElementById('add_entry_0');
      var name_add = document.getElementById('name_add_0');
   } else {
      var select = document.getElementById(select_id);
      var added = document.getElementById('added_'+select.selectedIndex);
      var add_entry = document.getElementById('add_entry_'+select.selectedIndex);
      var name_add = document.getElementById('name_add_'+select.selectedIndex);
   }

   // toggle display
   if(add_entry.style.display=='none') {
      add_entry.style.display='';
   } else {
      add_entry.style.display='none';
   }

   // toggle value
   if(added.value=='1') {
      added.value='0';
   } else {
      added.value='1';
   }

   // focus
   name_add.focus();
}

function dnstools_select(select) {
   for(i=0;i<select.length;i++) {
      if(select.options[i].selected) {
         document.getElementById('domain_'+i).style.display = '';
      } else {
         document.getElementById('domain_'+i).style.display = 'none';
      }
   }
}

function toggle_row(num) {
   if(document.getElementById('disp_entry_'+num).style.display=='none') {
      document.getElementById('disp_entry_'+num).style.display='';
      document.getElementById('editted_'+num).value='0';
   } else {
      document.getElementById('disp_entry_'+num).style.display='none';
   }

   if(document.getElementById('edit_entry_'+num).style.display=='none') {
      document.getElementById('edit_entry_'+num).style.display='';
      document.getElementById('delete_entry_disp_'+num).checked=false;
      document.getElementById('disp_entry_'+num).style.color='';
      document.getElementById('editted_'+num).value='1';
      document.getElementById('name_'+num).focus();
   } else {
      document.getElementById('edit_entry_'+num).style.display='none';
   }

//   document.getElementById(flag).value='1';
//   document.getElementById(focus).focus();
}

function showtip(divID) {
document.getElementById(divID).style.visibility = 'visible';
//IE
//document.getElementById('maincontent').style.filter='Alpha(Opacity=30)'; 
//document.getElementById('announcement').style.filter='Alpha(Opacity=30)'; 
//NS6
//document.getElementById('maincontent').style.MozOpacity = '.3';
//document.getElementById('announcement').style.MozOpacity = '.3';
}

function removetip(divID){
//IE
//document.getElementById('maincontent').style.filter='none';
//document.getElementById('announcement').style.filter='none'; 
//NS6
//document.getElementById('maincontent').style.MozOpacity = '1';
//document.getElementById('announcement').style.MozOpacity = '1';

document.getElementById(divID).style.visibility = 'hidden';

}



function fadeOut(element) {
//IE
document.getElementById(element).style.filter='Alpha(Opacity=30)'; 
//NS6
document.getElementById(element).style.MozOpacity = '.3';
}

function fadeIn(divID) {
//IE
document.getElementById(element).style.filter='Alpha(Opacity=100)'; 
//NS6
document.getElementById(element).style.MozOpacity = '1';
}

function switchdisplay(element_off,element_on)
{
  document.getElementById(element_on).style.display ='';
  document.getElementById(element_off).style.display ='none';
}

function changedisplay(element,display)
{
  if (display=='on') 
  {  document.getElementById(element).style.display =''; }
  else
  { document.getElementById(element).style.display ='none';}
}

function displayOff(element)
{
  document.getElementById(element).style.display ='none';
}

function displayOn(element)
{
  document.getElementById(element).style.display ='';
}


function highlightrow(row,checkbox_name)
{
  if (document.getElementById(checkbox_name).checked) 
   {  
     document.getElementById(row).className ='caution'; 
   }
    else
   { document.getElementById(row).className ='';}
}

<!-- Change style class on any object -->
function changeclass(id, newClass) {

identity=document.getElementById(id);

identity.className=newClass;

}

//Use this like onKeyDown="limitTextarea(this.form.textareaname,this.form.countdown,100);"
function limitTextarea(limitField, limitCount, limitNum) {
   if (limitField.value.length > limitNum) {
      limitField.value = limitField.value.substring(0, limitNum);
   } else {
      limitCount.value = limitNum - limitField.value.length;
   }
}

//Use this like onKeyDown="limitTextareaNoCount(this.form.textareaname,100);"
function limitTextareaNoCount(limitField, limitNum) {
   if (limitField.value.length > limitNum) {
      limitField.value = limitField.value.substring(0, limitNum);
   }
}

