/* 
*   +------------------------------------------------------------------------------+ 
*       The CCL
*   +------------------------------------------------------------------------------+ 
*       Copyright (c) 2005 - 2006 Phoenix Login Solutions Pvt Ltd  
*   +------------------------------------------------------------------------------+ 
*       Disclaimer Notice(s)                                                          
*       This software is provided "as is", without warranty of any kind, express or
*		implied, including  but not limited  to the warranties of  merchantability,
*		fitness for a particular purpose and noninfringement. In no event shall the
*		authors or  copyright  holders be  liable for any claim,  damages or  other
*		liability, whether  in an  action of  contract, tort  or otherwise, arising
*		from,  out of  or in  connection with  the software or  the  use  or  other
*		dealings in the software.
*   +------------------------------------------------------------------------------+ 
*       Author(s): Naveen Kumar
*   +------------------------------------------------------------------------------+ 
*		default.js
*		handles javascript functions
*   +------------------------------------------------------------------------------+ 
*		Release:
*		Version 1.0 - Initial Release
*   +------------------------------------------------------------------------------+ 
*/
 
/** function to check for validations if fields are empty **/
function validateForm(f, rqF, rqL) {
	var alertMsg	= "";
	var alertLen	= alertMsg.length;

	for(i=0; i < f.elements.length; i++) {
		obj		= f.elements[i];
		
		for(j=0; j < rqF.length; j++) {
			if(obj.name == rqF[j]) {
							
				 switch(obj.type) {
					   case	'text':
					   case 'textarea':
					   case	'password':
						   if((obj.value == '') || (obj.value == null)) {
								alertMsg = alertMsg + rqL[j] + '\n';
							} 
						break;
						case 'select-one':
						   if(obj.selectedIndex == 0) {
								alertMsg = alertMsg + rqL[j] + '\n';
							} 
						break;

				 }
			}
		}
	}

	if(alertMsg.length > alertLen) {
	  	alertMsg	= "Please complete following fields...\n" + alertMsg;
		alert(alertMsg);
		return false;
	} else {
		return true;
	} 

return false;
}

/** function to set tr color**/
function setBox(mode, tr, rowColor) {
	
	trObj	= document.getElementById(tr);
	trCol	= (mode == true) ? "#DEDEFF" : rowColor;

	/** set color */
	trObj.bgColor	= trCol;
}

/** function to set action **/
function setAction(f, elem, what) {
	alert(elem);
	check	= false;
	num		= 0;
	action	= f.action.value;

	//if no action selected.. */
	if(action == "" || action == "- select -"/** needed for mozilla/opera browser */) {
		alert('Error\r\nPlease select action from the drop down list!');
		return false;
	}

	/** check if any items selected */
	for(i=0; i < f.elements.length; i++) {
		if(f.elements[i].name == elem) {
			if(f.elements[i].checked == true) {
				check = true;
				num++;
			}
		}
	}

	/** if no utems selected */
	if(check == false) {
		alert("Error\r\nPlease select item(s) from the "+what+" list to "+action+"!");
		return false;
	}

	/** confirm */
	ask		= confirm(action+" "+what+"\r\nAre you sure you want to "+action+" "+num+" "+what+"(s)?");
	return ask;

}//end setAction


// To confirm for delete of message in front end by registered users
function confirmDelete(screen, action, id, what, where) {

	if( (screen == 'registeredit') || (screen == 'mybook')) {
		ID		= "mID";
		page	= "";
	}
	if(screen == 'mymessage') {
		ID		= "privmsg_id";
		page	= "&action=preview&privid="+id;
	}
	ask	= confirm("Are you sure you want to delete "+what+" from "+where+"?");
	if(ask) {
		document.location.href = "?"+"screen="+screen+"&"+"action="+action+"&"+ID+"="+id+"&"+"cnt=1";
	}
	else {
		document.location.href = "?"+"screen="+screen+page;
	}
}

/* To remove all white spaces in a string */
function Trim(TRIM_VALUE){
if(TRIM_VALUE.length < 1){
return"";
}
TRIM_VALUE = RTrim(TRIM_VALUE);
TRIM_VALUE = LTrim(TRIM_VALUE);
if(TRIM_VALUE==""){
return "";
}
else{
return TRIM_VALUE;
}
} //End Function

/* To remove all white spaces in end of a string */
function RTrim(VALUE){
var w_space = String.fromCharCode(32);
var v_length = VALUE.length;
var strTemp = "";
if(v_length < 0){
return"";
}
var iTemp = v_length -1;

while(iTemp > -1){
if(VALUE.charAt(iTemp) == w_space){
}
else{
strTemp = VALUE.substring(0,iTemp +1);
break;
}
iTemp = iTemp-1;

} //End While
return strTemp;

} //End Function

/* To remove all white spaces in front of a string */
function LTrim(VALUE){
var w_space = String.fromCharCode(32);
if(v_length < 1){
return"";
}
var v_length = VALUE.length;
var strTemp = "";

var iTemp = 0;

while(iTemp < v_length){
if(VALUE.charAt(iTemp) == w_space){
}
else{
strTemp = VALUE.substring(iTemp,v_length);
break;
}
iTemp = iTemp + 1;
} //End While
return strTemp;
} //End Function

/* To check given fields are empty or not */
function checkEmpty(f, chkF) {

for(i=0; i < chkF.length; i++) {
	var fld  = document.getElementById(chkF[i]).value;
	var name = Trim(fld);
	if(name != '') break;
}
if(name == '') {
	alert('Please enter atleast any one of the fields');
	return false
} else {
	return true;
}
return false;
}
/****
**    EMAIL VALIDATION
****/
function emailCheck (emailStr) {

var checkTLD=1;
var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
var emailPat=/^(.+)@(.+)$/;
var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
var validChars="\[^\\s" + specialChars + "\]";
var quotedUser="(\"[^\"]*\")";
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
var atom=validChars + '+';
var word="(" + atom + "|" + quotedUser + ")";
var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
var matchArray=emailStr.match(emailPat);

	if (matchArray==null) {
		//alert("Email address seems incorrect (check @ and .'s)");
		return "Invalid Email Id.";
		//return str_er;
		//return false;
		}
var user=matchArray[1];
var domain=matchArray[2];
for (i=0; i<user.length; i++) {
		if (user.charCodeAt(i)>127) {
			return "Invalid Email Id.";
			//return false;
		}
	}
for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) {
			return "Email Id contains invalid characters.";
			//return false;
	}
	}
if (user.match(userPat)==null) {
		return "Invalid Email Id.";
		//return false;
}
var IPArray=domain.match(ipDomainPat);
if (IPArray!=null) {
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				return "Destination IP address is invalid!";
				//return false;
			}
		}
		return "";
		//return true;
}
var atomPat=new RegExp("^" + atom + "$");
var domArr=domain.split(".");
var len=domArr.length;
for (i=0;i<len;i++) {
		if (domArr[i].search(atomPat)==-1) {
			return "Invalid Email Id.";
			//return false;
		}
	}
if (checkTLD && domArr[domArr.length-1].length!=2 && 
		domArr[domArr.length-1].search(knownDomsPat)==-1) {
		return "Invalid Email Id. ";
		//return false;
}
if (len<2) {
		return "This address is missing a hostname!";
		//return false;
}
		return "";
		//return true;
	}
	
/* ADD BY SRAVANTHI */

var preEl;						/** selected element */
var orgBColor;					/** old bg color */
var orgTColor;					/** old text color */
var newBColor	= "#F9E4BF";	/** new bg color #CDF2FE*/
var newTColor	= "#000000";	/** new text color */
/*************************
@ grid selector function
@ param id
*************************/
function adminGrid(gridTR) {
	//alert('=======>'+gridTR.id);
	if(typeof(preEl) != "undefined") {
		preEl.bgColor = orgBColor; 
		try {
			ChangeTextColor(preEl, orgTColor);
		}
		catch(e) {;}
	}

	orgBColor		= gridTR.bgColor;
	orgTColor		= gridTR.style.color;
	gridTR.bgColor	= newBColor;

	try {
		ChangeTextColor(gridTR, newTColor);
	}
	catch(e) {;}
	preEl			= gridTR;
	parent.preEl	= gridTR;
}

function ChangeTextColor(a_obj, a_color){  ;
	for (i=0;i<a_obj.cells.length;i++){//put condition before increase!!!!!
		a_obj.cells(i).style.color = a_color; 
	}
}
function goPrev(f) {
	
	f.SearchAction.value=0; 
	f.search_2.value	='';
	f.action.value='save';
	f.submit();
}

function goNext(f) {
	f.SearchAction.value= 1;
	f.search_2.value	='';
	f.action.value='save';
	f.submit();
}

/* END */