﻿function goToURL(strURL) {
	window.location = strURL; 
}
function popup(url,h,w) 
{
	var width  = w;
	var height = h;
	var left   = (screen.width  - width)/2;
	var top    = (screen.height - height)/2;
	var params = 'width='+width+', height='+height;
	params += ', top='+top+', left='+left;
	params += ', directories=no';
	params += ', location=no';
	params += ', menubar=no';
	params += ', resizable=yes';
	params += ', scrollbars=yes';
	params += ', status=no';
	params += ', toolbar=no';
	newwin=window.open(url,'windowname5', params);
	if (window.focus) {newwin.focus()}
	return false;
}
function confirmDelete(delUrl) {
	if (confirm("Are you sure you want to delete")) {
	document.location = delUrl;}
}

function enablefield(fform,ffield,enable)
{
	document.fform.ffield.disabled=enable;
}
function EnableDisable(input1,form1,input2) {
	var Pall = document.getElementById(input1).value;
	var disableButton = false;
	if (Pall == "") disableButton = true;
	document.forms[form1].elements[input2].disabled = disableButton;
}
function toggleField (id) {
  fld = getFieldById(id);
  if (fld.style.display == 'none') {
    fld.style.display = 'block';
  }
  else {
    fld.style.display = 'none';
  }
  return true;
}

function showField (id) {
  fld = getFieldById(id);
  fld.style.display = 'block';
  return true;
}

function hideField (id) {
  fld = getFieldById(id);
  fld.style.display = 'none';
  return true;
}

function getFieldById (fld) {
  var thisdetail;
  if (document.getElementById && document.getElementById(fld) != null) {
    return document.getElementById(fld);
  }
  else if (document.layers && document.layers[fld] != null) {
    return document.layers[fld];
  }
  else if (document.all) {
    return document.all(fld);
  }
  else {
    return true;
  }
}

function resetForm () {
  frm = getFieldById('sform');
  frm.reset();
  return true;
}

function generateCode () {
  // initialize value
  str = '';
  
  // prepend
  str += '<'+'script type="text/javascript"'+'>'+"\n";
  str += '// encoded using mailto encoder by SOLMETRA (www.solmetra.com)'+"\n";
  
  // encode email
  email_fld = getFieldById('email');
  email_str = '';
  pfx = '';
  cnt = email_fld.value.length;
  for (i = 0; i < cnt; i++) {
    chr = email_fld.value.charCodeAt(i) + i;
    email_str += pfx + chr;
    pfx = ',';
  }
  str += 'var spaf_eml = ['+email_str+'];' + "\n";
  
  // encode text
  text_str = '';
  fld = getFieldById('alt_text_cb');
  if (fld.checked) {
    text_fld = getFieldById('alt_text');
    pfx = '';
    cnt = text_fld.value.length;
    for (i = 0; i < cnt; i++) {
      chr = text_fld.value.charCodeAt(i) + i;
      text_str += pfx + chr;
      pfx = ',';
    }
    str += 'var spaf_txt = ['+text_str+'];' + "\n";
  }
  else {
    str += 'var spaf_txt = ['+email_str+'];' + "\n";
  }
  
  // encode subject
  subj_str = '';
  fld = getFieldById('subj_cb');
  if (fld.checked) {
    subj_fld = getFieldById('subj');
    pfx = '';
    cnt = subj_fld.value.length;
    for (i = 0; i < cnt; i++) {
      chr = subj_fld.value.charCodeAt(i) + i;
      subj_str += pfx + chr;
      pfx = ',';
    }
    str += 'var spaf_sub = ['+subj_str+'];' + "\n";
  }
  else {
    str += 'var spaf_sub = [];' + "\n";
  }
  
  // add code to display link
  str += 'document.write(\'<a href=\\"mailto:\');' + "\n";
  str += 'cnt = spaf_eml.length; for (i=0; i<cnt; i++) { document.write(String.fromCharCode(spaf_eml[i]-i)); }' + "\n";
  if (subj_str != '') {
    str += 'document.write(\'?Subject=\');' + "\n";
    str += 'cnt = spaf_sub.length; for (i=0; i<cnt; i++) { document.write(String.fromCharCode(spaf_sub[i]-i)); }' + "\n";
  }
  str += 'document.write(\'\\">\');' + "\n";
  str += 'cnt = spaf_txt.length; for (i=0; i<cnt; i++) { document.write(String.fromCharCode(spaf_txt[i]-i)); }' + "\n";
  str += 'document.write(\'</a>\');' + "\n";
  
  // append
  str += '<'+'/script'+'>' + "\n";
  
  // add noscript
  fld = getFieldById('js');
  str += '<'+'noscript'+'>'+fld.value+'<'+'/noscript'+'>';
  
  // add to result 
  showField('resset');
  res = getFieldById('result');
  res.value = str;
}
/* check Valid Dates & times*/
function checkForm(form)
{
if(!checkDate(form.date1)) return false;
if(!checkDate(form.date2)) return false;
return true;
}
function checkDate(field)
  {
    var allowBlank = true;
    var minYear = 1902;
    var maxYear = (new Date()).getFullYear();

    var errorMsg = "";

    // regular expression to match required date format
    re = /^(\d{1,2})\-(\d{1,2})\-(\d{4})$/;
    
    if(field.value != '') {
      if(regs = field.value.match(re)) {
        if(regs[1] < 1 || regs[1] > 31) {
          errorMsg = "Invalid value for day: " + regs[1];
        } else if(regs[2] < 1 || regs[2] > 12) {
          errorMsg = "Invalid value for month: " + regs[2];
        } else if(regs[3] < minYear || regs[3] > maxYear) {
          errorMsg = "Invalid value for year: " + regs[3] + " - must be between " + minYear + " and " + maxYear;
        }
      } else {
        errorMsg = "Invalid date format: " + field.value;
      }
    } else if(!allowBlank) {
      errorMsg = "Empty date not allowed!";
    }
    
    if(errorMsg != "") {
      alert(errorMsg);
      field.focus();
      return false;
    }
    
    return true;
    }
function checkTime(field)
  {
    var errorMsg = "";

    // regular expression to match required time format
    re = /^(\d{1,2}):(\d{2})(:00)?([ap]m)?$/;
    
    if(field.value != '') {
      if(regs = field.value.match(re)) {
        if(regs[4]) {
          // 12-hour time format with am/pm
          if(regs[1] < 1 || regs[1] > 12) {
            errorMsg = "Invalid value for hours: " + regs[1];
          }
        } else {
          // 24-hour time format
          if(regs[1] > 23) {
            errorMsg = "Invalid value for hours: " + regs[1];
          }
        }
        if(!errorMsg && regs[2] > 59) {
          errorMsg = "Invalid value for minutes: " + regs[2];
        }
      } else {
        errorMsg = "Invalid time format: " + field.value;
      }
    }

    if(errorMsg != "") {
      alert(errorMsg);
      field.focus();
      return false;
    }
    
    return true;
  }
