/* system wide functions*/
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 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, 'Email can not be empty');
		if(ValidateEmail(frmObj.checkout_email.value))
		{
			result &= true;
		}		
		else
		{
			throw 'Email is not in valid format';
		}						
		result &= CheckFieldEmpty(frmObj.checkout_firstname, 'First name can not be empty');
		result &= CheckFieldEmpty(frmObj.checkout_lastname, 'Last name can not be empty');		
		result &= CheckFieldEmpty(frmObj.checkout_mobile, 'Mobile phone can not be empty');		
		result &= CheckFieldEmpty(frmObj.checkout_shipping_address, 'Address can not be empty');
		result &= CheckFieldEmpty(frmObj.checkout_shipping_city, 'City can not be empty');
		result &= CheckFieldEmpty(frmObj.checkout_shipping_zip, 'Zip/postal code can not be empty');		
		result &= CheckFieldEmpty(frmObj.checkout_shipping_country, 'Country can not be empty');
	}
	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, 'Email can not be empty');
		if(ValidateEmail(frmObj.register_email.value))
		{
			result &= true;
		}		
		else
		{
			throw 'Email is not in valid format';
		}
		result &= CheckFieldEmpty(frmObj.register_pwd, 'Password can not be empty');				
		result &= CheckFieldEmpty(frmObj.register_passwordconfirm, 'Password confirm can not be empty');
				
		if(frmObj.register_pwd.value!=frmObj.register_passwordconfirm.value)
		{
			throw 'Passwords do not match';			
		}
		
		result &= CheckFieldEmpty(frmObj.register_firstname, 'First name can not be empty');
		result &= CheckFieldEmpty(frmObj.register_lastname, 'Last name can not be empty');		
		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 ValidateAccountUpdateForm(formId)
{	
	var result = true;
	try
	{
		var frmObj = document.getElementById(formId);
		result &= CheckFieldEmpty(frmObj.register_pwd, 'Password can not be empty');				
		result &= CheckFieldEmpty(frmObj.register_passwordconfirm, 'Password confirm can not be empty');
				
		if(frmObj.register_pwd.value!=frmObj.register_passwordconfirm.value)
		{
			throw 'Passwords do not match';			
		}		
		result &= CheckFieldEmpty(frmObj.register_firstname, 'First name can not be empty');
		result &= CheckFieldEmpty(frmObj.register_lastname, 'Last name can not be empty');		
		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();
	}	
}
