//http://www.pl4e.com (Plat Form for E-Commerce)

//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)
{
	if(isOk(str))
		return(isFloat(str) || isInteger(str));
	return(true);
}
//return a transparent image 
//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,'\\\"').Trim()+'\')');
			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 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-z]([\.\-\_]{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()
{
	var test=false;
	window.scroll(0,1);
	if(window.document.body.scrollTop!=0)
		test=true;
	window.scroll(0,0);
	return (test);
}

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);
}

//to initialize a forme element to the value value
function initValue(forme,element,value)
{
	value=value.replace(/7~~7/g,"\"").replace(/\+/g," ").replace(/~~~/g,"\r\n").replace(/~\^~/g,'"').replace(/%~%/g,",").replace(/~1~/g,"#");
	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 i=0;i<select.options.length;i++)
			{
				if(select.options[i].value==value)
				{
					select.options[i].selected=true;
					break;
				}
			}
		break;
		case "checkbox":
			forme[element].checked=true;
		break;
		case "radio":
			for(var i=0;i<forme[element].length;i++)
			{
				if(forme[element](i).value==value)
				{
					forme[element](i).checked=true;
					break;
				}
			}
		break;
		default:
			forme[element].value=value;
		break;
	}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

