function initPage()
{
/*--popup with gallery--*/
	if(typeof($) == 'function') {
		var _hold = $('#popup');
		if(_hold.length > 0) {
			var img_hold = _hold.find('.big-img');
			img_hold.find('img').remove();
			var btn_list = _hold.find('ul a');
			var _a = 0;
			btn_list.each(function(_ind, _el){
				var t_src = $(_el).attr('href');
				img_hold.append('<img src="' + t_src + '" alt="" />');
				if($(_el).hasClass('active')) {
					_a = _ind;
					img_hold.find('img').eq(_ind).show();
				}
				else {
					img_hold.find('img').eq(_ind).hide();
				}
				$(_el).click(function() {
					if(_a != _ind) {
						btn_list.eq(_a).removeClass('active');
						btn_list.eq(_ind).addClass('active');
						img_hold.find('img').eq(_a).hide();
						img_hold.find('img').eq(_ind).show();
						_a = _ind;
					}
					return false;
				});
			});
		}
/*--button hover--*/
		$('input.button').hover(function() {
			this.src = this.src.replace(".gif", "-hover.gif");
		}, function() {
			this.src = this.src.replace("-hover.gif", ".gif");
		});
	}
/*--td width--*/
	var _lenght = $('div.compare-area table tr:eq(0) td:not(.properties)').length;
	$('div.compare-area table td:not(.properties)').css('width',(950-238)/_lenght);
}
if (window.addEventListener) window.addEventListener("load", initPage, false);
else if (window.attachEvent) window.attachEvent("onload", initPage);

function trimString(sInString) {
	  sInString = sInString.replace( /^\s+/g, "" );// strip leading
	  return sInString.replace( /\s+$/g, "" );// strip trailing
	}

function validateRegisterForm(element) {
	var validatePanel = $('.form-popup').hide();
	
	validatePanel.find('a.close').click(function() {
		validatePanel.fadeOut(250);
		return false;
	});

	document.getElementById('userAgreedMsg').style.display = "none";
	document.getElementById('burthDayError').style.display = "none";
	document.getElementById('passmatch').style.display = "none";
	
	nameValue = $("#name").val();
	emailValue = $("#email").val();
	usernameValue = $("#username").val();
	passwordValue = $("#password").val();
	repasswordValue = $("#repassword").val();
	phoneValue = $("#phone").val();
	genderValue = $("#gender").val();
	birthYearValue = $("#birthYear").val();
	surname = $("#surname").val();
	bvInt = parseInt(birthYearValue);
	
	var offset = $(document.getElementById('btn-register')).offset();
	
	if (nameValue != "" && emailValue != "" && usernameValue != "" && passwordValue != "" 
		&& repasswordValue != "" && phoneValue != ""
		&& genderValue != "-1" && birthYearValue != ""
		&& birthYearValue != "" && surname != "") {
		
		if (!document.regForm.agreement.checked){
			document.getElementById('requirements').style.display = "none";
			document.getElementById('userAgreedMsg').style.display = "block";
			validatePanel.fadeIn(250);
			validatePanel.css('top', offset.top - 400);
			return false;
		}
		
		return true;
	}
	else{
		document.getElementById('requirements').style.display = "block";
		document.getElementById('burthDayError').style.display = "none";
		var offset = $(document.getElementById('btn-register')).offset();
		validatePanel.fadeIn(250);
		validatePanel.css('top', offset.top - 450);
		//validatePanel.css('left', 400);
		return false;
	}
	
	
	if((isNaN(bvInt) || bvInt == 0) ) {
		document.getElementById('requirements').style.display = "none";
		document.getElementById('burthDayError').style.display = "block";
		document.getElementById('passmatch').style.display = "none";
		validatePanel.fadeIn(250);
		validatePanel.css('top', offset.top - 450);
		return false;
	}
	
	if (passwordValue != repasswordValue){
		document.getElementById('requirements').style.display = "none";
		document.getElementById('burthDayError').style.display = "none";
		document.getElementById('passmatch').style.display = "block";
		validatePanel.fadeIn(250);
		validatePanel.css('top', offset.top - 450);
		return false;
	}
	
	if (birthYearValue.length < 4 || birthYearValue.length > 4){
		document.getElementById('requirements').style.display = "none";
		document.getElementById('burthDayError').style.display = "block";
		document.getElementById('passmatch').style.display = "none";
		var offset = $(document.getElementById('btn-register')).offset();
		validatePanel.fadeIn(250);
		validatePanel.css('top', offset.top - 300);
		return false;
	}
	
	
	if (!document.regForm.agreement.checked){
		document.getElementById('requirements').style.display = "none";
		document.getElementById('userAgreedMsg').style.display = "block";
		validatePanel.fadeIn(250);
		validatePanel.css('top', offset.top - 400);
		return false;
	}
}


function validateMyAccountForm() {
	var validatePanel = $('.form-popup').hide();
	
	validatePanel.find('a.close').click(function() {
		validatePanel.fadeOut(250);
		return false;
	});

	nameValue = $("#name").val();
	emailValue = $("#email").val();
	
	phoneValue = $("#phone").val();
	genderValue = $("#gender").val();
	birthYearValue = $("#birthYear").val();
	surname = $("#surname").val();
	bvInt = parseInt(birthYearValue);
	
	if((isNaN(bvInt) || bvInt == 0) ) {
		document.getElementById('requirements').style.display = "none";
		document.getElementById('burthDayError').style.display = "block";
		document.getElementById('passmatch').style.display = "none";
		var offset = $(document.getElementById('btn-register')).offset();
		validatePanel.fadeIn(250);
		validatePanel.css('top', 400);
		return false;
	}
	
	
	if (birthYearValue.length < 4 || birthYearValue.length > 4){
		document.getElementById('requirements').style.display = "none";
		document.getElementById('burthDayError').style.display = "block";
		document.getElementById('passmatch').style.display = "none";
		var offset = $(document.getElementById('btn-register')).offset();
		validatePanel.css('top', 400);
		validatePanel.css('marginLeft', 200);
		return false;
	}
	
	if (nameValue != "" && emailValue != "" && phoneValue != ""
			&& genderValue != "-1" && birthYearValue != ""
			&& birthYearValue != "" && surname != "") {
		return true;
	}
	else{
		document.getElementById('requirements').style.display = "block";
		document.getElementById('burthDayError').style.display = "none";
		var offset = $(document.getElementById('btn-register')).offset();
		validatePanel.css('top', 400);
		validatePanel.css('marginLeft', 200);
		validatePanel.fadeIn(250);
		//validatePanel.css('left', 400);
		return false;
	}
}

function validateUpdatePassForm() {
	var validatePanel = $('.form-popup').hide();
	validatePanel.find('a.close').click(function() {
		validatePanel.fadeOut(250);
		return false;
	});
	oldValue = $("#oldPassword").val();
	passValue = $("#password").val();
	repassValue = $("#repassword").val();

	if (passValue != repassValue){
		document.getElementById('requirements').style.display = "none";
		document.getElementById('burthDayError').style.display = "none";
		document.getElementById('passmatch').style.display = "block";
		validatePanel.css('top', 500);
		validatePanel.fadeIn(250);
		return false;
	}
	
	if (oldValue != "" && passValue != "" && repassValue != "") {
		return true;
	}
	else{
		document.getElementById('requirements').style.display = "block";
		document.getElementById('burthDayError').style.display = "none";
		var offset = $(document.getElementById('btn-register')).offset();
		validatePanel.css('top', 500);
		validatePanel.fadeIn(250);
		//validatePanel.css('left', 400);
		return false;
	}
}

function validateContactForm() {
	var validatePanel = $('.form-popup').hide();
	validatePanel.find('a.close').click(function() {
		validatePanel.fadeOut(250);
		return false;
	});
	
	if(document.contactForm.suggestions.value.length > 4000) {
		validatePanel.css('top', 500);
		validatePanel.fadeIn(250);
		return false;
	}
	else {
		return true;
	}
}

function showContactPopUp(){
	var validatePanel = $('.form-popup').hide();
	alert('asdsadsa');
	validatePanel.find('a.close').click(function() {
		validatePanel.fadeOut(250);
		document.contactForm.submit=1;
		document.contactForm.submit();
		validatePanel.fadeIn(250);
		return false;
	});
}

function countProductForCompare(id, offset){
	var numOfProducts = 0;
	  $.ajax({
		type: "GET",
		cache: false,
		url: "../products/countProducts.eg",
		success: function(msg){
			var form_popup = $('.form-popup').hide();
			form_popup.find('a.close').click(function() {
				form_popup.fadeOut(250);
				return false;
			});
			var checkMax;
			numOfProducts = parseInt(trimString(msg));
			if (numOfProducts > 1 && numOfProducts < 5){
				location.href="../products/compare.eg?selectedCategoryId=" + id;
			}
			else  {
				form_popup.css('top', offset);
				form_popup.fadeIn(250);
				return false;
			} 
			
		}
	});  
}




function selectProductForCompare(productId){
	
	  $.ajax({
			type: "GET",
			cache: false,
			url: "../products/saveForCompare.eg",
			data: "productId=" + productId + "",
			success: function(msg){
				var form_popup = $('.form-popup').hide();
				var checkMax;
				if (trimString(msg) == "false"){
					checkMax = new Boolean(false);
				}
				else {
					checkMax = new Boolean(true);
				}
				if(checkMax == true) {
					var element = '#compare-' + productId;
					var offset = $(element).offset();
					form_popup.css('top', offset.top - 300);
					form_popup.fadeIn(250);
					document.getElementById('compare-' + productId).checked = false;
					return false;
				} 
				form_popup.find('a.close').click(function() {
					form_popup.fadeOut(250);
					return false;
				});
			}
		});  
}

function disableLinks() {
	var elementsA = document.getElementsByTagName("a");
	for (i = 0; i < elementsA.length; i++){
		elementsA[i].href = "#";
	}
	var elementsIn = document.getElementsByTagName("input");
	for (i = 0; i < elementsIn.length; i++){
		elementsIn[i].disabled = "disabled";
	}
}

