// $Id: form_scripts.js 1210 2006-01-31 15:56:50Z zeke $

// load htmlarea
// Automatically calculates the editor base path based on the current URL.
var html_editor_url = document.location.pathname.substring(0,document.location.pathname.lastIndexOf('/')) + '/classes/fckeditor/';
var onload_handlers = new Array();
var document_loaded = false;

//
// Global definitions
//
// counter - for js tag additions
var counter = 0;

function currencyFormat(fld, milSep, decSep, e) {
  var sep = 0;
  var key = '';
  var i = j = 0;
  var len = len2 = 0;
  var strCheck = '0123456789';
  var aux = aux2 = '';
  var whichCode = (window.Event) ? e.which : e.keyCode;

  if (whichCode == 13) return true;  // Enter
  if (whichCode == 8) return true;  // Delete
  key = String.fromCharCode(whichCode);  // Get key value from key code
  if (strCheck.indexOf(key) == -1) return false;  // Not a valid key
  len = fld.value.length;
  for(i = 0; i < len; i++)
  if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break;
  aux = '';
  for(; i < len; i++)
  if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt(i);
  aux += key;
  len = aux.length;
  if (len == 0) fld.value = '';
  if (len == 1) fld.value = '0'+ decSep + '0' + aux;
  if (len == 2) fld.value = '0'+ decSep + aux;
  if (len > 2) {
    aux2 = '';
    for (j = 0, i = len - 3; i >= 0; i--) {
      if (j == 3) {
        aux2 += milSep;
        j = 0;
      }
      aux2 += aux.charAt(i);
      j++;
    }
    fld.value = '';
    len2 = aux2.length;
    for (i = len2 - 1; i >= 0; i--)
    fld.value += aux2.charAt(i);
    fld.value += decSep + aux.substr(len - 2, len);
  }
  return false;
}

//
// Show/hide any tag by its id.
// parameters:
// id - element id
// @status - can be true (expand) or false (collapse)
function fn_show_tag(id, status)
{
	if (document.getElementById(id)) {
		if (status == true || status == false) {
			document.getElementById(id).style.display = (status == true)?"none":"";
		} else {
			document.getElementById(id).style.display = (document.getElementById(id).style.display == "")?"none":"";
		}
	}
}

//
// Change section and 
// id - section id

function fn_show_section(id, scts, separate_form, redraw_all)
{

	// Fix bug with changing hash string in opera 8.5+
	if (is_opera && document_loaded == false) {
		onload_handlers.push("fn_show_section('"+id+"', sections ,'"+separate_form+"', "+redraw_all+");");
		return;
	}

	for (i = 0; i < scts.length; i++)
	{
		if (scts[i] == id) {
			if (document.getElementById('product_save_button')) {
				document.getElementById('product_save_button').style.display = (separate_form == 'Y') ? 'none' : '';
			}
			document.getElementById('content_'+scts[i]).style.display = '';
			document.getElementById('tab_'+scts[i]+'_bg').className = 'section-active-tab-bg';
			document.getElementById('tab_'+scts[i]+'_left').src = tab_left_active_image_path;
			document.getElementById('tab_'+scts[i]+'_right').src = tab_right_active_image_path;
			document.getElementById('selected_section').value = id;
			location.hash = '#'+id;
		} else {
			if (document.getElementById('tab_'+scts[i]+'_bg').className == 'section-active-tab-bg' || redraw_all) {
				document.getElementById('content_'+scts[i]).style.display = 'none';
				document.getElementById('tab_'+scts[i]+'_bg').className = 'section-inactive-tab-bg';
				document.getElementById('tab_'+scts[i]+'_left').src = tab_left_image_path;
				document.getElementById('tab_'+scts[i]+'_right').src = tab_right_image_path;
			}
		}
	}
}

//
// Disable 'delete tag' button if there is only one copy of tag to clone
//
function fn_check_last_tag(tag)
{

	if ((!tag.previousSibling || tag.previousSibling.id == 'header' || typeof(tag.previousSibling.id) == 'undefined') && (!tag.nextSibling || typeof(tag.nextSibling.id) == 'undefined'))
		return true;
	
	return false;
}

//
// Remove the tag
//
function fn_remove_tag(tag_id)
{
	if(document.getElementById) {
		src = document.getElementById("box_"+tag_id);
		if (!fn_check_last_tag(src))
			src.parentNode.removeChild(src);
	}
}


function fn_replace(substring, level)
{
	var re = new RegExp();
	var i = 0;
	re.compile("\\[\\d+\\]", "g");

	for (i=1; i<=level; i++) {
		re.exec(substring);
	}

	substring = RegExp.leftContext+'['+counter+']'+RegExp.rightContext;
	return substring;
}

//
// Adds the tag
//
// @tag_id - tag id that should be cloned
// @level - level in variable name that should be replaced
// @clone - if set, the field values will be copied
// E.g. (replace on '30')
// level = 1, varname = data[20][sub][50] - after replacement data[30][sub][50]
// level = 2, varname = data[20][sub][50] - after replacement data[20][sub][30]

function fn_add_tag(tag_id, level, clone)
{
	var j = 0;
	var i = 0;
	var re_names = new RegExp();
	var re_ids = new RegExp();

	if(document.getElementById) {
		counter++;
		new_id = tag_id+"_"+counter;

		src = document.getElementById("box_"+tag_id);
		var newNode = src.cloneNode(true);
		newNode.id = "box_"+new_id;

		re_names.compile("name=(\\\"|\\\')*\\w*(\\[\\w*\\])+(\\\"|\\\')*", "gi");
		var arr_names = newNode.innerHTML.match(re_names);

		node_elms = newNode.getElementsByTagName('*');

		for (i=0;i<node_elms.length;i++) {
			if (node_elms[i].id.length>0) {
				node_elms[i].id = node_elms[i].id + '_'+counter;
			}

			if (node_elms[i].nodeName=='TD') {
				if (node_elms[i].style.display == 'none' && clone != 'Y') {
					node_elms[i].style.display = '';
				}

				// Replace [%d] in nodes names
				for (j=0; j<arr_names.length; j++) {
					if (node_elms[i].innerHTML.indexOf(arr_names[j])!=-1) {
						str = fn_str_replace_string(node_elms[i].innerHTML,arr_names[j],fn_replace(arr_names[j],level));
						if (str.length>0) {
							node_elms[i].innerHTML = str;
						}
					}
				}
			}
		}

		// Insert node into the document
		newNode = src.parentNode.insertBefore(newNode,src.nextSibling);
		// Process INPUT types...
		child_inputs = newNode.getElementsByTagName('INPUT');
		parent_inputs = src.getElementsByTagName('INPUT');
		for (i=0;i<child_inputs.length;i++) {
			if (child_inputs[i].type!='file') { // Skip values assignment for file input type.
				child_inputs[i].value = (clone == "Y")? parent_inputs[i].value : '';
			}
			if (child_inputs[i].type=='button' || child_inputs[i].type=='radio' || child_inputs[i].type=='checkbox') { // Skip values assignment for file input type.
				child_inputs[i].value = parent_inputs[i].value;
			}
			if (child_inputs[i].type == 'radio' || child_inputs[i].type == 'checkbox') { // Assignment checked state for checkboxes and radios.
				child_inputs[i].checked = (clone == "Y")? parent_inputs[i].checked : parent_inputs[i].defaultChecked; 
			}
		}

		// Process TEXTAREA type...
		child_texts = newNode.getElementsByTagName('TEXTAREA');
		parent_texts = src.getElementsByTagName('TEXTAREA');
		for (i=0;i<child_texts.length;i++) {
			child_texts[i].value = (clone == "Y")? parent_texts[i].value : '';
		}

		// Process SELECT type...
		child_selects = newNode.getElementsByTagName('SELECT');
		parent_selects = src.getElementsByTagName('SELECT');
		for (i=0;i<child_selects.length;i++) {
			for (j=0;j<child_selects[i].options.length;j++) {
				child_selects[i].options[j].selected = (clone == "Y")? parent_selects[i].options[j].selected : parent_selects[i].options[j].defaultSelected;
			}
		}
	}
}


function fn_check_option_type_helper(tag_id, hide)
{
	trs = document.getElementById('variantsglobbo'+tag_id).getElementsByTagName('TR');
	if (trs.length>0) {
		document.getElementById('bulk_'+tag_id).style.display=hide;
		document.getElementById('name_'+tag_id).style.display=hide;
		document.getElementById('position_'+tag_id).style.display=hide;
		document.getElementById('hbulk_'+tag_id).style.display=hide;
		document.getElementById('hname_'+tag_id).style.display=hide;
		document.getElementById('hposition_'+tag_id).style.display=hide;
		for (i=1;i<trs.length;i++) {
			//if (trs[i].id.indexOf('no_hide') == -1) {
				trs[i].style.display=hide;
			//}
		}
	}
}
//
// Check option type
// Parameters:
// @value - can be (S)elect, (R)adio, (I)nput, (T)extarea, (C)heckbox
// @tag_id - tag identifier that should be hided or showed
function fn_check_option_type(value, tag_id)
{
	var i = 0;

	if (document.getElementById(tag_id)) {
		if (value == 'S') {
			document.getElementById('variantsglobbo'+tag_id).parentNode.parentNode.style.display = '';
			fn_check_option_type_helper(tag_id, '');
		}
		if (value == 'R') {
			document.getElementById('variantsglobbo'+tag_id).parentNode.parentNode.style.display = '';
			fn_check_option_type_helper(tag_id, '');
		}
		if (value == 'C') {
			document.getElementById('variantsglobbo'+tag_id).parentNode.parentNode.style.display = '';
			fn_check_option_type_helper(tag_id, 'none');
		}
		if (value == 'I') {
			document.getElementById('variantsglobbo'+tag_id).parentNode.parentNode.style.display = 'none';
		}
		if (value == 'T') {
			document.getElementById('variantsglobbo'+tag_id).parentNode.parentNode.style.display = 'none';
		}
	}
}

// 
// Expand/collapse row
// @tag_id - tag identifier that should be expanded/collapsed
// @flag - can be 1 (expand) or -1 (collapse)
function fn_invert_row(tag_id, flag)
{
	fn_show_tag(tag_id, flag);
	fn_show_tag('plus' + tag_id, flag);
	fn_show_tag('minus' + tag_id, flag);
}

// 
// Expand/collapse all rows in list
// @tag_id - tag identifier of list that should be expanded or collapsed
// @flag - can be 1 (expand) or -1 (collapse)
function fn_invert_all_rows(tag_id, flag)
{
	rows = document.getElementById(tag_id).getElementsByTagName('div');

	for (i = 0; i < rows.length; i++) {
			fn_show_tag(rows[i].id, flag);
			fn_show_tag('plus' + rows[i].id, !flag);
			fn_show_tag('minus' + rows[i].id, flag);
	}

	fn_show_tag('minus_plus', flag);
	fn_show_tag('plus_minus', !flag);

}


//
// Check / uncheck all checkboxes in form
// @form_name - form name whose checkboxes should be checked or unchecked
// @checkbox_id - tag identifier of checkboxes that should be checked or unchecked
// @flag - can be true or false
function fn_check_all_checkboxes(form_name, flag, checkbox_id)
{
	if (!checkbox_id)
		checkbox_id = 'delete_checkbox';

	if(!(d_form = document.forms[form_name]))
		return false;

	for(i=0; i < d_form.length; i++) {
		if (d_form.elements[i].id == checkbox_id && !d_form.elements[i].disabled)
			d_form.elements[i].checked = flag;
	}
	return true;
}

//
// Move selected elements of multiple select input
//
function fn_select_move_elements(from, to)
{

	from_elm = document.getElementById(from);
	to_elm = document.getElementById(to);

	if(!from_elm || !to_elm)
		return false;

	while(to_elm.selectedIndex != -1) {
		from_elm.options[from_elm.options.length] = new Option(to_elm.options[to_elm.selectedIndex].text, to_elm.options[to_elm.selectedIndex].value);
		to_elm.options[to_elm.selectedIndex] = null;
	}

	return true;
}

//
// Select all elements in selects input 
//
function fn_select_save_elements()
{
	var args = fn_select_save_elements.arguments;

	for (i = 0; i < args.length; i++) {
		if (document.getElementById(args[i])) {
			var select = document.getElementById(args[i]);
			for(var k = select.length-1; k >= 0; k--) {
				select.options[k].selected = true;
			}
		}
	}

	return true;
}

//
// Unselect all elements in selects input 
//
function fn_unselect_save_elements()
{
	var args = fn_unselect_save_elements.arguments;

	for (i = 0; i < args.length; i++) {
		if (document.getElementById(args[i])) {
			var select = document.getElementById(args[i]);
			for(var k = select.length-1; k >= 0; k--) {
				select.options[k].selected = false;
			}
		}
	}

	return true;
}


//
// Select text in text input
//
function select_input(select) 
{
	select.select();
}


// Compare 2 strings
// @haystack - where search
// @needle - what search
// @strict - exact compare or partial
function fn_compare_strings(haystack, needle, strict)
{
	if (strict == true) {
		return (haystack == needle);
	} else {
		return (haystack.indexOf(needle) == -1) ? false : true;
	}
}
//
// Check if there are any selected checkboxes in the form
// @form_name - form name in which checkboxes should be selected or deselected
// @checkbox_id - tag id of checkboxes that should be selected or deselected
// @no_alert - do not display notification
// @strict - compare ids strictly or partially
function fn_check_selected(form_name, checkbox_id, no_alert, strict) {
{
	if (typeof(strict) == 'undefined') {
		strict = true;
	}
	
	if (!checkbox_id) {
		checkbox_id = 'delete_checkbox';
	}

	if(!(d_form = document.forms[form_name])) {
		return false;
	}

	for(i=0; i < d_form.length; i++) {
		if (fn_compare_strings(d_form.elements[i].id, checkbox_id, strict)) {
			if (d_form.elements[i].checked) {
				return true;
			}
		}
	}
	if (!no_alert) {
		alert(error_no_items_selected);
	}
	return false;
}																													    }

function fn_delete_selected(form_name, mode_value, no_confirmation, checkbox_id, elm_name, strict) 
{

	if (typeof(strict) == 'undefined') {
		strict = true;
	}
	if (!fn_check_selected(form_name, checkbox_id, '', strict)) {
		return false;
	}
	if (!no_confirmation) {
		if (!confirm(text_delete_confirmation)) {
			return false;
		}
	}

	if (typeof(elm_name)== 'undefined' || elm_name.length == 0) {
		elm_name = mode_name;
	}

	//document.forms[form_name].elements[elm_name].value = mode_value;
	document.forms[form_name].submit();
	return true;
}

function fn_load_handlers()
{
	document_loaded = true;
	var i=0;
	for (i=0; i<onload_handlers.length; i++) {
		eval(onload_handlers[i]);
	}
}

//
// Perform request via <SCRIPT> tag
//
function fn_http_request(script_id,url,status_msg,status_id)
{

	var now = new Date();
	url = url + '&'+now.getTime(); // Prevent script from caching
	if (status_msg && status_id) {
		if (document.getElementById(status_id)) {
			document.getElementById(status_id).innerHTML = status_msg;
		}
	}

	var script_tag = document.getElementById(script_id);
	if (script_tag) {
		script_tag.parentNode.removeChild(script_tag);
	}

	script_tag = document.createElement('SCRIPT');
	script_tag.type = 'text/javascript';
	script_tag.language = 'javascript';
	script_tag.id = script_id;
    script_tag.src = url;
	document.body.appendChild(script_tag);
}

//
// String utility functions
//

// extract front part of string prior to searchString
function fn_str_get_front(mainStr,searchStr)
{
	foundOffset = mainStr.indexOf(searchStr)
	if (foundOffset == -1) {
		return null
	}
	
	return mainStr.substring(0,foundOffset)
}

// extract back end of string after searchString
function fn_str_get_end(mainStr,searchStr) 
{
	foundOffset = mainStr.indexOf(searchStr)
	if (foundOffset == -1) {
		return null
	}
	
	return mainStr.substring(foundOffset+searchStr.length,mainStr.length)
}

// insert insertString immediately before searchString
function fn_str_insert_string(mainStr,searchStr,insertStr) 
{
	var front = fn_str_get_front(mainStr,searchStr)
	var end = fn_str_get_end(mainStr,searchStr)

	if (front != null && end != null) {
		return front + insertStr + searchStr + end
	}
	
	return null
}

// remove deleteString
function fn_str_delete_string(mainStr,deleteStr) 
{
	return fn_str_replace_string(mainStr,deleteStr,"");
}

// replace searchString with replaceString
function fn_str_replace_string(mainStr,searchStr,replaceStr) 
{
	var front = fn_str_get_front(mainStr,searchStr)
	var end = fn_str_get_end(mainStr,searchStr)

	if (front != null && end != null) {
		return front + replaceStr + end
	}
	
	return '';
}

function fn_get_window_sizes()
{
	var wnd_arr = new Array();

	if (is_nav) {
		wnd_arr['offset_x'] = self.pageXOffset;
		wnd_arr['offset_y'] = self.pageYOffset;
		wnd_arr['view_height'] = self.innerHeight;
		wnd_arr['view_width'] = self.innerWidth;
		wnd_arr['height'] = window.height;
		wnd_arr['width'] = window.width;
	}
	
	if (is_ie || is_firefox || is_opera8) {
		wnd_arr['offset_x'] = document.body.scrollLeft;
		wnd_arr['offset_y'] = document.body.scrollTop;
		wnd_arr['view_height'] = document.body.clientHeight;
		wnd_arr['view_width'] = document.body.clientWidth;
		wnd_arr['height'] = document.body.scrollHeight;
		wnd_arr['width'] = document.body.scrollWidth;
	}

	return wnd_arr;
}

function fn_str_compare(a, b)
{
  if (a < b) return -1;
  if (a > b) return 1;
  return 0;
}

// Switch elements visibility
function fn_switch_elements(elms_str, exclude_list, hide)
{
	elms_list = elms_str.split(',');
	for (i=0;i<elms_list.length;i++) {
		elms = document.getElementsByTagName(elms_list[i]);
		if (elms.length>0) {
			for (j=0;j<elms.length;j++) {
				if (exclude_list.indexOf(elms[j].name)==-1) {
					elms[j].style.display = (hide == true) ? 'none' : '';
				}
			}
		}
	}
}

function fn_sleep(millis)
{
	date = new Date();
	var curDate = null;
	do { 
		curDate = new Date(); 
	} while(curDate-date < millis);
}


var html_editor_window;
var html_editor_id;
//var html_editor_object;
//var html_editor_area;

function fn_open_editor(id, object, area) {
	html_editor_id = id;
//	html_editor_object = object;
//	html_editor_area = area;

	if (html_editor_window) {
		html_editor_window.close();
	}
	if (!html_editor_window || html_editor_window.closed) {
		html_editor_window = window.open(html_editor_url+"popup.html?lang="+cart_language+'&btn_save='+lang['save']+'&btn_close='+lang['close'],"html_ed","status=no,top=100,left=100,height=600,width=800,resizable=no");
	}
	html_editor_window.focus();
}

function fn_check_product_field_type(value, tag_id)
{
	var i = 0;

	if (document.getElementById(tag_id)) {
		if (value == 'S') {
			document.getElementById('box_'+tag_id).style.display = '';
		}
		if (value == 'T') {
			document.getElementById('box_'+tag_id).style.display = 'none';
		}
	}
}

// Check email address for validity
function fn_check_email(email, msg) {
	if (email.length>0)
	{
		var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
		if (filter.test(email)) {
			return true;
		} else{
			alert(msg);
			return false;
		}
	} else {
		return true;
	}
}

//
// This function checks required fields and set a mark if something wrong
//
function fn_check_fields(form_name, extra_ids)
{
	var is_ok = true;
	var seqb_exists = document.getElementById('seqb');

	if (!document.forms[form_name]) {
		return false;
	}
	
	var is_ok = true;
	var set_mark = false;

	elms = document.forms[form_name].elements;
	for (i=0; i<elms.length; i++) {
		set_mark = false;

		// Check the email field
		if (extra_ids[elms[i].id] == 'E') {
			if (fn_check_email(elms[i].value, email_invalid) == false) {
				if (required_fields[elms[i].id] == 'Y' || fn_is_blank(elms[i].value) == false) {
					is_ok = false;
					set_mark = true;
				}
			}
		// Check for integer field
		} else if (extra_ids[elms[i].id] == 'I') {
			if (fn_is_integer(elms[i].value) == false) {
				if (required_fields[elms[i].id] == 'Y' || fn_is_blank(elms[i].value) == false) {
					is_ok = false;
					set_mark = true;
				}
			}
		// Check for blank value
		} else {
			if (required_fields[elms[i].id] == 'Y' && fn_is_blank(elms[i].value) == true) {
				is_ok = false;
				set_mark = true;
				if (seqb_exists && document.getElementById('seqb').checked == true && elms[i].id.substr(0,2)=='s_') {
					document.getElementById('seqb').click();
				}
			}
		}
		if (document.getElementById('status_'+elms[i].id)) {
			document.getElementById('status_'+elms[i].id).innerHTML = (set_mark == true)? warning_mark : "&nbsp;";
		}
	}

	if (is_ok == false) {
		alert(message);
	}
	return is_ok;
}

//
// Checks if the value is blank
//
function fn_is_blank(val){
	if (val==null){
		return true;
	}
	for (var i=0; i<val.length; i++) {
		if((val.charAt(i)!="\t")&&(val.charAt(i)!="\n")&&(val.charAt(i)!="\r")){
			return false;
		}
	}
	return true;
}

//
// Checks if the value is integer
//
function fn_is_integer(val)
{
	if(fn_is_blank(val)){
		return false;
	}
	for(var i=0; i<val.length; i++){
		if (!fn_is_digit(val.charAt(i))) {
			return false;
		}
	}
	return true;
}

//
// Checks if the value is digit
//
function fn_is_digit(num)
{
	if(num.length>1){
		return false;
	}
	var string="1234567890";
	if (string.indexOf(num)!=-1){
		return true;
	}
	return false;
}


function signupCheck(formobj,fieldRequired,fieldDescription){
	//1) Enter name of mandatory fields
	//var fieldRequired = Array( "Cust_Name", "Cust_Surname", "Cust_Email", "Username", "Password");
	//2) Enter field description to appear in the dialog box
	//var fieldDescription = Array( "Given Name", "Family Name", "Email", "Username", "Password");
	//3) Enter dialog message
	var alertMsg = "Bitte machen Sie die benoetigten Angaben, es fehlen :                       \n----------------------------------------------------------------                     \n";
	//alert(formobj.elements["plusdomain"].value);
	var l_Msg = alertMsg.length;

	for (var i = 0; i < fieldRequired.length; i++){
		var obj = formobj.elements[fieldRequired[i]];
		if (obj){
			switch(obj.type){
			case "select-one":
				if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "select-multiple":
				if (obj.selectedIndex == -1){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "text":
			case "textarea":
				if (obj.value == "" || obj.value == null){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			default:
				if (obj.value == "" || obj.value == null){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
			}
		}
	}
	if(document.getElementById('plz').value=="leer"){
		alertMsg += " - " + "PLZ";
	}

	if (alertMsg.length == l_Msg){
		formobj.submit();
		return true;
	}else{
		alert(alertMsg);
		return false;
	}
}


var reOneOrMoreDigits = /[\d+]/;
var reNoDigits = /[^\d]/gi;


function doMask(textBox) {

	var keyCode = event.which ? event.which : event.keyCode;

	// enter, backspace, delete and tab keys are allowed thru

	if(keyCode == 13 || keyCode == 8 || keyCode == 9 || keyCode == 46)
		return true;

	// get character from keyCode....dealing with the "Numeric KeyPad" 
	// keyCodes so that it can be used

	var keyCharacter = cleanKeyCode(keyCode);

	// grab the textBox value and the mask

	var val = textBox.value;
	var mask = textBox.mask;

	// simple Regex to check if key is a digit
	if(reOneOrMoreDigits.test(keyCharacter) == false)
		return false;

	// get value minus any masking by removing all non-numerics
	val = val.replace(reNoDigits,'');			

	// add current keystroke
	val += keyCharacter;

	// mask it...val holds the existing TextBox.value + the current keystroke
	textBox.value = val.maskValue(mask);
	setCaretAtEnd(textBox);
	return false;

}

// puts starting chars in field

function onFocusMask(textBox) {
	var val = textBox.value;
	var mask = textBox.mask;
	if(val.length == 0 || val == null) {
		var i = mask.indexOf('#');
		textBox.value = mask.substring(0,i);
	}
	setCaretAtEnd(textBox);
	// set just in case.
	textBox.maxlength = mask.length;
}

// blank field if no digits entered

function onBlurMask(textBox) {
	var val = textBox.value;
	// if no digits....nada entered.....blank it.
	if(reOneOrMoreDigits.test(val) == false) {
		textBox.value = '';
	}
}

String.prototype.maskValue = function(mask) {

	var retVal = mask;
	var val = this;

	//loop thru mask and replace #'s with current value one at a time
	// better way of doing this ???
	for(var i=0;i<val.length;i++) {
		retVal = retVal.replace(/#/i, val.charAt(i));
	}
	// get rid of rest of #'s

	retVal = retVal.replace(/#/gi, "");
	return retVal;
}

// The Numeric KeyPad returns keyCodes that ain't all that workable.
//
// ie: KeyPad '1' returns keyCode 97 which String.fromCharCode converts to an 'a'.
//
// This cheesy way allows the Numeric KeyPad to be used

function cleanKeyCode(key)
{
	switch(key)
	{
		case 96: return "0"; break;
		case 97: return "1"; break;
		case 98: return "2"; break;
		case 99: return "3"; break;
		case 100: return "4"; break;
		case 101: return "5"; break;
		case 102: return "6"; break;
		case 103: return "7"; break;
		case 104: return "8"; break;
		case 105: return "9"; break;
		default: return String.fromCharCode(key); break;
	}
}


function setCaretAtEnd (field) {
  if (field.createTextRange) {
    var r = field.createTextRange();
    r.moveStart('character', field.value.length);
    r.collapse();
    r.select();
  }
}

function extChk(objCheck){

	var st = objCheck.lastIndexOf(".")-(-1);
	var nd = objCheck.length;
	var ext = objCheck.substring(st,nd);

	if(ext == 'JPG' || ext == 'GIF' || ext == 'JPEG' || ext == 'PNG') {
		return true;
	} else {
		return false;
	}

}

function pictureUploadCheck(formobj,fieldRequired,fieldDescription){
	var alertMsg = "Invalid Picture Format ( *.jpg, *.gif, *.png) :                       \n----------------------------------------------------------------                     \n";
	//alert(formobj.elements["plusdomain"].value);
	var l_Msg = alertMsg.length;

	for (var i = 0; i < fieldRequired.length; i++){
		var obj = formobj.elements[fieldRequired[i]];
		if (obj){
			if (obj.value == "" || obj.value == null){
					//alertMsg += " - " + fieldDescription[i] + "\n";
			}else {
				if (!extChk(obj.value.toUpperCase())) {
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
			}
		}
	}

	if (alertMsg.length == l_Msg){
		return true;
	}else{
		alert(alertMsg);
		return false;
	}
}
