/**
* Common JS for Custom functionalities in WHS Delta.
*
* @author Professional Access
*/

/**
* Used to limit the text area boxs in forms to a maximum character field of
* maxlimit passed.
*
* @param field
*            to be validated
* @param maxlimit
*            field to be restricted to.
* @return
*/
function textCounter(field, maxlimit) {

var val = document.getElementById("req_message").value;
if (field.value!=null && val.length > maxlimit) {
field.value = field.value.substring(0, maxlimit);
}
}

/**
* Used to limit the text area boxs in forms to a maximum character field of
* maxlimit passed.
*
* @param obj
*            to be validated
* @param maxlimit
*            field to be restricted to.
* @return
*/
function imposeLength(obj, maxlimit) {
if (obj.value!=null && obj.value.length > maxlimit) {
obj.value = obj.value.substring(0, maxlimit);
}
}

/**
* Disable the go button if dropdown value is blank.
*
*/
function disableButton()
{
var val = document.getElementById("condition_select").value;
var btn = document.getElementById("condition_submit");
if(val=='')
{
btn.disabled=true;
}
else
{
btn.disabled=false;
}
}
/**
* Used to display the selected option label at some
* different place.
*
*/
function changeUnit() {

var userInput;
var selectedTextField = document.getElementById('unit_type_ddl').value;
if(selectedTextField !=1){
userInput = document.getElementById('unit_type_ddl')[document
.getElementById('unit_type_ddl').selectedIndex].innerHTML;
 document.getElementById('unitSelected').innerHTML = userInput;
 }
 else{
 document.getElementById('unitSelected').innerHTML = 'Units';
 }
}

/**
* Used to enable the field.
*
*/
function enableField() {
document.getElementById('mult_num_txtbx').disabled = false;
}

/**
* Used to disable the field.
*
*/
function disableField() {
document.getElementById('mult_num_txtbx').disabled = true;
}

/**
* This method on load checks if the multiple day
* radio button is checked than it enables the
* text box in front of the radio button
*/
function checkMultipleDaySelected()
{
if(document.getElementById('mult_chckbx').checked == true)
{
document.getElementById('mult_num_txtbx').disabled = false;
}
}


var request;

/**
* This method will initialized and create the
* XMLHTTP request object
*
* @author Pmukherjee
*/
function getHTTPRequest() {
var xmlhttp = false;
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
xmlhttp = new XMLHttpRequest();
}
return xmlhttp;
}


function getCustomerInfo() {

request = getHTTPRequest();
var checkPrintId = document.getElementById('tc_accept_chckbx').checked;
var url = "displayidnumber.jsp?selected=" + escape(checkPrintId);
request.open("GET", url);
request.onreadystatechange = updatePage;
request.send(null);
}

function updatePage() {
    if (request.readyState == 4) {
}
  }

function changeDependentList(fromId,toID){
var selectedValue = document.getElementById(fromId).value;

document.getElementById(toID).value = selectedValue;

}

function validateDays(dobMonths, dobDays) {
var ind = $('#' + dobMonths).val();
var dobDaySel = $('#' + dobDays);

var arr31 = ['', '0', '1', '01', '3', '03', '5', '05', '7', '07', '8', '08', '10', '12'];
var arr30 = ['4', '04', '6', '06', '9', '09', '11'];
var days = ['0', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15',
            '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31'];

dobDaySel.html('');
dobDaySel.append($("<option></option>").attr("value",'').text('Day:'));

if(jQuery.inArray(ind, arr31) != -1){
for(var i = 1; i <= 31; i++) {
dobDaySel.append($("<option></option>").attr("value",days[i]).text(days[i]));
}
} else if(jQuery.inArray(ind, arr30) != -1) {
for(var i = 1; i <= 30; i++) {
dobDaySel.append($("<option></option>").attr("value",days[i]).text(days[i]));
}
} else if((ind == '2') || (ind == '02')) {
for(var i = 1; i <= 29; i++) {
dobDaySel.append($("<option></option>").attr("value",days[i]).text(days[i]));
}
}
}
/**
 * This method is to make any DOM object disabled
 * @param object
 * @return
 */
function makeDisable(object) {

object.disabled = true;

}
/**
 * This method is to make any DOM object disabled
 * @param object
 * @return
 */
function makeEnable(object) {

object.disabled = false;

}

function validateRx(object){
str = object.value;
if(str != null && (jQuery.trim(str)).length > 0){

count = str.length;
for (var i = 0; i < str.length; i++) {
if (str[i] == '0') {
count--;
}
}
if (count == 0) {
alert('Please enter valid Prescription Number');
return false;
} else {
// count = str.length;
// if(count >= 5){
// if((str[0] == '0' && str[1] == '0' && str[2] == '0' && str[3] == '0' && str[4] == '0') ||
// (str[count-1] == '0' && str[count-2] == '0' && str[count-3] == '0' && str[count-4] == '0' && str[count-5] == '0')){
// alert('Please enter valid Prescription Number');
// return false;
// }
// }
}

}
return true;
}

function validateRxAndStore(objectRx, objectStore){
strRx = objectRx.value;
if(strRx != null && (jQuery.trim(strRx)).length > 0){

count = strRx.length;
for (var i = 0; i < strRx.length; i++) {
if (strRx[i] == '0') {
count--;
}
}
if (count == 0) {
alert('Please enter valid Prescription Number');
return false;
} else {
// count = strRx.length;
// if(count >= 5){
// if((strRx[0] == '0' && strRx[1] == '0' && strRx[2] == '0' && strRx[3] == '0' && strRx[4] == '0') ||
// (strRx[count-1] == '0' && strRx[count-2] == '0' && strRx[count-3] == '0' && strRx[count-4] == '0' && strRx[count-5] == '0')){
// alert('Please enter valid Prescription Number');
// return false;
// }
// }
}

}

strStore = objectStore.value;
if(strStore != null && (jQuery.trim(strStore)).length > 0){

count = strStore.length;
for (var i = 0; i < strStore.length; i++) {
if (strStore[i] == '0') {
count--;
}
}
if (count == 0) {
alert('Please enter valid Store Number');
return false;
} else {
// count = strStore.length;
// if(count >= 4){
// if((strStore[0] == '0' && strStore[1] == '0' && strStore[2] == '0' && strStore[3] == '0' ) ||
// (strStore[count-1] == '0' && strStore[count-2] == '0' && strStore[count-3] == '0' && strStore[count-4] == '0' )){
// alert('Please enter valid Store Number');
// return false;
// }
// }
}

}

return true;
}

// Method added for EO-10474 to set security questions in registartion and change Security questions page
function setSecurityQues(obj, dropDownName){
      var sel = document.getElementById(dropDownName);
      var selectedValue=sel.value;
      sel.options.length = null;
      sel.options[0]=new Option('Select', '');
      var tempVar = 1;
      for(var i = 0; i < securityQuesArray.length; i++) {
            if (securityQuesArray[i] != obj.value) {
                  sel.options[tempVar]=new Option(securityQuesArray[i], securityQuesArray[i]);
                  tempVar++;

            }

      }
      for ( var i = 0; i < sel.options.length; i++ ) {
            if ( sel.options[i].value ==selectedValue) {
               sel.options[i].selected = true;
              return;

           }

       }

}
// Method added for EO-11905 to set appropriate supply as daily,weekly or monthly in priceprescriptiondrug page
// Method modified for EO-14124 to set appropriate supply as daily,weekly or monthly in priceprescriptiondrug page for onsite option
function setSupplyValue(pSupplyValue)
{
document.getElementById("supply").innerHTML=pSupplyValue;
document.getElementById("supplyMail").innerHTML=pSupplyValue; 
document.getElementById("supplyOnsite").innerHTML=pSupplyValue; 
if(pSupplyValue=='weeks')
{
document.getElementById("ret_pharm_txtbx").value=4;
document.getElementById("mail_pharm_txtbx").value=12; 
document.getElementById("onsite_pharm_txtbx").value=4; 
}
else if(pSupplyValue=='months')
{
document.getElementById("ret_pharm_txtbx").value=1;
document.getElementById("mail_pharm_txtbx").value=3; 
document.getElementById("onsite_pharm_txtbx").value=1; 
}
else
{
document.getElementById("ret_pharm_txtbx").value=30;
document.getElementById("mail_pharm_txtbx").value=90; 
document.getElementById("onsite_pharm_txtbx").value=30; 
}
}




