//http://www.pl4e.com (Plat Form for E-Commerce)
//alain
function left(str,len)
{
	return str.substring(0,len);
}

//make an href even be lost
function looseEvent()
{
	window.event.returnValue = false; 
}

//tests if a user name is in correct format or no
function testUserName(str)
{
	var patern=/^[a-z]{1,}([a-z]|\d|_)*$/i;
	return patern.test(str);
}

//builds a validation object
function vO()
{
	var args=new Array();
	for(var i=0;i<arguments.length;i++)
		args[i]=arguments[i]+"";
	return(new validation_c(args));
}
//tests for ' or " in the fields
function testQuotes(str)
{
	var patern = /'|"/;
	return(!patern.test(str));
}
//tests if any thing selected in a combo box
function isSelected(forme,select)
{
	var select  = document.forms[forme](select);
	if(select.selectedIndex==-1)
		return(false);
	return(true);
}
//filed the text field
//section number of characters between seperator
//number of seperators
//rebuild(value("xxxxxxxxxx"),2,3) = xxx-xxx-xxxx
function rebuild(field,section,length)
{
	var ret='';
	var value=field.value.replace(/-| /g,'');
	var counter=0;
	for(var j=1;j<=length;j++)
	{
		for(var i=1;i<=section;i++)
		{
			if(value.length>counter)
				ret+=value.charAt(counter);
			else
				ret+=' ';
			counter++;
		}
		ret+='-';
	}
	if(value.length>counter)
		ret+=value.substring(counter,value.length);
	field.value=ret;
}

//tests if a phone number is written in the good format(xxx-xxx-xxxx)
function testPhone(str)
{
	var patern=/^\d{3}-\d{3}-\d{4}$/;
	return(patern.test(str));
}

//test if a variable is a float number or no
function isFloat(str)
{
	str+='';
	var patern=/^(-|\d*)(\d*|\.\d{1,})$/;
	return patern.test(str);
}
//tests for an integer value
function isInteger(str)
{
	str+='';
	var patern=/^(-|\d)\d*$/;
	return patern.test(str);
}
function isNumber(str,empty)
{
	if(isOk(str))
		return(isFloat(str) || isInteger(str));
	return empty?false:true;
}
//return a transparent image  instead
//caution the image must be in the images folder
function trans(a,b,br,name)
{
	name=name?(" name=\""+name+"\""):"";
	return('<img src=\"<%=vroot%>/images/T.gif\" width=\"'+a+'\" height=\"'+b+'\"'+name+'>'+(br?'':'<br>'));
}

function compareDates(date1,date2)
{
	var dd=new Date(date1);
	var dd_=new Date(date2);
	return(dd<=dd_);
}

//validation object constructor
function validation_c(name,display,min,max,selectable,character,other,other_msg,isObject,formName)
{
	var f=0;
	if(arguments.length>1) f=1;
	var o = new Object();
	o["name"]= f?name:name[0]; //name of the field
	o["display"] = f?display:name[1]; //Display Name
	o["min"] = parseInt(f?min:name[2]); //minimum length
	o["max"] = parseInt(f?max:name[3]); //maximum length
	o["selectable"] = parseInt(f?selectable:name[4]); //the field can be selected or no
	o["char"]=parseInt(f?character:name[5]); //the field is a caracter 1 or a number 0
	o["other"] = f?other:name[6]; //other function to test
	o["other_msg"] = f?other_msg:name[7]; //if other function returned false the msg to use
	o["isObject"] = parseInt(f?isObject:name[8]); //to test the object or the value in the other function true means object
	o["formName"] = f?formName:name[9]; //name of the form used when to test is an object in the form(like a select)
	return(o);
}

//cicle thrue the validation array and tests every field on its own
function check_validation(forme,validation_array)
{
	var msg;
	var validation =validation_array;
	for(i=0;i<validation.length;i++)
	{
		if(validation[i].other)
		{
			if(!validation[i].isObject)
			{
				eval("var test = "+validation[i].other+"(\""+forme[validation[i].name].value.replace(/'/g,"`").replace(/"/g,"\\"+"\"")+"\")");
			}
			else
			{
				if(typeof(forme.name)=='object')
				{
					var a=forme.outerHTML;
					var b = a.substring(a.indexOf('<'),a.indexOf('>')).replace(/"/g,'');
					a=b.substring((b.indexOf('name')+('name').length+1),b.length);
					b=a.substring(0,a.indexOf(' '));
				}
				else
					var b=forme.name;
				eval('var test = '+validation[i].other+'(\''+b+'\',\''+[validation[i].name]+'\')');
			}
			if(!test)
			{
				if(validation[i].selectable)
					return(err_m(forme,validation[i].name,validation[i].other_msg));
				else
					return(err_s(forme,validation[i].name,validation[i].other_msg));
			}
		}
		var tic=validation[i]["char"];
		if(tic)
			var b=forme[validation[i].name].value.Trim().length;
		else
		{
			var temp=isOk(forme[validation[i].name].value)?forme[validation[i].name].value:0;
			if(!isNumber(temp))
			{
				if(validation[i].selectable)
					return(err_m(forme,validation[i].name,'Please enter only numbers in the \"'+validation[i].display+'\" field.'));
				else
					return(err_s(forme,validation[i].name,'Please enter only numbers in the \"'+validation[i].display+'\" field.'));
			}
			var b=parseFloat(temp);
		}
		if(b >validation[i].max && isOk(validation[i].max))
		{
			msg = 'Please enter no more than '+validation[i].max
									+(!tic?'':' characters')+' in the \"'+validation[i].display+'\" field.';
			if(validation[i].selectable)
				return(err_m(forme,validation[i].name,msg));
			else
				return(err_s(forme,validation[i].name,msg));
		}
		if(b<validation[i].min && isOk(validation[i].min))
		{
			if(validation[i].min>1 || !validation[i]["char"])
				msg = 'Please enter '+validation[i].min+' or more than '+validation[i].min+(!tic?'':' Characters')+' in the \"'+validation[i].display+'\" field.';
			else
				msg = 'Please enter a value in the \"'+validation[i].display+'\" field.';
			if(validation[i].selectable)
				return(err_m(forme,validation[i].name,msg));
			else
				return(err_s(forme,validation[i].name,msg));
		}
	}
	return(true);
}

//return if an email is correct or no
function checkEmail(str)
{
	var pattern =  /^[a-z1-9_]([\.\-\_]{0,}([a-z_0-9]|[a-z_0-9]\.[a-z_0-9])*)+@([a-z0-9]|[a-z0-9][\.\-][a-z0-9])+\.[a-z]{2,}$/i
  return pattern.test(str);
}

//alerts a message and select and focus the error field
//used on text fields
function err(forme,for1,str)
{
	alert(str);
	forme(for1).select();
	forme(for1).focus();
	return(false);
}

function err_m(forme,for1,str)
{
	alert(str);
	forme[for1].select();
	forme[for1].focus();
	return(false);
}

//alerts a message and focus on the error field with no selection
function err_s(forme,for1,str)
{
	alert(str);
	forme[for1].focus();
	return(false);
}

//removes sapces at the begening or the end of a string
function trim_string()
{
   return(this.replace(/(^\s*)|(\s*$)/g, ""));
}
String.prototype.Trim = trim_string;

//return if a variable is ok or no
function isOk(a)
{
	var ret = (typeof(a)=="undefined");
	if (ret == true) 
	   return false;
	if (typeof(a) == "string" && a=="")
	   return false;
	return (a!=null);
}

//to make a number comma formated and to have 2 digits after the decimal point
function putVirgule(str)
{
	str = Math.round(str*100)/100;
	str = str+'';
	var temp = str.split('.');
	if(temp[0].length>3)
		temp[0] =putVirgule(temp[0].substring(0,temp[0].length-3))+','+temp[0].substring(temp[0].length-3,temp[0].length);
	var temp2 = temp[0].split(',');
	return(temp[0]+(temp.length>1?('.'+(temp[1].length==2?temp[1]:temp[1]+'0')):''));
}

//tests if str exists in the array
function inArray(array,str)
{
	for(var i=0;i<array.length;i++)
		if(array[i]==str) return(true);
	return(false);
}

//return true if the document contains scrolling
function isScrolled(_window)
{
	_window=_window?_window:self;
	try
	{
		var test=false;
		_window.scroll(0,1);
		if(_window.document.body.scrollTop!=0)
			test=true;
		_window.scroll(0,0);
		return (test);
	}
	catch(e){}
}

function existsIn(arr,val)
{
	return(inArray(arr,val));
}
//tests a variable returns other if the variable is not
//defined or returns null as default
function tV(x,other)
{
	return(x?x:other?other:null);
}

function Decode(x1,x2)
{
	this.codeFrom=x1;
	this.codeTo=x2;
}
function HTMLDecode(str)
{
	if(!str) return ("");
	var codes=new Array();
	codes[0]=new Decode("&#39;","'");
	codes[1]=new Decode("&#34;","\"");
	for(var i=0;i<codes.length;i++)
	{
		var x=new RegExp(codes[i].codeFrom,"g");
		str=str.replace(x,codes[i].codeTo);
	}
	return str;
}
//to initialize a forme element to the value value
function initValue(forme,element,value)
{
	value=value.replace(/%2F/g,"/").replace(/7~~7/g,"\"").replace(/\+/g," ").replace(/~~~/g,"\r\n").replace(/~\^~/g,'"').replace(/%~%/g,",").replace(/~1~/g,"#").replace(/\+/g," ").replace(/%20/g," ");
	value=HTMLDecode(value);
	var type=forme[element].type;
	if(!type) type[element](0).type;
	switch(type)
	{
		case "file":
		break;
		case "select-one":
			var select =forme[element];
			for(var j=0;j<select.options.length;j++)
			{
				if(select.options[j].value==value)
				{
					select.options[j].selected=true;
					break;
				}
			}
		break;
		case "checkbox":
			forme[element].checked=true;
		break;
		case "radio":
			for(var j=0;j<forme[element].length;j++)
			{
				if(forme[element](j).value==value)
				{
					forme[element](j).checked=true;
					break;
				}
			}
		break;
		default:
			forme[element].value=value;
		break;
	}
}

//puts the value of each field in form to nothing
function eraseForm(forme)
{
	forme.reset();
	with(forme)
	{
		for(var i=0;i<elements.length;i++)
		{
			with(elements(i))
			{
				if(!type) type=element(i)(0).type;
				switch(type)
				{
					case "file":
					break;
					case "select-one":
						selectedIndex=0;
					break;
					case "checkbox":
						checked=0;
					break;
					case "radio":
						for(var j=0;j<length;j++)
							elements(i)(j).checked=0;
					break;
					case "text":
						value="";
					break;
					default:
					break;
				}
			}
		}
	}
}
function initHeight(frame1)
{
	try
	{
		if(typeof(frame1)=="string")
			frame = parent.frames[frame1];
		else
			frame=frame1;
		frame=frame?frame:self;
		try
		{
			parent.document.getElementById("loadingDiv").style.visibility="hidden";
		}catch(e){}
		frame.scroll(0,1);
		if(frame.document.body.scrollTop!=0)
		{
			frame.parent.frameHeight=document.body.offsetHeight+1;
			frame.parent.animateFrame(frame.name);
		}
		else
			return;
		window.setTimeout("initHeight('"+frame1+"')",1);
	}catch(e){}
}