/**
 * Javascript file for Sugar
 *
 * LICENSE: The contents of this file are subject to the SugarCRM Professional
 * End User License Agreement ("License") which can be viewed at
 * http://www.sugarcrm.com/EULA.  By installing or using this file, You have
 * unconditionally agreed to the terms and conditions of the License, and You
 * may not use this file except in compliance with the License.  Under the
 * terms of the license, You shall not, among other things: 1) sublicense,
 * resell, rent, lease, redistribute, assign or otherwise transfer Your
 * rights to the Software, and 2) use the Software for timesharing or service
 * bureau purposes such as hosting the Software for commercial gain and/or for
 * the benefit of a third party.  Use of the Software may be subject to
 * applicable fees and any use of the Software without first paying applicable
 * fees is strictly prohibited.  You do not have the right to remove SugarCRM
 * copyrights from the source code or user interface.
 *
 * All copies of the Covered Code must include on each user interface screen:
 *  (i) the "Powered by SugarCRM" logo and
 *  (ii) the SugarCRM copyright notice
 * in the same form as they appear in the distribution.  See full license for
 * requirements.
 *
 * Your Warranty, Limitations of liability and Indemnity are expressly stated
 * in the License.  Please refer to the License for the specific language
 * governing these rights and limitations under the License.  Portions created
 * by SugarCRM are Copyright (C) 2005 SugarCRM, Inc.; All Rights Reserved.
 */



/**
 * Namespace for Sugar Objects
 */
var SUGAR = function() {
    return {
    	/**
    	 * Namespace for Homepage
    	 */
    	sugarHome: {},
    	/**
    	 * Namespace for Subpanel Utils
    	 */
    	subpanelUtils: {},
    	/**
    	 * AJAX status class 
    	 */ 
    	ajaxStatusClass: {},
    	/**
    	 * Tab selector utils
    	 */ 
    	tabChooser: {},
    	/**
    	 * General namespace for Sugar utils
    	 */
    	util: {},
    	savedViews: {},
    	/**
    	 * Dashlet utils
    	 */
    	dashlets: {},
    	unifiedSearchAdvanced: {},

    	themes: {},

    	searchForm: {},
    	language: {},
    	Studio:{}
    }
}();


/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
 * All Rights Reserved.
 * Contributor(s): ______________________________________..
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "-";
var minYear=1900;
var maxYear=2100;
var nameIndex = 0;
var typeIndex = 1;
var requiredIndex = 2;
var msgIndex = 3;
var jstypeIndex = 5;
var minIndex = 10;
var maxIndex = 11;
var compareToIndex = 7;
var allowblank = 8;
var validate = new Array();
var maxHours = 24;
var secondsSinceLoad = 0;
var inputsWithErrors = new Array();
var lastSubmitTime = 0;
var oldStartsWith = '';

function isSupportedIE() {
	var userAgent = navigator.userAgent.toLowerCase() ;

	// IE Check supports ActiveX controls
	if (userAgent.indexOf("msie") != -1 && userAgent.indexOf("mac") == -1 && userAgent.indexOf("opera") == -1) {
		var version = navigator.appVersion.match(/MSIE (.\..)/)[1] ;
		if(version >= 5.5 ) {
			return true;
		} else {
			return false;
		}
	}
}
var isIE = isSupportedIE();

// escapes regular expression characters
RegExp.escape = function(text) { // http://simon.incutio.com/archive/2006/01/20/escape
  if (!arguments.callee.sRE) {
    var specials = ['/', '.', '*', '+', '?', '|','(', ')', '[', ']', '{', '}', '\\'];
    arguments.callee.sRE = new RegExp('(\\' + specials.join('|\\') + ')', 'g');
  }
  return text.replace(arguments.callee.sRE, '\\$1');
}


function toggleDisplay(id) {		
	if(this.document.getElementById(id).style.display == 'none') {
		this.document.getElementById(id).style.display = '';
		if(this.document.getElementById(id+"link") != undefined) {
			this.document.getElementById(id+"link").style.display = 'none';
		}
	}
	else {
		this.document.getElementById(id).style.display = 'none'
		if(this.document.getElementById(id+"link") != undefined) {
			this.document.getElementById(id+"link").style.display = '';
		}
	}
}

function checkAll(form, field, value) {
	for (i = 0; i < form.elements.length; i++) {
		if(form.elements[i].name == field)
			form.elements[i].checked = value;
	}
}

function replaceAll(text, src, rep) {
	offset = text.toLowerCase().indexOf(src.toLowerCase());
	while(offset != -1) {
		text = text.substring(0, offset) + rep + text.substring(offset + src.length ,text.length);
		offset = text.indexOf( src, offset + rep.length + 1);
	}
	return text;
}

function addForm(formname) {
	validate[formname] = new Array();
}

function addToValidate(formname, name, type,required, msg) {
	if(typeof validate[formname] == 'undefined') {
		addForm(formname);
	}
	validate[formname][validate[formname].length] = new Array(name, type,required, msg);
}

function addToValidateRange(formname, name, type,required,  msg,min,max) {
	addToValidate(formname, name, type,required,  msg);
	validate[formname][validate[formname].length - 1][jstypeIndex] = 'range'
	validate[formname][validate[formname].length - 1][minIndex] = min;
	validate[formname][validate[formname].length - 1][maxIndex] = max;
}

function addToValidateDateBefore(formname, name, type, required, msg, compareTo) {
	addToValidate(formname, name, type,required,  msg);
	validate[formname][validate[formname].length - 1][jstypeIndex] = 'isbefore'
	validate[formname][validate[formname].length - 1][compareToIndex] = compareTo;
}

function addToValidateDateBeforeAllowBlank(formname, name, type, required, msg, compareTo, allowBlank) {
	addToValidate(formname, name, type,required,  msg);
	validate[formname][validate[formname].length - 1][jstypeIndex] = 'isbefore'
	validate[formname][validate[formname].length - 1][compareToIndex] = compareTo;
	validate[formname][validate[formname].length - 1][allowblank] = allowBlank;
}

function addToValidateBinaryDependency(formname, name, type, required, msg, compareTo) {
	addToValidate(formname, name, type, required, msg);
	validate[formname][validate[formname].length - 1][jstypeIndex] = 'binarydep';
	validate[formname][validate[formname].length - 1][compareToIndex] = compareTo;
}

function removeFromValidate(formname, name) {
	for(i = 0; i < validate[formname].length; i++){
		if(validate[formname][i][nameIndex] == name){
			validate[formname].splice(i, 1);
		}
	}
}

function toDecimal(original) {
	temp = Math.round(original*100)/100;
	if((original * 100) % 100 == 0)
		return temp + '.00';
	if((original * 10) % 10 == 0)
		return temp + '0';
	return temp
}

function isInteger(s) {
	if(typeof num_grp_sep != 'undefined' && typeof dec_sep != 'undefined')
		s = unformatNumber(s, num_grp_sep, dec_sep).toString();

	var i;
    for (i = 0; i < s.length; i++){
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function isNumeric(s) {
  if(!/^(?:\$\s*)?(?:(?:\d{0,3}(?:[, ]\d{0,3})*[, ])+\d{3}|\d+)(?:\.\d*)?(?:\s*\$)?$/.test(s)){
  //if(!/^-*[0-9\.]+$/.test(s)) {
   		return false
   }
   else {
		return true;
   }
}

function stripCharsInBag(s, bag) {
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary(year) {
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   }
   return this
}

var date_reg_positions = {'Y': 1,'m': 2,'d': 3};
var date_reg_format = '([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})'
function isDate(dtStr) {
	if(dtStr.length== 0) {
		return true;
	}
	myregexp = new RegExp(date_reg_format)
	if(!myregexp.test(dtStr))
		return false

return true
}

function getDateObject(dtStr) {
	if(dtStr.length== 0) {
		return true;
	}

	myregexp = new RegExp(date_reg_format)

	if(myregexp.exec(dtStr)) var dt = myregexp.exec(dtStr)
	else return false;

	var yr = dt[date_reg_positions['Y']];
	var mh = dt[date_reg_positions['m']];
	var dy = dt[date_reg_positions['d']];
	var date1 = new Date();
	date1.setFullYear(yr); // xxxx 4 char year
	date1.setMonth(mh-1); // 0-11 Bug 4048: javascript Date obj months are 0-index
	date1.setDate(dy); // 1-31
	return date1;
}

function isBefore(value1, value2) {
	var d1 = getDateObject(value1);
	var d2 = getDateObject(value2);

	return d2 >= d1;
}
function isValidEmail1(emailStr) {
	if(emailStr.length== 0) return false;
	if(!(/^[a-zA-Z0-9._-]+[a-zA-Z0-9._-]?[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+[a-zA-Z0-9.-]?[a-zA-Z0-9._-]+\.[a-zA-Z]{2,4}$/.test(emailStr))) 
		return false;
	else
		return true;
}

function isValidEmail(emailStr1) {
	if(emailStr1.length == 0) 
		return true;
	if(!(/^[a-zA-Z0-9._-]+[a-zA-Z0-9._-]?[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+[a-zA-Z0-9.-]?[a-zA-Z0-9._-]+\.[a-zA-Z]{2,4}$/.test(emailStr1)))
		return false;
	else
		return true;
}

function isValidPhone(phonenum) {
	if(phonenum!=null)
		val = phonenum;
	if (val== '-') return false;
	var myPhone = val.split("-");
	cc = myPhone[0];
	if(myPhone[2] != null)
		number = myPhone[1]+"-"+myPhone[2];
	else
		number = myPhone[1];

	flag=0;
	if(cc!="") {
		if(/^\+[0-9]{2}$/.test(cc))
			flag=1;
		else
			flag=0;
	} 

	if(number !="") {
		if(/^([0-9]{2}|[0-9]{3}|[0-9]{4})[ ]([0-9]{3}|[0-9]{4}|[0-9]{5})-?([0-9]+)?$/.test(number))
			flag=1;
		else
			flag=0;
	} else {
		flag=0;
	}
	if(flag ==1)
		return true;
	else
		return false;
}
function isValidPhone1(phonenum1) {


	
	if(phonenum1!=null)
		val = phonenum1;
	if (val== '-') return false;
	var myPhone = val.split("-");
	cc = myPhone[0];
	aa = myPhone[1];

	if(myPhone[2] != null)
		number = myPhone[1]+"-"+myPhone[2];
	else
		number = myPhone[1];
	flag=0;
	if(cc!="" && cc!=null) {
		if(/^\+[0-9]{2}$/.test(cc))
			flag=1;
		else
			flag=0;
	} 

	if(number !="") {
		if(/^([0-9]{2}|[0-9]{3}|[0-9]{4})[ ]([0-9]{3}|[0-9]{4}|[0-9]{5}|[0-9]{6})-?([0-9]+)?$/.test(number))
			flag=1;
		else
			flag=0;
	} else {
		flag=0;
	}
	if(flag ==1)
		return true;
	else
		return false;
	
}

function isValidAreaCode(acode)
{
	if(acode!="" && acode!=null)
	{
		if(/^\d{3}|d{4}|d{5}|d{6}$/.test(acode))
			return true;
		else
			return false;
	} 
}
//-------  allow alphanumeric input and blank and & - <>() @ # $ 

function isValidVarchar3(varStr3) {
	if(varStr3.length== 0) return false;
	if(!/^[a-zA-Z0-9-\ \<>()&$#@,.]+$/.test(varStr3))
		return false;
	else 
		return true;
}

function isValidCountryCode(ccode)
{
	if((ccode == '000') || (ccode == '00') || (ccode == '0') || (ccode == '+') || (ccode == '+0') || (ccode == '+00') || (ccode == '+000'))
	{
		return false;
	}
	if(ccode.length == 4)
	{
		var ifExists = ccode.search(/\+/);
		if(ifExists == -1)
			return false;
	}
	if(/^[\+]{0,1}[0-9]{1,3}$/.test(ccode))
		return true ;
	else
		return false;
}

function isValidPhoneNumber(pcode)
{
	if(pcode!="" && pcode!=null && pcode.length >= 4 && pcode.length <=15)
	{
		//if(/^([0-9]{2}|[0-9]{3}|[0-9]{4})[ ]([0-9]{3}|[0-9]{4}|[0-9]{5}|[0-9]{6})-?([0-9]+)?$/.test(pcode))
		//if(/^[0-9]{12}|[0-9]{13}|[0-9]{14}|[0-9]{15}$/.test(pcode))
		//if(/^\d{10}|d{11}d{12}|d{13}|d{14}|d{15}$/.test(pcode))
		var Chars = "0123456789-& !@#$%^*()_=+<>,./?;:'\"\\";
		for (var i = 0; i < pcode.length; i++)
		{ if (Chars.indexOf(pcode.charAt(i)) == -1)
			return false;
		}
			return true;
	} 
}

//----- Alphabetic format ----- //
function isValidVarchar(varStr) {
//alert(varstr);
	if(varStr.length== 0) return false;
	if(!(/^[a-zA-Z]+$/.test(varStr)))
		return false;
	else 
		return true;
}

function isValidVarchar(varStr) {
//alert(varstr);
	if(varStr.length== 0) return false;
	if(!(/^[a-zA-Z]+$/.test(varStr)))
		return false;
	else 
		return true;
}
//----- Numeric format ----- //
function isValidVarchar1(varStr1) {
	if(varStr1.length== 0) return false;
	if(!(/^[0-9]+$/.test(varStr1)))
		return false;
	else 
		return true;
}
//----- Alphanumeric format ----- //
function isValidVarchar2(varStr2) {
	if(varStr2.length== 0) return false;
	if(!/^[a-zA-Z0-9]+$/.test(varStr2))
		return false;
	else 
		return true;
}

/* function isValidAllSpecialCharacter(varStr3)
{
	if(varStr3.length== 0) return false;
	if(!/^[a-zA-Z-Ä_äÖöÜüß]+$/.test(varStr3))
		return false;
	else 
		return true;
} */

function isValidVarcharSpaces(varStr3)
{
	if(varStr3.length== 0) return false;
	if(!/^[a-zA-Z ]+$/.test(varStr3))
		return false;
	else 
		return true;
}

function isValidVarcharSpacesNum(varStr4)
{
	if(varStr4.length== 0) return false;
	if(!/^[a-zA-Z0-9]+$/.test(varStr4))
		return false;
	else 
		return true;
}

function isValidVarcharCommaNum(varStr5)
{
	if(varStr5.length== 0) return false;
	if(!/^[a-zA-Z0-9 , .]+$/.test(varStr5))
		return false;
	else 
		return true;
}
/* function isValidPassword(varStr5)
{
if(varStr4.length== 0) return false;
	if(!/^[a-zA-Z0-9]+$/.test(varStr4))
		return false;
	else 
		return true;
} */

function isValidVarcharNumSpaces(varStr6)
{
	if(varStr6.length== 0) return false;
	if(!/^[a-zA-Z0-9 @]+$/.test(varStr6))
		return false;
	else 
		return true;
}


function isFloat(floatStr) {
	floatStr = unformatNumber(floatStr, num_grp_sep, dec_sep).toString();
	if(floatStr.length== 0) {
		return true;
	}
	if(!/^[0-9\.]+$/.test(floatStr))
		return false
	return true	
}
var time_reg_format = "[0-9]{1,2}\:[0-9]{2}";
function isTime(timeStr) {
	time_reg_format = time_reg_format.replace('([ap]m)', '');
	time_reg_format = time_reg_format.replace('([AP]M)', '');
	if(timeStr.length== 0){
		return true;
	}
	//we now support multiple time formats
	myregexp = new RegExp(time_reg_format)
	if(!myregexp.test(timeStr))
		return false

	return true
}

function inRange(value, min, max) {
	return value >= min && value <= max;
}

function bothExist(item1, item2) {
	if(typeof item1 == 'undefined') { return false; }
	if(typeof item2 == 'undefined') { return false; }
	if((item1 == '' && item2 != '') || (item1 != '' && item2 == '') ) { return false; }
	return true;
}

function trim(s) {
	if(typeof(s) == 'undefined')  
		return s;
	while (s.substring(0,1) == " ") {
		s = s.substring(1, s.length);
	}
	while (s.substring(s.length-1, s.length) == ' ') {
		s = s.substring(0,s.length-1);
	}

	return s;
}


function check_form(formname) {
	var flag=false;
	if (typeof(siw) != 'undefined' && siw 
		&& typeof(siw.selectingSomething) != 'undefined' && siw.selectingSomething)
			return false;
	//return validate_form(formname, '');
	
 	if(validate_form(formname, ''))
	{
		if(formname == 'Registration' || formname == 'ContactsEditView')
		{
			//mergeNumberH();
			mergeNumberW();
			mergeNumberM(); 
		}
		if(formname == 'ForgotPassword' || formname == 'AcountCheck')
		{
			 $(document).ready(function(){
			 	if(formname == 'ForgotPassword')
				{
					var uname=document.ForgotPassword.portal_name.value;
					var email=document.ForgotPassword.email1.value;
				}
				if(formname == 'AcountCheck')
				{
				var uname=document.AcountCheck.portal_name.value;
				var email=document.AcountCheck.email1.value;
				}
				var result = $.ajax({ type:"POST",url: "chkCredential.php", data: {uname:uname,email:email}, async: false}).responseText;
				//$.post("http://localhost/bb/crm/trunk/chkPortalUserCredential.php",{uname:uname,email:email}, function(data){	
				if(result == 'p_not_exist') 
				{
					invalidTxt1="Check Portal user name.";
					$(this).next("span").show();
					flag=true;
						if(formname == 'ForgotPassword')
						{
							add_error_style('ForgotPassword', 'portal_name', invalidTxt1 + " " +	"");
						}
						if(formname == 'AcountCheck')
						{
							add_error_style('AcountCheck', 'portal_name', invalidTxt1 + " " +	"");
						}
				}
				else
				{
					if(result == 'not_exist')
					{
					invalidTxt1="Check email address.";
					$(this).next("span").show();
				//document.Registration.portal_name.value="";
						if(formname == 'ForgotPassword')
						{
							add_error_style('ForgotPassword', 'email1', invalidTxt1 + " " +	"");
						}
						if(formname == 'AcountCheck')
						{
							add_error_style('AcountCheck', 'email1', invalidTxt1 + " " +	"");
						}
					flag=true;
					}
				}
			});
		}
		if(flag == false)
			return true;
		else
			return false;
	}
	else
	{
		return false;
	}
}

function mergeNumberW()
{
	var phUpdate = document.getElementById("phone_work_n").value;
	var country_code = checkCountryCode(document.getElementById("phone_work_c").value);
	var area_code = document.getElementById("phone_work_a").value;
	var ext = document.getElementById("phone_work_e").value;
	
	if(phUpdate != "")
	{
		var phUpdate = phUpdate.replace(/[^0-9-]+/g,"");
		for($i=0;$i<=phUpdate.length;$i++)
		{
			if(phUpdate.charAt(phUpdate.length-1) == "-")
			{
				var phUpdate = phUpdate.substring(0,phUpdate.length-1);
			}
		}
		var dash = phUpdate.search(/-/g);
		if(dash != -1)
		{
			var hyphen = phUpdate.match(/-/g);
			var numberofDash = hyphen.length;
			if(numberofDash > 1)
			{
				for($j=1;$j<=numberofDash-1;$j++)
				{
					var phUpdate = phUpdate.replace("-","");
				}
			}
			else
			{
				var phUpdate = phUpdate;
			}
		}
		else
		{
			var phUpdate = phUpdate;
		}
	}
	if (area_code != "" && country_code != "" && phUpdate != "")
	{
		if(ext != "")
			document.getElementById("phone_work").value = country_code+"-"+area_code+"-"+phUpdate+"x"+ext;
		else
			document.getElementById("phone_work").value = country_code+"-"+area_code+"-"+phUpdate;
	}
}


function mergeNumberM()
{
	var phUpdate = document.getElementById("phone_mobile_n").value;
	var country_code = checkCountryCode(document.getElementById("phone_mobile_c").value);
	var area_code = document.getElementById("phone_mobile_a").value;
	var ext = document.getElementById("phone_mobile_e").value;
	
	if(phUpdate != "")
	{
		var phUpdate = phUpdate.replace(/[^0-9-]+/g,"");
		for($i=0;$i<=phUpdate.length;$i++)
		{
			if(phUpdate.charAt(phUpdate.length-1) == "-")
			{
				var phUpdate = phUpdate.substring(0,phUpdate.length-1);
			}
		}
		var dash = phUpdate.search(/-/g);
		if(dash != -1)
		{
			var hyphen = phUpdate.match(/-/g);
			var numberofDash = hyphen.length;
			if(numberofDash > 1)
			{
				for($j=1;$j<=numberofDash-1;$j++)
				{
					var phUpdate = phUpdate.replace("-","");
				}
			}
			else
			{
				var phUpdate = phUpdate;
			}
		}
		else
		{
			var phUpdate = phUpdate;
		}
	}
	if (!(area_code == "" && country_code == "" && phUpdate == "")) 
	{
		if(ext != "")
			document.getElementById("phone_mobile").value = country_code+"-"+area_code+"-"+phUpdate+"x"+ext;
		else
			document.getElementById("phone_mobile").value = country_code+"-"+area_code+"-"+phUpdate;
	}
	if((document.getElementById("phone_mobile").value == '11') || (document.getElementById("phone_mobile").value == '+--'))
	{
		document.getElementById("phone_mobile").value = '';
	}
}

function mergeNumberH()
{
	var phUpdate = document.getElementById("phone_home_n").value;
	var country_code = checkCountryCode(document.getElementById("phone_home_c").value);
	var area_code = document.getElementById("phone_home_a").value;
	var ext = document.getElementById("phone_home_e").value;
	
	if(phUpdate != "")
	{
		var phUpdate = phUpdate.replace(/[^0-9-]+/g,"");
		for($i=0;$i<=phUpdate.length;$i++)
		{
			if(phUpdate.charAt(phUpdate.length-1) == "-")
			{
				var phUpdate = phUpdate.substring(0,phUpdate.length-1);
			}
		}
		var dash = phUpdate.search(/-/g);
		if(dash != -1)
		{
			var hyphen = phUpdate.match(/-/g);
			var numberofDash = hyphen.length;
			if(numberofDash > 1)
			{
				for($j=1;$j<=numberofDash-1;$j++)
				{
					var phUpdate = phUpdate.replace("-","");
				}
			}
			else
			{
				var phUpdate = phUpdate;
			}
		}
		else
		{
			var phUpdate = phUpdate;
		}
	}
	if (!(area_code == "" && country_code == "" && phUpdate == "")) 
	{
		if(ext != "")
			document.getElementById("phone_home").value = country_code+"-"+area_code+"-"+phUpdate+"x"+ext;
		else
			document.getElementById("phone_home").value = country_code+"-"+area_code+"-"+phUpdate;
	}
	if((document.getElementById("phone_home").value == '11') || (document.getElementById("phone_home").value == '+--'))
	{
		document.getElementById("phone_home").value = '';
	}
}

function checkCountryCode(country_code)
{
	var country_code = country_code.replace(/[^0-9]+/g,"");
	var len = country_code.length;
	for(i=0;i<=len;i++)
	{
		if(country_code.charAt(0) == "0")
			var country_code = country_code.substring(1,len);
	}
	return "+"+country_code;
}

function add_error_style(formname, input, txt) {
	inputHandle = eval("document." + formname + "['" + input + "']");
	style = get_current_bgcolor(inputHandle);
		
	if(inputHandle.parentNode.innerHTML.search(txt) == -1) {
		errorTextNode = document.createElement('span');
		errorTextNode.className = 'required';
		errorTextNode.innerHTML =  txt;
		inputHandle.parentNode.appendChild(errorTextNode);		
	}
	 inputHandle.style.backgroundColor = "#FF0000"; 

	inputsWithErrors.push(inputHandle);
	for(wp = 1; wp <= 10; wp++) {
		window.setTimeout('fade_error_style(style, ' + wp * 10 + ')', 1000 + (wp * 50));
	} 
	return false;
}



function add_error_style1(formname, input, txt) {
	inputHandle = eval("document." + formname + "['" + input + "']");
	style = get_current_bgcolor(inputHandle);
	
	if(inputHandle.parentNode.innerHTML.search(txt) == -1) {
		errorTextNode = document.createElement('span');
		errorTextNode.className = 'required';
		errorTextNode.innerHTML = txt;
		inputHandle.parentNode.appendChild(errorTextNode);		
	}
	inputsWithErrors.push(inputHandle);
	 var arr_ph=new Array("phone_work_c","phone_work_a","phone_work_n");
	 if(input == 'error1')
	{
		for(i=0;i<3;i++)
		{
			inputHandle = eval("document." + formname + "['"+ arr_ph[i]+"']");
			style = get_current_bgcolor(inputHandle);
			inputHandle.style.backgroundColor = "#FF0000"; 
			inputsWithErrors.push(inputHandle);
			for(wp = 1; wp <= 10; wp++) 
			{
				window.setTimeout('fade_error_style(style, ' + wp * 10 + ')', 1000 + (wp * 50));
			} 
		}
	}
	
	//alert(">>"+inputsWithErrors);inputsWithErrors[wp].parentNode.lastChild
	return false;
}




function get_current_bgcolor(input) {
	if(input.currentStyle) {// ie 
		style = input.currentStyle.backgroundColor;
		return style.substring(1,7);
	}
	else {// moz
		style = '';
		styleRGB = document.defaultView.getComputedStyle(input, '').getPropertyValue("background-color");
		comma = styleRGB.indexOf(',');
		style += dec2hex(styleRGB.substring(4, comma));
		commaPrevious = comma;
		comma = styleRGB.indexOf(',', commaPrevious+1);
		style += dec2hex(styleRGB.substring(commaPrevious+2, comma));
		style += dec2hex(styleRGB.substring(comma+2, styleRGB.lastIndexOf(')')));
		return style;
	}
}

function hex2dec(hex){return(parseInt(hex,16));}
var hexDigit=new Array("0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F");
function dec2hex(dec){return(hexDigit[dec>>4]+hexDigit[dec&15]);}

function fade_error_style(normalStyle, percent) {	
	errorStyle = 'f44366';
	var r1 = hex2dec(errorStyle.slice(0,2));
	var g1 = hex2dec(errorStyle.slice(2,4));
	var b1 = hex2dec(errorStyle.slice(4,6));
	
	var r2 = hex2dec(normalStyle.slice(0,2));
	var g2 = hex2dec(normalStyle.slice(2,4));
	var b2 = hex2dec(normalStyle.slice(4,6));


	var pc = percent / 100;

	r= Math.floor(r1+(pc*(r2-r1)) + .5);
	g= Math.floor(g1+(pc*(g2-g1)) + .5);
	b= Math.floor(b1+(pc*(b2-b1)) + .5);

	for(var wp = 0; wp < inputsWithErrors.length; wp++) {
		//inputsWithErrors[wp].style.backgroundColor = "#" + dec2hex(r) + dec2hex(g) + dec2hex(b);
		var bgcolor1= "#" + dec2hex(r) + dec2hex(g) + dec2hex(b);
		if(bgcolor1 == '#000000')
		{
			inputsWithErrors[wp].style.backgroundColor ="#FF0000";
		}
		else
		{
			inputsWithErrors[wp].style.backgroundColor =bgcolor1;
		}
			//alert("bg color="+inputsWithErrors[wp].style.backgroundColor);	
	}
}


function validate_form(formname, startsWith){
//	alert("in validate_form");
	if ( typeof (formname) == 'undefined')
	{
		return false;
	}
	if ( typeof (validate[formname]) == 'undefined')
	{
		return true;
	}
	var form = "document." + formname;
	var isError = false;
	var errorMsg = "";
	var strW = "";
	var strW_a = strW_c = strW_n = strW_e = 0;
	var strM = "";
	var strM_a = strM_c = strM_n = strM_e = 0;
	var strH = "";
	var strState="";
	//var strH_a = strH_c = strH_n = strH_e = 0;
	var str1="";var str2="";var str3="";
	/* var _date = new Date();
	if(_date.getTime() < (lastSubmitTime + 2000) && startsWith == oldStartsWith) { // ignore submits for the next 2 seconds
		return false;
	}
	lastSubmitTime = _date.getTime();
	oldStartsWith = startsWith; */
	
	for(var wp = 0; wp < inputsWithErrors.length; wp++) {
	//alert("line 682:"+inputsWithErrors[wp].parentNode.lastChild);
	//alert(">>>"+inputsWithErrors[wp].parentNode.lastChild);
	if(inputsWithErrors[wp].parentNode.id == 't1' || inputsWithErrors[wp].parentNode.id == 't3' || inputsWithErrors[wp].parentNode.id == 't2' || inputsWithErrors[wp].parentNode.id == 't4'  || inputsWithErrors[wp].parentNode.id == 't5')
	{
		//alert("in if"+inputsWithErrors[wp].parentNode.lastChild);
		errorTextNode = document.createElement('span');
		//errorTextNode.innerHTML = '<br />';
		inputsWithErrors[wp].parentNode.appendChild(errorTextNode);
	}
		inputsWithErrors[wp].parentNode.removeChild(inputsWithErrors[wp].parentNode.lastChild);
	} // remove previous error messages
	
	inputsWithErrors = new Array();
			var j=0;		
	for(var i = 0; i < validate[formname].length; i++){
//alert(validate[formname][i][nameIndex]);
		if(validate[formname][i][nameIndex].indexOf(startsWith) == 0){
			if(typeof eval(form + "['" + validate[formname][i][nameIndex] + "']" ) != 'undefined'){
				var bail = false;
				if(validate[formname][i][requiredIndex]){
				
					if(typeof eval(form + "['" + validate[formname][i][nameIndex] + "']") == 'undefined' || trim(eval(form + "['" + validate[formname][i][nameIndex] + "']" + ".value")) == ""){
							if( validate[formname][i][nameIndex] == 'phone_work' )//|| validate[formname][i][nameIndex] != 'phone_home' || validate[formname][i][nameIndex] != 'phone_mobile')
												j=0;
							else
							if(validate[formname][i][nameIndex] == 'phone_home')
												j=0;
							else
							if(validate[formname][i][nameIndex] == 'phone_mobile')
												j=0;
							else
							if(validate[formname][i][nameIndex] == 'portal_password')
												j=0;
							else
							{
							add_error_style(formname, validate[formname][i][nameIndex], requiredTxt +' ' + validate[formname][i][msgIndex]);
							isError = true;
							}
					}
				}
				if (validate[formname][i][nameIndex] == 'primary_address_country') {
					if(document.getElementById("primary_address_country").value=="select"){
					isError = true;
					add_error_style(formname, validate[formname][i][nameIndex], invalidTxt + " " +	validate[formname][i][msgIndex]);
					}
				}
				
		
				
				if(!bail){
						if (validate[formname][i][requiredIndex] == 1 ) {
			//alert(validate[formname][i][typeIndex]);
			//alert(validate[formname][i][nameIndex]);
					        switch(validate[formname][i][typeIndex]){
            				    case 'email':
									if(document.getElementById("email1").value!="") {
										
										if(!isValidEmail1(trim(eval(form + "['" + validate[formname][i][nameIndex] + "']" + ".value")))){
											isError = true;
											add_error_style(formname, validate[formname][i][nameIndex], invalidTxt + " " +	validate[formname][i][msgIndex]);
										}
									}
            					    break;
            				     case 'phone':
								 //alert(validate[formname][i][nameIndex]);
								
								 //alert("hi");
										//if(document.getElementById('phone_work_c'))
										if(formname != 'Registration')
										if(validate[formname][i][nameIndex] == 'phone_home' || validate[formname][i][nameIndex] == 'phone_work' || validate[formname][i][nameIndex] == 'phone_mobile')
										{
										
										if(isError == true)	isError=true;
										else isError= false; 
						
										}
										var phone_work_country = document.getElementById('phone_work_c').value;
										if(phone_work_country != "")
										{
											if(!isValidCountryCode(phone_work_country))
											{
												isError = true;
												//alert(invalidTxt);
												strW_c=1;
											}
										}
										//if(document.getElementById('phone_work_a'))
										if(document.getElementById('phone_work_a').value!="")
										{
												if(!isValidAreaCode(trim(eval(form + "['" + 'phone_work_a' + "']" + ".value"))))
												{
												isError = true;
												strW_a=1;
												}
										}
										//if(document.getElementById('phone_work_n'))
										if(document.getElementById('phone_work_n').value!="")
										{
												if(!isValidPhoneNumber(trim(eval(form + "['" + 'phone_work_n' + "']" + ".value"))))
												{
												isError = true;
												strW_n=1;
												}
										}
										if(document.getElementById('phone_work_e').value!="")
										{
												if(!isValidCountryCode(trim(eval(form + "['" + 'phone_work_e' + "']" + ".value"))))
												{
												isError = true;
												strW_e=1;
												}
										}
										//if(document.getElementById('phone_mobile_c'))
										var phone_mobile_country = document.getElementById('phone_mobile_c').value;
										if(phone_mobile_country != "")
										{
												if(!isValidCountryCode(phone_mobile_country))
												{
												isError = true;
												strM_c=1;
												//add_error_style(formname, 'phone_mobile_c', 'Invalid value : country code' + " " +	validate[formname][i][msgIndex]);
												}
										}
										//if(document.getElementById('phone_mobile_a'))
										if(document.getElementById('phone_mobile_a').value!="")
										{
												if(!isValidAreaCode(trim(eval(form + "['" + 'phone_mobile_a' + "']" + ".value"))))
												{
												isError = true;
												strM_a=1;
												//add_error_style(formname, 'phone_mobile_a', 'Invalid value : area code' + " " +	validate[formname][i][msgIndex]);
												}
										}
										//if(document.getElementById('phone_mobile_n'))
										if(document.getElementById('phone_mobile_n').value!="")
										{
												if(!isValidPhoneNumber(trim(eval(form + "['" + 'phone_mobile_n' + "']" + ".value"))))
												{
												isError = true;
												strM_n=1;
												//add_error_style(formname, 'phone_mobile_n', 'Invalid value : phone number'  + " " +	validate[formname][i][msgIndex]);
												}
										}
										if(document.getElementById('phone_mobile_e').value!="")
										{
												if(!isValidCountryCode(trim(eval(form + "['" + 'phone_mobile_e' + "']" + ".value"))))
												{
												isError = true;
												strM_e=1;
												}
										}
										//if(document.getElementById('phone_home_c'))
										
            					    break; 
								
            				    case 'time':
            					    if( !isTime(trim(eval(form+"['" + validate[formname][i][nameIndex] + "']" + ".value")))){
            						    isError = true;
            						    add_error_style(formname, validate[formname][i][nameIndex], invalidTxt + " " +	validate[formname][i][msgIndex]);
            					    } break;
            				    case 'date':
            				        if(!isDate(trim(eval(form + "['" + validate[formname][i][nameIndex] + "']" + ".value")))){
            						    isError = true;
            						    add_error_style(formname, validate[formname][i][nameIndex], invalidTxt + " " +	validate[formname][i][msgIndex]);
            					    }
                                    break;
            				    case 'alpha':
								//alert(validate[formname][i][nameIndex] );
									if(trim(eval(form + "['" + validate[formname][i][nameIndex] + "']" + ".value"))!="") {
										if(!isValidVarchar(trim(eval(form + "['" + validate[formname][i][nameIndex] + "']" + ".value")))){
											isError = true;
											add_error_style(formname, validate[formname][i][nameIndex], invalidTxt + " " +	validate[formname][i][msgIndex]);
										}
									}
            					    break;
								/*  case 'password':
												if(!isValidPassword(trim(eval(form + "['" + validate[formname][i][nameIndex] + "']" + ".value"))))
														{
															isError = true;
															add_error_style(formname, validate[formname][i][nameIndex], invalidTxt + " " +	validate[formname][i][msgIndex]);
														}
								 
								 break; */
            				    case 'varchar':
								if(trim(eval(form + "['" + validate[formname][i][nameIndex] + "']" + ".value"))!="") 
									{
										switch(validate[formname][i][nameIndex])
										{
										case 'first_name':
										case 'last_name': 
														if(!isValidVarchar(trim(eval(form + "['" + validate[formname][i][nameIndex] + "']" + ".value"))))
														{
															isError = true;
															add_error_style(formname, validate[formname][i][nameIndex], invalidTxt + " " +	validate[formname][i][msgIndex]);
														}
														break;
										case 'employees_c':
														if(!isNumeric(trim(eval(form + "['" + validate[formname][i][nameIndex] + "']" + ".value"))))
														{
															isError = true;
															add_error_style(formname, validate[formname][i][nameIndex], invalidTxt + " " +	validate[formname][i][msgIndex]);
														}		
														break;
										case 'primary_address_postalcode':
														if(!isValidVarchar2(trim(eval(form + "['" + validate[formname][i][nameIndex] + "']" + ".value"))))
														{
															isError = true;
															add_error_style(formname, validate[formname][i][nameIndex], invalidTxt + " " +	validate[formname][i][msgIndex]);
														} 
														break;
										case 'portal_name':
														if(!isValidVarcharSpacesNum(trim(eval(form + "['" + validate[formname][i][nameIndex] + "']" + ".value"))))
														{
															isError = true;
															add_error_style(formname, validate[formname][i][nameIndex], invalidTxt + " " +	validate[formname][i][msgIndex]);
														} 
														break;
										case 'primary_address_street':
														if(!isValidVarchar3(trim(eval(form + "['" + validate[formname][i][nameIndex] + "']" + ".value"))))
														{
															isError = true;
															add_error_style(formname, validate[formname][i][nameIndex], invalidTxt + " " +	validate[formname][i][msgIndex]);
														} 
														break;	
                                        case 'title':
														if(!isValidVarchar3(trim(eval(form + "['" + validate[formname][i][nameIndex] + "']" + ".value"))))
														{
															isError = true;
															add_error_style(formname, validate[formname][i][nameIndex], invalidTxt + " " +	validate[formname][i][msgIndex]);
														} 
														break;	
														
												
										case 'account_name':
														if(!isValidVarchar3(trim(eval(form + "['" + validate[formname][i][nameIndex] + "']" + ".value"))))
														{
															isError = true;
															add_error_style(formname, validate[formname][i][nameIndex], invalidTxt + " " +	validate[formname][i][msgIndex]);
														} 
														break;	
										case 'department':
														if(!isValidVarchar3(trim(eval(form + "['" + validate[formname][i][nameIndex] + "']" + ".value"))))
														{
															isError = true;
															add_error_style(formname, validate[formname][i][nameIndex], invalidTxt + " " +	validate[formname][i][msgIndex]);
														} 
														//alert("dept "+isError);
														break;
										case 'primary_address_city':
														if(!isValidVarchar3(trim(eval(form + "['" + validate[formname][i][nameIndex] + "']" + ".value"))))
														{
															isError = true;
															add_error_style(formname, validate[formname][i][nameIndex], invalidTxt + " " +	validate[formname][i][msgIndex]);
														} 
														break;	
														
										
										default  :		
														if(!isValidVarcharSpaces(trim(eval(form + "['" + validate[formname][i][nameIndex] + "']" + ".value"))))
														{
															isError = true;
															add_error_style(formname, validate[formname][i][nameIndex], invalidTxt + " " +	validate[formname][i][msgIndex]);
														} 
														 
														
										}
									}
            					    break;
            				    case 'int':
								if(trim(eval(form + "['" + validate[formname][i][nameIndex] + "']" + ".value"))!="") {
            					    if(!isInteger(trim(eval(form + "['" + validate[formname][i][nameIndex] + "']" + ".value")))){
            						    isError = true;
            						    add_error_style(formname, validate[formname][i][nameIndex], invalidTxt + " " +	validate[formname][i][msgIndex]);
            					    }
								}
            					    break;
            				    case 'float':
            					    if(!isFloat(trim(eval(form + "['" + validate[formname][i][nameIndex] + "']" + ".value")))){
            						    isError = true;
            						    add_error_style(formname, validate[formname][i][nameIndex], invalidTxt + " " +	validate[formname][i][msgIndex]);
            					    }
            					    break;
						    }
                        } 

						if(typeof validate[formname][i][jstypeIndex]  != 'undefined'/* && !isError*/){

							switch(validate[formname][i][jstypeIndex]){
							case 'range':
								if(!inRange(trim(eval(form + "['" + validate[formname][i][nameIndex] + "']" + ".value")), validate[formname][i][minIndex], validate[formname][i][maxIndex])){
									isError = true;
									add_error_style(formname, validate[formname][i][nameIndex], validate[formname][i][msgIndex] + " value " + eval(form + "['" + validate[formname][i][nameIndex] + "']" + ".value") + " is not within the valid range (" +validate[formname][i][minIndex] + " - " + validate[formname][i][maxIndex] +  ") ");
								}
							break;
							case 'isbefore':
								compareTo = form + "." + validate[formname][i][compareToIndex];
								if(	typeof compareTo != 'undefined'){
									if( trim(eval(compareTo + '.value')) == '' && (validate[formname][i][allowblank] == 'true') ) {
										date2 = '2200-01-01';
									} else {
										date2 = trim(eval(compareTo + '.value'));
									}
																		
									date1 = trim(eval(form + "['" + validate[formname][i][nameIndex] + "']" + ".value"));

									if(trim(date1).length != 0 && !isBefore(date1,date2)){ 		
										isError = true;
										add_error_style(formname, validate[formname][i][nameIndex], validate[formname][i][msgIndex] + "(" + date1 + ") is not before " + date2);
									}
								}
							break;
							case 'binarydep':
								compareTo = form + "." + validate[formname][i][compareToIndex];
								if( typeof compareTo != 'undefined') {
									item1 = trim(eval(form + "['" + validate[formname][i][nameIndex] + "']" + ".value"));
									item2 = trim(eval(compareTo + '.value'));
									if(!bothExist(item1, item2)) {
										isError = true;
										add_error_style(formname, validate[formname][i][nameIndex], validate[formname][i][msgIndex]);
									}
								}
							break;
							}
						}
					}
				}
			}
		}
		
		if(formname == "ContactsEditView")
		{
			if(document.getElementById("portal_rpassword").value != "")
			{
				
				document.getElementById("portal_password").value = document.getElementById("portal_rpassword").value;
				//alert(document.getElementById("portal_password").value);
			}
		}
		// for checking account name empty ? last_name.value : account_name.value , 11 april2009
		if(formname == 'Registration')
		{	if(document.Registration.account_name.value == '')
				document.Registration.account_name.value=document.Registration.last_name.value;
		}
		
//		var arrForExtraPhField = new Array('phone_work_a','phone_work_c','phone_work_n','phone_mobile_a','phone_mobile_c','phone_mobile_n','phone__a','',);
		
		if(document.getElementById('phone_work_a').value == '')
		{
			if(typeof eval(form + "['" + 'phone_work_a'+ "']") == 'undefined' || trim(eval(form + "['" + 'phone_work_a' + "']" + ".value")) == "")
			{
						//add_error_style(formname, 'phone_work_a', requiredTxt +' ' + 'Area Code');
						str1=str1+"Area Code,";
						isError = true;
			}
		}
		if(document.getElementById('phone_work_c').value == '')
		{
			if(typeof eval(form + "['" + 'phone_work_c'+ "']") == 'undefined' || trim(eval(form + "['" + 'phone_work_c' + "']" + ".value")) == "")
			{
						//add_error_style(formname, 'phone_work_c', requiredTxt +' ' + 'Country Code');
						str1=str1+"Country Code,";
						isError = true;
			}
		}
		if(document.getElementById('phone_work_n').value == '')
		{
			if(typeof eval(form + "['" + 'phone_work_n'+ "']") == 'undefined' || trim(eval(form + "['" + 'phone_work_n' + "']" + ".value")) == "")
			{
						//add_error_style(formname, 'phone_work_n', requiredTxt +' ' + 'Office Phone');
						str1=str1+"Office Number";
						isError = true;
			}
		}
		if(document.getElementById('primary_address_country').value == 'USA')
		{
			if(document.getElementById('primary_address_state').value == '')
			{
				strState="State";
			}
		}

 		if(str1 != '') 
		{
			add_error_style1(formname, 'error1', 'Missing Value '+str1); // missin value  
		}
			
		if(strW_c == 1) strW=strW+"Country Code,";
		if(strW_a == 1) strW=strW+"Area Code,";
		if(strW_n == 1) strW=strW+"Office Number";
		if(strW_e == 1) strW=strW+"Extension";
		if(strW)  add_error_style(formname, 'error1', 'Invalid value '+strW); // invalid value} 
		
		if(document.getElementById('primary_address_country').value == 'USA')
		{
		    if(strState == 'State') 
		    {
		        isError = true;
		        add_error_style(formname, 'primary_address_state', 'Missing required field: '+strState); // invalid value} 
		    }
		}
		//if(str3 != '')  add_error_style(formname, 'error3', requiredTxt + str3); // missin value  
		if(strM_c == 1) strM=strM+"Country Code,";
		if(strM_a == 1) strM=strM+"Area Code,";
		if(strM_n == 1) strM=strM+"Mobile Number";
		if(strM_e == 1) strM=strM+"Extension";
		if(strM)  add_error_style(formname, 'error3', 'Invalid value '+strM); // invalid value
		var k=false;
		if(formname == 'Registration')
		var uname=document.Registration.portal_name.value;
		if(uname != "")
	{
			 $.post("chkUser.php",{uname:uname}, function(data){	
			if(data == 'exist') {
				invalidTxt1="Portal user name already exist.";
				$(this).next("span").show();
				//document.Registration.portal_name.value="";
				isError=true;
				add_error_style('Registration', 'portal_name', invalidTxt1 + " " +	"");
			}
		});
	}
	var userCount=document.getElementById("employees_c").value;
		var userStr=userCount.replace('.',"");
		userStr=userStr.replace(',',"");
		document.getElementById("employees_c").value=userStr;
	if (isError == true) {
		var nw, ne, sw, se;
		if (self.pageYOffset) // all except Explorer
		{
			nwX = self.pageXOffset;
			seX = self.innerWidth;
			nwY = self.pageYOffset;
			seY = self.innerHeight;
		}
		else if (document.documentElement && document.documentElement.scrollTop) // Explorer 6 Strict
		{
			nwX = document.documentElement.scrollLeft;
			seX = document.documentElement.clientWidth;
			nwY = document.documentElement.scrollTop; 
			seY = document.documentElement.clientHeight;
		}
		else if (document.body) // all other Explorers
		{
			nwX = document.body.scrollLeft;
			seX = document.body.clientWidth;
			nwY = document.body.scrollTop;
			seY = document.body.clientHeight;
		}
		
		return false;
	}
	
	/* mergeNumberH();
	mergeNumberW();
	mergeNumberM(); */
	if(k == true) return false;
	return true;
	
}


/**
 * This array is used to remember mark status of rows in browse mode
 */
var marked_row = new Array;


/**
 * Sets/unsets the pointer and marker in browse mode
 *
 * @param   object    the table row
 * @param   interger  the row number
 * @param   string    the action calling this script (over, out or click)
 * @param   string    the default background color
 * @param   string    the color to use for mouseover
 * @param   string    the color to use for marking a row
 *
 * @return  boolean  whether pointer is set or not
 */
function setPointer(theRow, theRowNum, theAction, theDefaultColor, thePointerColor, theMarkColor) {
    var theCells = null;

    // 1. Pointer and mark feature are disabled or the browser can't get the
    //    row -> exits
    if ((thePointerColor == '' && theMarkColor == '')
        || typeof(theRow.style) == 'undefined') {
        return false;
    }

    // 2. Gets the current row and exits if the browser can't get it
    if (typeof(document.getElementsByTagName) != 'undefined') {
        theCells = theRow.getElementsByTagName('td');
    }
    else if (typeof(theRow.cells) != 'undefined') {
        theCells = theRow.cells;
    }
    else {
        return false;
    }

    // 3. Gets the current color...
    var rowCellsCnt  = theCells.length;
    var domDetect    = null;
    var currentColor = null;
    var newColor     = null;
    // 3.1 ... with DOM compatible browsers except Opera that does not return
    //         valid values with "getAttribute"
    if (typeof(window.opera) == 'undefined'
        && typeof(theCells[0].getAttribute) != 'undefined') {
        currentColor = theCells[0].getAttribute('bgcolor');
        domDetect    = true;
    }
    // 3.2 ... with other browsers
    else {
        currentColor = theCells[0].style.backgroundColor;
        domDetect    = false;
    } // end 3

    // 4. Defines the new color
    // 4.1 Current color is the default one
    if (currentColor == ''
        || currentColor.toLowerCase() == theDefaultColor.toLowerCase()) {
        if (theAction == 'over' && thePointerColor != '') {
            newColor              = thePointerColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor              = theMarkColor;
            marked_row[theRowNum] = true;
        }
    }
    // 4.1.2 Current color is the pointer one
    else if (currentColor.toLowerCase() == thePointerColor.toLowerCase()
             && (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])) {
        if (theAction == 'out') {
            newColor              = theDefaultColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor              = theMarkColor;
            marked_row[theRowNum] = true;
        }
    }
    // 4.1.3 Current color is the marker one
    else if (currentColor.toLowerCase() == theMarkColor.toLowerCase()) {
        if (theAction == 'click') {
            newColor              = (thePointerColor != '')
                                  ? thePointerColor
                                  : theDefaultColor;
            marked_row[theRowNum] = (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])
                                  ? true
                                  : null;
        }
    } // end 4

    // 5. Sets the new color...
    if (newColor) {
        var c = null;
        // 5.1 ... with DOM compatible browsers except Opera
        if (domDetect) {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].setAttribute('bgcolor', newColor, 0);
            } // end for
        }
        // 5.2 ... with other browsers
        else {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].style.backgroundColor = newColor;
            }
        }
    } // end 5

    return true;
} // end of the 'setPointer()' function

var json_objects = new Object();

function parseDate(input, format) {
	date = input.value;
	format = format.replace(/%/g, '');
	sep = format.charAt(1);
	yAt = format.indexOf('Y')
	// 1-1-06 or 1-12-06 or 1-1-2006 or 1-12-2006
	if(date.match(/^\d{1,2}[\/-]\d{1,2}[\/-]\d{2,4}$/) && yAt == 4) {
		if(date.match(/^\d{1}[\/-].*$/)) date = '0' + date;
		if(date.match(/^\d{2}[\/-]\d{1}[\/-].*$/)) date = date.substring(0,3) + '0' + date.substring(3,date.length);
		if(date.match(/^\d{2}[\/-]\d{2}[\/-]\d{2}$/)) date = date.substring(0,6) + '20' + date.substring(6,date.length);
	}
	// 06-11-1 or 06-1-1
	else if(date.match(/^\d{2,4}[\/-]\d{1,2}[\/-]\d{1,2}$/)) {
		if(date.match(/^\d{2}[\/-].*$/)) date = '20' + date;
		if(date.match(/^\d{4}[\/-]\d{1}[\/-].*$/)) date = date.substring(0,5) + '0' + date.substring(5,date.length);		
		if(date.match(/^\d{4}[\/-]\d{2}[\/-]\d{1}$/)) date = date.substring(0,8) + '0' + date.substring(8,date.length);		
	}
	else if(date.match(/^\d{4,8}$/)) { // digits only
		digits = 0;
		if(date.match(/^\d{8}$/)) digits = 8;// match for 8 digits
		else if(date.match(/\d{6}/)) digits = 6;// match for 5 digits
		else if(date.match(/\d{4}/)) digits = 4;// match for 5 digits
		else if(date.match(/\d{5}/)) digits = 5;// match for 5 digits
		
		switch(yAt) {
			case 0:
				switch(digits) {
					case 4: date = '20' + date.substring(0,2) + sep + '0' + date.substring(2, 3) + sep + '0' + date.substring(3,4); break;
					case 5: date = '20' + date.substring(0,2) + sep + date.substring(2, 4) + sep + '0' + date.substring(4,5); break;
					case 6: date = '20' + date.substring(0,2) + sep + date.substring(2, 4) + sep + date.substring(4,6); break;				
					case 8: date = date.substring(0,4) + sep + date.substring(4, 6) + sep + date.substring(6,8); break;
				}
				break;
			case 2:
				switch(digits) {
					case 4: date = '0' + date.substring(0,1) + sep + '20' + date.substring(1, 3) + sep + '0' + date.substring(3,4); break;
					case 5: date = date.substring(0,2) + sep + '20' + date.substring(2, 4) + sep + '0' + date.substring(4,5); break;
					case 6: date = date.substring(0,2) + sep + '20' + date.substring(2, 4) + sep + date.substring(4,6); break;				
					case 8: date = date.substring(0,2) + sep + date.substring(2, 6) + sep + date.substring(6,8); break;
				}
			case 4:
				switch(digits) {
					case 4: date = '0' + date.substring(0,1) + sep + '0' + date.substring(1, 2) + sep + '20' + date.substring(2,4); break;
					case 5: date = '0' + date.substring(0,1) + sep + date.substring(1, 3) + sep + '20' + date.substring(3,5); break;
					case 6: date = date.substring(0,2) + sep + date.substring(2, 4) + sep + '20' + date.substring(4,6); break;				
					case 8: date = date.substring(0,2) + sep + date.substring(2, 4) + sep + date.substring(4,8); break;
				}
				break;
		}
	}
	date = date.replace(/[\/-]/g, sep);
	input.value = date;
}


// -- start sugarListView class
// js functions used for ListView
function sugarListView() {
}

sugarListView.update_count = function(count, add) {
	if(typeof document.MassUpdate != 'undefined') {
		the_form = document.MassUpdate;
		for(wp = 0; wp < the_form.elements.length; wp++) {
			if(typeof the_form.elements[wp].name != 'undefined' && the_form.elements[wp].name == 'selectCount[]') {
				if(add)	the_form.elements[wp].value = parseInt(the_form.elements[wp].value) + count;
				else the_form.elements[wp].value = count;
			}	
		}
	}
}

sugarListView.prototype.send_form = function(select, currentModule, action, no_record_txt,action_module) {

	sugarListView.get_checks();
	// create new form to post (can't access action property of MassUpdate form due to action input) 
	var newForm = document.createElement('form');
	newForm.method = 'post';
	newForm.action = action;
	newForm.name = 'newForm';
	newForm.id = 'newForm';
	
	var uidTa = document.createElement('textarea');
	uidTa.name = 'uid';
	uidTa.style.display = 'none';
	if(select) { // use selected items	
		uidTa.value = document.MassUpdate.uid.value;
	}
	else { // use current page
		inputs = document.MassUpdate.elements;
		ar = new Array();
		for(i = 0; i < inputs.length; i++) {
			if(inputs[i].name == 'mass[]')
				ar.push(inputs[i].value);
		}
		uidTa.value = ar.join(',');
	}
	
	if(uidTa.value == '') { 
		//alert(no_record_txt);
		return false;
	}
	
	newForm.appendChild(uidTa);
	
	var moduleInput = document.createElement('input');
	moduleInput.name = 'module';
	moduleInput.type = 'hidden';
	moduleInput.value = currentModule;	
	newForm.appendChild(moduleInput);
	
	var actionInput = document.createElement('input');
	actionInput.name = 'action';
	actionInput.type = 'hidden';
	actionInput.value = 'index';	
	newForm.appendChild(actionInput);
	
	if (action_module!= '') {
		var actionModule = document.createElement('input');
		actionModule.name = 'action_module';
		actionModule.type = 'hidden';
		actionModule.value = action_module;	
		newForm.appendChild(actionModule);
	}

	document.MassUpdate.parentNode.appendChild(newForm);

	newForm.submit();

	return false;
}
//return a count of checked row.
sugarListView.get_checks_count = function() {
	ar = new Array();

	// build associated array of uids, associated array ensures uniqueness	
	inputs = document.MassUpdate.elements;
	for(i = 0; i < inputs.length; i++) {
		if(inputs[i].name == 'mass[]') 
			ar[inputs[i].value]	= (inputs[i].checked) ? 1 : 0; // 0 of it is unchecked
	}
	
	// build regular array of uids
	uids = new Array(); 
	for(i in ar) { 
		if(ar[i] == 1) uids.push(i);
	}
	
	return uids.length;
}

// saves the checks on the current page into the uid textarea
sugarListView.get_checks = function() {
	ar = new Array();

	if(document.MassUpdate.uid.value != '') {
		oldUids = document.MassUpdate.uid.value.split(',');
		for(uid in oldUids) ar[oldUids[uid]] = 1;	
	}
	
	// build associated array of uids, associated array ensures uniqueness	
	inputs = document.MassUpdate.elements;
	for(i = 0; i < inputs.length; i++) {
		if(inputs[i].name == 'mass[]') 
			ar[inputs[i].value]	= (inputs[i].checked) ? 1 : 0; // 0 of it is unchecked
	}
	
	// build regular array of uids
	uids = new Array(); 
	for(i in ar) { 
		if(ar[i] == 1) uids.push(i);
	}
	
	document.MassUpdate.uid.value = uids.join(',');

	if(uids.length == 0) return false; // return false if no checks to get 
	return true; // there are saved checks
}

sugarListView.prototype.save_checks = function(offset, moduleString) {
	checks = sugarListView.get_checks();
	eval('document.MassUpdate.' + moduleString + '.value = offset');
	if(typeof document.MassUpdate.massupdate != 'undefined') document.MassUpdate.massupdate.value = 'false';
	if(checks) document.MassUpdate.submit();
	return !checks;
}

sugarListView.prototype.check_item = function(cb, form) {
	if(cb.checked) sugarListView.update_count(1, true);
	else sugarListView.update_count(-1, true);
}

sugarListView.prototype.check_all = function(form, field, value) {
	// count number of items
	count = 0;
	
	for (i = 0; i < form.elements.length; i++) {
		if(form.elements[i].name == field) {
			if(form.elements[i].checked != value) count++;
			form.elements[i].checked = value;
		}
	}

	if(value) sugarListView.update_count(count, true);
	else sugarListView.update_count(-1 * count, true);
}
sugarListView.check_all = sugarListView.prototype.check_all;

sugarListView.prototype.check_boxes = function() {
	var inputsCount = 0;	
	var checkedCount = 0;
	var existing_onload = window.onload;
	var theForm = document.MassUpdate;

	if(typeof theForm.uid.value != 'undefined' && theForm.uid.value != "") {
		inputs_array = theForm.elements;
		checked_items = theForm.uid.value.split(",");
		for(wp = 0 ; wp < inputs_array.length; wp++) {
			if(inputs_array[wp].name == "mass[]") {
				inputsCount++;				
				for(i in checked_items) {
					if(inputs_array[wp].value == checked_items[i]) {
						checkedCount++;
						inputs_array[wp].checked = true;
					}
				}
			}
		}
		sugarListView.update_count(checked_items.length);
	}
	else {
		sugarListView.update_count(0)
	}
	if(checkedCount > 0 && checkedCount == inputsCount) 
		document.MassUpdate.massall.checked = true;
}

sugarListView.prototype.send_mass_update = function(mode, no_record_txt, del) {
	formValid = check_form('MassUpdate');
	if(!formValid) return false;
	
	var ar = new Array();
	if(del == 1) {
		var deleteInput = document.createElement('input');
		deleteInput.name = 'Delete';
		deleteInput.type = 'hidden';
		deleteInput.value = true;	
		document.MassUpdate.appendChild(deleteInput);
	}
	
	switch(mode) {
		case 'page': 
			document.MassUpdate.uid.value = '';
			for(wp = 0; wp < document.MassUpdate.elements.length; wp++) {
				if(typeof document.MassUpdate.elements[wp].name != 'undefined' 
					&& document.MassUpdate.elements[wp].name == 'mass[]') {
							ar.push(document.MassUpdate.elements[wp].value);
				}
			}			
			document.MassUpdate.uid.value = ar.join(',');
			if(document.MassUpdate.uid.value == '') {
				alert(no_record_txt);
				return false;
			}
			break;
		case 'selected':
			for(wp = 0; wp < document.MassUpdate.elements.length; wp++) {
				if(typeof document.MassUpdate.elements[wp].name != 'undefined' 
					&& document.MassUpdate.elements[wp].name == 'mass[]' 
						&& document.MassUpdate.elements[wp].checked) {
							ar.push(document.MassUpdate.elements[wp].value);
				}
			}			
			if(document.MassUpdate.uid.value != '') document.MassUpdate.uid.value += ',';
			document.MassUpdate.uid.value += ar.join(',');
			if(document.MassUpdate.uid.value == '') {
				alert(no_record_txt);
				return false;
			}
			break;
		case 'entire': 
			var entireInput = document.createElement('input');
			entireInput.name = 'entire';
			entireInput.type = 'hidden';
			entireInput.value = 'index';	
			document.MassUpdate.appendChild(entireInput);
			confirm(no_record_txt);
			break;	
	}
				
	document.MassUpdate.submit();
	return false;
}


sugarListView.prototype.clear_all = function() {
	document.MassUpdate.uid.value = '';
	sugarListView.check_all(document.MassUpdate, 'mass[]', false);
	document.MassUpdate.massall.checked = false;
	sugarListView.update_count(0);
}

sListView = new sugarListView();
// -- end sugarListView class

// format and unformat numbers
function unformatNumber(n, num_grp_sep, dec_sep) {
	if(typeof num_grp_sep == 'undefined' || typeof dec_sep == 'undefined') return n;
	n = n.toString();
	if(n.length > 0) {
		n = n.replace(new RegExp(RegExp.escape(num_grp_sep), 'g'), '').replace(new RegExp(RegExp.escape(dec_sep)), '.');		
		return parseFloat(n);
	}
	return '';
}

// round parameter can be negative for decimal, precision has to be postive
function formatNumber(n, num_grp_sep, dec_sep, round, precision) {
  if(typeof num_grp_sep == 'undefined' || typeof dec_sep == 'undefined') return n;
  n = n.toString();
  if(n.split) n = n.split('.');
  else return n;

  if(n.length > 2) return n.join('.'); // that's not a num!
  // round
  if(typeof round != 'undefined') {
    if(round > 0 && n.length > 1) { // round to decimal
      n[1] = parseFloat('0.' + n[1]);
      n[1] = Math.round(n[1] * Math.pow(10, round)) / Math.pow(10, round);
      n[1] = n[1].toString().split('.')[1];
    }
    if(round <= 0) { // round to whole number
      n[0] = Math.round(parseInt(n[0]) * Math.pow(10, round)) / Math.pow(10, round);
      n[1] = '';
    }
  }

  if(typeof precision != 'undefined' && precision >= 0) {
    if(n.length > 1 && typeof n[1] != 'undefined') n[1] = n[1].substring(0, precision); // cut off precision 
	else n[1] = '';
    if(n[1].length < precision) {
      for(var wp = n[1].length; wp < precision; wp++) n[1] += '0';
    }
  }

  regex = /(\d+)(\d{3})/;
  while(regex.test(n[0])) n[0] = n[0].replace(regex, '$1' + num_grp_sep + '$2');
  return n[0] + (n.length > 1 && n[1] != '' ? dec_sep + n[1] : '');
}

// find obj's position
function findElementPos(obj) {
    var x = 0;
    var y = 0;
    if (obj.offsetParent) {
      while (obj.offsetParent) {
        x += obj.offsetLeft;
        y += obj.offsetTop;
        obj = obj.offsetParent;
      }
    }//if offsetParent exists
    else if (obj.x && obj.y) {
      y += obj.y
      x += obj.x
    }
	return new coordinate(x, y);
}//findElementPos

// --- begin ajax status class
SUGAR.ajaxStatusClass = function() {};
SUGAR.ajaxStatusClass.prototype.statusDiv = null;
SUGAR.ajaxStatusClass.prototype.oldOnScroll = null;
SUGAR.ajaxStatusClass.prototype.shown = false; // state of the status window

// reposition the status div, top and centered
SUGAR.ajaxStatusClass.prototype.positionStatus = function() {
	this.statusDiv.style.top = document.body.scrollTop + 8 + 'px';
	statusDivRegion = YAHOO.util.Dom.getRegion(this.statusDiv);
	statusDivWidth = statusDivRegion.right - statusDivRegion.left;
	this.statusDiv.style.left = YAHOO.util.Dom.getViewportWidth() / 2 - statusDivWidth / 2 + 'px';
}

// private func, create the status div
SUGAR.ajaxStatusClass.prototype.createStatus = function(text) {
	statusDiv = document.createElement('div');
	statusDiv.className = 'dataLabel';
	statusDiv.style.background = '#ffffff';
	statusDiv.style.color = '#c60c30';
	statusDiv.style.position = 'absolute';
	
	statusDiv.style.opacity = .8;
	statusDiv.style.filter = 'alpha(opacity=80)';
	statusDiv.id = 'ajaxStatusDiv';
	document.body.appendChild(statusDiv);
	this.statusDiv = document.getElementById('ajaxStatusDiv');
}

// public - show the status div with text
SUGAR.ajaxStatusClass.prototype.showStatus = function(text) {
	if(!this.statusDiv) {
		this.createStatus(text);	
	}
	else {
		this.statusDiv.style.display = '';
	}
	this.statusDiv.innerHTML = '&nbsp;<b>' + text + '</b>&nbsp;';
	this.positionStatus();
	if(!this.shown) {
		this.shown = true;
		this.statusDiv.style.display = '';
		if(window.onscroll) this.oldOnScroll = window.onscroll; // save onScroll
		window.onscroll = this.positionStatus;
	}
}

// public - hide it
SUGAR.ajaxStatusClass.prototype.hideStatus = function(text) {
	if(!this.shown) return;
	this.shown = false;
	if(this.oldOnScroll) window.onscroll = this.oldOnScroll;
	else window.onscroll = '';
	this.statusDiv.style.display = 'none';
}

var ajaxStatus = new SUGAR.ajaxStatusClass();
// --- end ajax status class

/**
 * General Sugar Utils
 */ 
SUGAR.util = function () {
	var additionalDetailsCache;
	var additionalDetailsCalls;
	var additionalDetailsRpcCall;
	
	return {
	    evalScript:function(text){
	        objRegex = /<\s*script[^>]*>((.|\s|\v|\0)*?)<\s*\/script\s*>/igm;
            result =  objRegex.exec(text)
              
            while(result){
                  try{
	                  eval(result[1]);
	              } 
	              catch(e) {
	              		alert(e);
                      alert(result[1]);
                  }
                  result =  objRegex.exec(text)
			}
	    },
		/** 
		 * Make an AJAX request.
		 *
		 * @param	url				string	resource to load
		 * @param	theDiv			string	id of element to insert loaded data into
		 * @param	postForm		string	if set, a POST request will be made to resource specified by url using the form named by postForm
		 * @param	callback		string	name of function to invoke after HTTP response is recieved
		 * @param	callbackParam	any		parameter to pass to callback when invoked
		 * @param	appendMode		bool	if true, HTTP response will be appended to the contents of theDiv, or else contents will be overriten.
		 */
	    retrieveAndFill: function(url, theDiv, postForm, callback, callbackParam, appendMode) {
			if(typeof theDiv == 'string') {
				try {
					theDiv = document.getElementById(theDiv);
				}
		        catch(e) {
					return;
				}
			}
			
			var success = function(data) {
				if (typeof theDiv != 'undefined' && theDiv != null)
				{
					try {
						if (typeof appendMode != 'undefined' && appendMode)
						{
							theDiv.innerHTML += data.responseText;
						}
						else
						{
							theDiv.innerHTML = data.responseText;
						}
					}
					catch (e) {
						return;
					}
				}
				if (typeof callback != 'undefined' && callback != null) callback(callbackParam);
		  	}

			if(typeof postForm == 'undefined' || postForm == null) {
				var cObj = YAHOO.util.Connect.asyncRequest('GET', url, {success: success, failure: success});
			}
			else {
				YAHOO.util.Connect.setForm(postForm); 
				var cObj = YAHOO.util.Connect.asyncRequest('POST', url, {success: success, failure: success});
			}
		},
		checkMaxLength: function() { // modified from http://www.quirksmode.org/dom/maxlength.html
			var maxLength = this.getAttribute('maxlength');
			var currentLength = this.value.length;
			if (currentLength > maxLength) {
				this.value = this.value.substring(0, maxLength);
			}
			// not innerHTML
		},
		/**
		 * Adds maxlength attribute to textareas
		 */
		setMaxLength: function() { // modified from http://www.quirksmode.org/dom/maxlength.html
			var x = document.getElementsByTagName('textarea');
			for (var i=0;i<x.length;i++) {
				if (x[i].getAttribute('maxlength')) {		
					x[i].onkeyup = x[i].onchange = SUGAR.util.checkMaxLength;
					x[i].onkeyup();
				}
			}
		},

        clear_form: function(form) {
			var newLoc = 'index.php?action=' + form.action.value + '&module=' + form.module.value + '&query=true&clear=true';
			if(typeof(form.searchFormTab) != 'undefined'){
				newLoc += '&searchFormTab=' + form.searchFormTab.value;
			}

			//check to see if alternate tpl has been specified, if so then pass location through url string
			var tpl ='';
			if(document.getElementById('search_tpl') !=null && typeof(document.getElementById('search_tpl')) != 'undefined'){
				tpl = document.getElementById('search_tpl').value;
				if(tpl != ''){newLoc += '&search_tpl='+tpl;}
			}
			document.location.href = newLoc;
		} 


	};
}(); // end util
//YAHOO.util.Event.addListener(window, 'load', SUGAR.util.setMaxLength); // allow textareas to obey maxlength attrib

SUGAR.language = function() {
	var languages = new Array();
	
	return {
		setLanguage: function(module, data) {
			languages[module] = data;
		},
		
		get: function(module, str) {
			if(typeof languages[module] == 'undefined' || typeof languages[module][str] == 'undefined') return 'undefined';
			return languages[module][str];
		}
	};
}();