function ltrim(str){
	return str.replace(/^\s\s*/, '');
}

function rtrim(str){
	return str.replace(/\s\s*$/, '');
}

function trim(str){
	return ltrim(rtrim(str));
}

function scrollToElement(theElement){
	var selectedPosX = 0;
	var selectedPosY = 0;
	while( theElement != null ){
		selectedPosX += theElement.offsetLeft;
		selectedPosY += theElement.offsetTop;
		theElement = theElement.offsetParent;
	}
	window.scrollTo( selectedPosX - 100, selectedPosY - 150 );
}

function validMarkElement( el, correct ){
	if( el != null ){
		var errSpan = document.getElementById( el.name + '.error' );
		if( errSpan != null ){
			if( correct )
				errSpan.innerHTML = '&nbsp;';
			else				
				errSpan.innerHTML = '<img title="Error" alt="Error" src="images/error.png"/>';
		}else{
			if( correct ){
				el.style.backgroundColor = '';
			}else{
				el.style.backgroundColor = '#ffe0e0';
			}
		}
	}
}

function validMarkElementById( id, correct ){
	validMarkElement( 
		document.getElementById( id ), 
		correct 
		);
}

function validMarkElementsById( arr, correct ){
	for( i = 0; i < arr.length; i++ )
		validMarkElementById( arr[i], correct );
}

function ValidateCheckCorrectFields( f, chkElNames, correctFun ){
	var err = new Array();
	var ok = true;
	for( var i = 0; i < f.elements.length; i++ ){
		for( var j = 0; j < chkElNames.length; j++){
			if( f.elements[i].name == chkElNames[j] ){
				if( !correctFun( f.elements[i] )){
					err[err.length] = f.elements[i].name;
				}
			}
		}
	}
	for( i = 0; i < chkElNames.length; i++ )
		validMarkElementById( chkElNames[i], true );
	if( err.length > 0 ){
		document.getElementById( err[0] ).focus();
		scrollToElement( document.getElementById( err[0] ));
		ok = false;
		for(i = 0; i < err.length; i++)
			validMarkElementById( err[i], false );
	}
	return ok;
}

function correctNotEmpty( fld ){
	var state = (
		( fld.type=='text' 
			|| fld.type=='password' 
			|| fld.type=='select-one' 
			|| fld.type=='textarea' 
		)
		&& trim( fld.value ) != ''
	)||(
		fld.type=='checkbox'
		&& fld.checked
	);
//	if( state != true )
//		alert( fld.name + " : " + fld.type + " : " + fld.value );
	return state;
}

function ValidateEqual( el1, el2 ){
	if( el1.value != el2.value ){
		validMarkElement( el2, false );
		el2.focus();
		scrollToElement( el2 );
		return false;
	}
	return true;
}

function ValidateCheckNonEmptyFields( f, chkElNames ){
	return ValidateCheckCorrectFields( f, chkElNames, correctNotEmpty );
}

function isDigit(num) {
	if (num.length>1){return false;}
	var string="1234567890";
	if (string.indexOf(num)!=-1){return true;}
	return false;
}

function formFormatPhone( fld ){
	var orig = fld.value;
	var num = '';
	for( var i=0; i<orig.length; i++ ){
		var c = orig.substr(i,1);
		if( isDigit(c) )
			num += c;
	}
	while( num.length < 10 )
		num = '0' + num;
	var phone='';
	if( num.length > 7 ){
		phone = '(' + num.substr(0,3) + ') ';
		num = num.substr(3,num.length-3);
	}
	if( num.length > 3 ){
		phone += num.substr(0,3) + '-';
		num = num.substr(3,num.length-3);
	}
	phone += num;
	fld.value = phone;
}

function countryType( country )
{
    switch( country ){
        case "AMERICAN SAMOA":
        case "GUAM":
		case "MICRONESIA":
		case "MARSHALL ISLANDS":
		case "N. MARIANA ISLANDS":
		case "PALAU":
		case "PUERTO RICO":
		case "US VIRGIN ISLANDS":
		    return "usterritory";

        case "UNITED STATES":
        case "CANADA":
            return "uscanada";
            
        default:
            return "other";
    }
}