/******************************************************************************
 qForm Extension : for Many to Many Containers

 Clayton Partridge
******************************************************************************/

// Removes Selected items from a Select box.
function _removeOptions(field1, field2, sort, type, selectItems, reset){
	var sort = _param(arguments[2], true, "boolean");
	var type = _param(arguments[3].toLowerCase(), "selected");
	if( type != "all" && type != "selected" ) type = "selected";
	var selectItems = _param(arguments[4], true, "boolean");
	var reset = _param(arguments[5], false, "boolean");
	var doAll = (type == "all") ? true : false;

	if( field1.type.substring(0,6) != "select" ) return alert("This method is only available to select boxes. \nThe field \"" + field1.name + "\" is not a select box.");
	if( field2.type.substring(0,6) != "select" ) return alert("This method is only available to select boxes. \nThe field \"" + field2.name + "\" is not a select box.");

	// clear the select box
	if( reset ) field2.length = 0;

	for( var i=0; i < field1.length; i++ ){
		// if the current option is selected, move it
		if( doAll || field1.options[i].selected ){
			//field2.options[field2.length] = new Option(field1.options[i].text, field1.options[i].value, false, selectItems);
			field1.options[i] = null;
			i--; // since you just deleted a option, redo this array position next loop
		}
	}

	// if sorting the fields
	if( sort ) _sortOptions(field2);
	return true;
}

// Removes Selected items from a Select box with some additional conditions for homepage flash nav.
function _removeOptionsFlashNav(field1, field2, sort, type, selectItems, reset){
	var sort = _param(arguments[2], true, "boolean");
	var type = _param(arguments[3].toLowerCase(), "selected");
	if( type != "all" && type != "selected" ) type = "selected";
	var selectItems = _param(arguments[4], true, "boolean");
	var reset = _param(arguments[5], false, "boolean");
	var doAll = (type == "all") ? true : false;

	if( field1.type.substring(0,6) != "select" ) return alert("This method is only available to select boxes. \nThe field \"" + field1.name + "\" is not a select box.");
	if( field2.type.substring(0,6) != "select" ) return alert("This method is only available to select boxes. \nThe field \"" + field2.name + "\" is not a select box.");

	// clear the select box
	if( reset ) field2.length = 0;

	for( var i=0; i < field1.length; i++ ){
		// if the current option is selected, move it
		if( doAll || field1.options[i].selected ){
			if( field1.options[i].value.indexOf('px') == -1 ){
				return false;
			}
			//field2.options[field2.length] = new Option(field1.options[i].text, field1.options[i].value, false, selectItems);
			field1.options[i] = null;
			i--; // since you just deleted a option, redo this array position next loop
		}
	}

	// if sorting the fields
	if( sort ) _sortOptions(field2);
	return true;
}

// define Field transferTo(); prototype
function _Field_RemoveFrom(field, sort, type, selectItems, reset){
	// if the current field is disabled or locked, then kill the method
	if( this.isLocked() || this.isDisabled() ) return false; 
	var sort = _param(arguments[1], true, "boolean");
	var type = _param(arguments[2], "selected");
	var selectItems = _param(arguments[3], true, "boolean");
	var reset = _param(arguments[4], false, "boolean");

	_removeOptions(this.obj, this.qForm[field].obj, sort, type, selectItems, reset);
	return true;
}

Field.prototype.RemoveFrom = _Field_RemoveFrom;


function _Field_RemoveFromFlashNav(field, sort, type, selectItems, reset){
	// if the current field is disabled or locked, then kill the method
	if( this.isLocked() || this.isDisabled() ) return false; 
	var sort = _param(arguments[1], true, "boolean");
	var type = _param(arguments[2], "selected");
	var selectItems = _param(arguments[3], true, "boolean");
	var reset = _param(arguments[4], false, "boolean");

	_removeOptionsFlashNav(this.obj, this.qForm[field].obj, sort, type, selectItems, reset);
	return true;
}

Field.prototype.RemoveFromFlashNav = _Field_RemoveFromFlashNav;

function _transferOptionsFlashNav(field1, field2, field3, sort, type, selectItems, reset){
	var sort=_param(arguments[2], true, "boolean");
	var type=_param(arguments[4].toLowerCase(), "selected");
	
	if(type!="all" && type!="selected")type="selected";
	var selectItems=_param(arguments[5], true, "boolean");
	var reset=_param(arguments[6], false, "boolean");
	var doAll=(type=="all")? true : false;
	
	if(field1.type.substring(0,6)!="select")return alert("This method is only available to select boxes. \nThe field \"" + field1.name + "\" is not a select box.");
	if(field2.type.substring(0,6)!="select")return alert("This method is only available to select boxes. \nThe field \"" + field2.name + "\" is not a select box.");
	
	if(reset)field2.length=0;for(var i=0;i<field1.length;i++){
		if(doAll || field1.options[i].selected){
			if( field1.options[i].value.indexOf('px') != -1 ){
				return false;
			}
			if( field1.options[i].value == field3.options[0].value){
				alert("Cannot remove, as this is the default category");
				return false;
			}
			field2.options[field2.length]=new Option(field1.options[i].text, field1.options[i].value, false, selectItems);
			field1.options[i]=null;i--;}
		}
	if(sort)_sortOptions(field2);
		return true;
	}

			
function _Field_transferFromFlashNav(field, field3, sort, type, selectItems, reset){
	if(this.isLocked()|| this.isDisabled())return false;
	var sort=_param(arguments[1], true, "boolean");
	var type=_param(arguments[2], "selected");
	var selectItems=_param(arguments[3], true, "boolean");
	var reset=_param(arguments[4], false, "boolean");
	
	_transferOptionsFlashNav(this.qForm[field].obj, this.obj, this.qForm[field3].obj, sort, type, selectItems, reset);
	return true;
}
Field.prototype.transferFromFlashNav=_Field_transferFromFlashNav;


// Copy selected items from one select box to another

function _copyOptions(field1, field2, sort, type, selectItems, reset){
	var sort = _param(arguments[2], true, "boolean");
	var type = _param(arguments[3].toLowerCase(), "selected");
	if( type != "all" && type != "selected" ) type = "selected";
	var selectItems = _param(arguments[4], true, "boolean");
	var reset = _param(arguments[5], false, "boolean");
	var doAll = (type == "all") ? true : false;

	if( field1.type.substring(0,6) != "select" ) return alert("This method is only available to select boxes. \nThe field \"" + field1.name + "\" is not a select box.");
	if( field2.type.substring(0,6) != "select" ) return alert("This method is only available to select boxes. \nThe field \"" + field2.name + "\" is not a select box.");

	// clear the select box
	if( reset ) field2.length = 0;

	for( var i=0; i < field1.length; i++ ){
		// if the current option is selected, move it
		if( doAll || field1.options[i].selected ){
			field2.options[field2.length] = new Option(field1.options[i].text, field1.options[i].value, false, selectItems);
			//field1.options[i] = null;
			//i--; // since you just deleted a option, redo this array position next loop
		}
	}

	// if sorting the fields
	if( sort ) _sortOptions(field2);
	return true;
}

// define Field transferTo(); prototype
function _Field_CopyTo(field, sort, type, selectItems, reset){
	// if the current field is disabled or locked, then kill the method
	if( this.isLocked() || this.isDisabled() ) return false; 
	var sort = _param(arguments[1], true, "boolean");
	var type = _param(arguments[2], "selected");
	var selectItems = _param(arguments[3], true, "boolean");
	var reset = _param(arguments[4], false, "boolean");

	_copyOptions(this.obj, this.qForm[field].obj, sort, type, selectItems, reset);
	return true;
}

Field.prototype.CopyTo = _Field_CopyTo;


function __isPositiveInt(){
  // check to make sure the current value is greater than zero
  var FldValue = parseFloat(this.value);
  
  if(FldValue < 0 || isNaN(FldValue) || this.value != FldValue){
    // here's the error message to display
    this.error = "The " + this.description + " field must contain a proper number.";
  }
}

_addValidator("isPositiveInt", __isPositiveInt);





function __isCanadaPostalCode(){
 
  if(this.value.length == 6 && this.value.match(/^([A-Za-z][0-9][A-Za-z][0-9][A-Za-z][0-9])/)){
    return true;
  }
  if(this.value.length == 7 && this.value.match(/^([A-Za-z][0-9][A-Za-z][\s][0-9][A-Za-z][0-9])/)){
    return true;
  }
  if(this.value.length == 7 && this.value.match(/^([A-Za-z][0-9][A-Za-z][-][0-9][A-Za-z][0-9])/)){
    return true;
  }
  return false;
}

_addValidator("isCanadaPostalCode", __isCanadaPostalCode);


// Copy selected item from a select box to a text field

function _copyToTextOptions(field1, field2, sort, type, selectItems, reset){
	var sort = _param(arguments[2], false, "boolean");
	var type = _param(arguments[3].toLowerCase(), "selected");
	if( type != "all" && type != "selected" ) type = "selected";
	var selectItems = _param(arguments[4], true, "boolean");
	var reset = _param(arguments[5], false, "boolean");
	var doAll = (type == "all") ? true : false;

	if( field1.type.substring(0,6) != "select" ) return alert("This method is only available to select boxes. \nThe field \"" + field1.name + "\" is not a select box.");
	if( field2.type.substring(0,6) != "select" ) return alert("This method is only available to select boxes. \nThe field \"" + field2.name + "\" is not a select box.");

	// clear the select box
	if( reset ) field2.length = 0;

	for( var i=0; i < field1.length; i++ ){
		// if the current option is selected, move it
		
		if( doAll || field1.options[i].selected ){
			if( field1.options[i].value.indexOf('px') != -1 ){
				return false;
			}
			if(field2.length){
				field2.options.length = null;
			}
			field2.options[field2.length] = new Option(field1.options[i].text, field1.options[i].value, false, selectItems);
			//field1.options[i] = null;
			//i--; // since you just deleted a option, redo this array position next loop
		}
	}

	// if sorting the fields
	if( sort ) _sortOptions(field2);
	return true;
}

// define Field transferTo(); prototype
function _Field_CopyToText(field, sort, type, selectItems, reset){
	// if the current field is disabled or locked, then kill the method
	if( this.isLocked() || this.isDisabled() ) return false; 
	var sort = _param(arguments[1], false, "boolean");
	var type = _param(arguments[2], "selected");
	var selectItems = _param(arguments[3], true, "boolean");
	var reset = _param(arguments[4], false, "boolean");

	_copyToTextOptions(this.obj, this.qForm[field].obj, sort, type, selectItems, reset);
	return true;
}

Field.prototype.CopyToText = _Field_CopyToText;
