/*DOL*/
var g_InvalidValueMsgHead = "Совершены следующие ошибки:";

var g_rgx_Email = /^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$/i;
var g_rgx_SSNNo = /^\d{3}-\d{2}-\d{4}$/i;
var g_rgx_URL = /^(ht|f)tp(s?)\:\/\/[a-zA-Z0-9\-\._]+(\.[a-zA-Z0-9\-\._]+){2,}(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#_]*)?$/;

function DefaultVal( val, ret ){
	if( (typeof(ret) == 'undefined' || ret == null || ret == '') && ret!=false )
		ret = ""; 
	
	if( typeof(val) == 'undefined' || val == null || val == '' )
		return ret;
	else return val;
}

function PopupPage(pageUrl, pageWidth, pageHeight,windowName)
{
	//Code to display in the center goes here
	var left = (screen.width - pageWidth)/2;
	var top = (screen.height - pageHeight)/2;
	if (isNaN(left))
		left = 0;
	if (isNaN(top))
		top = 0;
	if (null == windowName	 )
		windowName = "_blank";
	var newPopup = window.open(pageUrl, windowName , "dependent=yes, directories=no, location=no, menubar=no, personalbar=no, resizable=no, scrollbars=no, status=no, toolbar=no, height=" + pageHeight + ", width=" + pageWidth + ", left=" + left + ", top=" + top + ", dialog=yes");
	return newPopup;
}
function WinLocation(href){
	window.location = href;
}

function OnChange(){
	document.getElementById('change').value = 1;
}

function OnCheckbox(obj){
	var elem = document.getElementById(obj);
	if( elem.checked )
		elem.value ="ON"; else elem.value ="OFF";
}

function SetValue(obj,value){
	var elem = document.getElementById(obj);
	elem.value = value;
}

/****** Common String functions ******/
String.prototype.trim = trim;
function trim()
{
	if (this.length > 0)
	{
		var retstr = this.replace(/^\s+/,"");
		retstr = retstr.replace(/\s+$/,"");
		return retstr;
	}
	else return this;
}

function valdFlds(objArray, errMsg)
{
	var focusObj = null;
	var errIndex = -1;

	for (var i=0; i<objArray.length; i++)
	{
		var obj = objArray[i];
		var msg = null;
		if ('R' == obj[0])
		{
			if ('undefined' != typeof(obj[1].value)){ 
				if  ((0 >= obj[1].value.trim().length) || ( obj[1].value.trim() == 0)){				
					msg = "\r\n- " + obj[2] + " значение не должно быть пустым";
				}
			}
		}else
		if ('RC' == obj[0])
		{
			if ('undefined' != typeof(obj[1].value))
			{ 
				if  (0 >= obj[1].value.length)
				{
				
					msg = "\r\n- " + obj[2] + " значение не должно быть пустым";
				}
			}
		}
		else if ('C' == obj[0])
		{
			if ((typeof(obj[1].value) != 'undefined') && (typeof(obj[3].value) != 'undefined'))
			{
				if (0 < obj[1].value.length || 0 < obj[3].value.length)
				{
					if (obj[1].value != obj[3].value)
					{
						msg = "\r\n- " + obj[2];
					}
				}
			}
		}
		else if ('G' == obj[0])
		{
			if (typeof(obj[1].value) != 'undefined')
			{
				if (0 < obj[1].value.length)
				{
					if (!obj[3].test(obj[1].value))
					{
						msg = "\r\n- " + "Ошибочное значение для " + obj[2];
					}

				}
			}
		}
		else if ('D' == obj[0])
		{
			if (obj[1].options && obj[2].options && obj[3].options)
			{
				if (obj[1].options[obj[1].selectedIndex].value == '' &&
					obj[2].options[obj[2].selectedIndex].value == '' &&
					obj[3].options[obj[3].selectedIndex].value == '')
				{
					if (obj[4])	//Mandatory field
					{
						msg = "\r\n- " + obj[5] + " значение не должно быть пустым";
					}
				}
			}
		}
		else if ('N' == obj[0])
		{
			if ('undefined' != typeof(obj[1].value))
			{
				if  ((0 >= obj[1].value.trim().length) || ( obj[1].value == 0))
				{
					msg = "\r\n- " + obj[2] + " значение не должно быть пустым";
				}
			}
		}else if ('M' == obj[0])
		{
			var nLength=255;
			if  (obj[1].value.length>nLength)
				obj[1].value = obj[1].value.substring(0,nLength);			
		}


		if (null != msg)
		{
			if (null == focusObj)
			{
				focusObj = obj[1];
				errIndex = i;
			}
			if (null == errMsg)
				errMsg = g_InvalidValueMsgHead
			errMsg += msg;
		}
	}

	var status = true;
	if (null != focusObj)
	{
		status = false;
		focusObj.focus();
	}
	return Array(status, errIndex, errMsg, focusObj);

}

function showTab(tab) {
	// tabdivs
	usertab = tab;
	t1 = document.getElementById('pr_datasheet1');
	t2 = document.getElementById('pr_datasheet2');
	t3 = document.getElementById('pr_datasheet3');
	t4 = document.getElementById('pr_datasheet4');
	t5 = document.getElementById('pr_datasheet5');
	t6 = document.getElementById('pr_datasheet6');
	t7 = document.getElementById('pr_datasheet7');
	t8 = document.getElementById('pr_datasheet8');

	navbar = document.getElementById('tabbar');
	test = navbar.getElementsByTagName('DIV');
	for (i=0; i < test.length; i++) {
		if (test[i].id == tab+'_tab') {
			test[i].className = 'formtab_on';
		} else {
			test[i].className = 'formtab';
		}
	}
	if (t1) {
	if (t1.id == 'pr_'+tab) {
			t1.className = 'pr_show';
	} else {
			t1.className = 'pr_hide';
	}
	}
	if (t2) {
	if (t2.id == 'pr_'+tab) {
			t2.className = 'pr_show';
	} else {
			t2.className = 'pr_hide';
			
	}
	}
	if (t3) {
	if (t3.id == 'pr_'+tab) {
		t3.className = 'pr_show';
	} else {
		t3.className = 'pr_hide';
	}
	}
	if (t4) {
	if (t4.id == 'pr_'+tab) {
		t4.className = 'pr_show';
	} else {
		t4.className = 'pr_hide';
	}
	}
	if (t5) {
	if (t5.id == 'pr_'+tab) {
		t5.className = 'pr_show';
	} else {
		t5.className = 'pr_hide';
	}
	}
}

var QL_DataRowHoverCss = 'dataH';
var QL_DataRowSelectedCss = 'dataS';
function QL_Submit(controlID, sourceType, sourceValue1, sourceValue2, dataRowIndex)
{
	var TEXT_SEARCH_LINK = 1;
	var ALPHABETIC_SEARCH_LINK = 2;
	var SORT_LINK = 3;
	var PAGE_LINK = 4;

	if (sourceType == TEXT_SEARCH_LINK)
	{
		// Text search button clicked
		var srchText = 	document.getElementById(controlID + "_tNSrchTxt");
		var srchField = document.getElementById(controlID + "_sNSrchFld");
		/*var Page = 		document.getElementById(controlID +"_hPg");
		Page.value=0;*/
				
		// Check for valid values
		if ((srchField.value.length > 0 && srchText.value.length <= 0) ||
			(srchField.value.length <= 0 && srchText.value.length > 0))
		{
			//if (typeof(container.srchInvalidValueMsg) != 'undefined')
				//alert(container.srchInvalidValueMsg);
			srchText.value='';
			//alert("Неверные параметры поиска");
			//return false;
		}
	}
	else if (sourceType == ALPHABETIC_SEARCH_LINK)
	{
		var srchTextObj = document.getElementById(controlID + "_hASrch");
		srchTextObj.value = sourceValue1;
	}
	else if (sourceType == SORT_LINK)
	{
		// Sort link clicked
		var orderObj = document.getElementById(controlID + "_hOFld");
		orderObj.value = sourceValue1;
		var orderDir = document.getElementById(controlID + "_hODirc");
		//orderDir.value = sourceValue2;
		if(sourceValue2=='asc')
			orderDir.value = 'desc'; else orderDir.value = 'asc';
	}
	else if (sourceType == PAGE_LINK)
	{
		// Page link clicked
		var pageObj = document.getElementById(controlID + "_hPg");
		pageObj.value = sourceValue1;
	}
	else
	{
		// External Source caused submit
	}
	var src = document.getElementById(controlID + "_hSubmitSrc");
	src.value = sourceType;
	src.form.submit();
}

function QL_MOver(sourceRow)
{
	if (sourceRow.className != QL_DataRowSelectedCss)
	{
		sourceRow.originalClass = sourceRow.className;
		sourceRow.className = QL_DataRowHoverCss;
	}
}

function QL_MOut(sourceRow)
{
	if (sourceRow.className != QL_DataRowSelectedCss)
		sourceRow.className = sourceRow.originalClass;
}

////
function QL_CBClick(controlID, srcCheckBox, rowIndex)
{
	var container = document.getElementById(controlID);
	if (srcCheckBox.checked)
	{
		//Check the limit
		if (QL_LimitExceeded(container, srcCheckBox.form))
		{
			QL_LimitExceedAction(container, srcCheckBox.form, rowIndex);
		}
		else
		{
			QL_SetCBState(srcCheckBox, rowIndex);
		}
	}
	else
	{
		// If the state is unchecked, Update the Header Check box state if required
		var headerCB = document.getElementById(controlID + "_cSlcHd");
		if (headerCB)
		{
			headerCB.checked = false;
		}
		QL_SetCBState(srcCheckBox, rowIndex);
	}
}

function QL_LimitExceeded(container, form)
{
	var slctLimit = container.getAttribute("slctLimit");
	if (slctLimit)
	{
		var checkBoxArray = eval('form.elements["' + container.id + '_cSlcPK[]"]');
		// Get Total Selected Checkboxes
		var selectionCount = 0;
		for (var idx=0; idx<checkBoxArray.length; idx++)
		{
			if (checkBoxArray[idx].checked)
				selectionCount++;
		}

		// Check if limit exceeded
		if (selectionCount > slctLimit)
		{
			return true;
		}
	}
	return false;
}

function QL_HeaderCBClick(controlID, headerCB)
{
	var frm = headerCB.form;
	var targetCheckBox = eval('frm.elements["' + controlID + '_cSlcPK[]"]');

	// Return if no child-checkboxes exist
	if (null == targetCheckBox || typeof(targetCheckBox) == 'undefined') return;
	
	if (targetCheckBox.length)
	{
		for (var idx=0; idx < targetCheckBox.length; idx++)
		{	
			QL_SetCBState(targetCheckBox[idx], idx, headerCB.checked);
		}
	}
	else
	{
		QL_SetCBState(targetCheckBox, 0, headerCB.checked);
	}
}

//function QL_SetCBState(container, targetCheckBox, rowIndex, checked)
function QL_SetCBState(targetCheckBox, rowIndex, checked)
{
	var QL_PKValue_Enclosing_Char = '^';
	if (targetCheckBox)
	{
		var originalState = targetCheckBox.checked;

		//Set the checked-state, if passed
		if (null != checked)
		{
			targetCheckBox.checked = checked;
		}

		var containerRow = targetCheckBox.parentNode.parentNode;
		if (targetCheckBox.checked)
		{
			if (containerRow.className != QL_DataRowHoverCss && containerRow.className != QL_DataRowSelectedCss)
				containerRow.originalClass = containerRow.className;
			containerRow.className = QL_DataRowSelectedCss;
		}
		else
		{
			if (null != checked)
			{
				if (targetCheckBox.checked != originalState)
					containerRow.className = containerRow.originalClass;
			}
			else
				containerRow.className = containerRow.originalClass;
		}
	}
}

function QL_LimitExceeded(container, form)
{
	var slctLimit = container.getAttribute("slctLimit");
	if (slctLimit)
	{
		var checkBoxArray = eval('form.elements["' + container.id + '_cSlcPK[]"]');
		// Get Total Selected Checkboxes
		var selectionCount = 0;
		for (var idx=0; idx<checkBoxArray.length; idx++)
		{
			if (checkBoxArray[idx].checked)
				selectionCount++;
		}

		// Check if limit exceeded
		if (selectionCount > slctLimit)
		{
			return true;
		}
	}
	return false;
}

function QL_LimitExceedAction(container, form, rowIndex)
{
	var checkBoxArray = eval('form.elements["' + container.id + '_cSlcPK[]"]');
	var slctLimit = container.getAttribute("slctLimit");
	if (slctLimit == 1)
	{
		for (var idx=0; idx<checkBoxArray.length; idx++)
		{
		
			if (idx == rowIndex)
				QL_SetCBState(checkBoxArray[idx], idx, true);
			else
				QL_SetCBState(checkBoxArray[idx], idx, false);
		}
	}
	else
	{
		QL_SetCBState(checkBoxArray[rowIndex], rowIndex, false);
		var slctLimitMsg = container.getAttribute("slctLimitExceedMsg");
		if (slctLimitMsg)
			alert(slctLimitMsg);
	}
}

/**** JS Functions that can be called from elsewhere ****/
function QL_GetSelectorCheckBoxes(controlID)
{
	var src = document.getElementById(controlID + "_hSubmitSrc");
	var frm = src.form;

	var checkBoxArray = eval('frm.elements["' + controlID + '_cSlcPK[]"]');
	if (checkBoxArray)
	{
		return checkBoxArray;
	}
	return null;
}

function QL_GetSelectionCount(controlID)
{
	var src = document.getElementById(controlID + "_hSubmitSrc");
	var frm = src.form;

	var selectionCount=0;
	var checkBoxArray = eval('frm.elements["' + controlID + '_cSlcPK[]"]');
	if (checkBoxArray)
	{
		if (checkBoxArray.length)
		{
			
			for(var idx=0; idx <  checkBoxArray.length; idx++)
			{
				if (checkBoxArray[idx].checked)
					selectionCount++;
			}
		}
		else
		{
			if (checkBoxArray.checked) selectionCount++;
		}
	}
	return selectionCount;
}

function PhoneRule(oComp , length, name){
	var temp;
	temp = "";
	if (typeof(oComp) == 'undefined' || oComp == null || oComp == '')
	{
		alert('Error: Input object not specified.');
		return false;
	}
	if((length==1 && oComp.value.length==1 )|| (length==3 && oComp.value.length==3 ))
		document.getElementById(name).value = "";
	if(length==9){
		for (var i = 0;i < oComp.value.length;i++){
			if( i==3 && oComp.value.charAt(i)!="-" )
				temp += "-";
			if( i==6 && oComp.value.charAt(i)!="-")
				temp += "-";
			temp += oComp.value.charAt(i);
		}
		oComp.value = temp;
	}
	if( typeof(name) != 'undefined' && name != null && name != '' && oComp.value.length==length)
	document.getElementById(name).focus();
}

//Standard Rules
var g_ruleUserName = ".0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz";
var g_ruleName = ".0123456789 ABCDEFGHIJKLMNOPQRSTUVWXYZ_-abcdefghijklmnopqrstuvwxyz'";
var g_ruleZip = "0123456789- ";
var g_rulePhone = "0123456789- ()";
var m_rulePhone = "0123456789-";
var g_ruleNumber = ".0123456789";
var g_ruleSSNNo = "-0123456789";
var g_ruleCtyStateCntry = " #\'()-.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz\"";

function isRule(oComp, sRule, nLength, fdecimal)
{

	if (fdecimal == "" || typeof(fdecimal) == "undefined")
	{
		fdecimal = false;
	}

	//If the object is not specified return false
	if (typeof(oComp) == 'undefined' || oComp == null || oComp == '')
	{
		alert('Error: Input object not specified.');
		return false;
	}
	//If neither rule nor max length is specified, return false
	else if (typeof(sRule) == 'undefined' && typeof(nLength) == 'undefined')
	{
		alert('Error: No rule/maximum lenght for input object specified.');
		return false;
	}

	var noErrorFlg = true;

	//If object is specified and either of rule is specified,
	if (typeof(sRule) != 'undefined' && sRule != null)
	{
		var temp;
		sRule = sRule + "";
		var discardChars = false;
		if (sRule.length > 0 && sRule.charAt(0) == "~")
		{
			sRule = sRule.substring(1);
			discardChars = true;
		}

		if (typeof(oComp) == "undefined" || typeof(sRule) == "undefined")
			return false;

		for (var i = 0;i < oComp.value.length;i++)
		{
			temp = oComp.value.charAt(i);

			if ((!discardChars && sRule.indexOf(temp) == -1) || (discardChars && sRule.indexOf(temp) >= 0))
			{
				alert("Введен неверный символ!");
				oComp.value = oComp.value.substring(0,i);// + (oComp.value.length > i ? oComp.value.substring(i+1):"");
				noErrorFlg = false;
				break;
			}
		}
	}
	if (nLength)
	{
		if (fdecimal)
		{
			nLength -= fdecimal;
			var dp = oComp.value.indexOf(".");
			var p1;
			var p2 = "";
			if (dp >= 0)
			{
				p1 = oComp.value.substring(0,dp);
				p2 = oComp.value.substring(dp+1);
			}
			else
			{
				p1 = oComp.value;
			}
			if (p1.length > nLength)
			{
				oComp.value = oComp.value.substring(0,nLength);
				return noErrorFlg;
			}
			for (var i = 0;i < p2.length;i++)
			{
				var ch = p2.charAt(i);
				if (ch < '0' || ch > '9')
				{
					oComp.value = p1 + "." + p2.substring(0,i);
					return noErrorFlg;
				}
			}
			if (p2.length > fdecimal)
			{
				oComp.value = p1 + "." + p2.substring(0,fdecimal);
			}
		}
		else if (oComp.value.length > nLength)
		{
			oComp.value = oComp.value.substring(0,nLength);
		}
	}
	return noErrorFlg;
}