var DEFAULT_HIGHLIGHT_COLOR = '#ff0000';
var DEFAULT_RESET_COLOR     = '#333333';
var DEFAULT_RESET_BORDER    = '#00FF00';

// Global array required for storing
// required fields information for
// the current form
var requiredFields = new Array();


//
// Commonly Used Regular Expressions
//
RE_US_ZIPCODE = /\d{5}(-\d{4})?/;
RE_EMAIL_ADDRESS = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/;

//
// Standar cancel button function
//
function doCancel()
{
   document.location.href = CANCEL_URL;
}

//
// Show error messages (if any)
//
function showErrors(frm, setColor, resetColor)
{
   // if highlight (set) color is not provided
   // use default
   if (! setColor)
      setColor = DEFAULT_HIGHLIGHT_COLOR;


   // if reset color is not provided
   // use default
   if (! resetColor)
      resetColor = DEFAULT_RESET_COLOR;

   with (frm)
   {

     // Is there error messages from PHP side?
     if (errorMsgList.length > 0)
     {
         var errCnt = errorMsgList.length;
         var msgBlock = "";
         for(i=0; i<errCnt; i++)
         {
            msgBlock += errorMsgList[i] + "\n";
            tdColumnID = errorMsgFieldList[i];
            highlightTableColumn(tdColumnID, setColor);

            //fields = document.getElementsByName(errorMsgFieldList[i]);
            //fields[0].focus();
         }

         // If we are supposed to show alert() popup
         // for errors, show one.
         if (alertPopup)
             alert(msgBlock);
     }
   }

   return true;
}

//
// Highlight a table column foreground color and style using ID
//
function highlightTableColumn(id, highColor)
{

   if (!highColor)
       highColor = DEFAULT_HIGHLIGHT_COLOR;

   thisElement                  = document.getElementById(id);
   thisElement.style.color      = highColor;
   thisElement.style.fontWeight = "bold";
}
function highlightBoxBG(id, highColor)
{

   if (!highColor)
       highColor = DEFAULT_HIGHLIGHT_COLOR;

   thisElement                  = document.getElementById(id);
   thisElement.style.borderColor     = highColor;
}

//
// Reset a table column foreground color and style using ID
//
function resetTableColumn(id, resetColor)
{
   if (!resetColor)
       resetColor = DEFAULT_RESET_COLOR;

   thisElement                  = document.getElementById(id);
   thisElement.style.color      = resetColor;
   thisElement.style.fontWeight = "bold";
   
}
function resetBoxBG(id, resetColor)
{
   if (!resetColor)
       resetbgColor = DEFAULT_RESET_COLOR;

   thisElement                  = document.getElementById(id);
   thisElement.style.borderColor     = resetbgColor;
}
//
// Purpose: check if a drop-down list has at least one item
//          selected or not
//
// Return:  true if at least one item is selected, else false
function itemSelectedFromDropDownList(menu)
{
   // If nothing is selected return false
   if (menu.selectedIndex == null ||
       menu.selectedIndex == 0 )
   {
       return false;
   }

   // An item is selected, return true
   return true;
}

//
// Purpose: this function allows you to define a form field
//          as a required field. A subsequent call to
//          validateForm() allows you to ensure that the
//          form cannot be submitted without required fields
//
//
function setRequiredField(formFieldObject,
                          fieldType,
                          fieldLabel,
                          fieldValidator,
                          fieldErrorMsg)
{
   var i = requiredFields.length;

   // Store the given field's requirements
   // information in the global array
   requiredFields[i] = new Array();
   requiredFields[i]['field'] = formFieldObject;
   requiredFields[i]['type']  = fieldType;
   requiredFields[i]['label']  = fieldLabel;

   // Reset the color
   resetTableColumn(fieldLabel);

   if (fieldValidator)
       requiredFields[i]['validator']  = fieldValidator;

   // MK-TODO: current version does not use error_msg data
   if (fieldErrorMsg)
      requiredFields[i]['error_msg']  = fieldErrorMsg;
   else
      requiredFields[i]['error_msg']  = "The " + formFieldObject + " value is missing or incorrect";

   // MK TODO: need to add data type validation support

}

//
// Purpose: do required field validation for a form
//          This function relies on setRequiredField()
//          set up data in requiredFields array for
//          doing appropriate validation
//
//
function validateForm(thisForm)
{
   // Get the count of required fields
   var reqFieldCnt = requiredFields.length;
   var errCnt = 0;
   with (thisForm)
   {
      for(i=0; i<reqFieldCnt; i++)
      {
         var formFieldObject  =  requiredFields[i]['field'];
         var fieldType  =  requiredFields[i]['type'];
         var fieldLabel =  requiredFields[i]['label'];
         var fieldErr   =  requiredFields[i]['error_msg'];

         // If current field is a drop down list and
         // not a single item is selected, we have a
         // required field violation!
         if (fieldType == 'dropdown' && !itemSelectedFromDropDownList(formFieldObject))
         {
             if (fieldLabel)
                highlightTableColumn(fieldLabel);

             formFieldObject.focus();
             errCnt++;
         }
         // If current field is a text box and it is empty
         // we have a required field violation
         else if (fieldType == 'textbox' && formFieldObject.value == "")
         {
             //alert("No value selected for " + fieldLabel);

             if (fieldLabel)
                highlightTableColumn(fieldLabel);

             //formFieldObject.focus();
             errCnt++;
         }
         // If current field is a check box and it is not selected
         // we have a required field violation
         else if (fieldType == 'checkbox' && ! isCheckBoxSelected(formFieldObject))
         {
             if (fieldLabel)
                highlightTableColumn(fieldLabel);

             errCnt++;
         }
         // If current field is a radio box and it is not selected
         // we have a required field violation
         else if (fieldType == 'radio' && ! isRadioSelected(formFieldObject))
         {
             if (fieldLabel)
                highlightTableColumn(fieldLabel);

             errCnt++;
         }

      }
   }

   return errCnt;
}


//
// Purpose: allows you to find out if a checkbox is
//          selected. If you have a checkbox group (i.e.
//          checkbox with same name, it will work too
//
function isCheckBoxSelected(chkbox)
{
   var noneChecked = true;
   if (typeof chkbox.length == 'undefined')
   {
     // there's only one checkbox in the form
     // normalize it to an array
     chkbox = new Array(chkbox);
   }
   for (var i = 0; i < chkbox.length; i++)
   {
      if (chkbox[i].checked)
      {
         noneChecked = false;
         break;
      }
    }

   if (noneChecked)
       return false;
   return true;
}

function isRadioSelected(btnName)
{
  // If only one item
  if (typeof btnName.length == 'undefined')
  {
    if (btnName.checked == true)
        return true;
    else
        return false;
  }

  // There are many radio options
  var len = btnName.length;

  var i=0;
  var noneSelected = true;

  while(noneSelected && i<len)
  {
    if (btnName[i].checked == true)
    {
      noneSelected = false;
    }
    i++;
  }

  return !noneSelected;

}

//
// Purpose: allows you to select a list of items for a
//          checkbox group
//
function selectChosenCheckBoxItems(formFieldString, chosenList)
{
   var frm = document.forms[0];

   with (frm)
   {
   if (chosenList.length > 0)
      {
          var chkbox = elements[formFieldString];

          if (typeof chkbox.length == 'undefined')
          {
            chkbox = new Array(chkbox);
          }

          for (var i = 0; i < chkbox.length; i++)
          {
             for(var j=0; j < chosenList.length; j++)
             {
                if (chkbox[i].value == chosenList[j])
                {
                  chkbox[i].checked = true;
                }
             }
          }
      }

   }
}


//
// Utility Function
//
// Purpose: strip everything nut [0-9.] set from given string
// Return : string containing only [0-9.] characters
function makeNumber(str)
{
   if (str != null && str.length > 0)
       return str.replace(/[^0-9.]/g, '');
   else
       return null;
}


//
// Toggle a div block using the div id
//
// Example div:
// <div id="A" style="display: none"> something </div>
//
// Example call:
//
// <a href="javascript:toggle('A')"><strong>Show Details of section A</strong></a>

function toggle(targetId) {

  if ("none" == document.getElementById(targetId).style.display) {
     document.getElementById(targetId).style.display = "block";
  }
  else {
     document.getElementById(targetId).style.display = "none";
  }
}

function doConfirm(msg)
{
    return confirm(msg);
}

/**
 * This function gets the
 * order to be displayed
 * as an icon in the table heading

 * @param number id--the index in the document.images collection
 * @return none
 */

function toggleSort(id)
{
    counter = getClickCount();

    for(i=0; i<=document.images.length; i++)
    {
       if (document.getElementById(i))
       {
          document.getElementById(i).style.display = 'none';
       }
    }

   if(counter % 2 == 1)
   {
       document.getElementById(id+1).style.display = 'inline';
   }
   else
   {
       document.getElementById(id+2).style.display = 'inline';
   }
}

function getClickCount()
{
   return ++counter;
}

function openAWindow( pageToLoad, winName, width, height, center)
{
   xposition=0;
   yposition=0;

   if ((parseInt(navigator.appVersion) >= 4 ) && (center))
   {
      xposition = (screen.width - width) / 2;
      yposition = (screen.height - height) / 2;
   }

   winName = "'" + winName + "'";

   args = "width=" + width + ","
   + "height=" + height + ","
   + "location=0,"
   + "menubar=0,"
   + "resizable=1,"
   + "scrollbars=1,"
   + "status=0,"
   + "titlebar=0,"
   + "toolbar=0,"
   + "hotkeys=0,"
   + "screenx=" + xposition + "," //NN Only
   + "screeny=" + yposition + "," //NN Only
   + "left=" + xposition + "," //IE Only
   + "top=" + yposition; //IE Only

   window.open(pageToLoad, 'win', args);
}

/*
 * Purpose: this function shows the Div
 */
function showDiv(divId)
{
   if (document.getElementById)
	  { // DOM3 = IE5, NS6
      document.getElementById(divId).style.display = 'inline';
   }
   else
	  {
      if (document.layers)
	     { // Netscape 4
         document.divId.style.display = 'inline';
      }
      else
	     { // IE 4
         document.all.divId.style.display = 'inline';
      }
   }
}

/*
 * Purpose: this function hides the Div
 */
function hideDiv(divId)
{
   if (document.getElementById)
   { // DOM3 = IE5, NS6

      document.getElementById(divId).style.display = 'none';
   }
   else
   {
      if (document.layers)
      { // Netscape 4
         document.divId.display = 'none';
      }
      else
      {   // IE 4
         document.all.divId.style.display = 'none';
      }
   }
}

function showItem(itemId)
{
	if (document.getElementById)
	{ // DOM3 = IE5, NS6
		document.getElementById(itemId).style.display = 'inline';
	}
	else
	{
		if (document.layers)
		{ // Netscape 4
			document.itemId.style.display = 'inline';
		}
		else
		{ // IE 4
			document.all.itemId.style.display = 'inline';
		}
	}
}

/*
 * Purpose: this function hides the Item
 */
function hideItem(itemId)
{
	if (document.getElementById)
	{ // DOM3 = IE5, NS6
		document.getElementById(itemId).style.display = 'none';
	}
	else
	{
		if (document.layers)
		{ // Netscape 4
			document.itemId.display = 'none';
		}
		else
		{   // IE 4
			document.all.itemId.style.display = 'none';
		}
	}
}

function contentConfirm(id)
{

	var val = document.getElementById(id).value;
	if(val <= 0)
	{
		return doConfirm(PROMPT_DELETE_CONFIRM);		
	}
	else
	{
		alert(val+" Content(s) available. Please Delete them first.");
		return false;
	}
}

function showImg($id, $imgPath) 
{
	document.getElementById($id).src = $imgPath;
}

function passwordChanged(pwdField, highlight) 
{
	var strength = document.getElementById(highlight);
	var strongRegex = new RegExp("^(?=.{8,})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*\\W).*$", "g");
	var mediumRegex = new RegExp("^(?=.{7,})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$", "g");
	var enoughRegex = new RegExp("(?=.{6,}).*", "g");

	var pwd = document.getElementById(pwdField);
	if (pwd.value.length == 0) 
	{
		strength.innerHTML = "Type Password";
	} 
	else if (false == enoughRegex.test(pwd.value)) 
	{
		strength.innerHTML = "More Characters";
	} 
	else if (strongRegex.test(pwd.value)) 
	{
		strength.innerHTML = '<span style="color:green">Strong!</span>';
	} 
	else if (mediumRegex.test(pwd.value)) 
	{
		strength.innerHTML = '<span style="color:orange">Medium!</span>';
	} 
	else 
	{ 
		strength.innerHTML = '<span style="color:red">Weak!</span>';
	}
}

function comparePassword(passId, cpassId, lblId)
{
	var pass	= document.getElementById(passId).value;
	var cpass	= document.getElementById(cpassId).value;
	var lbl		= document.getElementById(lblId);
	if(cpass == "")
	{
		lbl.innerHTML	= '<span>&nbsp;</span>';
	}
	else if(pass == cpass)
	{
		lbl.innerHTML	= '<span style="color:green">matched!</span>';
	}
	else
	{
		lbl.innerHTML	= '<span style="color:red">not matched!</span>';
	}
}

function daysBetween(date1, date2)
{
	if (date1.indexOf("-") != -1) 
	{
		date1 = date1.split("-"); 
	} 
	else if (date1.indexOf("/") != -1) 
	{ 
		date1 = date1.split("/"); 
	} 
	else 
	{ 
		return 0; 
	}
	if (date2.indexOf("-") != -1) 
	{ 
		date2 = date2.split("-"); 
	}
	else if (date2.indexOf("/") != -1) 
	{
		date2 = date2.split("/");
	} 
	else
	{
		return 0; 
	} 
	
	if (parseInt(date1[0], 10) >= 1000) 
	{ 
		var sDate = new Date(date1[0]+"/"+date1[1]+"/"+date1[2]); 
	}
	else if (parseInt(date1[2], 10) >= 1000)
	{ 
		var sDate = new Date(date1[2]+"/"+date1[0]+"/"+date1[1]); 
	}
	else 
	{
		return 0; 
	} 
	
	if (parseInt(date2[0], 10) >= 1000) 
	{ 
		var eDate = new Date(date2[0]+"/"+date2[1]+"/"+date2[2]); 
	}
	else if (parseInt(date2[2], 10) >= 1000) 
	{ 
		var eDate = new Date(date2[2]+"/"+date2[0]+"/"+date2[1]); 
	}
	else
	{ 
		return 0; 
	} 
	
	var one_day = 1000*60*60*24; 
	var daysApart = Math.abs(Math.ceil((sDate.getTime()-eDate.getTime())/one_day)); 
	return daysApart; 
}
function isNumeric(num)
{
	if( isNaN(num) )
		return false;
	else
		return true;
}
function checkIsEmpty(val)
{
	if(val.length < 1)
		return false;
	else
		return true;
}

function validateFieldsValue(frm, tergetId, msg)
{
	if(! checkIsEmpty(frm.tergetId.value) )
	{
		highlightBoxBG(tergetId);
		alert(msg);
		return false;
	}
	else
	{
		resetBoxBG(tergetId);
		return true;
	}
}

function LTrim(str)
{
	var whitespace = new String(" \t\n\r");
	var s = new String(str);
	if (whitespace.indexOf(s.charAt(0)) != -1) 
	{
		var j=0, i = s.length;
		while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
			j++;
		s = s.substring(j, i);
	}
	return s;
}

function RTrim(str)
{
	var whitespace = new String(" \t\n\r");
	var s = new String(str);
	if (whitespace.indexOf(s.charAt(s.length-1)) != -1) 
	{
		var i = s.length - 1;       // Get length of string
		while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
			i--;
		s = s.substring(0, i+1);
	}
	return s;
}
function Trim(str)
{
  return RTrim(LTrim(str));
}

function doSelect(a) {
	var theForm = document.listForm;
    for (i=0; i<theForm.elements.length; i++) {
        if (theForm.elements[i].name=='id[]')
            theForm.elements[i].checked = a;
    }
}

function doSubmitForm(action)
{
   requiredFields.length = 0;

   var errCnt = 0;
   var ac = action;
   var frm = document.listForm;
   if(checkCheckBoxes())
   {
	 
	 if(action == "assign" )
	 {
	 	frm.cmd.value	="assign";
		$msg = "Assign";
	 }
	 else if(action == "DOforVissa")
	 {
		frm.cmd.value = "DOforVissa";
		$msg = "D.O for visa";
	 }
	 else if(action == "death_report")
	 {
		frm.cmd.value	="death_report";
		$msg = "Death Reporting";
	 }
	 else if(ac == 'DRofPassport')
	 {
		 frm.cmd.value = "DRofPassport";
		 $msg = "D.R. of Passport";
	 }
	 
	 $ok=doConfirm("Are you sure you want to "+$msg+"?")
	 if($ok)
	    frm.submit();
	 else
	   return false;
   }
   else
   {
	 alert ('You didn\'t choose any of the checkboxes!');
	 return false;
   }
   
   
}

function checkCheckBoxes() {
	var theForm = document.listForm;
	for (i=0; i<theForm.elements.length; i++) 
	{
        
		
		if (theForm.elements[i].name=='id[]')
        {    
			if(theForm.elements[i].checked )
			 return true;
		}
	    	
    }
	return false;
}

function NewWindow(mypage, myname, w, h, scroll) {
var winl = (screen.width - w) / 2;
var wint = (screen.height - h) / 2;
winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable'
win = window.open(mypage, myname, winprops)
if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

// start visa lodgement information 
//function setVisaLodgementInfo(pilgrimId)
//{
//	alert(pilgrimId);
//	
//	
//}

function setVisaLodgementInfo()
{	
	var pilgrimId 			= document.visa_form.pilgrim_id.value;
	var PI_VISA_TRACKING_ID = document.visa_form.PI_VISA_TRACKING_ID.value;
	//var PI_VISA_NO 			= document.visa_form.PI_VISA_NO.value; 
	//var PI_VISA_EXPARE_DATE = document.visa_form.PI_VISA_EXPARE_DATE.value;
	var PI_ID_NO			= document.visa_form.PI_ID_NO.value;
	var PI_PASSPORT_NO		= document.visa_form.PI_PASSPORT_NO.value;
	var PI_FIRST_NAME		= document.visa_form.PI_FIRST_NAME.value;
	var PI_MIDDLE_NAME		= document.visa_form.PI_MIDDLE_NAME.value;
	var PI_LAST_NAME		= document.visa_form.PI_LAST_NAME.value;
	var PI_NICK_NAME		= document.visa_form.PI_NICK_NAME.value;
	
	
	var PI_BOTH_HNW		= document.visa_form.PI_BOTH_HNW.value;
	var PI_MAH_PASSPORT		= document.visa_form.PI_MAH_PASSPORT.value;
	var PI_GENDER			= document.visa_form.PI_GENDER.value;
	
	if( PI_ID_NO =='')
	{
		alert('Pilgrim ID Numbner not empty! please fill pilgrim ID No.');
		document.visa_form.PI_ID_NO.focus;
		return false;
	}
	else if(PI_PASSPORT_NO =='')
	{
		alert('Passport Numbner not empty! please fill Passport No.');
		document.visa_form.PI_PASSPORT_NO.focus;
		return false;
	}
	else if(PI_LAST_NAME =='' )
	{
		alert('Pilgrim Name not empty! please fill Pilgrim Name.');
		document.visa_form.PI_LAST_NAME.focus;
		return false;
	}
	
	//PI_VISA_NO,PI_VISA_EXPARE_DATE,
	cpaintCall('SELF', 'POST', "setVisaLodgement",pilgrimId,PI_VISA_TRACKING_ID,PI_ID_NO,PI_PASSPORT_NO,PI_FIRST_NAME,PI_MIDDLE_NAME,PI_LAST_NAME,PI_NICK_NAME,PI_BOTH_HNW,PI_MAH_PASSPORT,PI_GENDER,setVisaLodgementInfoDone);
}

function setVisaLodgementInfoDone(val)
{
	//alert(val);
	document.getElementById("divSaved").innerHTML = "Done";
}


//end visa lodgenent information 




