﻿function findPosX(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function RepositionLayer(RefObj, MoveObj, OffsetX, OffsetY) {
	var RefObj, MoveObj;
	var RefObjLeftPos, RefObjTopPos;
	var MoveObjLeftPos, PosDiff;
	RefObjLeftPos = findPosX(RefObj);
	RefObjTopPos = findPosY(RefObj);
	MoveObj.style.left = (RefObjLeftPos + OffsetX) + 'px';
	MoveObj.style.top = (RefObjTopPos + OffsetY) + 'px';
}

function DoPopup(TheURL) {
	DoPopupAdvanced(TheURL, 'oPopup', -1, -1, 700, 600, 'yes', 'yes', 'no', 'no', 'no', 'no', 'no');
}

function DoPopupAdvanced(theURL, theWinName, theLeft, theTop, theWidth, theHeight, theresize, thescrollbars, thestatusbar, themenu, thetools, thelocationbar, thedirs) {
	// EXAMPLE doPopup('popup.asp', 'popwin', 10, 10, 500, 300, 'no', 'no', 'yes', 'no', 'no', 'no', 'no')
	// USE theLeft AND theTop = -1 FOR AUTO-CENTER

	if (theLeft == -1 && theTop == -1) {
		theLeft = (screen.width - theWidth) / 2;
		theTop = (screen.height - theHeight) / 2;
	}
	var paramstr;
	paramstr = "width=" + theWidth + "px,height=" + theHeight + "px,left=" + theLeft + "px,top=" + theTop + "px,resizable=" + theresize + ",scrollbars=" + thescrollbars + ",status=" + thestatusbar + ",menubar=" + themenu + ",toolbar=" + thetools + ",location=" + thelocationbar + ",directories=" + thedirs;
	newwindow = window.open(theURL, theWinName, paramstr);
	if (window.focus) { newwindow.focus() }
	return false;
}

function ViewImage(TheImg, TheAlt) {
	TheURL = "/Assets/ViewImage.aspx?Img=" + TheImg + "&Alt=" + TheAlt
	DoPopupAdvanced(TheURL, 'oEnlarge', -1, -1, 700, 600, 'yes', 'yes', 'no', 'no', 'no', 'no', 'no');
}

function doLogin(usernameId, passwordId, actionUrl) {
    $("body").append('<form method="post" action="' + actionUrl + '" id="loginForm" style="display:none;"></form>');
    $("#loginForm").append('<input type="hidden" name="Email" value="'+ $('#'+usernameId).val() +'"/>');
    $("#loginForm").append('<input type="hidden" name="Password" value="' + $('#' + passwordId).val() + '"/>');
    document.getElementById("loginForm").submit();
}

var validationResult = false;
function ValidateEmailIsUnique(source, arguments) {
    jQuery.ajax({
        async: false,
        type: "POST",
        data: "{ 'value': '" + arguments.Value + "' }",
        url: "/SendData.asmx/EmailIsUnique",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (res) { validationResult = res.d; },
        error: function () { validationResult = false; }
    });
    arguments.IsValid = validationResult;
}


function ValidateProfileEmailIsUnique(source, arguments) {
    jQuery.ajax({
        async: false,
        type: "POST",
        data: "{ 'value': '" + arguments.Value + "', 'id':"+idVal+" }",
        url: "/SendData.asmx/ProfileEmailIsUnique",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (res) { validationResult = res.d; },
        error: function () { validationResult = false; }
    });
    arguments.IsValid = validationResult;
}


function CloseMsgBoxes() {
    $(".success").hide();
    $(".warning").hide();
    $(".error").hide();
    $(".info").hide();
}

function SubmitToPaypal(addr) {
	jQuery.ajax({
		async: false,
		type: "POST",
		data: "{'value': '" + $('#valData').val() + "','agent':'" + $('#valAgent').val() + "','lang':'" + $('#valLang').val() + "'}",
		url: "/SendData.asmx/CheckCartData",
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		success: function (res) {
			document.forms[0].invoice.value = res.d;
			if (parseInt(res.d) <= 0) {
				validationResult = false; return false;
			}
			else {
				document.forms[0].action = addr;
				document.forms[0].submit();
			}
		},
		error: function () { validationResult = false; return false; }
	});
	return false;

}


//------: CHECK BROWSER
var version = "";
var browser = "";
var isIE6 = false;

function getBrowser() {
	if (navigator.appVersion.indexOf("MSIE") > 0) {
		version = parseFloat(navigator.appVersion.split("MSIE")[1]);
		browser = "MSIE";
	} else {
		browser = "not MSIE"
	}
}
getBrowser();

if (browser == "MSIE") {
	if (version < 7) {
		isIE6 = true;
	}
}
//------: GENERAL FUNCTIONS
function LoginInput(obj, dValue, setValue) {
	if (obj.value == dValue)
		obj.value = setValue;
}

function showInactive(id, show) {
	var obj = $('#' + id);
	if (show) {
		obj.css('display', 'block');
	} else {
		obj.css('display', 'none');
	}
}


//------: HEADER IMAGES SCRIPTS
var headerImages, headerText;
var cHead = 0;

function showHead(num, linkObj) {
	if (num != cHead) {
		swapLinkActive(linkObj);

		/* PREVIOUS HEAD MANIPULATION */
		var previmg = $('#' + headerImages[cHead]);
		previmg.fadeOut();

		var prevText = $('#' + headerText[cHead]);
		if (browser != "MSIE") {
			prevText.fadeOut();
		} else {
			prevText.css('display', 'none');
		}

		/* PRESSED HEAD MANIPULATION */
		cHead = num;
		var img = $('#' + headerImages[num]);
		img.fadeIn();

		var Text = $('#' + headerText[num]);
		if (browser != "MSIE") {
			Text.fadeIn();
		} else {
			Text.css('display', 'block');
		}
	}
}
function swapLinkActive(linkObj) {
	var ch = $('#links').children(".active");
	ch[0].className = "";
	linkObj.className = "active";
}
