// JavaScript Document
hs.graphicsDir = SITE_ROOT + 'highslide/graphics/';
hs.align = 'center';
hs.transitions = ['expand', 'crossfade'];
hs.outlineType = 'rounded-white';
hs.fadeInOut = true;
//hs.numberPosition = 'caption';
hs.dimmingOpacity = 0.5;

// Add the controlbar
hs.addSlideshow
(
	{
		//slideshowGroup: 'group1',
		interval: 5000,
		repeat: false,
		useControls: true,
		fixedControls: 'fit',
		overlayOptions: 
		{
			opacity: .75,
			position: 'bottom center',
			hideOnMouseOut: true
		}
	}
);

function trim(str)
{ 
	return str.replace(/^\s+|\s+$/g, '');
}

function SelectTab(n, tabs)
{
	// Tabs
	for ( i=1; i<=tabs; i++ )
	{
		if (document.getElementById('tab_' + i) && document.getElementById('content_tab_' + i))
		{
			if ( n == i )
			{
				document.getElementById('tab_' + i).className = 'tab selected';
				document.getElementById('content_tab_' + i).style.display = 'block';
			}
			else
			{
				document.getElementById('tab_' + i).className = 'tab';
				document.getElementById('content_tab_' + i).style.display = 'none';
			}
		}
	}
}

function pagesMenuOver(obj)
{
	obj.oldClass = obj.className;
	obj.className = 'selected';
}

function pagesMenuOut(obj)
{
	if (obj.oldClass && obj.oldClass != '')
	{
		obj.className = obj.oldClass;
	}
	else
	{
		obj.className = '';
	}
}

function contactSubmit()
{
	var is_submit = true;
	
	// Name
	if ( trim(document.frmContact.txtName.value).length == 0 )
	{
		document.frmContact.txtName.className = 'input-error';
		document.frmContact.txtName.focus();
		document.frmContact.txtName.select();
		is_submit = false;
	}
	else
	{
		document.frmContact.txtName.className = 'input';
	}
	
	// Country
	/*
	if ( trim(document.frmContact.txtCountry.value).length == 0 )
	{
		document.frmContact.txtCountry.className = 'input-error';
		document.frmContact.txtCountry.focus();
		document.frmContact.txtCountry.select();
		is_submit = false;
	}
	else
	{
		document.frmContact.txtCountry.className = 'input';
	}
	*/
	
	// City
	if ( trim(document.frmContact.txtCity.value).length == 0 )
	{
		document.frmContact.txtCity.className = 'input-error';
		document.frmContact.txtCity.focus();
		document.frmContact.txtCity.select();
		is_submit = false;
	}
	else
	{
		document.frmContact.txtCity.className = 'input';
	}
	
	// Phone
	if ( trim(document.frmContact.txtPhone.value).length == 0 )
	{
		document.frmContact.txtPhone.className = 'input-error';
		document.frmContact.txtPhone.focus();
		document.frmContact.txtPhone.select();
		is_submit = false;
	}
	else
	{
		document.frmContact.txtPhone.className = 'input';
	}

	// Subject
	if ( trim(document.frmContact.txtSubject.value).length == 0 )
	{
		document.frmContact.txtSubject.className = 'input-error';
		document.frmContact.txtSubject.focus();
		document.frmContact.txtSubject.select();
		is_submit = false;
	}
	else
	{
		document.frmContact.txtSubject.className = 'input';
	}
	
	// Email
	if ( !emailCheck(document.frmContact.txtEmail.value) )
	{
		document.frmContact.txtEmail.className = 'input-error';
		document.frmContact.txtEmail.focus();
		document.frmContact.txtEmail.select();
		is_submit = false;
	}
	else
	{
		document.frmContact.txtEmail.className = 'input';
	}
	
	//
	if (is_submit)
	{
		document.frmContact.submit();
	}
}

function supportSubmit()
{
	var is_submit = true;
	
	if ( !emailCheck(document.frmSupport.txtEmail.value) )
	{
		document.frmSupport.txtEmail.className = 'input-error';
		document.frmSupport.txtEmail.focus();
		document.frmSupport.txtEmail.select();
		is_submit = false;
	}
	else
	{
		document.frmSupport.txtEmail.className = 'input';
	}
	
	if (is_submit)
	{
		document.frmSupport.submit();
	}
}

/* Products */
function getProducts()
{
	var area_url 		= 0;
	var group_url		= 0;
	var subgroup_url	= 0;
	var assortment_url	= 0;
	var firm_url		= 0;
	var type_url		= 0;
	var model_url		= 0;
	var color_url		= 0;
	
	if (document.getElementById('area_url'))
	{
		area_url = document.getElementById('area_url').value;
	}
	if (document.getElementById('group_url'))
	{
		group_url = document.getElementById('group_url').value;
	}
	if (document.getElementById('subgroup_url'))
	{
		subgroup_url = document.getElementById('subgroup_url').value;
	}
	if (document.getElementById('assortment_url'))
	{
		assortment_url = document.getElementById('assortment_url').value;
	}
	if (document.getElementById('firm_url'))
	{
		firm_url = document.getElementById('firm_url').value;
	}
	if (document.getElementById('type_url'))
	{
		type_url = document.getElementById('type_url').value;
	}
	if (document.getElementById('model_url'))
	{
		model_url = document.getElementById('model_url').value;
	}
	if (document.getElementById('color_url'))
	{
		color_url = document.getElementById('color_url').value;
	}
	document.location = SITE_ROOT + LANG + '/gproducts/' + area_url + '/' + group_url + '/' + subgroup_url + '/' + assortment_url + '/' + firm_url + '/' + type_url + '/' + model_url + '/' + color_url + '/';
}

function emailCheck(emailStr)
{
	/* The following pattern is used to check if the entered e-mail address
	fits the user@domain format. It also is used to separate the username
	from the domain. */

	//var emailPat=/^(.+)@(.+)$/
	var emailPat=/^([a-zA-Z0-9._-]+)@(.+)$/;
	/* The following string represents the pattern for matching all special
	characters. We don't want to allow special characters in the address. 
	These characters include ( ) < > @ , ; : \ " . [ ] */
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
	/* The following string represents the range of characters allowed in a 
	username or domainname. It really states which chars aren't allowed. */
	var validChars="\[^\\s" + specialChars + "\]";
	/* The following pattern applies if the "user" is a quoted string (in
	which case, there are no rules about which characters are allowed
	and which aren't; anything goes). E.g. "jiminy cricket"@disney.com
	is a legal e-mail address. */
	var quotedUser="(\"[^\"]*\")";
	/* The following pattern applies for domains that are IP addresses,
	rather than symbolic names. E.g. joe@[123.124.233.4] is a legal
	e-mail address. NOTE: The square brackets are required. */
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	/* The following string represents an atom (basically a series of
	non-special characters.) */
	var atom=validChars + '+';
	/* The following string represents one word in the typical username.
	For example, in john.doe@somewhere.com, john and doe are words.
	Basically, a word is either an atom or quoted string. */
	var word="(" + atom + "|" + quotedUser + ")";
	// The following pattern describes the structure of the user
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	/* The following pattern describes the structure of a normal symbolic
	domain, as opposed to ipDomainPat, shown above. */
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	
	
	/* Finally, let's start trying to figure out if the supplied address is
	valid. */
	
	/* Begin with the coarse pattern to simply break up user@domain into
	different pieces that are easy to analyze. */
	var matchArray=emailStr.match(emailPat);
	if (matchArray==null)
	{
		/* Too many/few @'s or something; basically, this address doesn't
		even fit the general mould of a valid e-mail address. */
		//alert("Wrong Email!");
		return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];
	
	// See if "user" is valid 
	if (user.match(userPat)==null)
	{
		// user is not valid
		//alert("Wrong Email!");
		return false;
	}

	/* if the e-mail address is at an IP address (as opposed to a symbolic
	host name) make sure the IP address is valid. */
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null)
	{
		// this is an IP address
		for (var i=1;i<=4;i++)
		{
			if (IPArray[i]>255)
			{
				//alert("Wrong Email!");
				return false;
			}
		}
		return true;
	}

	// Domain is symbolic name
	var domainArray=domain.match(domainPat);
	if (domainArray==null)
	{
		//alert("Wrong Email!");
		return false;
	}
	
	/* domain name seems valid, but now make sure that it ends in a
	three-letter word (like com, edu, gov) or a two-letter word,
	representing country (uk, nl), and that there's a hostname preceding 
	the domain or country. */
	
	/* Now we need to break up the domain to get a count of how many atoms
	it consists of. */
	var atomPat=new RegExp(atom,"g");
	var domArr=domain.match(atomPat);
	var len=domArr.length;
	if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3)
	{
		// the address must end in a two letter or three letter word.
		//alert("Wrong Email!");
		return false;
	}
	
	// Make sure there's a host name preceding the domain.
	if (len<2)
	{
		//alert("Wrong Email!");
		return false;
	}

	// If we've gotten this far, everything's valid!
	return true;
}

function showGalleryImage(img)
{
	obj = document.getElementById('image_big');
	
	if (obj)
	{
		obj.src = SITE_ROOT + 'showimage.php?type=gallery&id=' + img + '&w=550&h=412&wm=0';
	}
}

function loginFocus(obj)
{
	if ( !obj.init )
	{
		obj.initValue = obj.value;
		obj.value = '';
		obj.init  = true;
	}
	else if ( obj.value == obj.initValue )
	{
		obj.value = '';
	}
}

function loginBlur(obj)
{
	if ( obj.value == '' )
	{
		obj.value = obj.initValue;
	}
}

function loginPasswordFocus(obj)
{
	obj.style.display = 'none';
	
	if (obj.type && obj.type == 'text')
	{
		obj_1 = document.getElementById('login_password');
	}
	else
	{
		obj_1 = document.getElementById('login_password_text');
	}
	obj_1.style.display = 'block';
	obj_1.focus();
}

function loginPasswordBlur(obj)
{
	if (obj.type && obj.type == 'text')
	{
		obj.style.display = 'none';
		obj_1 = document.getElementById('login_password');
		obj_1.style.display = 'block';
	}
	else
	{
		if (obj.value == '')
		{
			obj.style.display = 'none';
			obj_1 = document.getElementById('login_password_text');
			obj_1.style.display = 'block';
		}
	}
}

function signupStep1()
{
	var is_submit = true;
	
	// txtFirstName
	if ( trim(document.frmSignUp.txtFirstName.value).length == 0 )
	{
		document.frmSignUp.txtFirstName.className = 'input-error';
		document.frmSignUp.txtFirstName.focus();
		document.frmSignUp.txtFirstName.select();
		is_submit = false;
	}
	else
	{
		document.frmSignUp.txtFirstName.className = 'input';
	}
	
	// txtLastName
	if ( trim(document.frmSignUp.txtLastName.value).length == 0 )
	{
		document.frmSignUp.txtLastName.className = 'input-error';
		document.frmSignUp.txtLastName.focus();
		document.frmSignUp.txtLastName.select();
		is_submit = false;
	}
	else
	{
		document.frmSignUp.txtLastName.className = 'input';
	}
	
	// txtEmail
	if ( !emailCheck(document.frmSignUp.txtEmail.value) )
	{
		document.frmSignUp.txtEmail.className = 'input-error';
		document.frmSignUp.txtEmail.focus();
		document.frmSignUp.txtEmail.select();
		is_submit = false;
	}
	else
	{
		document.frmSignUp.txtEmail.className = 'input';
	}
	
	// txtLastName
	if ( document.frmSignUp.ddlOperator.value == 0 )
	{
		document.frmSignUp.ddlOperator.focus();
		is_submit = false;
	}

	//
	if (is_submit)
	{
		document.frmSignUp.submit();
	}
}

function signupStep2()
{
	
}

function signupSelectOperator()
{
	var operator_id = document.getElementById('ddlOperator').value;
	if (operator_id != '0')
	{
		destination.push('skuku_operetor_status');
		script_name = SITE_ROOT + "ajax.php?lang=" + LANG;
		ajax_req = 'switch=skuku_operetor_status&operator_id=' + operator_id;
		cache.push(ajax_req);
		getAjaxRequest();
	}
	else
	{
		document.getElementById('skuku_operetor_status').innerHTML = 'Choose your network operator / country';
	}
}

function setProductsQuantity(id, action)
{
	var obj = document.getElementById(id);
	if (obj)
	{
		if (isNaN(obj.value))
		{
			obj.value = 1;
		}
		
		if (action == '-' && parseInt(obj.value) > 1)
		{
			obj.value = parseInt(obj.value) - 1;
		}
		else if (action == '+')
		{
			obj.value = parseInt(obj.value) + 1;
		}
	}
}

// Refresh DIV to show Shopping Cart
function showShoppingCart()
{
	destination.push('shopping_cart');
	script_name = SITE_ROOT + "ajax.php?lang=" + LANG;
	ajax_req = 'switch=b2b_shopping_cart';
	cache.push(ajax_req);
	getAjaxRequest();
}

// Add selected product to Shopping Cart
function addToShoppingCart(product_id)
{
	var quantity = 1;
	
	if ( document.getElementById('product_qty_' + product_id) )
	{
		quantity = parseInt(document.getElementById('product_qty_' + product_id).value);
	}
	
	destination.push('shopping_cart');
	script_name = SITE_ROOT + "ajax.php?lang=" + LANG;
	ajax_req = 'product_id=' + product_id + '&quantity=' + quantity + '&switch=b2b_addtoShoppingCart';
	cache.push(ajax_req);
	getAjaxRequest();
	
	// Refresh Shopping Cart
	showShoppingCart();
}

// Open menu Item
function menuClick(id, cId, cName)
{
	var obj = document.getElementById(cId+id);
	
	if (obj)
	{
		if (obj.className == cName + 'close')
		{
			obj.className = cName + 'open';
		}
		else
		{
			obj.className = cName + 'close';
		}
	}
}
