function pup(str)

{

 alert(str);

}



function urlDomain(url)

{

  prefix = url.substring(0,5);

  switch (prefix)

  {

    case 'http:':

      prefixStripped = url.substring(7);

      fullPrefix = 'http://';

      break;



    case 'https':

      prefixStripped = url.substring(8);

      fullPrefix = 'https://';

      break;



    default:

      prefixStripped = url;

      fullPrefix = 'http://';

      break;

  }



  firstSlash = prefixStripped.indexOf('/');

  domain = fullPrefix + (prefixStripped.substring(0,firstSlash));

  return domain;

}



function permitOnly (permissibleString, haystack)

{

  aryHaystack = haystack.split('');

  returnInt = true;



  for (x = 0; x < aryHaystack.length; x++)

  {

    tmpIndex = permissibleString.indexOf(aryHaystack[x]);

    if (tmpIndex == -1)

    {

      returnInt = false;

      break;

    }

  }

  return returnInt;

}



function strlen(str)

{

  return str.legnth;

}



function iv_parseInt(stringNumber)

{

  raw = stripLeadingZero(stringNumber);

  switch (raw)

  {

    case '8':

      parsedNumber = 8;

      break;

    case '9':

      parsedNumber = 9;

      break;

    default:

      parsedNumber = parseInt(raw);

      break;

  }

  return parsedNumber;

}



function stripLeadingZero(stringNumber)

{

  for (x = 0; x < stringNumber.length; x++)

  {

    if (stringNumber.charAt(x) != '0')

    {

      break;

    }

  }

  noZeroNumber = stringNumber.substring(x);

  if (noZeroNumber.length == 0)

  {

    return 0;

  }

  else

  {

    return noZeroNumber;

  }

}



function str_replace(needle, newNeedle, haystack)

{

  maxLoop = haystack.length;

  newNeedleLength = newNeedle.length;

  needleLength = needle.length;



  x = 0;



  proceed = true



  while (proceed)

  {

    //if this is the needle we're looking for...

    if (haystack.substring(x, (x + needleLength)) == needle)

    {

      //get the substring before this character (watch out for the first character)

      if (x == 0)

      {

        preString = '';

      }

      else

      {

        preString = haystack.substring(0, x);

      }



      //get the substring after this character (watch out for the last character

      if (x == (haystack.length - needleLength))

      {

        postString = '';

      }

      else

      {

        postString = haystack.substring(x + needleLength);

      }



      //build new haystack

      haystack = preString + newNeedle + postString;



      x = x + newNeedleLength;



      //if the newNeedle is 0 characters long, the haystack is shorter, so run the search for charAt(x) again

      if (newNeedleLength == 0)

      {

        if (x != 0)

        {

          x = x - 1;

        }

      }

    }



    //Move along

    x = x + 1;



    //if we're shortened the haystack so that the new length is less than the original, and we're at the end of the new, then break.

    if (haystack.length < x)

    {

      proceed = false;

    }

    else

    {

      proceed = true;

    }



  }



  //return the new haystack

  return haystack;

}



function trim(sString)

{

  //alert('{'+sString+'}');

  trimString = lTrim(rTrim(sString));

  return trimString;

}



function rTrim(str)

{

  var sString = str;

  while (sString.substring(sString.length-1, sString.length) == ' ')

  {

    sString = sString.substring(0,sString.length-1);

  }

  return sString;

}



function lTrim(sString)

{

  while (sString.substring(0,1) == ' ')

  {

    sString = sString.substring(1, sString.length);

  }

  return sString;

}



function strtolower(str)

{

  return str.toLowerCase();

}



function strtoupper(str)

{

  return str.toUpperCase();

}



function findDropDownText(dropDownObject, lookForText)

{

  textFound = -1;



  targetText = lookForText.toLowerCase();



  dropDownLength = dropDownObject.length;



  for (x = 0; x < dropDownLength; x++)

  {

    currentText = dropDownObject[x].text;

    arrowText = currentText.toLowerCase();



    if (arrowText == targetText)

    {

      textFound = x;

      break;

    }

  }



  return textFound;

}



function enDisRadio(radioObject, clickedValue, thisForm, targetObjectPrefix) //(rdoTest, 'cats', 'document.frmTest', 'slt')

{

  //get number of elements in object

  objectLength = radioObject.length;

  //alert (objectLength);

  for (x = 0; x < objectLength; x++)

  {

    thisRadioValue = radioObject[x].value;

    changeObject = eval(thisForm+"."+targetObjectPrefix+thisRadioValue);

    //alert(changeObject);



    if (radioObject[x].value == clickedValue)

    {

      changeObject.disabled = false;

    }

    else

    {

      changeObject.disabled = true;

    }

  }

}



function clearMultipleSelect(obj)

{

  objLength = obj.length;

  for (x = 0; x < objLength; x++)

  {

    obj[x].selected = false;

  }

}



function getMultipleSelect(obj)

{

  optCount = obj.length;

  selectedIDs = '';

  selectedCount = 0;

  for (x = 0; x < optCount; x++)

  {

    if (obj.options[x].selected == true)

    {

      selectedIDs = selectedIDs + obj.options[x].value + ',';

      selectedCount = selectedCount + 1;

    }

  }

  if (selectedCount > 0)

  {

    selectedIDs = selectedIDs.substring(0, (selectedIDs.length - 1));

  }

  else

  {

    selectedIDs = 'noSelection';

  }

  //alert(selectedIDs);

  return selectedIDs;

}



function refreshPage()

{

  location.reload(true);

}



function checkTextLength(thisObject, targetID, maxLength) //use onKeyUp event for this function

{

  currText = thisObject.value;

  textLength = currText.length;

  charMsg = maxLength - textLength;

  if (charMsg >= 0)

  {

    document.getElementById(targetID).innerHTML = charMsg;

  }

  else

  {

    thisObject.value = currText.substring(0, maxLength);

    textLength = thisObject.value.length;

    charMsg = maxLength - textLength;

    document.getElementById(targetID).innerHTML = charMsg;

    alert('The input data is only permitted to be '+maxLength+' characters in length\n\nThe input data has been reduced to '+maxLength+' characters');

  }

}



function showSuccess(changeType)

{

  showNothing = 0;

  switch (changeType)

  {

    case 'loginFail':

      successMsg = 'The username and password entered is not recognized by the database\n\n'+

                   'Please try again, or contact the systems administrator for assistance';

      break;



    case 'dbScs':

      successMsg = 'The database has been successfully updated';

      break;



    case 'fail':

      successMsg = 'The requested operation failed';

      break;



    case 'unUse':

      successMsg = 'The person record WAS NOT saved\n\n'+

                   'The username is currently is use by another person';

      break;



    default:

      showNothing = 1;

      break;

  }

  if (showNothing == 0)

  {

    alert (successMsg);

  }

}



function findDropDownValue(dropDownObject, lookForValue)

{

  valueFound = -1;



  targetValue = lookForValue.toLowerCase();



  dropDownLength = dropDownObject.length;  //alert(dropDownLength); alert(lookForValue);



  for (x = 0; x < dropDownLength; x++)

  {

    currentValue = dropDownObject[x].value;

    arrowValue = currentValue.toLowerCase();



    if (arrowValue == targetValue)

    {

      valueFound = x;

      break;

    }

  }

  return valueFound;

}



function getOut()

{

  window.location = 'logout.php';

}



function makeDateFromInt(intDate, showTime)

{

  var aryWeekday = new Array(7);

  aryWeekday[0] = "Sunday";

  aryWeekday[1] = "Monday";

  aryWeekday[2] = "Tuesday";

  aryWeekday[3] = "Wednesday";

  aryWeekday[4] = "Thursday";

  aryWeekday[5] = "Friday";

  aryWeekday[6] = "Saturday";



  var aryMonth = new Array(12);

  aryMonth[0]  = "January";

  aryMonth[1]  = "February";

  aryMonth[2]  = "March";

  aryMonth[3]  = "April";

  aryMonth[4]  = "May";

  aryMonth[5]  = "June";

  aryMonth[6]  = "July";

  aryMonth[7]  = "August";

  aryMonth[8]  = "September";

  aryMonth[9]  = "October";

  aryMonth[10] = "November";

  aryMonth[11] = "December";



  var aryHour = new Array(24);

  aryHour[0] = "12";

  aryHour[1] = "1";

  aryHour[2] = "2";

  aryHour[3] = "3";

  aryHour[4] = "4";

  aryHour[5] = "5";

  aryHour[6] = "6";

  aryHour[7] = "7";

  aryHour[8] = "8";

  aryHour[9] = "9";

  aryHour[10] = "10";

  aryHour[11] = "11";

  aryHour[12] = "12";

  aryHour[13] = "1";

  aryHour[14] = "2";

  aryHour[15] = "3";

  aryHour[16] = "4";

  aryHour[17] = "5";

  aryHour[18] = "6";

  aryHour[19] = "7";

  aryHour[20] = "8";

  aryHour[21] = "9";

  aryHour[22] = "10";

  aryHour[23] = "11";



  var aryAmPm = new Array(24);

  aryAmPm[0] = "am";

  aryAmPm[1] = "am";

  aryAmPm[2] = "am";

  aryAmPm[3] = "am";

  aryAmPm[4] = "am";

  aryAmPm[5] = "am";

  aryAmPm[6] = "am";

  aryAmPm[7] = "am";

  aryAmPm[8] = "am";

  aryAmPm[9] = "am";

  aryAmPm[10] = "am";

  aryAmPm[11] = "am";

  aryAmPm[12] = "pm";

  aryAmPm[13] = "pm";

  aryAmPm[14] = "pm";

  aryAmPm[15] = "pm";

  aryAmPm[16] = "pm";

  aryAmPm[17] = "pm";

  aryAmPm[18] = "pm";

  aryAmPm[19] = "pm";

  aryAmPm[20] = "pm";

  aryAmPm[21] = "pm";

  aryAmPm[22] = "pm";

  aryAmPm[23] = "pm";



  year = parseInt(intDate.substring(0, 4));

  month = intDate.substring(4, 6);

  day = intDate.substring(6, 8);

  hour = intDate.substring(8, 10);

  minute = intDate.substring(10, 12);



  //alert ('hour = '+hour+'; minute = '+minute);



  intYear = parseInt(year);

  intMonth = iv_parseInt(month) - 1;

  intDay = iv_parseInt(day);



  if (intDate.substring(8, 10) == '00')

  {

    intHour = 0;

  }

  else

  {

    intHour = iv_parseInt(intDate.substring(8, 10));

  }



  var jsDate = new Date();

  jsDate.setFullYear(intYear, intMonth, intDay);

  strDate = aryWeekday[jsDate.getDay()]+', '+aryMonth[jsDate.getMonth()]+' '+intDay+', '+year;



  if (showTime == 1)

  {

    strDate = aryHour[intHour]+':'+minute+''+aryAmPm[intHour]+', '+strDate;

  }

  return strDate;

}



function makeSQLDate(intDate)

{

  sqlDate = intDate.substring(0,4)+'-'+intDate.substring(4,6)+'-'+intDate.substring(6,8)+' '+intDate.substring(8,10)+':'+intDate.substring(10,12)+':00';

  //alert (sqlDate);

  return sqlDate;

}



function makeIntDateFromHTML(strHour, strMinute, strAmPm)

{

  if (strAmPm == 'pm')

  {

    switch (strHour)

    {

      case '08':

        strHour = '20';

        break;



      case '09':

        strHour = '21';

        break;



      case '12':

        strHour = '12';

        break;



      default:

        strHour = parseInt(strHour) + 12;

        break;

    }

  }

  else

  {

    if (strHour == '12')

    {

      strHour = '00';

    }

  }

  strTime = strHour+''+strMinute+'00';

  return strTime;

}



function makeIntFromSQLDate(sqlDate)

{

  if (sqlDate.length > 16)

  {

    intYear = sqlDate.substring(0,4);

    intMonth = sqlDate.substring(5,7);

    intDay = sqlDate.substring(8,10);

    intHour = sqlDate.substring(11,13);

    intMinute = sqlDate.substring(14,16);

    intDate = intYear+''+intMonth+''+intDay+''+intHour+''+intMinute+'00';

  }

  else

  {

    intDate = -1;

  }

  //alert('From '+sqlDate+' to '+intDate);

  return intDate;

}



function setNow(now, target, dateVersion)

{

  switch (dateVersion)

  {

    case 2:

      intDate = makeIntFromSQLDate(now);

      currentTime = makeDateFromInt(intDate, 1);

      break;



    case 1:

    default:

      currentTime = now;

      break;

  }

  targetElement = eval(target);

  targetElement.value = currentTime;

}



function getForm(formName, cmd)

{

  formName.cmd.value = cmd;

  formName.submit();

}



function selectRadioOption(objForm, radioName, radioIndex)

{

  //alert(objForm);



  formName = objForm;//.name;

  thisRadio = eval(formName+'.'+radioName);

  for (x = 0; x < thisRadio.length; x++)

  {

    if (x == radioIndex)

    {

      thisRadio[x].checked = true;

      //alert('index['+x+'] is checked');

    }

    else

    {

      thisRadio[x].checked = false;

      //alert('index['+x+'] is NOT checked');

    }

  }

}



function pup_calendar(dateField, sqlDate, showTime, dateFormat)

{

  day = new Date();

  id = day.getTime();
	
	URL = glbl_root+'crm/calendar.php?dtt='+dateField+'&shtm='+showTime;

  if ((trim(sqlDate) != '-1') && (trim(sqlDate) != '') && (trim(sqlDate) != 'NULL') && (trim(sqlDate)!='0000-00-00 00:00:00'))
  {
    URL = URL + '&dtfmt='+dateFormat+'&dt='+(sqlDate.substring(0,4))+''+(sqlDate.substring(5,7))+''+(sqlDate.substring(8,10))+''+(sqlDate.substring(11,13))+''+(sqlDate.substring(14,16))+''+(sqlDate.substring(17,19));
  }

  if (showTime == 1)

  {

    ppHeight = '400';

  }

  else

  {

    ppHeight = '350';

  }

  //alert(URL);

  //window.open(URL);

  eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=320,height="+ppHeight+",left=25,top=25');");

}







function openURL(theURL, windowWidth, windowHeight)

{

  day = new Date();

  id = day.getTime();

  //alert(theURL);

  //window.open(theURL);

  eval("page" + id + " = window.open(theURL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width="+windowWidth+",height="+windowHeight+"');");

}





function pup_x(page)

{

  //alert('here @ the pup');

  switch (page)

  {

    case 'person':

      openURL('person.php?pup=1', 800, 600);

      break;



    case 'staff':

      openURL('usrxs.php?pup=1', 800, 600);

      break;



    case 'customer':

      openURL('customer.php?pup=1', 800, 600);

      break;



    case 'company':

      openURL('company.php?pup=1&shw=cmp', 800, 600);

      break;

  }

}





function pup(page)

{

  //alert('here @ the pup');

  switch (page)

  {

    case 'person':

      openURL('person.php?pup=1', 800, 600);

      break;



    case 'staff':

      openURL('usrxs.php?pup=1', 800, 600);

      break;



    case 'customer':

      openURL('customer.php?pup=1', 800, 600);

      break;



    case 'company':

      openURL('company.php?pup=1&shw=cmp', 800, 600);

      break;

  }

}



function pup_banner(cnt,stc,cnttyp)

{

  openURL('banner.php?pup=1&cnt='+cnt+'&stc='+stc+'&cnttyp='+cnttyp, 800, 600);

}



function pup_person(personID,show,includeExcldueCustomer)

{

  //alert(personID);

  if (personID > 0)

  {

    //openURL('person.php?pup=1&cmd=edit&tb=detl&shw='+show+'&p='+personID+'&xcst='+includeExcldueCustomer, 800, 600);

    openURL('person.php?pup=1&cmd=edit&tb='+show+'&shw='+show+'&p='+personID+'&xcst='+includeExcldueCustomer, 800, 600);

  }

  else

  {

    openURL('person.php?pup=1&xcst='+includeExcldueCustomer, 800, 600);

  }

}



function pup_source(srcFor,srcForID)

{

  openURL('p_source.php?pup=1&fid='+srcForID+'&f='+srcFor, 700, 500);

}



function pup_contractline(contractID,extraStr)

{

  openURL('p_contractline.php?pup=1&c='+contractID+extraStr, 700, 500);

}



function pup_latestissuefeature(latestIssueFeatureID,extraStr)

{

  openURL('p_latestissuefeature.php?lif='+latestIssueFeatureID+'&'+extraStr, 600, 300);

}



function pup_appointmentsalevalue(apptID)

{

  //alert(personID);

  openURL('p_appointmentsalevalue.php?a='+apptID, 600, 500);

}



function pup_bundle(productID)

{

  if (productID > 0)

  {

    openURL('p_bundle.php?pup=1&prd='+productID, 700, 500);

  }

  else

  {

    alert('A product must be saved before items can be assigned as a bundle');

  }

}



function pup_tmk(tmkID,customerID)

{

  openURL('p_tmk.php?pup=1&c='+customerID+'&t='+tmkID, 700, 500);

}



function pup_parentchild(table,show)

{

  openURL('p_parentchild.php?pup=1&tbl='+table, 300, 500);

}



function pup_product(productID,show)

{

  openURL('product.php?pup=1&prd='+productID+'&shw='+show+'&tb='+show, 600, 500);

}



function pup_gallery(productID,str)

{

  //url = glbl_root + '/crm/p_gallery.php?p=' + productID + str;

  url = glbl_root + 'crm/p_gallery.php?p=' + productID + str;

  openURL(url, 600, 500);

}



function pup_company(companyID,show,includeExcludeCustomer)

{

  openURL('company.php?pup=1&cmd=edit&shw='+show+'&tb='+show+'&c='+companyID+'&xcst='+includeExcludeCustomer, 800, 600);

}



function pup_customer(customerID,show)

{

  openURL('customer.php?pup=1&tb='+show+'&shw='+show+'&c='+customerID, 800, 600);

}



function pup_qkedt(et,id,fld)

{

  openURL('p_qkedt.php?et='+et+'&u='+id+'&df='+fld, 400, 200);

}



function pup_standalone_address(addressID)

{

  openURL('p_address.php?pup=1&a='+addressID, 800, 600);

}



function pup_address(customerID)

{

  openURL('customer.php?pup=1&tb=detl&shw=addr&c='+customerID, 800, 600);

}



function pup_appointment(appointmentID,strDate,customerID,personID)

{

  //if (customerID > 0)

  //{

    openURL('appointment.php?pup=1&ct=cr8&aid='+appointmentID+'&fdt='+strDate+'&cid='+customerID+'&rpid='+personID, 650, 650);

  //}

  //else

  //{

  //  openURL('appointment.php?pup=1&ct=cr8&aid='+appointmentID+'&fdt='+strDate, 650, 650);

  //}

}



function pup_tmk_appointment(strDate,customerID,personID,apptType)

{

  openURL('appointment.php?pup=1&clse=tmk&ct=cr8&aid=-1&fdt='+strDate+'&cid='+customerID+'&rpid='+personID+'&atid='+apptType, 650, 650);

}



function pup_note(noteClass, classID, noteID, cmd)

{

  openURL('p_note.php?pup=1&nc='+noteClass+'&cid='+classID+'&n='+noteID+'&cmd='+cmd, 750, 550);

}



function pup_staff(from, to)

{

  openURL('usrxs.php?pup=1&tb=blnk&cmd=chkbsy&fdt='+from+'&tdt='+to, 800, 500);

}



function pup_staff_target(nameTarget)

{

  //openURL('usrxs.php?pup=1&tb=blnk&idt='+idTarget+'&nmt='+nameTarget, 800, 500);

  openURL('usrxs.php?pup=1&tb=blnk&nmt='+nameTarget, 800, 500);

}



function pup_appointment_grid(strDT)

{

  openURL('appointment.php?pup=1&ct=DeptView', 1000, 500);

}



function htmlColor(clr)

{

  openURL('htmlcolor.php?clr='+clr, 470, 660);

}



function pressEnter(executeString)

{

  if (event.keyCode==13)

  {

    eval(executeString);

  }

}



function validEmail(email)

{

  if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email))

  {

    return 1;

  }

  else

  {

    return 0;

  }

}



function reverseSQLOrder(ord)

{

  if (ord == 'ASC')

  {

    return 'DESC';

  }

  else

  {

    return 'ASC';

  }

}



function sltElementDisplay(showMe, target)

{

  switch (showMe)

  {

    case 1:

    case 'block':

      document.getElementById(target).style.display = 'block';

      break;



    case 2:

    case 'inline':

      document.getElementById(target).style.display = 'none';

      break;



    case 'none':

    default:

      document.getElementById(target).style.display = 'none';

      break;

  }

}



function fileType(fileName)

{

  str = trim(fileName);

  ary = str.split('.');

  lastIdx = ary.length - 1;

  typ = ary[lastIdx];

  return typ.toLowerCase();

}



function elementDisplay(showMe, target)

{

  if (showMe == true)

  {

    document.getElementById(target).style.display = 'block';

  }

  else

  {

    document.getElementById(target).style.display = 'none';

  }

}



function showHideSpan(target, showType)

{

  if (document.getElementById(target).style.display == 'none')

  {

    document.getElementById(target).style.display = showType;

  }

  else

  {

    document.getElementById(target).style.display = 'none';

  }

}



function reverseElementDisplay(showMe, target)

{

  if (showMe == true)

  {

    elementDisplay(false, target);

  }

  else

  {

    elementDisplay(true, target);

  }

}



function alphaNumeric(stringValue, fieldName, showMsg)

{

  cleanString = permitOnly(glbl_permissibleString, stringValue);

  if ((showMsg == 1) && (!(cleanString)))

  {

    alert('The '+fieldName+' can only contain alpha-numeric characters, periods ("."), dashes ("-"), or underscores ("_")');

  }

  return cleanString;

}



function validatePassword(password, showMsg)

{

  if ((alphaNumeric(password,'password',0) == 1) && (password.length >= 5) && (password.length < 21))

  {

    return 1;

  }

  else

  {

    if (showMsg == 1)

    {

      alert('The password must:\n'+

            '\n1) Contain only alpha-numeric characters, periods ("."), dashes ("-"), or underscores ("_")'+

            '\n2) Be between 5 to 20 characters in length');

    }

    return 0;

  }

}



function numericOnly(fieldValue, showMsg)

{

  cleanString = permitOnly(glbl_numeric, fieldValue);

  if ((!(cleanString)) && (showMsg == 1))

  {

    alert('This data field can only contain numeric characters (0-9)');

  }

  return cleanString;

}



function resizeWindow(curWindow, width, height)

{

    curWindow.resizeTo(width, height);

}





function showFirstHideSecond (first, second, showType)

{

  document.getElementById(second).style.display = 'none';

  document.getElementById(first).style.display = showType;

}





function changeTab(tabName)

{

  //alert(tabName);

  switch (tabName)

  {

    case 'cstlist':

      document.frm.CustomerStatusID.value = -1;

      document.frm.CustomerID.value = -1;

      break;

  }

  document.frm.tab.value = tabName;

  document.frm.submit();

}



