function submitForm(err_msg_inner)
{	
	if(!formInst.chkField(err_msg_inner))
		return false;
	
	return true;
	
}

function vlForm(frm)
{
	// members
	this.frm		= frm;
	this.cntErr		= 0;
	this.errmsg		= '';

	// structures
	this.cvalInf		= new Array();
	this.ovalInf		= new Array();

	// methods
	this.addCField		= addCField;
	this.replaceCField		= replaceCField;
	this.addOField		= addOField;
	this.chkField		= chkField;
	this.addCFieldCAP   = addCFieldCAP;
}

//======================
// public methods
//======================

function chkField(popup)
{	
	

	var len = this.cvalInf.length;
	this.errmsg = '';
	this.cntErr = 0;
	for(i=len-1; i>=0; i--)
	
	{	
		field = this.cvalInf[i]['field'];
		
		if( typeof field  != 'object')
		{
			field= document.getElementById(field);
		}
			
		chktype = this.cvalInf[i]['type'];
		label = this.cvalInf[i]['label'];
		msg = this.cvalInf[i]['msg'];
		
		if(label) dFlash(label, this.cvalInf[i]['color'], this.cvalInf[i]['weight']);		
		
		if(chktype == 'TEXT_ONLY' && chkEmpty(field.value, field))
		{
			
			//alert(document.getElementById(field.id).value);			
			if(label) Flash(label);
			if(msg) this.errmsg = msg + '\n' + this.errmsg ;
			field.focus();
			this.cntErr++;
		}
		else if(chktype == 'TEXT_TINYMCE' && emptyTINYMCE())
		{	
			if(label) Flash(label);
			if(msg) this.errmsg = msg + '\n' + this.errmsg ;
			field.focus();
			this.cntErr++;
		}
		else if(chktype == 'TEXT_EMAIL' && !chkEMail(field.value, field))
		{
			if(label) Flash(label);
			if(msg) this.errmsg = msg + '\n' + this.errmsg ;
			field.focus();
			this.cntErr++;
		}
		else if(chktype == 'TEXT_NUMBER' && !chkNumber(field.value, field))
		{
			if(label) Flash(label);
			if(msg) this.errmsg = msg + '\n' + this.errmsg ;
			field.focus();
			this.cntErr++;
		}
		else if(chktype == 'TEXT_HELLO' && !chkHello(field.value, field))
		{
			if(label) Flash(label);
			if(msg) this.errmsg = msg + '\n' + this.errmsg ;
			field.focus();
			this.cntErr++;
		}
		else if(chktype == 'TEXT_FILE' && chkFile(field.value, field))
		{
			if(label) Flash(label);
			if(msg) this.errmsg = msg + '\n' + this.errmsg ;
			//field.focus();
			this.cntErr++;
		}
		else if(chktype == 'HTML_OPTION' && field.selectedIndex == 0)
		{
			if(label) Flash(label);
			if(msg) this.errmsg = msg + '\n' + this.errmsg ;
			field.focus();
			this.cntErr++;
		}
		else if(chktype == 'CF_DATE' && !chkCFDate(field.value, field))
		{
			if(label) Flash(label);
			if(msg) this.errmsg = msg + '\n' + this.errmsg ;
			field.focus();
			this.cntErr++;
		}
		
	}

	len = this.ovalInf.length;
	for(i=len-1; i>=0; i--)
	{
		field = this.ovalInf[i]['field'];
		chktype = this.ovalInf[i]['type'];
		label = this.ovalInf[i]['label'];
		msg = this.ovalInf[i]['msg'];
		
		if(label) dFlash(label, this.ovalInf[i]['color'], this.ovalInf[i]['weight']);
		if(chkEmpty(field.value, field)) continue;

		if(chktype == 'TEXT_EMAIL' && !chkEMail(field.value, field))
		{
			if(label) Flash(label);
			if(msg) this.errmsg = msg + '\n' + this.errmsg ;
			field.focus();
			this.cntErr++;
		}
		else if(chktype == 'TEXT_NUMBER' && !chkNumber(field.value, field))
		{
			if(label) Flash(label);
			if(msg) this.errmsg = msg + '\n' + this.errmsg ;
			field.focus();
			this.cntErr++;
		}
		else if(chktype == 'TEXT_HELLO' && !chkHello(field.value, field))
		{
			if(label) Flash(label);
			if(msg) this.errmsg = msg + '\n' + this.errmsg ;
			field.focus();
			this.cntErr++;
		}
		else if(chktype == 'HTML_OPTION' && field.selectedIndex == 0)
		{
			if(label) Flash(label);
			if(msg) this.errmsg = msg + '\n' + this.errmsg ;
			field.focus();
			this.cntErr++;
		}
	}
	if(this.cntErr)
	{	
		//alert(this.cntErr);
		if(err_msg_inner){
			testStr = this.errmsg.split('\n');
			htmlStr = '<ul>';
			for(i=0;;i++){
				if(!testStr[i])
				break;
				htmlStr +='<li>'+testStr[i]+'</li>';
			}
			htmlStr += '</ul>';			
			//tmpStr = str_replace(this.errmsg,'\n', '<br/>');
			err_msg_inner.innerHTML = htmlStr;
			return false;
		}
		if(this.errmsg>'')
			alert(this.errmsg);
		return false;
	}
	else
		return true;
	
}


function addCField(field, type)
{
	var len = this.cvalInf.length;
	
	this.cvalInf[len] = new Array();
	this.cvalInf[len]['field'] = document.getElementById(field);
	this.cvalInf[len]['type'] = type;
	this.cvalInf[len]['label'] = null;
	this.cvalInf[len]['msg'] = null;
 
	
	if (arguments.length>2) 
		if (1) 
		{	
			this.cvalInf[len]['label']  = field + '_err';
			thisElement                 = document.getElementById(field + '_err');
			
   			this.cvalInf[len]['color']  = thisElement.style.color;
   			this.cvalInf[len]['weight'] = thisElement.style.fontWeight;
		}
			
	if (arguments.length>3)
		if (arguments[3]!=null&&arguments[3]!='') 
			this.cvalInf[len]['msg'] = arguments[3];
		
}    

function replaceCField(index,field,type,msg)
{
	this.cvalInf[index] = new Array();
	this.cvalInf[index]['field'] = document.getElementById(field);
	this.cvalInf[index]['type'] = type;
	this.cvalInf[index]['label'] = null;
	this.cvalInf[index]['msg'] = msg;
}

function addCFieldCAP(field, type)
{
	var len = this.cvalInf.length;
	

	this.cvalInf[len] = new Array();
	this.cvalInf[len]['field'] = field;
	this.cvalInf[len]['type'] = type;
	this.cvalInf[len]['label'] = null;
	this.cvalInf[len]['msg'] = null;
 
	//alert('2');
	if (arguments.length>2) 
		if (1) 
		{	
			this.cvalInf[len]['label']  = field + '_err';
			thisElement                 = document.getElementById(field + '_err');
			
   			this.cvalInf[len]['color']  = thisElement.style.color;
   			this.cvalInf[len]['weight'] = thisElement.style.fontWeight;
		}
			
	if (arguments.length>3)
		if (arguments[3]!=null&&arguments[3]!='') 
			this.cvalInf[len]['msg'] = arguments[3];
		
}     



function addOField(field, type, label, msg)
{
	
	var len = this.ovalInf.length;
	this.ovalInf[len] = new Array();
	this.ovalInf[len]['field'] = field;
	this.ovalInf[len]['type'] = type;
	this.ovalInf[len]['label'] = null;
	this.ovalInf[len]['msg'] = null;


	if (arguments.length>2) 
		if (arguments[2]!=null&&arguments[2]!='') 
		{
			this.ovalInf[len]['label']  = arguments[2];
			thisElement                 = document.getElementById(arguments[2]);
   			this.ovalInf[len]['color']  = thisElement.style.color;
   			this.ovalInf[len]['weight'] = thisElement.style.fontWeight;
		}
			
	if (arguments.length>3)
		if (arguments[3]!=null&&arguments[3]!='') 
			this.ovalInf[len]['msg'] = arguments[3];
			
	
}


//======================
// public functions
//======================

function dFlash(id, color, weight)
{
	
	thisElement                  = document.getElementById(id);
   	thisElement.style.color      = color;
   	thisElement.style.fontWeight = weight;   
}

function Flash(id)
{
	
	thisElement                  = document.getElementById(id);
   	thisElement.style.color      = 'red';
   	thisElement.style.fontWeight = "bold";   
}

function mkNumber(str)
{
	
	if(str!=null&&str.length>0)
		return str.replace(/[^0-9]/g, '');
	else
		return null;
}

function remZero(str)
{

	if(str!=null&&str.length>0)
	{
		str = str.replace(/^([0]{2,})?/i, '');
		if(str == '')	str = str.replace(/^/i, '0');
		return str;
	}
	else
		return null;
}

function Trim(str)
{

	str = str.replace(/^([ ]+)?/i, '');
	str = str.replace(/([ ]+)?$/i, '');
	return str;
}

function chkEmpty(str, id)
{	
	str = Trim(str);
	if(typeof id != 'undefined')
		id.value = str;
	return str == '';
}

function emptyTINYMCE(){
	strInputCode = tinyMCE.getContent().replace(/&(lt|gt);/g, function (strMatch, p1){
 		 	return (p1 == "lt")? "<" : ">";
 		});
 		strTagStrippedText = strInputCode.replace(/<\/?[^>]+(>|$)/g, "");
		strTagStrippedText = strTagStrippedText.replace(/&nbsp;/g, "");
		
	return strTagStrippedText == '';
}

function chkCFDate(str, id)
{
	var monthStr = new Array();
	monthStr[1]="January";
	monthStr[2]="February";
	monthStr[3]="March";
	monthStr[4]="April";
	monthStr[5]="May";
	monthStr[6]="June";
	monthStr[7]="July";
	monthStr[8]="August";
	monthStr[9]="September";
	monthStr[10]="October";
	monthStr[11]="November";
	monthStr[12]="December";
	
	var date = new Date();
	cur_month = date.getMonth()+1;
	cur_day = date.getDate();
	cur_year = date.getFullYear();
	
	cur_month_str = monthStr[cur_month];
	
	input_date = id.value;
	if(!input_date){
	return false;
	}
	
	temp = input_date.split(' ');
	
	input_year = temp[2];
	input_month = temp[0];
	input_day = temp[1].replace(',','');
	//alert(input_day);
	var i=1;
	for(i in monthStr){
		if(monthStr[i] == input_month){
			input_month = i;
			break;
		}
		
	}
	
	wrong_date = false;
	
	
	if(input_year >= cur_year){
		if(input_year > cur_year)
			return true;
		else if(input_month >= cur_month){
			if(input_month > cur_month)
				return true;
			else if(input_day >=cur_day)
				return true;
			else
				wrong_date = true;
		}
		else
			wrong_date = true;
	}
	else
		wrong_date = true;
	
	if(wrong_date){
		id.value ='';
		return false;
	}
	//alert(wrong_date);
	//alert(cur_month_str+' '+cur_day+' '+cur_year);	
	
}





function chkFile(str, id)
{
	return str == '';
}
function chkNumber(str, id)
{
	var isNum = /^([-])?[0-9]+(\.[0-9]+)?$/;
	str = remZero(Trim(str));
	if(typeof id != 'undefined')
		id.value = str;
	return isNum.test(str);
}

function chkEMail(str, id)
{
	var isEMail = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/;
	str = Trim(str);
	if(typeof id != 'undefined')
		id.value = str;
	return isEMail.test(str);
}

function chkHello(str, id)
{
	//var isHello = /^([\+])?([ ]+)?([0-9]{2,3})?([ ]+)?([0-9]{3,5})([ ]+|-)([0-9]{5,10})$/;
	var isHello = /^([\+])?([ ]+)?([0-9]{2,3})?([ ]+)?([0-9]{3,3})([ ]+|-)?([0-9]{7,10})$/;
	str = Trim(str);
	str = str.replace(/([\(\)])/g, '');
	//str = str.replace(/([-])/g, ' ');
	//alert(str);
	if(typeof id != 'undefined')
		id.value = str;
	return isHello.test(str);
}
function checkZipCodeTotalChar(id)
{	
	var length=document.getElementById(id).value.length;	
	var value=document.getElementById(id).value;
	if(length>5)
	{
		alert('No more five digits are allowed here');
		document.getElementById(id).value=value.substr(0,5);
	}
	//alert(value.substr(0,length-1))
}

function str_replace(haystack, needle, replacement) {
    var temp = haystack.split(needle);
    return temp.join(replacement);
}

function change_submit_button_bg(button_id)
{
	
button = document.getElementById(button_id);

button.style.background = 'url(/images/button_hover.jpg)'; 

button.style.backgroundPosition ='center';
button.style.fontWeight='bold';
button.style.color='#FFFFFF';
button.style.height='30px';
}	

function reset_submit_button_bg(button_id)
{
button = document.getElementById(button_id);
button.style.background = 'url(/images/button_normal.jpg)'; 
button.style.backgroundPosition ='center';
button.style.fontWeight='bold';
button.style.color='#000';
button.style.height='30px';
}