// JavaScript Document
delay = 20;
step = 1;

is_start = true;
fullwidth = 0;
cpos = 0;
scrollw = 0;

function start_scroll()
{
  var scrolltext = document.getElementById('scrolltext');
  var scroll = document.getElementById('scroll');

  fullwidth = scrolltext.offsetWidth;
  scrolltext.style.left = scroll.offsetWidth;
  scrollw = scroll.offsetWidth;
  scrolltext.style.position = 'relative';
  
  myinterval = setInterval(do_scroll, delay);
}

function do_scroll()
{
  var scroll = document.getElementById('scroll');
  var scrolltext = document.getElementById('scrolltext');
  
  if (is_start)
  {
    cpos = scroll.offsetWidth;
    scrolltext.style.left = cpos;
    is_start = false;
  }
  else
  {
    cpos -= step;
    scrolltext.style.left = cpos;
    if (cpos < -fullwidth) { is_start = true; }
  }
}

function stop_scroll()
{
  clearInterval(myinterval);
}

function continue_scroll()
{
  myinterval = setInterval(do_scroll, delay);
}

function do_resize()
{
  var scroll = document.getElementById('scroll');
  newscrollw = scroll.offsetWidth;
  
  if (cpos >= 0) { absstp = scrollw - cpos; }
  else { absstp = scrollw + Math.abs(cpos); }

  oldprc = absstp / (scrollw + fullwidth);
  newlen = newscrollw + fullwidth;
  newabsstp = newlen * oldprc;

  if (newabsstp <= newscrollw) { cpos = newscrollw - newabsstp; }
  else { cpos = -(newabsstp - newscrollw); }

  scrollw = newscrollw;
}


function group_disable(group_name)
	{
	var obj=document.getElementById(group_name);
	obj.style.display='none';
	return false;	
	}
function group_enable(group_name)
	{
	var obj=document.getElementById(group_name);
	obj.style.display='';
	return false;	
	}

function group_status(group_name)
	{
	var obj=document.getElementById(group_name);
	return obj.style.display;	
	}
function insertNthChar(str,chr,nth)
	{
	var newstr = "";
	var c;
	for (var i = 0; i<str.length; i++)
		{
		c = str.charAt(i);
		newstr = newstr + c;
		if (i%nth==0 && (i<str.length-1) && i!=0)
			{
			newstr = newstr + chr;
			}
		}
	return newstr;
	}
   /*
   **  Returns the caret (cursor) position of the specified text field.
   **  Return value range is 0-oField.length.
   */
   function doGetCaretPosition (oField) {

     // Initialize
     var iCaretPos = 0;

     // IE Support
     if (document.selection) { 

       // Set focus on the element
       oField.focus ();
  
       // To get cursor position, get empty selection range
       var oSel = document.selection.createRange ();
  
       // Move selection start to 0 position
       oSel.moveStart ('character', -oField.value.length);
  
       // The caret position is selection length
       iCaretPos = oSel.text.length;
     }

     // Firefox support
     else if (oField.selectionStart || oField.selectionStart == '0')
       iCaretPos = oField.selectionStart;

     // Return results
     return (iCaretPos);
   }


   /*
   **  Sets the caret (cursor) position of the specified text field.
   **  Valid positions are 0-oField.length.
   */
   function doSetCaretPosition (oField, iCaretPos) {

     // IE Support
     if (document.selection) { 

       // Set focus on the element
       oField.focus ();
  
       // Create empty selection range
       var oSel = document.selection.createRange ();
  
       // Move selection start and end to 0 position
       oSel.moveStart ('character', -oField.value.length);
  
       // Move selection start and end to desired position
       oSel.moveStart ('character', iCaretPos);
       oSel.moveEnd ('character', 0);
       oSel.select ();
     }

     // Firefox support
     else if (oField.selectionStart || oField.selectionStart == '0') {
       oField.selectionStart = iCaretPos;
       oField.selectionEnd = iCaretPos;
       oField.focus ();
     }
   }	
function watch_insert_input(fieldname,chr,nth)
	{
//				alert(fieldname.value.length);
	if (fieldname)
		{
		var input=fieldname.value;
		if (input.length%nth==0 && input.length>0)
			{
//				alert(input.length%nth);
			fieldname.value=fieldname.value+chr;
			}
		return;
		}
	}
	
function check_user_cart(userdata)
	{
	var confirm_str='Зараз Ваше замовлення буде надіслано.\nПеревірте, будь-ласка усі поля форми на коректність даних та відповідність до Вашого бажання.\nВи ввели наступні дані:\n\n'
	+document.getElementById('username').value+'\n'
	+document.getElementById('userphone_a').value+' '+document.getElementById('userphone_b').value+'\n'
	+insertNthChar(document.getElementById('userphone2').value,' ',3)+'\n'
	+document.getElementById('usermail').value+'\n'
	+document.getElementById('usercard').value+'\n'
	+document.getElementById('usercity').value+'\n'
	+'\nНадіслати замовлення?';	
	if (document.getElementById('username').value.length<6)
		{
		alert('Заповніть поле ПІБ');	
		return false;
		}
	if (document.getElementById('userphone_a').value.length<3)
		{
		alert('Заповніть поле мобільний телефон');	
		return false;
		}
	if (document.getElementById('userphone_b').value.length<7)
		{
		alert('Заповніть поле мобільний телефон');	
		return false;
		}
	if (document.getElementById('key').value.length<3)
		{
		alert('Заповніть поле код підтвердження');	
		return false;
		}
	return confirm(confirm_str);	
	}
function check_user_resume()
	{
	if (document.getElementById('username').value.length<6)
		{
		alert('Заповніть поле ПІБ');	
		return false;
		}
	if (document.getElementById('userphone').value.length<6)
		{
		alert('Заповніть поле контактний телефон');	
		return false;
		}
	if (document.getElementById('usertext').value.length<10)
		{
		alert('Заповніть поле Резюме');	
		return false;
		}
	if (document.getElementById('key').value.length<3)
		{
		alert('Заповніть поле код підтвердження');	
		return false;
		}
	return true;	
	}
function check_user_consult()
	{
	if (document.getElementById('username').value.length<6)
		{
		alert('Заповніть поле ПІБ');	
		return false;
		}
	if (document.getElementById('usermail').value.length<10)
		{
		alert('Заповніть поле e-mail');	
		return false;
		}
	if (document.getElementById('usertext').value.length<10)
		{
		alert('Заповніть поле Запитання');	
		return false;
		}
	if (document.getElementById('key').value.length<3)
		{
		alert('Заповніть поле код підтвердження');	
		return false;
		}
	return true;	
	}
