﻿function StartCopy()
{
	LoopTrough("input");
	LoopTrough("textarea");
	LoopTrough("select");
}

function LoopTrough(tagName)
{
	var thisElements = document.getElementsByTagName(tagName);
	var openerElements = window.opener.document.getElementsByTagName(tagName);
	var i, j;
	var openerShortName, thisShortName;
	for(i=0; i < openerElements.length; i++)
	{
		openerShortName = openerElements[i].id;
		if(openerShortName == null || openerShortName.length == 0) {
			continue;
		}
    	if(openerElements[i].type == "checkbox" || openerElements[i].type == "radio"){
			openerShortName = GetLongName(openerShortName);
		} else {
			openerShortName = GetShortName(openerShortName);
		}
		try
		{
			for(j=0; j < thisElements.length; j++)
			{
				thisShortName = thisElements[j].id;
				if(thisShortName == null || thisShortName.length == 0) {
					continue;
				}
				if(thisElements[j].type == "checkbox" || thisElements[j].type == "radio"){
					thisShortName = GetLongName(thisShortName);
				} else {
					thisShortName = GetShortName(thisShortName);
				}
				if(thisShortName == openerShortName) {
				    //alert(thisShortName + ' ' + openerShortName);
					CopyValues(openerElements[i], thisElements[j]);
					break;
				}
			}
		}
		catch(e) {}
	}
}

function GetShortName(value)
{
	return value.substring(value.lastIndexOf("_")+1, value.length);
}

function GetLongName(value)
{
	var s, i;
	var arr = value.split("_");
	for(i = arr.length - 2; i<arr.length; i++) {
		s += arr[i] + "_";
	}
	return s.substring(0, s.length-1);
}


function CopyValues(source, destination)
{
	switch(source.type)
	{
		case "select-one":
			destination.selectedIndex = source.selectedIndex;
			break;
		case "textarea":
			destination.innerText = source.innerText;
			break;
		case "text":
			destination.value = source.value;
			break;
		case "radio":
			destination.checked = source.checked;
			break;
		case "checkbox":
			destination.checked = source.checked;
			break;
		default:
			break;
	}
}
