var xmlhttp;
var atbilde = null;

function actionOnClose()
  {
  if(actionAfterDialogClose==null || actionAfterDialogClose=='') return;
  eval(actionAfterDialogClose);
}

function onlyInt(id)
{
  var v = document.getElementById('countField_'+id).value;
  if(v!=null && v!='')
  {
    if(parseFloat(v)==v) var ret = v;
    else ret = '1';
  }
  else
  {
    var ret = '1';
  }
  a('setProductCount|'+id+'|'+ret);
  getGozaProcuListi();
}

function onMouseOverEfect(id)
{
  document.getElementById('menu_'+id).style.backgroundPosition = '0px -38px';
  try{document.getElementById('submenu_'+id).style.display = 'block';}catch(e){}
}

function onMouseOutEfect(id)
{
  document.getElementById('menu_'+id).style.backgroundPosition = '0px 0px';
  try{document.getElementById('submenu_'+id).style.display = 'none';}catch(e){}
}

function atveramBildi(fails, w, h) 
{
  w = parseFloat(w)+20;
	h = parseFloat(h)+20;
	var winl = (screen.width-w)/2;
	var wint = (screen.height-h)/2;
	var settings ='height='+h+',';
	settings +='width='+w+',';
	settings +='top='+wint+',';
	settings +='left='+winl+',';
	settings +='scrollbars=no,';
	settings +='resizable=no';
	open(fails, 'Motodroms', settings);
}

function a(Dati)
{
  var Dati = 'Dati='+Dati;
	var G_POST_Adrese = '/lv/ajax/';
	var Metode = 'POST';
	Adrese = G_POST_Adrese;
	xmlhttp = null;
	if (window.XMLHttpRequest)
	xmlhttp = new XMLHttpRequest;
	else if (window.ActiveXObject)
	xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	xmlhttp.open(Metode, Adrese, false);
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.onreadystatechange = function()
	{
	 if (xmlhttp.readyState == 4)
	 {
      if (xmlhttp.status == 200)
			{
			   atbilde = null; 
				atbilde = xmlhttp.responseText;
			}
	 }
	};
	xmlhttp.send(Dati);
	atbilde = null; 
	atbilde = xmlhttp.responseText;
}

function closeAndGoHome()
{
  a('clearGrozs|');
  dialog('', 'hide', '');
  window.location = '/'+sys_lang+'/home';
}

function getGroza_satursCount()
{
  atbilde==null
  a('getGroza_satursCount|');
  if(atbilde==null || atbilde=='' || atbilde=='0'){atbilde = '0';}
//	else{document.getElementById('mirgo').style.visibility = 'visible';}
  var grozacount = atbilde;
  if(atbilde.substring(atbilde.length-1, 1)==1) grozacount = grozacount;
  else grozacount = grozacount;
  document.getElementById('grozaa_x_preces').innerHTML = grozacount;
}

		function userStatuss()
		{
			var logedin = document.getElementById('registred_user').value;

			if(logedin=='Y')
			{
				var user_statuss = document.getElementById('user_statuss').value;
				if(user_statuss!=null && user_statuss=='Y')
				{
					a('logOff|');
					document.getElementById('user_statuss').value = 'N';
					document.getElementById('reg_name').value = '';
					document.getElementById('reg_surname').value = '';
					document.getElementById('reg_email').value = '';
					document.getElementById('reg_phone').value = '';
					document.getElementById('reg_address').value = '';
					document.getElementById('reg_pay').value = '';
				}
				document.getElementById('registred_user_radio_button').className = 'radio_checked';
				document.getElementById('unregistred_user_radio_button').className = 'radio';
				document.getElementById('registred_user_form').style.display = 'block';
				document.getElementById('unregistred_user_form').style.display = 'none';
			}
			else
			{
				document.getElementById('registred_user_radio_button').className = 'radio';
				document.getElementById('unregistred_user_radio_button').className = 'radio_checked';
				document.getElementById('registred_user_form').style.display = 'none';
				document.getElementById('unregistred_user_form').style.display = 'block';
			}
		}


		function samaksasVeids()
		{
			var curr_mode = document.getElementById('reg_pay').value;
			if(curr_mode!=null && curr_mode=='1')
			{
				document.getElementById('reg_payMode1_radio_button').className = 'radio_checked';
				document.getElementById('reg_payMode2_radio_button').className = 'radio';
			}
			else if(curr_mode!=null && curr_mode=='2')
			{
				document.getElementById('reg_payMode2_radio_button').className = 'radio_checked';
				document.getElementById('reg_payMode1_radio_button').className = 'radio';
			}
		}

		function validateRegistrationForm(registred)
		{
			var regNameField = document.getElementById('reg_name');
			var regSurnameField = document.getElementById('reg_surname');
			var regEmailField = document.getElementById('reg_email');
			var regPhoneField = document.getElementById('reg_phone');
			if(regNameField==null || regNameField.value.length==0)
			{
				alert('Nav norādīts vārds');
				regNameField.focus();
				return false;				
			}
			if(regSurnameField==null || regSurnameField.value.length==0)
			{
				alert('Nav norādīts uzvārds');
				regSurnameField.focus();
				return false;				
			}
			if(regEmailField==null || regEmailField.value.length==0)
			{
				alert('Nav norādīts e-pasts');
				regEmailField.focus();
				return false;				
			}
			var em1 = regEmailField.value.split('@');
			var em2 = regEmailField.value.split('.');
			if(em1.length!=2 || em2.length<2)
			{
				alert('Nav norādīts korekts e-pasts');
				regEmailField.focus();
				return false;
			}
			if(regPhoneField==null || regPhoneField.value.length==0)
			{
				alert('Nav norādīts telefons');
				regPhoneField.focus();
				return false;				
			}
			if(registred!=null && registred!='Y')//ja nav reģistrējies
			{
					dialog('typePassword', 'show', regEmailField.value);
			}
			if(registred!=null && registred=='Y')//ja ir reģistrējies
			{
				document.getElementById('registrationForm').submit();
			}
		}

    function validateBeforeInformFriend()
		{
			var name = document.getElementById('inform_friend_name');
			var mail = document.getElementById('inform_friend_email');
			if(name==null || name.value.length==0)
			{
				alert('Nav norādīts vārds');
				name.focus();
				return false;		
			}
			if(mail==null || mail.value.length==0)
			{
				alert('Nav norādīts e-pasts');
				mail.focus();
				return false;		
			}
			var em1 = mail.value.split('@');
			var em2 = mail.value.split('.');
			if(em1.length!=2 || mail.length<2)
			{
				alert('Nav norādīts korekts e-pasts');
				mail.focus();
				return false;
			}
			document.getElementById('informMailForm').submit();
		}

		function changeInputElementStyleOnFocus(id)
		{
			document.getElementById(id).className='activeInputTextElement';
		}

		function changeInputElementStyleOnBlur(id)
		{
			document.getElementById(id).className='inactiveInputTextElement';
		}


		function loadHotOffer()
		{
			a('getHotSuffer|');
			if(atbilde!=null && atbilde!='')
			{
//				alert(atbilde);
				var data = atbilde.split('||');
				document.getElementById('hotSufferTitle').innerHTML = data[0];
				if(data[1]!=null && data[1]!='') var product_id = data[1];
				if(data[2]!=null && data[2]!='') var product_group_id = data[2];
				document.getElementById('hotSufferHref').href=data[3];
				document.getElementById('hotSufferTitle').href=data[3];
				if(data[4]!=null && data[4]!='')
				{
					var Dimage = '/catalog/'+product_id+'/thumb/'+data[4];
				}
				else
				{
					var Dimage = '{pic_url}/nopicture.jpg';
				}
//				alert(image);
				document.getElementById('hotSufferImage').src = Dimage;
//				document.getElementById('hotSufferImage').style.backgroundRepeat = "no-repeat";
			}
		}


		function validateSearch()
		{
			var d = document.getElementById('searchvalue').value;
			if(d.length==0)
			{
				alert('Ievadiet meklējamo preci');
				return false;
			}
			return true;

		}


function getGozaProcuListi()
{
  atbilde=null;
  a('getGrozaContentAll|');
  var ter = document.getElementById('precuListeAll');
  if(atbilde!=null && atbilde!='')
  {
    if(ter!=null)
    {
      document.getElementById('precuListeAll').innerHTML = atbilde;
			a('calculateProductSum|');
			if(atbilde!=null && atbilde!='' && atbilde!='0')
			{
				document.getElementById('summa').innerHTML = lang[1]+atbilde+' '+lang[2];
			}
			else
			{
				window.location = window.location;
			}
		}
	}
	else if(ter!=null)
	{
		window.location = window.location;
	}
}

function deleteFromGrozs(p_id)
{
	if(p_id!=null && p_id!='')
	{
		a('deleteFromGrozs|'+p_id);
		getGozaProcuListi();
	}
}


function showSubmenu(elmnt)
{
  elmnt.getElementsByTagName('ul')[0].style.width=screen.width-20;
  elmnt.style.backgroundPosition='0 -24px';
  elmnt.getElementsByTagName('ul')[0].style.display='block';
  elmnt.getElementsByTagName('ul')[0].style.zIndex=11;  
  elmnt.getElementsByTagName('ul')[0].style.height='100px';
}
function hideSubmenu(elmnt)
{
  if(elmnt.getElementsByTagName('ul')[0].id=='perm') return;
  elmnt.style.backgroundPosition='0 0';
  elmnt.getElementsByTagName('ul')[0].style.display='none';
  elmnt.getElementsByTagName('ul')[0].style.zIndex=1;
}






function dialog(type, act, data)
{
	if(act!=null && act=='show')
  {
  	switch(type)
  	{
  		case 'orderedNOTSuccessful':
  			actionAfterDialogClose = "dialog('', 'hide', '')";
  			var content = '';
        content += '<table cellpadding="0" cellspacing="0" style="width: 100%; height: 100%; border: 0px;">';
  			content += '<tr>';
        content += '<td align="center" style="font-size:14px;">'+paldies_par_pirkumu+'</td>';
        content += '</tr>';
        content += '<tr>';
        content += '<td valign="middle" style="text-align: center; height: 100%; color: #FF0000; font-size: 16px; font-weight: bold;">'+pasuutiijums_nav_sanjemts+'</td>';
        content += '</tr>';
        content += '<tr>';
        content += '<td align="center">';
        content += '<img src="/img/button_3.gif" style="width: 213px; height: 120px; border: 0px;" onClick="dialog(\'\', \'hide\', \'\');">';
        content += '</td>';
        content += '</tr>';
        content += '</table>';
			break;
			case 'orderedSuccessful':
  			actionAfterDialogClose = "closeAndGoHome()";
  			var content = '';
        content += '<table cellpadding="0" cellspacing="0" style="width: 100%; height: 100%; border: 0px;">';
        content += '<tr><td align="center" style="font-size:14px;">'+paldies_par_pirkumu+'</td></tr>';
        content += '<tr><td valign="middle" style="text-align: center; height: 100%; color: #52E036; font-size: 16px; font-weight: bold;">'+pasuutiijums_ir_pienjemts+'</td></tr>';
        content += '<tr><td align="center">';
        content += '<img src="/img/button_3.gif" style="width: 213px; height: 120px; border: 0px;" onClick="closeAndGoHome()">';
        content += '</td></tr>';
        content += '</table>';
      break;
  		case 'typePassword':
  			if(data==null || data=='')
  			{
  				alert('Nav norādīts e-pasts');
  				regEmailField.focus();
  				return false;
  			}
  			actionAfterDialogClose = "dialog('', 'hide', '')";
  			var content = '';
        content += '<table cellpadding="0" cellspacing="6" style="border: 0px; width: 100%;">';
        content += '<tr><td colspan="2" valign="top" style="font-size: 15px;">'+dialog_description_1+'</td></tr>';
        content += '<tr><td colspan="2" valign="top"><div id="messageField">&nbsp;</div></td></tr>';
        content += '<tr><td class="darkTextStyle" style="text-align: left; padding: 0px 0px 0px 0px;">'+login_epasts+'</td>';
        content += '<td class="darkTextStyle" style="text-align: left; padding: 0px 0px 0px 0px;">'+data+'</td></tr>';
        content += '<tr><td colspan="2" style="height:10px;">&nbsp;</td></tr>';
        content += '<tr><td class="darkTextStyle" style="text-align: left; padding: 0px 0px 0px 0px;">'+login_parole+'</td>';
        content += '<td style="width: 330px; text-align: left;">';
        content += '<input id="popup_reg_password" type="password" style="width:100%; margin: 0px;" onFocus="changeInputElementStyleOnFocus(\'popup_reg_password\')" onBlur="changeInputElementStyleOnBlur(\'popup_reg_password\')" class="inactiveInputTextElement">';
        content += '</td>';
        content += '</tr>';
        content += '<tr><td colspan="2" style="height:10px;">&nbsp;</td></tr>';
        content += '<tr>';
        content += '<td class="darkTextStyle" style="text-align: left; padding: 0px 0px 0px 0px;">'+login_parole_velreiz+'</td>';
        content += '<td style="width: 330px; padding: 0px 0px 0px 0px;">';
        content += '<input id="popup_reg_password2" type="password" style="width:100%; margin: 0px;" onFocus="changeInputElementStyleOnFocus(\'popup_reg_password2\')" onBlur="changeInputElementStyleOnBlur(\'popup_reg_password2\')" class="inactiveInputTextElement">';
        content += '</td>';
        content += '</tr>';
        content += '<tr><td colspan="2" style="height:10px;">&nbsp;</td></tr>';
        content += '<tr>';
        content += '<td colspan="2">';
        content += '<table cellpadding="0" cellspacing="0" style="border: 0px; width: 100%;">';
        content += '<tr>';
        content += '<td><div onClick="itisTimeToRegister()" style="background: url(\'/img/button_1.gif\') no-repeat; width: 213px; height: 120px; cursor: pointer;">&nbsp</div></td>';
        content += '<td style="font-size:0px width: 5px;">&nbsp;</td>';
        content += '<td><div onClick="document.getElementById(\'registrationForm\').submit(); dialog(\'\', \'hide\', \'\');" style="background: url(\'/img/button_2.gif\') no-repeat; width: 213px; height: 120px; cursor: pointer;">&nbsp</div></td>';
        content += '</tr>';
        content += '</table>';
        content += '</td>';
        content += '</tr>';
        content += '</table>';
 			break;
  		case 'informFriend':
  			actionAfterDialogClose = "dialog('', 'hide', '')";
  		  var content = '';
        content += '<form method="post" id="informMailForm">';
        content += '<table cellpadding="0" cellspacing="6" style="border: 0px; width: 100%;">';
        content += '<tr><td colspan="2" valign="top" style="font-size: 15px;">'+drauga_epasts+'</td></tr>';
        content += '<tr><td colspan="2" valign="top"><div id="messageField">&nbsp;</div></td></tr>';
        content += '<tr><td colspan="2" style="height:10px;">&nbsp;</td></tr>';
        content += '<tr><td class="darkTextStyle" style="text-align: left; padding: 0px 0px 0px 0px;">'+tavs_vards+'</td>';
        content += '<td style="width: 330px; text-align: left;">';
        content += '<input id="inform_friend_name" name="informFriend[name]" type="text" style="width:100%; margin: 0px;" onFocus="changeInputElementStyleOnFocus(\'inform_friend_name\')" onBlur="changeInputElementStyleOnBlur(\'inform_friend_name\')" class="inactiveInputTextElement">';
        content += '</td></tr>';
        content += '<tr><td colspan="2" style="height:10px;">&nbsp;</td></tr>';
        content += '<tr><td class="darkTextStyle" style="text-align: left; padding: 0px 0px 0px 0px;">'+tava_drauga_e_pasts+'</td>';
        content += '<td style="width: 330px; padding: 0px 0px 0px 0px;">';
        content += '<input id="inform_friend_email" name="informFriend[mail]" type="text" style="width:100%; margin: 0px;" onFocus="changeInputElementStyleOnFocus(\'inform_friend_email\')" onBlur="changeInputElementStyleOnBlur(\'inform_friend_email\')" class="inactiveInputTextElement">';
        content += '</td></tr>';
        content += '<tr><td colspan="2" style="height:10px;">&nbsp;</td></tr>';
        content += '<tr><td colspan="2" align="center">';
        content += '<div onClick="validateBeforeInformFriend()" style="background: url(\'/img/send_large.gif\') no-repeat; width: 213px; height: 120px; cursor: pointer;">&nbsp</div>';
        content += '</td></tr>';
        content += '</table>';
        content += '</form>';
  		break;
  	}
		if(content!=null)
		{
			scroll(0,0);
			document.getElementById('dialog_bg').style.width = document.body.clientWidth;
			document.getElementById('dialog_bg').style.height = document.body.scrollHeight;
			document.getElementById('dialog_bg').style.display = 'block';
			document.getElementById('dialog_content_all').style.display = 'block';
			document.getElementById('contentField').innerHTML = content;
			var r = parseFloat(document.getElementById('dialog_content_all').clientWidth);
			var rr = parseFloat(document.getElementById('mid_cont').style.width);
			var r2 = (r-rr)/2;
			document.getElementById('mid_cont').style.marginLeft = r2+'px';
		}
	}
	else
	{
		document.getElementById('dialog_bg').style.display = 'none';
  	document.getElementById('dialog_content_all').style.display = 'none';
  }
}


function itisTimeToRegister()
{
  var p1 = document.getElementById('popup_reg_password');
  var p2 = document.getElementById('popup_reg_password2');
  var mess = document.getElementById('messageField');
	if(p1==null || p1.value.length=='')
  {
    mess.innerHTML = '{nav_ievadiita_parole}';
    p1.focus();
    return false;
  }
  if(p2==null || p2.value.length=='')
  {
    mess.innerHTML = nav_noraadiita_parole;
    p2.focus();
    return false;
  }
  if(p1.value != p2.value)
  {
    mess.innerHTML = paroles_nesakriit;
    p1.focus();
    return false;
  }
  var regEmailField = document.getElementById('reg_email');
  a('checkEmailAndPassword|'+regEmailField.value+'|'+p1.value);
  if(atbilde!=null && atbilde=='1')
  {
    mess.innerHTML = shis_epasts_jau_ir_registreets;
    return false;
  }
	document.getElementById('reg_password').value = p1.value;
	dialog('', 'hide', '');
	document.getElementById('registrationForm').submit();
}

  
function validateLoginForm()
{
  var loginField = document.getElementById('login_email');
  var passwField = document.getElementById('login_passw');
  if(loginField==null || loginField.value.length==0)
  {
    alert('Nav norādīts e-pasts');
    loginField.focus();
    return false;
  }
  if(passwField==null || passwField.value.length==0)
  {
    alert('Nav norādīta parole');
    passwField.focus();
    return false;
  }
  a('checkRegistredUser|'+loginField.value+'|'+passwField.value);
  if(atbilde!=null && atbilde=='1')
  {
    document.getElementById('loginForm').submit();
  }
  else
  {
    alert('Nepareizs lietotājvārds vai parole');
    return false;
  }
}

function order(p_id)
{
  var defs = document.getElementById('defined_groups').getElementsByTagName('select');
  var def_groups = '';
  if (defs!=null && defs.length>0)
  {
    for(var i=0; defs.length>i; i++)
    {
      if(defs[i].id.substr(0,14)=='defined_group_')
      {
        def_groups = def_groups+defs[i].id.substr(14, (defs[i].id.length-14))+','+defs[i].value+'#';
      }
    }
  }
  var ret = 'orderProduct|'+p_id+'#'+def_groups;
  atbilde=null;
  a(ret);
  if(atbilde!=null){window.location = '/lv/grozs';}
  else{alert(def_lang[2]);}
  getGroza_satursCount();
}


function validateKontaktform()
{
  var name = document.getElementById('kontakti_vards');
  var surname = document.getElementById('kontakti_uzvards');
  var mail = document.getElementById('kontakti_epasts');
  var phone = document.getElementById('kontakti_telefons');
  var data = document.getElementById('kontakti_zina');
  if(name==null || name.value.length==0)
  {
    alert('Nav norādīts vārds');
    name.focus();
    return false;
  }
	if(surname==null || surname.value.length==0)
  {
    alert('Nav norādīts uzvārds');
    surname.focus();
    return false;
  }
	if(mail==null || mail.value.length==0)
  {
    alert('Nav norādīts e-pasts');
    mail.focus();
    return false;
  }
	var m1 = mail.value.split('@');
  var m2 = mail.value.split('.');
  if(m1.length!=2 || m2.length<2)
  {
    alert('Nav norādīts korekts e-pasts');
    mail.focus();
    return false;
  }
	if(phone==null || phone.value.length==0)
  {
    alert('Nav norādīts telefons');
    phone.focus();
    return false;
  }
  if(data==null || data.value.length==0)
  {
    alert('Nav ziņojuma');
    data.focus();
    return false;
  }
  document.getElementById('kontaktforma').submit();
}




function showSubmenu2(elmnt){
	document.getElementById('submenu_'+elmnt).style.display='block';
	document.getElementById(elmnt).style.backgroundPosition='0 -24px';
	document.getElementById('submenu_'+elmnt).style.width=document.getElementsByTagName('body')[0].clientWidth;
}
function hideSubmenu2(elmnt){
  if(document.getElementById('submenu_'+elmnt).style.zIndex=='9') return;
  document.getElementById('submenu_'+elmnt).style.display='none';
  document.getElementById(elmnt).style.backgroundPosition='0 0';  
}





