//-------------------------------------------------------
//---used in sndFollowUpSelectedId.php coded by Kamala---
//-------------------------------------------------------
//alert("1");
function setType(type)
{
	if(type!="All")
	{
		var typeValue="";
		var isChecked=false;
		for(var i=1;i<=15;i++)
		{
			if(document.sndFollowUpSelectedId.typeChk[i].checked)
			{
				typeValue =(typeValue!="")?typeValue+", "+document.sndFollowUpSelectedId.typeChk[i].value:document.sndFollowUpSelectedId.typeChk[i].value;
				isChecked=true;
			}
		}
		document.getElementById("type").value=(isChecked)?typeValue:"All";
		document.sndFollowUpSelectedId.typeChk[0].checked = (isChecked)?false:true;
		document.sndFollowUpSelectedId.typeChk[0].disabled = (isChecked)?false:true;
	}
	else
	{
		document.getElementById("type").value="All";
		document.sndFollowUpSelectedId.typeChk[0].disabled = true;
		for(var i=1;i<=15;i++)
			document.sndFollowUpSelectedId.typeChk[i].checked=false;
	}
}

//-------------------------------------------------------
//-------------------------------------------------------
function takeAction(val){
	
	fileController=document.getElementById('doc');
	haveDocCtrl=document.getElementById('have_doc');
	replaceDocCtrl=document.getElementById('replace_doc');
	keepDocCtrl=document.getElementById('keep_doc');
//	form=document.getElementById('updNews');
	
	switch(val)
	{
		case 'useCurrent':
		  fileController.disabled=true;
		  replaceDocCtrl.value=false;
		  keepDocCtrl.value=true;
		  break;
		  
		case 'replace':
		  fileController.disabled=false;
		  replaceDocCtrl.value=true;
		  keepDocCtrl.value=false;
			
		  
		
	}
	
	//alert("keep:"+keepImgCtrl.value+"/"+"Have:"+haveImgCtrl.value+"/replace:"+replaceImgCtrl.value+"/rem:"+removeImgCtrl.value+"\\"+fileController.value);
}


var selectedCat;	// shows the selected value of the updClient drop down.
function loadCategories(caller, selectedVal)
{
	// checks 'coz this is 'undefined' in insClient...
	if(typeof(selectedVal)!='undefined')
		selectedCat = selectedVal;
	val=caller.options[caller.selectedIndex].value;
	if(val == 'All') // if 'All' selected IE get delayed...
	{
		initCategories();
		return;
	}
	ajx=new AJAXHandler();
	ajx.setTarget('loadCategoryAjx.php');
	ajx.sendRequest("country="+val,'POST');
}

function loadCategoriesOnReset(val)
{
	ajx=new AJAXHandler();
	ajx.setTarget('loadCategoryAjx.php');
	ajx.sendRequest("country="+val,'POST');
}

function initCategories() // for 'getClientList' pages...
{
	catList = ['All'];
	dropDown = document.getElementById('cat');
	dropDown.options.length=catList.length;
	dropDown.options[0].value=catList[0];
	dropDown.options[0].text=catList[0];
					
}

function hideEmail()
{
	emailId = document.getElementById("em");
	if(emailId.value != '' && emailId.value == 'ccrm@evisalaw.com')
		emailId.value = '';
}

function showEmail()
{
	emailId = document.getElementById("em");
	if(emailId.value == '')
		emailId.value = 'ccrm@evisalaw.com';
}

function AJAXHandler()
{
	var target=null;
  var respVal=null;
    	
	this.setTarget=function(target)
	{
		this.target=target;
	}
	
	this.createRequestObject=function()
	{
		var httpRequest=false;
		if(window.XMLHttpRequest){
			httpRequest=new XMLHttpRequest();
		}
		else if(window.ActiveXObject){
			httpRequest=new ActiveXObject("Microsoft.XMLHTTP");
		}
		else{
			alert("This page will not work as desired, lack of support from browser");
		}
		return httpRequest;
	}

	var xmlhttp=this.createRequestObject();
	
	this.sendRequest=function(paramslist,method)
	{
		params=paramslist; 	
		 
		if(method=='POST')
		{
			xmlhttp.open("POST",this.target,true);
		 	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	        xmlhttp.setRequestHeader("Content-length", params.length);
	        xmlhttp.setRequestHeader("Connection", "close");
	        xmlhttp.onreadystatechange=this.handleResponce;
	        xmlhttp.send(params);
		}
		else if(method=='GET')
		{
			
			xmlhttp.open("GET",this.target+params,true);
			xmlhttp.onreadystatechange=this.handleResponce;
		    xmlhttp.send(null);
		}
	}
	
	this.handleResponce=function()
	{
			formName = document.forms[0].name
			//updated from here...
			/*if(formName == 'sndNewsLetterMsg')
			{
				msgDiv = document.getElementById('msg');
				if(xmlhttp.readyState==4)
				{
					msgDiv.innerHTML = "";
					
					responce=xmlhttp.responseText;
					output = responce.split("<br>");
					
					tbl     = document.createElement("table");
        			tblBody = document.createElement("tbody");
        			for(i=0;i<output.length;i++)
        			{
	        			trTable = document.createElement("tr")
	        
						tdTable = document.createElement('td');//responce;
						
						if(i == output.length-1)
							 tdTable.className = 'msg';
						
						tdText = document.createTextNode(output[i]);
						
						tdTable.appendChild(tdText);
						trTable.appendChild(tdTable);
						tblBody.appendChild(trTable);
        			}
					
					tbl.appendChild(tblBody);
					msgDiv.appendChild(tbl);
				}
				else
				{
					msgDiv.innerHTML = "Sending in progress ...";
				}
			}*/
			// to here...
			if(formName == 'updPassword')
			{
				if(xmlhttp.readyState==4)
				{
					responce=xmlhttp.responseText;
					if(responce==0)
					{
						alert("Invalid Current Password.")
						obj=document.getElementById('oldpwd');
						obj.value="";
						obj.focus();
					}
					else if(responce!=1)
					{
						alert(responce);
						obj=document.getElementById('oldpwd');
						obj.value="";
						obj.focus();
					}
				}
			}
			else if(formName == 'insClient' || formName == 'updClient')
			{
				dropDown=document.getElementById('cat');
				if(xmlhttp.readyState==4)
				{
					responce=xmlhttp.responseText;
					catList=responce.split('<elem>')
					dropDown.options.length=catList.length;
					for(i=0;i<catList.length;i++)
					{				
					   if(catList[i])
					   {
					   	// for updClient selected value... checks 'coz this is 'undefined' in insClient...
					   	if(typeof(selectedCat)!='undefined')
								if(selectedCat == catList[i])
									dropDown.options[i].selected=true;
					    	dropDown.options[i].value=catList[i];
					    	dropDown.options[i].text=catList[i];
					   }
					   else
					   {
					   	dropDown.options.length=catList.length-1;
					   }
					}
				}
				else
				{
					dropDown.options.length=1;
					dropDown.options[0].text='Loading...';
				}
			}
			else if(formName == 'getClientsId' ||formName == 'sndNewsLetterId')
			{
				dropDown=document.getElementById('cat');
				if(xmlhttp.readyState==4)
				{
					catList = new Array('All'); 	//adds all option to the Drop Down...
					responce=xmlhttp.responseText;
					catListT=responce.split('<elem>')
					catList = catList.concat(catListT);
					dropDown.options.length=catList.length;
					for(i=0;i<catList.length;i++)
					{				
					   if(catList[i])
					   {
					    	dropDown.options[i].value=catList[i];
					    	dropDown.options[i].text=catList[i];
					   }
					   else
					   {
					   	dropDown.options.length=catList.length-1;
					   }
					}
				}
				else
				{
					dropDown.options.length=1;
					dropDown.options[0].text='Loading...';
				}
			}
			else if(formName == 'insRequestDoc')
			{
				addBtn = document.getElementById('add')
				if(xmlhttp.readyState==4)
				{
					attachName = document.getElementById('attachmentName').value;
					if(xmlhttp.responseText == '1')
					{
						createTableElement(attachName)
					}
					else
					{
						alert(xmlhttp.responseText);
					}
					addBtn.disabled = false;
					addBtn.value = 'Add';
				}
				else
				{
					addBtn.value = 'Adding...';
					addBtn.disabled = true;
					
				}
			}
				onChTriggered = 0;
	}
}

function showConfirmation(msg)
{
	return confirm(msg);
}

var checked=0;
function checkAll(count)
{
  	var x=document.getElementsByTagName("input");
	for(i=0;i<x.length;i++)
		if(x[i].type == 'checkbox')
		{
			if(x[i].checked == false)
			{
				x[i].checked = true;
				checked++;
			}
		}
}

function uncheckAll(count)
{
	checked = 0;
  	var x=document.getElementsByTagName("input");
	for(i=0;i<x.length;i++)
		if(x[i].type == 'checkbox')
			x[i].checked = false;
}

//to confirm deletion
function confirmAction(msg,checked,type)
{

	if(checked==0)
	{
		if(type=='qClient')
			alert('Please select Qualified Client(s) to be removed.');
		else if(type=='followUp')
			alert('Please select Qualified Client(s) to Send Follow Up.');
		else if(type=='client')
			alert('Please select Client(s) to Send News Letter.');
		else if(type=='Picture')
			alert('Please select Picture(s) to be removed.');
		else if(type=='Client')
			alert('Please select Client(s) to be removed.');
		else if(type=='appointment')
			alert('Please select Potential Client(s) to Send Appointment Letter.');
		else if(type=='emailDoc')
			alert('Please select Attachment(s) to be removed.');
		else if(type=='clientDocs')
			alert('Please select Individual Document(s) to be removed.');
		else if(type=='docReq')
			alert('Please select Request(s) to be removed.');
		return false;
	}
	else
	{
	result=confirm(msg);
	}
	return result;
}

/*
 ****************Validation begins here...*********************
 */

function validate(field, msg, elementType)
{
	with (field)
	{
		//For text or textareas...
		if(elementType=='text' || elementType=='textarea'|| elementType=='password' || elementType=='file')
		{
			if (value==null||value=="")
			{
				alert(msg);
				return false;
			}
		}
		//For drop downs...
		else if(elementType=='select-one')
		{
			if (selectedIndex==0)
			{
				alert(msg);
				return false;
			}
		}
		else if(elementType=='checkbox')
		{
			if(checkboxIsChecked == 0)
				if (checked == false)
				{
					alert(msg);
					return false;
				}
		}
		else 
		{
			return true;
		}
	}
}
//E mail validation function.
function validateEmail(field, msg)
{
	with (field)
	{
		apos=value.indexOf("@")
		dotpos=value.lastIndexOf(".")
		
		if (apos<1||dotpos-apos<2) 
		{
			alert(msg);
			value='';
			return false;
		}
		else 
		{
			return true;
		}
	}
}

function confirmationValidation(msg)
{
	pass = document.getElementsByName(passwordFieldName);
	passC = document.getElementsByName(confirmationFieldName);
	
	if(pass[0].value != passC[0].value)
	{
		alert(msg);
		passC[0].value = '';
		passC[0].focus();
		return false;
	}
	else
		return true;
}

function isChecked(caller)
{
   if(caller.checked==true)
     checkboxIsChecked++;
   else
     checkboxIsChecked--;
}

/**
 * validateForm(string form element[, string tag ids[, string flag]])
 * thisForm = Form name.
 * elementIds = Should be an element ID seperated by commas. ( Eg:'name,address' ).
 * flag = 'email' -> Validates email format.
 *		  'password' -> Validates password confirmation format.
 *		  'email+password' -> Validates email format & password confirmation.
 * Usage : validateForm(this, 'name,address') -> Validates only the 'name' & 'address'.
 *  	   validateForm(this) -> Validates all the fields.
 *		   validateForm(this,'*') -> Validates all the fields. Same as validateForm(this).
 *		   validateForm(this,'*','email+password') -> Validates email format & password confirmation.
 **/
var checkboxIsChecked = 0;
var passwordFieldName = 'newPassword';	//should give the name of the password field.
var confirmationFieldName = 'confirmation';	//should give the name of the password confirmation field.
var emailFieldName = 'email';	//should give the name of the email field.
var emailSecFieldName = 'emailSec';	//should give the name of the email field.
function validateForm(thisForm, elementIds, flag, msg)
{
	var alertText;
	with (thisForm)
		{
			//Checks whether the Ids were given.
			if(typeof elementIds != 'undefined' && elementIds!='*')
			{
				if(typeof msg != 'undefined')
					alertText = msg;
				else
					alertText='All fields marked with * needs to be complete in full.';
				var tempElementIdArray = new Array();
				var tempElementsArray = new Array();
				var i;
				
				tempElementIdArray = elementIds.split(',');
				
				for(i in tempElementIdArray)
				{
					tempElementsArray[i] = elements[tempElementIdArray[i]];
				}
				
				if(validateThis(tempElementsArray, alertText, flag) == false)
					return false;
			}
			//Else validates all the fields.
			else
			{
				if(typeof msg != 'undefined')
					alertText = msg;
				else
					alertText='All fields need to be complete in full.';
				if(validateThis(elements, alertText, flag) == false)
					return false;
			}
		}
}
function validateThis(elementObj, alertText, flag)
{
	for (i=0;i<elementObj.length;i++) 
	{/*alert(elementObj[i].type);*/
		if(elementObj[i].disabled)
		{
			continue;
		}
		//Text field validation done here...
		if(elementObj[i].type=='text')
		{
			// if the email second is set it validates here...
			if(elementObj[i].name == emailSecFieldName)
			{
				//if the element doesn't contain any value, it doesn't validates...
				if(elementObj[i].value != '')
				if(typeof flag != 'undefined' && flag.indexOf("Sec")!=-1)
					if (validateEmail(elementObj[i], "Invalid e-mail address format.", elementObj[i].type)==false)
					{
						elementObj[i].focus();
						return false;
					}
			}
			//Checks whether text fields are empty...
			else if (validate(elementObj[i], alertText, elementObj[i].type)==false)
				{
					elementObj[i].focus();
					return false;
				}
	 			
			//E mail validation is done here...	
			else if(elementObj[i].name == emailFieldName)
			{
				if(typeof flag != 'undefined' && flag.indexOf("email")!=-1)
					if (validateEmail(elementObj[i], "Invalid e-mail address format.", elementObj[i].type)==false)
					{
						elementObj[i].focus();
						return false;
					}
			}
			
		}
		//Drop down validation done here...
		else if(elementObj[i].type=='select-one')
		{
			if (validate(elementObj[i], alertText, elementObj[i].type)==false)
				{
					elementObj[i].focus();
					return false;
				}
		}
		//Textarea validation done here...
		else if(elementObj[i].type=='textarea')
		{
			if (validate(elementObj[i], alertText, elementObj[i].type)==false)
			{
				elementObj[i].focus();
				return false;
			}
		}
		else if(elementObj[i].type=='password')
		{
			if (validate(elementObj[i], alertText, elementObj[i].type)==false)
			{
				elementObj[i].focus();
				return false;
			}
		}
		else if(elementObj[i].type=='checkbox')
		{
			if (validate(elementObj[i], alertText, elementObj[i].type)==false)
			{
				elementObj[i].focus();
				return false;
			}
		}
		else if(elementObj[i].type=='file')
		{
			if(!elementObj[i].disabled)
			if (validate(elementObj[i], alertText, elementObj[i].type)==false)
			{
				elementObj[i].focus();
				return false;
			}
		}
		
	}
	if(typeof flag != 'undefined' && flag.indexOf("password")!=-1)
		if(confirmationValidation('New Password and Confirmation must be identical.') == false)
		{
			return false;
		}
}

function validChars(e, goods) {
	var key, keychar;
	key = (window.event) ? window.event.keyCode : ((e) ? e.which : null);
	if (key == null) return true;

	keychar = String.fromCharCode(key);
	keychar = keychar.toLowerCase();
	goods = goods.toLowerCase();

	if (goods.indexOf(keychar) != -1)
		return true;

	if (key==null || key==0 || key==8 || key==9 || key==13 || key==27)
		return true;

	return false;
}


//-------------------------------------------------
//-----------------coded by Kamala-----------------
//-------------------------------------------------
function validateInsDocForm(elementIds)
{
	var tempElementIdArray = new Array();
	var tempElementsArray = new Array();
	var i;
	var count=1;
	var parts='';
	
	tempElementIdArray = elementIds.split(',');
	var isValid =false;
	
	for(i in tempElementIdArray)
	{
		var titleId = document.getElementById(tempElementIdArray[i]).value;
		if(titleId.replace(/^\s+|\s+$/g, '')!='')
		{
			isValid = true;
			parts = (parts=='')?parts+count:parts+":"+count;
		}
		count = count+1;
	}
	if(isValid)
	{
		document.getElementById("completedParts").value=parts;
		return true;
	}
	else
	{
		alert("At least one Document Name need to be complete.");
		document.getElementById("title1").value='';
		document.getElementById("title2").value='';
		document.getElementById("title3").value='';
		document.getElementById("title4").value='';
		document.getElementById("title5").value='';
		document.getElementById("title1").focus();
		return false;
	}
}
//-------------------------------------------------
//-------------------------------------------------