﻿function	register()
{

	var username		= document.m_form.Username.value;
	var	password		= document.m_form.Password.value;
	var password2		= document.m_form.Password2.value;
	var firstname		= document.m_form.First_Name.value;
	var lastname		= document.m_form.Last_Name.value;
	var email			= document.m_form.Email.value;
	var firmname		= document.m_form.Firm_Name.value;
	var address			= document.m_form.Address.value;
	var phone			= document.m_form.Phone.value;
	var consigment		= document.m_form.Consigment.value;
	
	var validUsername	= new RegExp("^[a-z0-9_-]{4,15}$");// регулярен израз за потребителско име.
	var validPassword	= new RegExp("^[a-z0-9]{6,17}$");// регулярен израз за парола.
	var emailPattern 	= /^[a-zA-z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$/i;
	var validPhone		= new RegExp("^[0-9\-]{1,17}$");// регулярен израз за телефон.

	
	if( validUsername.test(username) == false ) 
	{
		document.getElementById( "Username" ).focus();
	  	return false;
	}
	
	if( validPassword.test(password) == false ) 
	{
      	document.getElementById( "Password" ).focus();
		return false;
	}
	if( password != password2 )
	{
	  	document.getElementById( "Password2" ).focus();
		return false;
	}
	if( firstname.replace(/\s/g,"").length == 0 )
	{
		document.getElementById( "First_Name" ).focus();
		return false;
	}
	if( lastname.replace(/\s/g,"").length == 0 )
	{
		document.getElementById( "Last_Name" ).focus();
		return false;
	}
	if( email.search(emailPattern) == -1 )
	{
		document.getElementById( "Email" ).focus();
		return false;
	}
	if( firmname.replace(/\s/g,"").length == 0 )
	{
		document.getElementById( "Firm_Name" ).focus();
		return false;
	}
	if( address.replace(/\s/g,"").length == 0 )
	{
		document.getElementById( "Address" ).focus();
		return false;
	}
	if( validPhone.test(phone) == false || phone.charAt(0) != "0" )
	{
		document.getElementById( "Phone" ).focus();
		return false;
	}
	if( consigment.replace(/\s/g,"").length == 0 )
	{
		document.getElementById( "Consigment" ).focus();
		return false;
	}
	var	chbAgree		= document.getElementById( "chbAgree" );
	if( chbAgree.checked == false )
	{
		document.getElementById( "span_agree" ).style.display = "inline";
		return false;
	}
		
	return true;
}
function	myProfile()
{

	var username		= document.m_form.Username.value;
	var	password		= document.m_form.Password.value;
	var password2		= document.m_form.Password2.value;
	var firstname		= document.m_form.First_Name.value;
	var lastname		= document.m_form.Last_Name.value;
	var email			= document.m_form.Email.value;
	var firmname		= document.m_form.Firm_Name.value;
	var address			= document.m_form.Address.value;
	var phone			= document.m_form.Phone.value;
	var consigment		= document.m_form.Consigment.value;
	
	var validUsername	= new RegExp("^[a-z0-9_-]{4,15}$");// регулярен израз за потребителско име.
	var validPassword	= new RegExp("^[a-z0-9]{6,17}$");// регулярен израз за парола.
	var emailPattern 	= /^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$/i;
	var validPhone		= new RegExp("^[0-9\-]{1,17}$");// регулярен израз за телефон.

	
	if( validUsername.test(username) == false ) 
	{
		document.getElementById( "Username" ).focus();
	  	return false;
	}
	
	if( validPassword.test(password) == false ) 
	{
      	document.getElementById( "Password" ).focus();
		return false;
	}
	if( password != password2 )
	{
	  	document.getElementById( "Password2" ).focus();
		return false;
	}
	if( firstname.replace(/\s/g,"").length == 0 )
	{
		document.getElementById( "First_Name" ).focus();
		return false;
	}
	if( lastname.replace(/\s/g,"").length == 0 )
	{
		document.getElementById( "Last_Name" ).focus();
		return false;
	}
	if( email.search(emailPattern) == -1 )
	{
		document.getElementById( "Email" ).focus();
		return false;
	}
	if( firmname.replace(/\s/g,"").length == 0 )
	{
		document.getElementById( "Firm_Name" ).focus();
		return false;
	}
	if( address.replace(/\s/g,"").length == 0 )
	{
		document.getElementById( "Address" ).focus();
		return false;
	}
	if( validPhone.test(phone) == false || phone[0] != "0")
	{
		document.getElementById( "Phone" ).focus();
		return false;
	}
	if( consigment.replace(/\s/g,"").length == 0 )
	{
		document.getElementById( "Consigment" ).focus();
		return false;
	}
	var	chbAgree		= document.getElementById( "chbAgree" );
	if( chbAgree.checked == false )
	{
		document.getElementById( "span_agree" ).style.display = "inline";
		return false;
	}
		
	return true;
}
function myLogin(){
	var username		= document.m_form.Username.value;
	var	password		= document.m_form.Password.value;
	var validUsername	= new RegExp("^[a-z0-9_-]{4,15}$");// регулярен израз за потребителско име.
	var validPassword	= new RegExp("^[a-z0-9]{6,17}$");// регулярен израз за парола.
	if( validUsername.test(username) == false ) 
	{
		document.getElementById( "Username" ).focus();
	  	return false;
	}
	
	if( validPassword.test(password) == false ) 
	{
      	document.getElementById( "Password" ).focus();
		return false;
	}
	return true;
	
}

function	checkUsername( field )
{
	var username		= document.m_form.Username.value;
	
	var validUsername	= new RegExp("^[a-z0-9_-]{4,15}$");// регулярен израз за потребителско име.
	var fieldset 		= field.parentNode;
	
	if( validUsername.test(username) == false ) 
	{
		document.getElementById( "span_username" ).style.display = "inline";
		fieldset.className = "";
	}
	else
	{
		document.getElementById( "span_username" ).style.display = "none";
		fieldset.className = "welldone";
	}
}

function	checkPassword( field )
{
	var	password		= document.m_form.Password.value;
	
	var validPassword	= new RegExp("^[a-z0-9]{6,17}$");// регулярен израз за парола.
	var fieldset 		= field.parentNode;
	
	if( validPassword.test(password) == false )
	{
		document.getElementById( "span_password" ).style.display = "inline";
		fieldset.className = "";
	}
	else
	{
		document.getElementById( "span_password" ).style.display = "none";
		fieldset.className = "welldone";
	}
}

function	checkPassword2( field )
{
	var	password		= document.m_form.Password.value;
	var	password2		= document.m_form.Password2.value;
	
	var fieldset 		= field.parentNode;
	
	if( password != password2 )
	{
		
		document.getElementById( "span_password2" ).style.display = "inline";
		fieldset.className = "";
	}
	else
	{
		fieldset.className = "welldone";
		document.getElementById( "span_password2" ).style.display = "none";
	}
		
}

function	checkFirstName( field )
{
	var	fname			= document.m_form.First_Name.value;
	var fieldset 		= field.parentNode;
	
	if( fname.replace(/\s/g,"").length == 0 )
	{
		document.getElementById( "span_fName" ).style.display = "inline";
		fieldset.className = "";
	}
	else
	{
		fieldset.className = "welldone";
		document.getElementById( "span_fName" ).style.display = "none";
	}
}

function	checkLastName( field )
{
	var	lname			= document.m_form.Last_Name.value;
	var fieldset 		= field.parentNode;
	
	if( lname.replace(/\s/g,"").length == 0 )
	{
		document.getElementById( "span_lName" ).style.display = "inline";
		fieldset.className = "";
	}
	else
	{
		fieldset.className = "welldone";
		document.getElementById( "span_lName" ).style.display = "none";
	}
}

function	checkEmail( field )
{
	var	email			= document.m_form.Email.value;
	var fieldset 		= field.parentNode;
	var emailPattern 	= /^[a-zA-z0-9_\.\-]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$/i;
	
	if( email.search(emailPattern) == -1 )
	{
		document.getElementById( "span_email" ).style.display = "inline";
		fieldset.className = "";
	}
	else
	{
		fieldset.className = "welldone";
		document.getElementById( "span_email" ).style.display = "none";
	}
}
function	checkFirmName( field )
{
	var	firmname			= document.m_form.Firm_Name.value;
	var fieldset 		= field.parentNode;
	
	if( firmname.replace(/\s/g,"").length == 0 )
	{
		document.getElementById( "span_firmName" ).style.display = "inline";
		fieldset.className = "";
	}
	else
	{
		fieldset.className = "welldone";
		document.getElementById( "span_firmName" ).style.display = "none";
	}
}

function	checkAddress( field )
{
	var	address			= document.m_form.Address.value;
	var fieldset 		= field.parentNode;
	
	if( address.replace(/\s/g,"").length == 0 )
	{
		document.getElementById( "span_address" ).style.display = "inline";
		fieldset.className = "";
	}
	else
	{
		fieldset.className = "welldone";
		document.getElementById( "span_address" ).style.display = "none";
	}
}

function	checkPhone( field )
{
	var	phone			= document.m_form.Phone.value;
	
	var validPhone		= new RegExp("^[0-9\-]{6,17}$");// регулярен израз за телефон.
	var fieldset 		= field.parentNode;
	
	if( validPhone.test(phone) == false || phone.charAt(0) != "0" )
	{
		document.getElementById( "span_phone" ).style.display = "inline";
		fieldset.className = "";
	}
	else
	{
		fieldset.className = "welldone";
		document.getElementById( "span_phone" ).style.display = "none";
	}
}
function	checkConsigment( field )
{
	var	consigment			= document.m_form.Consigment.value;
	var fieldset 		= field.parentNode;
	
	if( consigment.replace(/\s/g,"").length == 0 )
	{
		document.getElementById( "span_Consigment" ).style.display = "inline";
		fieldset.className = "";
	}
	else
	{
		fieldset.className = "welldone";
		document.getElementById( "span_Consigment" ).style.display = "none";
	}
}

///////////////////////////////////////////////////////////////////////////////

function validateProduct()
{
	var productName				= document.m_form.pName.value;
	var	productDescription		= document.m_form.pDesc.value;
	var productCategory			= document.m_form.pCategory.value;
	
	if( productName.replace(/\s/g,"").length == 0 )
	{
		animateValidation( "pName", "	* Моля въведете име на артикула!", true, false, false );
		return false;
	}
	else
		unAnimateValidation( "pName", productName, true, false, false, -1 );
		
	if( productDescription.replace(/\s/g,"").length == 0 )
	{
		animateValidation( "pDesc", "	* Моля въведете описание на артикула!", false, true, false );
		return false;
	}
	else
		unAnimateValidation( "pDesc", productDescription, false, true, false, -1 );
		
	if( productCategory == -1 )
	{
		animateValidation( "pCategory", "	* Моля изберете категория на артикула!", false, false, true );
		return false;
	}
	else
		unAnimateValidation( "pCategory", productCategory, false, false, true, productCategory );
	
	return true;
}
///////////////////////////////////////////////////////////////////////////////

function	animateValidation( field, innerText, isTextBox, isTextArea, isDropDown )
{
	var field				= document.getElementById( field );
		
	field.style.border		= "1px dashed red";
	
	var	parent				= field.parentNode;
	var parentInnerHTML		= parent.innerHTML;
	var endOfInput			= -1;
	if( isTextBox )
		endOfInput			= parentInnerHTML.indexOf( ">" );
	else if( isTextArea )
		endOfInput			= parentInnerHTML.indexOf( "</textarea>" );
	else if( isDropDown )
		endOfInput			= parentInnerHTML.indexOf( "</select>" );
		
	parent.style.color		= "#ff0000";
	
	if( isTextBox )
		parentInnerHTML			= parentInnerHTML.substring( 0,  endOfInput + 1 );
	else if( isTextArea || isDropDown )
		parentInnerHTML			= parentInnerHTML.substring( 0,  endOfInput );
	
	if( isTextBox )
		parent.innerHTML		= parentInnerHTML + innerText;
	else if( isTextArea )
		parent.innerHTML		= parentInnerHTML + "</textarea>" + innerText;
	else if( isDropDown )
		parent.innerHTML		= parentInnerHTML + "</select>" + innerText;
}
///////////////////////////////////////////////////////////////////////////////

function	unAnimateValidation( fieldName, value, isTextBox, isTextArea, isDropDown, selectedIndex )
{
	var field					= document.getElementById( fieldName );
		
	field.style.border			= "1px solid #c0c0c0";
	
	var	parent					= field.parentNode;
	var parentInnerHTML			= parent.innerHTML;
	var endOfInput				= -1;
	
	if( isTextBox )
		endOfInput				= parentInnerHTML.indexOf( ">" );
	else if( isTextArea )
		endOfInput				= parentInnerHTML.indexOf( "</textarea>" );
	else if( isDropDown )
		endOfInput				= parentInnerHTML.indexOf( "</select>" );
	
	if( isTextBox )
		parentInnerHTML			= parentInnerHTML.substring( 0,  endOfInput + 1 );
	else if( isTextArea || isDropDown )
		parentInnerHTML			= parentInnerHTML.substring( 0,  endOfInput );
		
	if( isTextBox )
		parent.innerHTML		= parentInnerHTML;
	else if( isTextArea )
		parent.innerHTML		= parentInnerHTML + "</textarea>";
	else if( isDropDown )
		parent.innerHTML		= parentInnerHTML + "</select>";
	
	var	form						= document.forms["m_form"];
	alert( form );
	if( ! isDropDown )
		form.elements[fieldName].value	= value;
	else
	{
		var ddl			= form.elements[fieldName];
		var options  	= ddl.options; 
		for( var i = 0; i < options.length; i++ )
		{
			var found				= false;
			
			if( options[i].value == selectedIndex )
				options[i].selected		= true
		}
	}
}
///////////////////////////////////////////////////////////////////////////////

function	getFileExtention( filename )
{
	var ext = /^.+\.([^.]+)$/.exec( filename );
  return ext == null ? "" : ext[1];
}
///////////////////////////////////////////////////////////////////////////////

function	canUpload()
{
	var file					= document.m_form.userfile.value;
	var	partner					= document.m_form.bpartner.value;
	var supportedFiles			= new Array( "pdf", "doc", "docx", "xls", "xlsx" );
	
	if( file.replace(/\s/g,"").length == 0 )
	{
		animateValidation( "userfile", "	* Моля изберете ценова листа!", true, false, false );
		return false;
	}
	
	var	fileExtention	= getFileExtention( file );
	var	isSupportedFile	= false;
	
	//	check if uploaded file is supported
	for( i = 0; i < supportedFiles.length; i++ )
	{
		
		if( fileExtention == supportedFiles[i] )
			isSupportedFile = true;
	}
	
	if( ! isSupportedFile )
	{
		animateValidation( "userfile", "	* Грешен файлов формат!", true, false, false );
		return false;
	}
	//	Check if the file name contains cyrillic letters.
	
	var fileNameRegEx	= new RegExp("/^[a-zA-Z0-9\.]+$/");// регулярен израз.
	var splitFilename	= file.split( "\\" );
	var	fileNameIndex	= splitFilename.length - 1;
	var	filename		= splitFilename[ fileNameIndex ];
	
	/*if( fileNameRegEx.test( filename) == false )
	{
		animateValidation( "userfile", "	* Невалидно име на файл. Името може да съдържа само цифри и латински букви без интервали и символите : _&nbsp;&nbsp;&nbsp;-&nbsp;&nbsp;&nbsp;!", true, false, false );
		return false;
	}*/
		
	if( partner.replace(/\s/g,"").length == 0 )
	{
		animateValidation( "bpartner", "	* Моля въведете име на фирмата партньор!", true, false, false );
		return false;
	}
	else
		unAnimateValidation( "bpartner", partner, true, false, false, -1 );
		
	return true;
}
///////////////////////////////////////////////////////////////////////////////

function validatePromo()
{
	var	promoDescription		= document.m_form.pDesc.value;
	var promoCompany			= document.m_form.pCompany.value;
	var hasPeriod				= document.m_form._type.item(1).checked;
	var from					= document.m_form.start_date.value;
	var to						= document.m_form.end_date.value;
	
	if( promoDescription.replace(/\s/g,"").length == 0 )
	{
		animateValidation( "pDesc", "	* Моля въведете описание на промоцията!", false, true, false );
		return false;
	}
	else
		unAnimateValidation( "pDesc", promoDescription, false, true, false, -1 );
		
	if( promoCompany == -1 )
	{
		animateValidation( "pCompany", "	* Моля изберете фирма за промоцията!", false, false, true );
		return false;
	}
	else
		unAnimateValidation( "pCompany", promoCompany, false, false, true, promoCompany );
	
	if( hasPeriod )
	{
		if( from.replace(/\s/g,"").length == 0 )
		{
			animateValidation( "start_date", "", true, false, false );
			return false;
		}
		else
			unAnimateValidation( "start_date", from, true, false, false, -1 );
			
		if( to.replace(/\s/g,"").length == 0 )
		{
			animateValidation( "end_date", "", true, false, false );
			return false;
		}
		else
			unAnimateValidation( "end_date", to, true, false, false, -1 );
	}
	
	return true;
}
///////////////////////////////////////////////////////////////////////////////

function	togglePromotions( sender )
{
	var	promotions		= document.getElementsByName( "promo" );
	var promotionsCount	= promotions.length;
	for( var i = 0; i < promotionsCount; i++ )
	{
		promotions[i].style.display	= "none";
	}
	var	promoTextDiv	= sender.parentNode.parentNode.children[2];
	var	count			= promoTextDiv.children.length;
	var child			= null;
	
	for( var j = 0; j < count; j++ )
	{
		child		= promoTextDiv.children[j];
		child.style.display = "block";
	}
}




