
var initMsg;
var msgs;
var isSubmit;
var insertField = ""; //Stores the reference of the text object within which tag is inserted.
var nextIndex= 1;	 // Stores the index of the attachments which are created dynamically

function resetMsgs(mainMsg) {
	msgs = "";
	isSubmit = true;
	//initMsg = mainMsg;
	initMsg = "";
}

function isBlank(formVar, fieldList, capList, capMsg)
{
	var len = fieldList.length;
	var curMsg = "";
	for(var i = 0; i < len; i++)
	{
		var curField = fieldList[i];
		var curValue = formVar.elements[curField].value;
		// start
		var isBlank = true;
		for(var j = 0; j < curValue.length; j++)
		{
			var c = curValue.charAt(j);
			if((c != ' ') && (c != "\n") && (c != "\t"))
			{
				isBlank = false;
				break;
			}
		}
		if(isBlank == true)
		{
			curMsg += capList[i] + ", ";
		}
		// end
	}

	if(curMsg != "")
	{
		curMsg = curMsg.substr(0, curMsg.length - 2);
		curMsg += " " + capMsg;
		addMsg(curMsg);
	}
}

function validateVehicleRegNumber(formVar, fieldList, capList, capMsg)
{
	var len = fieldList.length;
	var curMsg = "";
	for(var i = 0; i < len; i++)
	{
		var curField = fieldList[i];
		var curValue = formVar.elements[curField].value;
		// start
		if(curValue != "")
		{

			if(!(curValue.match(/^([a-zA-ZåÅäÄöüÖÁÀÉÈÒÓÙÚÜáàèéòóùúÝìíýÌÍÎÊÛÔËûïÌëË]{1,3})([0-9]{1,3})$/)) && !(curValue.match(/^([a-zA-ZåÅäÄöüÖÁÀÉÈÒÓÙÚÜáàèéòóùúÝìíýÌÍÎÊÛÔËûïÌ]{1,3})-([0-9]{1,3})$/)) && !curValue.match(/^([0-9]{1,3})([a-zA-ZZäÄÖöÅå]{1,3})$/) && !curValue.match(/^([0-9]{1,3})-([a-zA-ZZåÅäÄöüÖÁÀÉÈÒÓÙÚÜáàèéòóùúÝìíýÌÍÎÊÛÔËûïÌ]{1,3})$/))
			{
       			curMsg += capList[i]+", ";
    		}
    	}
    	// end
	}

   	if(curMsg != "")
   	{
		curMsg = curMsg.substr(0, curMsg.length - 2);
		curMsg += " " + capMsg;
		addMsg(curMsg);
	}
}

function validateString(formVar, fieldList, capList, capMsg)
{
	var len = fieldList.length;
	var curMsg = "";
	for(var i = 0; i < len; i++)
	{
		var curField = fieldList[i];
		var curValue = formVar.elements[curField].value;
		// start
		if(curValue != "")
		{
			if(! (curValue.match(/^['_&#\/,.a-zA-Z0-9 !@\?åÅäÄöüÖÁÀÉÈÒÓÙÚÜáàèéòóùúÝìíýÌÍÎÊÛÔËûïÌëË\n\r\f-]*$/) ))
			{
       			curMsg += capList[i]+", ";
    			}
    		}
    	// end
	}

   	if(curMsg != "")
   	{
		curMsg = curMsg.substr(0, curMsg.length - 2);
		curMsg += " " + capMsg;
		addMsg(curMsg);
	}
}

function validateNumber(formVar, fieldList, capList, capMsg)
{
	var len = fieldList.length;
	var curMsg = "";
	for(var i = 0; i < len; i++)
	{
		var curField = fieldList[i];
		var curValue = formVar.elements[curField].value;
		// start
		if(curValue != "")
		{
			if(! (curValue.match(/^[0-9]*$/) ))
			{
       			curMsg += capList[i]+", ";
    		}
    	}
    	// end
	}

   	if(curMsg != "")
   	{
		curMsg = curMsg.substr(0, curMsg.length - 2);
		curMsg += " " + capMsg;
		addMsg(curMsg);
	}
}

function validateMoreThanZero(formVar,curField,msg)
{
	var curValue = formVar.elements[curField].value;
	var curMsg="";
	var valid=true;
	if(curValue != "")
	{
		if(parseInt(curValue) < 1)
		{
			msg = " "+msg+".";
			valid=false;
    	}
    }

	if(valid == false)
   	{
		addMsg(msg);
	}
}
function validate_url(formVar, fieldList, capList, capMsg)
{
	var len = fieldList.length;
	var curMsg = "";
	for(var i = 0; i < len; i++)
	{
		var curField = fieldList[i];
		var curValue = formVar.elements[curField].value;
		// start
		if(curValue != "")
		{
			if(! (curValue.match(/^[_\a-zA-Z0-9åÅäÄöÖüÜ\n\r\f-]*$/) ))
			{
       			curMsg += capList[i]+", ";
    			}
    		}
    	// end
	}

   	if(curMsg != "")
   	{
		curMsg = curMsg.substr(0, curMsg.length - 2);
		curMsg += " " + capMsg;
		addMsg(curMsg);
	}
}

function validate_party_url(formVar, fieldList, capList, capMsg)
{
	var len = fieldList.length;
	var curMsg = "";
	for(var i = 0; i < len; i++)
	{
		var curField = fieldList[i];
		var curValue = formVar.elements[curField].value;
		// start
		if(curValue != "")
		{
			if(! (curValue.match(/^[_\a-zA-Z0-9\n\r\f-]*$/) ))
			{
       			curMsg += capList[i]+", ";
    			}
    		}
    	// end
	}

   	if(curMsg != "")
   	{
		curMsg = curMsg.substr(0, curMsg.length - 2);
		curMsg += " " + capMsg;
		addMsg(curMsg);
	}
}

function validatePhoneFaxMobile(formVar, fieldList, capList, capMsg)
{
	var len = fieldList.length;
	var curMsg = "";

	for(var i = 0; i < len; i++)
	{
		var curField = fieldList[i];
		var curValue = formVar.elements[curField].value;
		var isBlank = true;
		// start
		for(var j = 0; j < curValue.length; j++)
		{
			var c = curValue.charAt(j);
			if((c != ' ') && (c != "\n") && (c != "\t"))
			{
				isBlank = false;
				break;
			}
		}
		if(! isBlank)
		{
			if(! curValue.match(/^[0-9\s\+-]{1,25}$/) )
			{
				curMsg += capList[i]+", ";
			}
		}
		// end
	}

   	if(curMsg != "")
   	{
		curMsg = curMsg.substr(0, curMsg.length - 2);
		curMsg += " " + capMsg;
		addMsg(curMsg);
	}
}

function validateEmail(formVar, fieldList, capList, capMsg)
{
	var len = fieldList.length;
	var curMsg = "";
	for(var i = 0; i < len; i++)
	{
		var curField = fieldList[i];
		var curValue = formVar.elements[curField].value;
		// start
		if(curValue != "")
		{
			var isValid = true;
   			if (!(/^.+@.+\..{2,4}$/.test(curValue)))
   			{
				isValid = false;
	    		}
			if (curValue.match(/[\(\)\<\>\,\;\:\\\/\"\[\]]/))
			{
				isValid = false;
    			}
    			if(!isValid)
    			{
    				curMsg += capList[i] + ", ";
	    		}
		}
		// end
	}

   	if(curMsg != "")
   	{
		curMsg = curMsg.substr(0, curMsg.length - 2);
		curMsg += " " + capMsg;
		addMsg(curMsg);
	}
}

function showMsgs()
{
	if (msgs != "")
	{
		msgs = initMsg + msgs;
		alert(msgs);
		isSubmit = false;
	}
}

function addMsg(localMsg)
{
	//msgs = msgs + "\n--> " + localMsg;
	msgs = msgs + localMsg;
}

function doSubmit(formVar)
{
	formVar.save.value = "submit";
	if(isSubmit)
	{
		return true;
		//formVar.submit();
	}
	else
	{
		//formVar.elements[0].focus();
		return false;
	}
}

function validateContact(fieldNm, curMsg)
{
	var curValue = fieldNm.value;
	var isBlank = true;
	for(var j = 0; j < curValue.length; j++)
	{
		var c = curValue.charAt(j);
		if((c != ' ') && (c != "\n") && (c != "\t"))
		{
			isBlank = false;
			break;
		}
	}
	if(! isBlank)
	{
		if(! curValue.match(/^[0-9\s\+-]{5,25}$/) )
		{
			addMsg(curMsg);
		}
	}
	return;
}
function validateZip(formVar, curMsg)
{
	var curValue = formVar.zip.value;
	if(! (curValue.match(/^([A-Za-z0-9\s\-]{0,10})$/)))
	{
		addMsg(curMsg);
	}
	return;
}
function confirmAction(confirmMsg, forwardURL)
{
	if(confirm(confirmMsg))
	{
		window.location.replace(forwardURL);
	}
}

//Function to open new pop up window with fixed size
function openWin(win_nm,width,height,nm)
{
	var h=screen.height;
	h=h-height;

	var myfeatures="directories=no,location=no,menubar=no,status=no,titlebar=no,toolbar=no,resizable=yes,scrollbars=yes,width="+width+",height="+h+",left=0,top=0";

	if(nm == "")
		nm="new";

	var newImg=window.open(win_nm,nm,myfeatures);
	newImg.focus();
}

/*function openPopup(img_nm,w,h,nm) {

	var myfeatures='directories=no,location=no,menubar=no,status=no,titlebar=no,toolbar=no,resizable=yes,top=0,left=0,scrollbars=yes,width='+w+',height='+h+',left=0,top=0';

	if(nm == '') {
		nm='new';
	}

	var newImg=open(img_nm,nm,myfeatures);
	newImg.focus();
}*/

function checkMail(formVar,checkName,email,msg)
{
	var isValid = validateEmailAdd(formVar,email)
	if (! isValid)
	{
		alert(msg);
		formVar.elements[checkName].checked = false;
		return false;
	}
}

function fnMail(formVar,checkName,selName,email,mailCount,msg1,msg2)
{
	var isValid = validateEmailAdd(formVar,email)
	if (! isValid)
	{
		alert(msg1);
		formVar.elements[checkName].checked = false;
		return false;
	}
	else
	{
		if (mailCount > 1)
		{
			if(formVar.elements[checkName].checked)
			{
				formVar.elements[selName].disabled = false;
			}
			else
			{
				formVar.elements[selName].disabled = true;
			}
			return true;
		}
		else
		{
			alert(msg2);
			formVar.elements[checkName].checked = false;
			return false;
		}
	}
}

function validateEmailAdd(formVar, field)
{
	var curValue = formVar.elements[field].value;
	// start
	if(curValue != "")
	{
		var isValid = true;
   		if (!(/^.+@.+\..{2,4}$/.test(curValue)))
   		{
			isValid = false;
	    }
		if (curValue.match(/[\(\)\<\>\,\;\:\\\/\"\[\]]/))
		{
			isValid = false;
    	}
	}
	else
	{
		isValid = false;
	}
	return isValid;
	// end
}

function SelectRowsperPage(theForm,rowsPerPage)
{
		var url;
		url = theForm.script.value;
		theForm.method="POST";
		theForm.action=url + "?rowsPerPage=" + rowsPerPage;
		theForm.submit();
}

/***************** Functions to check & uncheck all checkboxes starts here**************************************/
	function AllChecked(f, obj)
	{
		ml = f;
		len = ml.elements.length;
		for(var i = 0 ; i < len ; i++)
		{
			if (ml.elements[i].name == obj && !ml.elements[i].checked)
			{
				return false;
			}
		}
		return true;
	}

	function Toggle(e, f, obj)
	{
		if (e.checked)
		{
			f.toggleAll.checked = AllChecked(f, obj);
		}
		else
		{
			f.toggleAll.checked = false;
		}
	}
	function ToggleAll(e, f, obj)
	{
		if (e.checked)
		{
			CheckAll(f, obj);
		}
		else
		{
			ClearAll(f, obj);
		}
	}
	function CheckAll(f, obj)
	{
		var ml = f;
		var len = ml.elements.length;
		for (var i = 0; i < len; i++)
		{
			var e = ml.elements[i];
			if (e.name == obj)
			{
				Check(e);
			}
		}
		ml.toggleAll.checked = true;
	}
	function ClearAll(f, obj)
	{
		var ml = f;
		var len = ml.elements.length;
		for (var i = 0; i < len; i++)
		{
			var e = ml.elements[i];
			if (e.name == obj)
			{
				Clear(e);
			}
		}
		ml.toggleAll.checked = false;
	}
	function Check(e)
	{
		e.checked = true;
	}
	function Clear(e)
	{
		e.checked = false;
	}
	function isChecked(ml, obj, msg)
	{
		var curMsg = "";
		var count = 0;
//		var ml = document.frmImpMem_3;
		var len = ml.elements.length;
		for (var i = 0; i < len; i++)
		{
			var e = ml.elements[i];
			if (e.name == obj && e.checked == true)
			{
				count++;
			}
		}
		if (count == 0)
		{
			curMsg = msg;
			addMsg(curMsg);
		}
	}

	function removeRec(ml, obj, msg)
	{
		var curMsg = "";
		var count = 0;
//		var ml = document.frmImpMem_3;
		var len = ml.elements.length;
		for (var i = 0; i < len; i++)
		{
			var e = ml.elements[i];
			if (e.name == obj && e.checked == true)
			{
				count++;
			}
		}
		if (count == 0)
		{
			curMsg = msg;
			addMsg(curMsg);
		}
		else
		{
			ml.listDel.value = "delete";
		}
	}

	function selectRec(ml, obj, msg)
	{
		var curMsg = "";
		var count = 0;
		var len = ml.elements.length;
		for (var i = 0; i < len; i++)
		{
			var e = ml.elements[i];
			if (e.name == obj && e.checked == true)
			{
				count++;
			}
		}
		if (count == 0)
		{
			curMsg = msg;
			addMsg(curMsg);
		}
	}

	function checkVal(formVar,msg)
	{
		if ((parseInt(formVar.tot_company.value)<1) && (parseInt(formVar.tot_person.value)<1) && (parseInt(formVar.tot_sperson.value)<1))
		{
			addMsg(msg);
		}
	}
	/***************** Functions to check & uncheck all checkboxes starts here (for lease mailing feature)**************************************/
	function ToggleAllForLease(e, f, obj)
	{
		if (e.checked)
		{
			CheckAllForLease(e.name, f, obj);
		}
		else
		{
			ClearAllForLease(e.name, f, obj);
		}
	}

	function ToggleForLease(checkname,e, f, obj)
	{
		if (e.checked)
		{
			f.elements[checkname].checked = AllChecked(f, obj);
		}
		else
		{
			f.elements[checkname].checked = false;
		}
	}

	function CheckAllForLease(checkname,f, obj,count)
	{
		var ml = f;
		var len = ml.elements.length;
		for (var i = 0; i < len; i++)
		{
			var e = ml.elements[i];
			if (obj == 'CP')
			{
				if(e.id.substr(0,2) == obj)
				{
					Check(e);
					if (count == 'T')
					{
						f.no_of_mails.value=parseInt(f.no_of_mails.value)-1;
					}
				}
			}
			else
			{
				if (e.name == obj)
				{
					Check(e);
					if (count == 'T')
					{
						f.no_of_mails.value=parseInt(f.no_of_mails.value)+1;
					}
				}
			}
		}
		ml.elements[checkname].checked = true;
	}

	function ClearAllForLease(checkname,f, obj,count)
	{
		var ml = f;
		var len = ml.elements.length;
		for (var i = 0; i < len; i++)
		{
			var e = ml.elements[i];
			if (obj == 'CP')
			{
				if(e.id.substr(0,2) == obj)
				{
					Clear(e);
					if (count == 'T')
					{
						f.no_of_mails.value=parseInt(f.no_of_mails.value)-1;
					}
				}
			}
			else
			{
				if(e.name == obj)
				{
					Clear(e);
					if (count == 'T')
					{
						f.no_of_mails.value=parseInt(f.no_of_mails.value)-1;
					}
				}
			}
		}
		ml.elements[checkname].checked = false;
	}

	function ToggleForCompany(e, f, obj,count)
	{
		if (e.checked)
		{
			CheckAllForLease(e.name, f, obj,count);
		}
		else
		{
			ClearAllForLease(e.name, f, obj,count);
		}
	}

	function ToggleForPerson(e, f, obj)
	{
		if (e.checked)
		{
			CheckAllForLease(e.name, f, obj);
		}
		else
		{
			ClearAllForLease(e.name, f, obj);
		}
	}

/***************** Functions to check & uncheck all checkboxes ends here**************************************/

//[ respa-ng-Feature Requests-1700 ] Facility of easy Tag - inserting within creating content for e-mails
//http://gforge.remres.com/tracker/?func=detail&atid=224&aid=1700&group_id=28

/***************** Functions which facilitates easy insertion of Tags start here ****************************/

/**
* Fuctionality of easy inserting the tag consists of three function which are addTag(), insertAtCursor()
* and initTxtField. Now When cursor is located in text area where the Tag is to be inserted. function initTxtField()
* is called. Which initalizes global variable insertField with the current text object in which currently text is to
* be inserted.Now when user click on any Tag to insert it, function addTag() is called, which calls the function
* insertAtCursor() with two parameters, one is the field within which the text to be inserted and the other is Tag
* which is to be inserted and inserts the Tag at current location of the cursor.
*/

function addTag(tag,msg)
{
	if(insertField=="")
		alert(msg);
	else
		insertAtCursor(insertField, tag);
}

function insertAtCursor(insertField, myTag)
{
	// For IE
	if (document.selection)
	{
		insertField.focus();
		sel = document.selection.createRange();
		sel.text = myTag;
	}
	// For Mozilla
	else if (insertField.selectionStart || insertField.selectionStart == '0')
	{
		var startPos = insertField.selectionStart;
		var endPos = insertField.selectionEnd;

		insertField.value = insertField.value.substring(0, startPos)
		+ myTag
		+ insertField.value.substring(endPos, insertField.value.length);
		insertField.selectionEnd = startPos + myTag.length;
		insertField.focus();
	}
	else
	{
		insertField.value += myTag;
	}
}

function initTxtField(txtField)
{
	insertField = txtField;
}

/***************** Functions which facilitates easy insertion of Tags ends here  ****************************/

/* Functions for adding attachments dynamically start here */
//[ respa-ng-Change Requests-3643 ] Attachments improvmenthttp://gforge.remres.com/tracker/?func=detail&atid=261&aid=3643&group_id=28
var countAttach=0;       //contains the total number of attachments
function addFileAttach(cap1,cap2,imagePath)
{
	if(document.createElement)
	{
		var div = document.createElement('div');
		div.setAttribute('id',nextIndex);

		var inputTag=document.createElement('input');
		inputTag.setAttribute('type','file');
		inputTag.setAttribute('style','border: 1px solid #000000;');
		inputTag.setAttribute('class','element');
		inputTag.setAttribute('name','attachDoc[]');
		inputTag.setAttribute('size','35');

		div.appendChild(inputTag);

		var newLink=document.createElement('a');
		newLink.setAttribute('href',"javascript:removeFileAttach("+nextIndex+",'"+cap1+"')");
		newLink.setAttribute('class','dlink');

		var linkImg=document.createElement('img');
		linkImg.setAttribute('src',imagePath + "/delete.gif");
		linkImg.setAttribute('alt',cap2);
		linkImg.setAttribute('title',cap2);
		linkImg.setAttribute('border','0');

		newLink.appendChild(linkImg);

		var space = document.createTextNode('    ');
		div.appendChild(space);
		div.appendChild(newLink);

		document.getElementById('attach').appendChild(div);
		nextIndex++;

		countAttach++;

		if(countAttach >= 1)
		{
			var attachLink = document.getElementById('attachLink');
			var linkText1 = document.getElementById('attachLink').firstChild;
			attachLink.removeChild(linkText1);
			var linkText2 = document.createTextNode(cap1);
			attachLink.appendChild(linkText2);
		}
	}
}

function removeFileAttach(i,cap1)
{
	if(document.createElement)
	{
		var div=document.getElementById(i);
		document.getElementById('attach').removeChild(div);
		countAttach--;
	}
	if(countAttach==0)
	{
		var attachLink = document.getElementById('attachLink');
		var linkText3 = document.getElementById('attachLink').firstChild;
		attachLink.removeChild(linkText3);
		var linkText4 = document.createTextNode(cap1);
		attachLink.appendChild(linkText4);
	}


}
/* Functions for adding attachments dynamically end here */

function submitNav(url,pageFrom)
{
	if(pageFrom != '')
		document.forms.pagingFrm.pagesFrom.value = pageFrom;
	document.forms.pagingFrm.action = url;
	document.forms.pagingFrm.submit();
}

/** 
* chagne for [respa-ng-Bugs][4768] Importing members-facility doesn't work.
* use the cookie for select all member to guest facility and delete all facility.
*
* function getCookie(byname)					::	get the cookie value
* function setCookie(name,value,time)			::  set the cookie value
* function deleteCookie(name)					::	delete perticuler cookie's all value.
* function deleteMGCookie(name,value) 			::  delete the given cookie's value's some part.
* function checkMGChk(v1,f,obj,tChk)		
* function ClearMGChk(mmVal,cookiName,hiddenVal)
* function isMGChecked(ml, v1, v2, msg)
*
**/

/*current cookie function is user for copy all member to guest in event part :: 20050117 :: date(YYYYMMDD)*/
function getCookie(byname)
{
	byname=byname+"=";
  	nlen = byname.length;
  	fromN = document.cookie.indexOf(byname)+0;
    if((fromN) != -1) {
    	fromN +=nlen 
    	toN=document.cookie.indexOf(";",fromN)+0;
    	if(toN == -1) {
    		toN=document.cookie.length;
		} 
		return unescape(document.cookie.substring(fromN,toN));
    } return null;
}

/*current cookie function is user for copy all member to guest in event part :: 20050117 :: date(YYYYMMDD)*/
function setCookie(name,value,time){
	exp = new Date();
	if((name==null)||(value==null)) 
   		return false;
   	if(time==null) 
	   	time=365*86400000; 
	exp.setTime(exp.getTime()+time);
   	document.cookie =escape(name)+"="+escape(value)+"; "+"expires="+exp.toGMTString();
   	return true;
} 
	
/*current cookie function is user for copy all member to guest in event part :: 20050117 :: date(YYYYMMDD)*/	
function deleteCookie(name) {
   	if (getCookie(name)) {
       	document.cookie = name + "=" +"; "+"expires=Thu, 01-Jan-70 00:00:01 GMT";
   	}
} 	

/* fucntion user for delete the cookie vlaue*/
function deleteMGCookie(name,value) {
	if(getCookie(name)){
		var allcookie = getCookie(name);
		var stP = allcookie.search(value);
		var enP = value.length+1;
		var nVal = allcookie.substring(0,stP)+""+allcookie.substring(stP+enP);
		deleteCookie(name);
		setCookie(name,nVal);
	}
}

/* fucntion for checking the load time member/guest checkbox is selected or not. */					
function checkMGChk(v1,f,obj,tChk)
{
	var frm = f;
	var len = frm.elements.length;
	
	if( v1 != null && v1 != "")
	{
		for (var i = 0; i < len; i++)
		{
			var e = f[i];
			if (e.name == obj)
			{
				var sr = v1.search(e.value);			
				if(sr >= 0){
					e.checked = false;
					if(tChk)	Toggle(e, frm, obj);
				}
			}
		}
	}
}		

function ClearMGChk(mmVal,cookiName,hiddenVal)
{
	var unChkVal = getCookie(cookiName);
	if(!mmVal.checked){
		
		if(unChkVal == null || unChkVal == ""){
			unChkVal = mmVal.value+",";
		}
		else
			unChkVal += mmVal.value+",";
			
		hiddenVal.value=unChkVal;
		setCookie(cookiName,unChkVal);
	} else {
		deleteMGCookie(cookiName,mmVal.value);
		oldClearVal=getCookie(cookiName);
		unChkVal=oldClearVal;
		hiddenVal.value=unChkVal;
	}
	return unChkVal;
}

/* function is  for checking the check the chk value is selected or not :: minimum one check box need to check */
function isMGChecked(ml, v1, v2, msg)
{
	var curMsg = "";
	var count = 0;
	
	var v1_val = ml.elements[v1.name].value;
	var v1_arr;
	
	if( v1_val != "" && v1_val != 'null'){
		v1_val = v1_val.substring(0,v1_val.length-1);
		v1_arr = v1_val.split(",");
	}
	var v2_val = v2.value;
	var v2_arr;
	if( v2_val != "" && v2_val != null){
		v2_val = v2_val.substring(0,v2_val.length-1);
		v2_arr = v2_val.split(",");
	}
	
	if( v1_arr != null && v2_arr != null){
		if( v1_arr.length == v2_arr.length){
			curMsg = msg;
			addMsg(curMsg);
		}
	}
}
	