﻿
var AjaxRequest;
var type = ""
var ResultID = ""
function AddToCart(bookID)
{
	/*
	if(BasicUserID == 1)
	{
		alert("بزودی سیستم فروش اینترنتی بوستان کتاب، فعال می گردد")
		return
	}*/
	var urlAdr = "/BView/GetData.ashx?BookID=" + bookID + "&Amount=1&Mode=AddToCart" + GetTime()
	type = "AddToCart"
	SetDataAjax(urlAdr,"AddToCart")
}
function AddToWish(bookID)
{
/*	if(BasicUserID == 1)
	{
		alert("بزودی سیستم فروش اینترنتی بوستان کتاب، فعال می گردد")
		return
	}*/
	var urlAdr = "/BView/GetData.ashx?BookID=" + bookID + "&Mode=AddToWish" + GetTime()
	type = "AddToWish"
	SetDataAjax(urlAdr,"AddToWish")
}

function MoveToCart(bookID,TR)
{
	var urlAdr = "/BView/GetData.ashx?BookID=" + bookID + "&Amount=1&Mode=MoveToCart" + GetTime()
	type = "MoveToCart"
	SetDataAjax(urlAdr,"MoveToCart")
	document.getElementById(TR).style.display = "none"
}

function DelOfWish(bookID,TR)
{
	var urlAdr = "/BView/GetData.ashx?BookID=" + bookID + "&Mode=DelOfWish" + GetTime()
	type = "DelOfWish"
	SetDataAjax(urlAdr,"DelOfWish")
	document.getElementById(TR).style.display = "none"
}

function CheckSendError(languageID)
{
	strText  = document.getElementById("TextErrorContent");
	strCode = document.getElementById("TextErrorCodeNumber");
	if (strText.value.length < 2)
	{
		window.alert(MsgErrorForm);
		strText.focus();
	}
	else if(strCode.value.length < 3)
	{
		window.alert(MsgErrorCaptcha);
		strCode.focus();
	}
	else
	{
	    var urlAdr = '/BView/GetData.ashx?Mode=SendError&LanguageID='
		            + languageID + '&PageAddress='
		            + window.location + '&TextContent=' + encodeURI(strText.value)
		            + '&CodeImage=' + strCode.value + GetTime()
		            
		type = "SendError"
		SetDataAjax(urlAdr,"SendError")
	}
}

function CheckSearch(num,languageID,SearchText,SearchType,BookID) {
    var urlAdr = '/BView/GetData.ashx?Mode=Search&LanguageID='
	            + languageID
	            + '&SearchText=' + encodeURI(SearchText)
	            + '&SearchType=' + SearchType
	            + '&BookID=' + BookID + GetTime()

    ResultID = num
    type = "Search"
    SetDataAjax(urlAdr, "Search")
}


function CheckSendPass(languageID) {
    strText = document.getElementById("TextEmail");
    strCode = document.getElementById("TextPassCodeNumber");
    if (strText.value.length < 2) {
        window.alert(MsgErrorPass);
        strText.focus();
    }
    else if (strCode.value.length < 3) {
        window.alert(MsgErrorCaptcha);
        strCode.focus();
    }
    else {
        var urlAdr = '/BView/GetData.ashx?Mode=SendPass&LanguageID='
		            + languageID + '&TextContent=' + strText.value
		            + '&CodeImage=' + strCode.value + GetTime()

        type = "SendPass"
        SetDataAjax(urlAdr, "SendPass")
    }
}



function CheckSendToFriend(languageID,PageAddress)
{
	strText  = document.getElementById("TextFriendContent");
	strCode = document.getElementById("TextFriendCodeNumber");
	strEmail = document.getElementById("Email");
	if(!CheckEmail(strEmail.value))
	{
		window.alert(ErrorBug2);
		strEmail.focus();
	}
	else if(strText.value.length < 2)
	{
		window.alert(ErrorBug1);
		strText.focus();
	}
	else if(strCode.value.length < 3)
	{
		window.alert(MsgErrorCaptcha);
		strCode.focus();
	}
	else
	{
	    var urlAdr = '/BView/GetData.ashx?Mode=SendToFriend&LanguageID=' + languageID + '&PageAddress=' + window.location + '&TextContent=' + encodeURI(strText.value) + '&Email=' + strEmail.value + '&CodeImage=' + strCode.value + GetTime()
		type = "SendToFriend"
		SetDataAjax(urlAdr,"SendToFriend")
	}
}

function CheckSendEForm(languageId, userId, eFormId) {
    strText = XMLDoc;
    strCode = document.getElementById("TextEFormCaptcha");
    if (strText.length < 2) {
        window.alert(MsgErrorForm);
        strText.focus();
    }
    else if (strCode.value.length < 3) {
        window.alert(MsgErrorCaptcha);
        strCode.focus();
    }
    else {
        var urlAdr = '/BView/GetData.ashx?Mode=SendEForm&LanguageID=' + languageId + '&UserID=' + userId + '&EFormID=' + eFormId + '&TextContent=' + strText + '&CodeImage=' + strCode.value + GetTime()
        type = "SendEForm"
        SetDataAjax(urlAdr, "SendEForm")
    }
}


function getPosition(e)
{
    try
    {
		e = e || window.event;
		var cursor = {x:0, y:0};
		if (e.pageX || e.pageY)
		{
			cursor.x = e.pageX;
			cursor.y = e.pageY;
		} 
		else
		{
			var de = document.documentElement;
			var b = document.body;
			cursor.x = e.clientX + 
				(de.scrollLeft || b.scrollLeft) - (de.clientLeft || 0);
			cursor.y = e.clientY + 
				(de.scrollTop || b.scrollTop) - (de.clientTop || 0);
		}
    }
    catch(error)
    {
//		alert("Error position")
		cursor.x = 100
		cursor.y = 200
    }
	return cursor;
}

function SetDataAjax(url,type)
{
	try
	{
		if (AjaxRequest == null)
		{
			if (window.XMLHttpRequest)
			{
				AjaxRequest = new XMLHttpRequest();
			}
			else if (window.ActiveXObject)
			{
				AjaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			}
		}
		if (AjaxRequest != null)
		{
			ShowLoading("show","LoadingShop");
			AjaxRequest.onreadystatechange = Callback;
			
			AjaxRequest.open("GET", url, true);
			AjaxRequest.send(null);
		}
		else
		{
			alert("Your browser does not support XMLHTTP Ajax.");
		}
	}
	catch(e)
	{
		alert("SetDataAjax")
	}
}

function Callback()
{
	try
	{
		if (AjaxRequest.readyState == 4)
		{
			if (AjaxRequest.status == 200)
			{
				ShowLoading("hide","LoadingShop");
				var result ;
				result = AjaxRequest.responseText;
				if(type == "AddToCart")
				{
					if (result > "1")
						alert(MsgAddCartOk)
					else if(result == "-2")
						alert(MsgAddError);
					else if(result == "-8")
						alert(MsgMaxCountError);
					else if(result == "1")	
						alert(MsgUpdateOk);
					else
						alert(MsgError);
				}
				else if(type == "AddToWish")
				{
					if (result == "-1")
						alert(MsgLogin)
					else
						alert(MsgAddWishOk);
				}
				else if(type == "MoveToCart")
				{
					if (result != "0")
						alert(MsgAddCartOk)
					else
						alert(MsgError);
				}
				else if(type == "DelOfWish")
				{
					if (result != "0")
						alert(MsgDelWishOk)
					else
						alert(MsgError);
				}
				else if(type == "DelOfCart")
				{
					if (result != "0")
						alert(MsgDelCartOk)
					else
						alert(MsgError);
				}
				else if(type == "SendError")
				{
					if (result == "1")
						alert(MsgSendErrorOk)
					else if (result == "-2")
						alert(MsgSendErrorNotOk);
					else 
						alert(MsgCaptchaNotOk);
				}
				else if(type == "SendToFriend")
				{
					if (result == "1")
						alert(MsgSendToFriendOk)
					else if (result == "-2")
						alert(MsgSendErrorNotOk);
					else 
						alert(MsgCaptchaNotOk);
				}
				else if(type == "SendEForm")
				{
					if (result == "1")
						alert(MsgSendEFormOk)
					else if (result == "-2")
						alert(MsgSendEFormNotOk);
					else 
						alert(MsgSendEFormNotOk);
				}
				else if (type == "Search") {
				    ViewResult(result)
				}
				else if(type == "Login")
				{
					if (result == "False")
					{
						alert(MsgWrongPass)
						document.getElementById("Password").value = ""
					} 
					else
					{
						window.location.href = window.location.href
					}
				}
				else if(type == "Logoff")
				{
					if (result == "True")
					{
						window.location.href = window.location.href
					} 
				}
				else if (type == "SendPass") {
				    if (result == "1")
				        alert(MsgSendPassOk)
				    else if (result == "-2")
				        alert(MsgEmailNoFount);
				    else if (result == "-1")
				        alert(MsgErrorPass);
				    else 
				        alert(MsgErrorCaptcha);
				    
				}
				///				alert(AjaxRequest)
				AjaxRequest = null;
			}
		}
	}
	catch(e)
	{
		alert("CallbackAjax")
	}
}

function ViewResult(rslt) {
    document.getElementById("Result" + ResultID).outerHTML = "<ul class='List3'>" + rslt + "</ul>"
}

function ShowLoading(action,OLoading)
{
	try
	{
		var de = document.documentElement;
		var b = document.body;

		var divObj = document.getElementById(OLoading);
		if (action == "show")
		{
			divObj.style.display = "block";
			divObj.style.left = getPosition(event).x
			divObj.style.top = getPosition(event).y - 100
		}
		else
		{
			divObj.style.display = "none";	
		}
	}
	catch(e)
	{
//		alert("showloading-" + action)
		divObj.style.display = "none";
	}
}

function LoginBox()
{
	var divObj = document.getElementById("LoginBox")
	if(divObj.style.display == "block")
	{
		divObj.style.display = "none"
	}
	else
	{
		if (window.ActiveXObject)
		{
			if(event != null)
			{
				divObj.style.left = getPosition(event).x
				divObj.style.top = getPosition(event).y + 10
			}
			else
			{
				divObj.style.right = 500
				divObj.style.top = 300
			}
		}
		else //firefox
		{
			divObj.style.left = 600
			divObj.style.top = 300
		}
		
		divObj.style.display = "block"
	}
}

function LoginDoIt(e) {
    var KeyID = (window.event) ? event.keyCode : e.keyCode;
    if (KeyID == 13) {
        Login()
    }
}

function Login()
{
	var Username = document.getElementById("Username").value;
	var Password = document.getElementById("Password").value;
	
	if(Username.lengh < 3 && Username.lengh < 3)
	{
		alert(MsgErrorLogin)
	}
	else
	{
		var urlAdr = "/BView/GetData.ashx?Username=" + Username + "&Password=" + Password + "&Mode=Login" + GetTime()
		type ="Login"
		SetDataAjax(urlAdr,"Login")
	}
}

function Logoff()
{
	var urlAdr = "/BView/GetData.ashx?Mode=Logoff" + GetTime()
	type ="Logoff"
	SetDataAjax(urlAdr,"Logoff")
}

// JScript File
function fnSendFriend(languageId)
{
	var url = window.location.href
//		window.open("/View/Public/SendToFriend.aspx?LanguageID=" + languageId + "&PageAddress=" + url ,null,"height=300,width=500,status=yes,toolbar=no,menubar=no,location=no,scrollbars=1,")
	window.location.host= "/View/Public/SendToFriend.aspx?LanguageID=" + languageId + "&PageAddress=" + url 
}

function fnAddFavorite()
{
	var url = window.location.href
	var title = window.document.title
	
	if(document.all)
		window.external.addfavorite(url,title)
	else if(window.sidebar)
	{
		window.sidebar.addPanel(title,url,"")
	}
}

function fnPrint(languageId)
{
	var obj = document.getElementById("divContent")
	var strContent = ""
	if(obj != null)
	{
		strContent += "<html><head>"
		strContent += "<link type='text/css' rel='stylesheet' href='/BFiles/CSS/Boxes.css'>"
		strContent += "<link type='text/css' rel='stylesheet' href='/BFiles/CSS/View"
		strContent += languageId 
		strContent += ".css'>"
		strContent += "</head><body>"
		strContent += "<table width='100%' height='38' cellpadding='0' cellspacing='0'>"
		strContent += "<tr>"
		strContent += "<td background='/BFiles/Images/PrintBG.jpg' align='left'>"
		strContent += "<img src='/BFiles/Images/PrintArm.jpg' />"
		strContent += "</td>"
		strContent += "<td background='/BFiles/Images/PrintBG.jpg' align='center'>"
		strContent += "<span style='color:white;font-family:Arial;font-size:14pt;'>"
		strContent += window.document.title
		strContent += "</span>"
		strContent += "</td>"
		strContent += "</tr>"
		strContent += "</table>"
		strContent += "<div align=left><address>URL : " + window.document.URL + "</address></div><hr/>"
		strContent += "<div align=left><input type='button' value='Print' onclick='print()'></div>"
		strContent += "<p>" + obj.innerHTML + "</p>"
		strContent += "</body></html>"
		winPreview = window.open ("","winPrint","width=800,height=500, menu=0, toolbar=0, status=0,scrollBars=1")
		winPreview.document.write (strContent)
		winPreview.print()
	}
	else
	{
		window.print()
	}
}

function fnSearchTop(LanguageID) 
{
	var obj = document.getElementById("SearchTop")
	if(obj.value != "")
	{
		window.location.href = "/BView/Book/SearchBook.aspx?LanguageID=" + LanguageID + "&SearchText=" + obj.value + "&ID=0"
	}
	else
		alert(MsgEnterSearch)
	
	return(false)
}

function CheckEmail(strEmail)
{
var exp = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
if(strEmail.match(exp) == null)
	return(false)
else
	return(true)
}

function GetTime()
{
    var date =new Date();
    result = "&rnd=" + escape(date.toDateString() + date.toTimeString());
    return result;
}
