/* system wide functions*/

function _TJS(dictionary_key)
{	
	var out = '';
	out = g_translations[g_jslang][dictionary_key];
	if(out==undefined)
	{
		out = '_NT_:'+dictionary_key;
	}	
	return out;
}

//danm smmapers may you brun in hlel
function dsmybih(pom, sad)
{	
	if(pom.length!=sad.length)
	{
		document.write('mm');//mismatch
	}
	else
	{
		var jut = '';
		for(var i=0; i<sad.length; i++)
		{
			var cca=sad.charCodeAt(i);
			var pz = pom.charCodeAt(i)-52;
			jut +=String.fromCharCode(cca+pz);			
		}		
	}
	document.write(jut);
}

function ForwardToPage(forwardLink)
{
	window.location = forwardLink;
}

function ForwardWithConfirm(warningMessage, forwardLink)
{	
	var confirm_result= confirm(warningMessage);
	if(confirm_result)
	{
		window.location = forwardLink;		
	}
	return false;
}


function CheckFieldEmpty(formField, message)
{
	if(formField.value=='')
	{ 		
		formField.focus();
		throw message;		
	}	
	return true;
}

function TestFieldValue(formField, value, message)
{	
	if(formField.value==value)
	{ 		
		formField.focus();
		throw message;		
	}	
	return true;
}

function ValidateEmail(email)
{	
	var email_pattern = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var re = new RegExp(email_pattern);	
  	if (email.match(re)) 
	{
    	return true;
    } 
	else 
	{
    	return false;
    }	
}

/*checkout related*/
function ValidateCheckoutForm(formId)
{	
	var result = true;
	try
	{
		var frmObj = document.getElementById(formId);
		result &= CheckFieldEmpty(frmObj.checkout_email, _TJS('user.Forms.EmailEmpty'));
		if(ValidateEmail(frmObj.checkout_email.value))
		{
			result &= true;
		}		
		else
		{
				throw _TJS('user.Forms.EmailNotValid').toString();
		}						
		result &= CheckFieldEmpty(frmObj.checkout_firstname, _TJS('user.Forms.FNameEmpty'));
		result &= CheckFieldEmpty(frmObj.checkout_lastname, _TJS('user.Forms.LNameEmpty'));		
		result &= CheckFieldEmpty(frmObj.checkout_mobile, _TJS('user.Forms.MobileEmpty'));		
		result &= CheckFieldEmpty(frmObj.checkout_shipping_address, _TJS('user.Forms.AddressEmpty'));
		result &= CheckFieldEmpty(frmObj.checkout_shipping_city, _TJS('user.Forms.CityEmpty'));
		result &= CheckFieldEmpty(frmObj.checkout_shipping_zip, _TJS('user.Forms.ZipEmpty'));		
		result &= CheckFieldEmpty(frmObj.checkout_shipping_country, _TJS('user.Forms.CountryEmpty'));
	}
	catch(exc)
	{
		alert(exc);		
		return false;
	}	
	if(result)
	{
		frmObj.submit();
	}
}

function ValidateContactForm(formId)
{
	var result = true;
	try
	{
		var frmObj = document.getElementById(formId);
		//contact form does not always have sector
		if(frmObj.cf_sector_emailid)
		{
			result &= TestFieldValue(frmObj.cf_sector_emailid, 0, _TJS('user.Forms.ContactSectorEmpty'));
		}
		result &= CheckFieldEmpty(frmObj.cf_email, _TJS('user.Forms.EmailEmpty'));
		if(ValidateEmail(frmObj.cf_email.value))
		{
			result &= true;
		}		
		else
		{
			//throw 'Email is not in valid format';
			throw _TJS('user.Forms.EmailNotValid').toString();
		}
						
		result &= CheckFieldEmpty(frmObj.cf_landline, _TJS('user.Forms.LandlineEmpty'));			
	}
	catch(exc)
	{
		alert(exc);		
		return false;
	}	
	if(result)
	{
		frmObj.submit();
	}
}




/*user related*/
function ValidateRegisterForm(formId)
{		
	var result = true;
	try
	{
		var frmObj = document.getElementById(formId);
		result &= CheckFieldEmpty(frmObj.register_email, _TJS('user.Forms.EmailEmpty'));
		if(ValidateEmail(frmObj.register_email.value))
		{
			result &= true;
		}		
		else
		{
			//throw 'Email is not in valid format';
			throw _TJS('user.Forms.EmailNotValid').toString();
		}
		result &= CheckFieldEmpty(frmObj.register_pwd, _TJS('user.Forms.PasswordEmpty'));				
		result &= CheckFieldEmpty(frmObj.register_passwordconfirm, _TJS('user.Forms.PwdConfirmEmpty'));
				
		if(frmObj.register_pwd.value!=frmObj.register_passwordconfirm.value)
		{
			//throw 'Passwords do not match';
			throw _TJS('user.Forms.PasswordMismatch').toString();
		}
		
		result &= CheckFieldEmpty(frmObj.register_firstname, _TJS('user.Forms.FNameEmpty'));
		result &= CheckFieldEmpty(frmObj.register_lastname, _TJS('user.Forms.LNameEmpty'));		
		result &= CheckFieldEmpty(frmObj.register_landline, _TJS('user.Forms.LandlineEmpty'));		
		result &= CheckFieldEmpty(frmObj.register_pib, _TJS('user.Forms.PIBEmpty'));
		result &= CheckFieldEmpty(frmObj.register_address, _TJS('user.Forms.AddressEmpty'));
				
	}
	catch(exc)
	{
		alert(exc);		
		return false;
	}	
	if(result)
	{
		frmObj.submit();
	}
}

function ValidateAccountUpdateForm(formId)
{	
	var result = true;
	try
	{
		var frmObj = document.getElementById(formId);
		result &= CheckFieldEmpty(frmObj.register_pwd, _TJS('user.Forms.PasswordEmpty'));				
		result &= CheckFieldEmpty(frmObj.register_passwordconfirm, _TJS('user.Forms.PwdConfirmEmpty'));
				
		if(frmObj.register_pwd.value!=frmObj.register_passwordconfirm.value)
		{
			throw 'Passwords do not match';			
		}		
		result &= CheckFieldEmpty(frmObj.register_firstname, _TJS('user.Forms.FNameEmpty'));
		result &= CheckFieldEmpty(frmObj.register_lastname, _TJS('user.Forms.LNameEmpty'));		
		result &= CheckFieldEmpty(frmObj.register_mobile, 'Mobile phone can not be empty');		
		result &= CheckFieldEmpty(frmObj.register_address, 'Address can not be empty');
		result &= CheckFieldEmpty(frmObj.register_city, 'City can not be empty');
		result &= CheckFieldEmpty(frmObj.register_zip, 'Zip/postal code can not be empty');
		result &= CheckFieldEmpty(frmObj.register_country, 'Country can not be empty');
		
	}
	catch(exc)
	{
		alert(exc);		
		return false;
	}	
	if(result)
	{
		frmObj.submit();
	}	
}


function getCookie(c_name)
{
	if (document.cookie.length>0)
  	{
  		c_start=document.cookie.indexOf(c_name + "=");
  		if (c_start!=-1)
  		{
  			c_start=c_start + c_name.length+1;
  			c_end=document.cookie.indexOf(";",c_start);
  		    if (c_end==-1) c_end=document.cookie.length;
  			return unescape(document.cookie.substring(c_start,c_end));
  		}
  	}
	return "";
}

function setCookie(c_name,value,expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
}

//if Mr Prebranac manages to slow down earth for to 1/10th of its speed
function deleteCookie(c_name)
{
	setCookie(c_name, null, -10);
}

function checkCookie(c_name)
{
	var cookie_value = getCookie(c_name);
	if(cookie_value!=null && cookie_value!='')
	{		
		return true;
	}
	return false;
}

