// JavaScript Document
/* s3cy common use javascript */
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

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.01
  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 && d.getElementById) x=d.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];}
}

function MM_validateForm() { //v4.0

  var i,p,p2,p3,q,nm,test,num,min,max,compareField,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) 
  {
  	test=args[i+2]; 
	val=MM_findObj(args[i]);
    //alert(test+"\n"+val);
	if (val) 
	{ 
		nm=val.name;
		id=val.id;
		
		// nm variable is for display purpose, so it doest not effect to field name
		// to get the field name use id
		nm=nm.replace(/_/," ");
		
		// special for inCompare in order to get the field name
		if (test.indexOf('inCompare') != -1)
		{
				p=test.indexOf('<');
				p2=test.indexOf('>');
				
				compareField=test.substring(p+1,p2);
				document.getElementById(compareField).className='input_text';
		}
		document.getElementById(id).className='input_text';
		document.getElementById(id+'_msg').style.display='none';
		
		if ((val=val.value)!="") 
		{	
			if (test.indexOf('isEmail')!=-1) 
			{ 
				p=val.indexOf('@');
				if (p<1 || p==(val.length-1))
				{
					document.getElementById(id).className='form_error';
					errors+='- '+nm+' must contain an e-mail address.<br />';
					
					document.getElementById(id+'_msg').innerHTML='<div id="box_error_msg">&nbsp;<img src="images/cross.png"> '+nm+' must contain an e-mail address.</div>';
					document.getElementById(id+'_msg').style.display='';
				}
			}//isEmail
			
			if (test.indexOf('isNum') != -1 || test.indexOf('inRange') != -1)//else if (test!='R') 
			{ 
				num = parseFloat(val);
				if (isNaN(val))
				{
					document.getElementById(id).className='form_error';
					errors+='- '+nm+' must contain a number.<br />';
					
					document.getElementById(id+'_msg').innerHTML='<div id="box_error_msg">&nbsp;<img src="images/cross.png"> '+nm+' must contain a number.</div>';
					document.getElementById(id+'_msg').style.display='';
				}
				
				if (test.indexOf('inRange') != -1) 
				{ 
					p=test.indexOf(':');
					min=test.substring(8,p);
					max=test.substring(p+1);
					if (num<min || max<num)
					{
						document.getElementById(id).className='form_error';
						errors+='- '+nm+' must contain a number between '+min+' and '+max+'.<br />';
					}
				} 
			}//isNum or inRange
			
			if (test.indexOf('inLength') != -1)
			{
				// length use (:), compare use <> to seperate the value
				p=test.indexOf(':');
				p2=test.indexOf('(');
				p3=test.indexOf(')');				  
								  
				min=test.substring(p2+1,p);
				max=test.substring(p+1,p3);
				
				// if max not empty, min and max is use to compare
				if (max != "")
				{
					if (val.length<min || val.length>max)
					{
						document.getElementById(id).className='form_error';
						
						document.getElementById(id+'_msg').innerHTML='<div id="box_error_msg">&nbsp;<img src="images/cross.png"> '+nm+' must be between '+min+' and '+max+' characters.</div>';
						document.getElementById(id+'_msg').style.display='';
						
						errors+='- '+nm+' must be between '+min+' and '+max+' characters.<br />';
					}
				}
				// use min only
				else if (max=="")
				{
					if (val.length!=min)
					{
						document.getElementById(id).className='form_error';
						document.getElementById(id+'_msg').innerHTML='<div id="box_error_msg">&nbsp;<img src="images/cross.png"> '+nm+' must be '+min+' characters.</div>';
						document.getElementById(id+'_msg').style.display='';
						errors+='- '+nm+' must be '+min+' characters.<br />';
					}
				}
			}// inLength
			
			if (test.indexOf('inCompare') != -1)
			{
				if (val != document.getElementById(compareField).value)
				{	
					document.getElementById(compareField).className='form_error';
					document.getElementById(id+'_msg').innerHTML='<div id="box_error_msg">&nbsp;<img src="images/cross.png"> '+compareField.replace(/_/," ")+' must same with '+nm+'.</div>';
					document.getElementById(id+'_msg').style.display='';
					errors+='- '+compareField.replace(/_/," ")+' must same with '+nm+'.<br />';
				}	
			}
		}// if val!=""
		else if (test.charAt(0) == 'R')
		{
			document.getElementById(id).className='form_error';
			document.getElementById(id+'_msg').innerHTML='<div id="box_error_msg">&nbsp;<img src="images/cross.png"> '+nm+' is required.</div>';
			document.getElementById(id+'_msg').style.display='';
			errors += '- '+nm+' is required.<br />';
		}	
	}
  } 
  
  if (errors)
  {
  	//alert('The following error(s) occurred:<br />'+errors);
	
	//document.getElementById('js_error').style.display='';
	//document.getElementById('js_error').innerHTML='<strong><u>The following error(s) occurred:</u></strong><br />'+errors;
  }
  
  document.MM_returnValue = (errors == '');
  //alert(document.MM_returnValue);
}

function onReset(url)
{
	location.href=url;
}

function openNewWindow(url)
{
	newWindow = window.open(url,'mywindow','width=600,height=800,toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,copyhistory=yes,resizable=yes');
	newWindow.focus( );
}

function CheckDigit(selObj)
{
	var val=selObj.value;
	
	if (isNaN(val))
	{
		selObj.value="";	
	}
}

function selRecord(url)
{
	if (url != "")
	{
		opener.location=(url);
		self.close();
	}
}

// Only allows certain keys to be used in the date field
function DigitsOnly(e) {
   var KeyCode = (e.keyCode) ? e.keyCode : e.which;
   return ((KeyCode == 8) // backspace
        || (KeyCode == 9) // tab
        || (KeyCode == 37) // left arrow
        || (KeyCode == 39) // right arrow
        || (KeyCode == 46) // delete
        || ((KeyCode > 47) && (KeyCode < 58)) // 0 - 9
   );
}

function convertCommaBasedToNum(amount) {
	var i = String(amount);
	var k =0;
	var num="";
	
	if (i.indexOf(",") != -1) {
		i=i.split(",");
		for (k=0;k<i.length;k++) {
			num+=""+i[k];
		}
	} else {
		num=amount;		
	}
	
	return (parseFloat(num));
}

function CurrencyFormatted(amount)
{
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
}
//

function CommaFormatted(amount)
{
	var delimiter = ","; // replace comma if desired
	var a = amount.split('.',2)
	var d = a[1];
	var i = parseInt(a[0]);
	if(isNaN(i)) { return ''; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	var n = new String(i);
	var a = [];
	while(n.length > 3)
	{
		var nn = n.substr(n.length-3);
		a.unshift(nn);
		n = n.substr(0,n.length-3);
	}
	if(n.length > 0) { a.unshift(n); }
	n = a.join(delimiter);
	if(d.length < 1) { amount = n; }
	else { amount = n + '.' + d; }
	amount = minus + amount;
	return amount;
}

function strReplace(repItem,strValue)
{//alert(strValue);
	while (strValue.indexOf(repItem) > -1)
	{
		strValue=strValue.replace(repItem,"");
	}
	return strValue;
}

function FormatNumber(field) 
{
	
	for (x = 0; x < field.form.elements.length; x++)
		if (field == field.form.elements[x])
			break;

	amount = field.form.elements[x].value;

  amount = amount.replace(/\,/gi, '');

	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;

	amount = s;

	var delimiter = ","; // replace comma if desired
	var a = amount.split('.',2)
	var d = a[1];
	var i = parseInt(a[0]);
	if(isNaN(i)) { return ''; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	var n = new String(i);
	var a = [];
	while(n.length > 3)
	{
		var nn = n.substr(n.length-3);
		a.unshift(nn);
		n = n.substr(0,n.length-3);
	}

	if(n.length > 0) { a.unshift(n); }
	n = a.join(delimiter);
	if(d.length < 1) { amount = n; }
	else { amount = n + '.' + d; }
	amount = minus + amount;

	field.form.elements[x].value = amount;
}

