var Ajax = new AjaxModule();

function AjaxModule()
{
	this.Client = new function () {
		var xmlhttp = false;  
		if (window.XMLHttpRequest) {
			xmlhttp = new XMLHttpRequest();
		}
		else if (window.ActiveXObject)
		{
			try  
			{
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP")
			} catch (e) {
			try  
			{  
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP")
			} catch (E) {
			xmlhttp=false;
			}
		}
		}
		return xmlhttp;
	}
	
	this.Get = function()
	{
		//var xmlHttp = new GetXmlHttp();
		this.Client.open("GET", arguments[0], false);
		this.Client.send(null);
		return this.Client.responseText;
	}
}


function getTCNowCookieValue( CookieKey )
{
	var CookieName = "TCNow";
	var Cookies = new CookieModule();
	var t = Cookies.getStringByCookieAndKey( CookieName, CookieKey )
	return t == "true"; 
}

function setTCNowCookieValue( CookieKey, CookieValue )
{
	var CookieName = "TCNow";
	var Cookies = new CookieModule();
	Cookies.setValueByCookieAndKeyAndValue( CookieName, CookieKey, CookieValue )
}

function getValidAuthId()
{	
	var CookieKey = "IsValidAuthId";
	
	var t = getTCNowCookieValue( CookieKey );
	
	if ( t != true ) 
	{
		var s = prompt("Please enter your PowerCore authentication ID", "");
		
		/*if (s.indexOf("700") == "0" || s.indexOf("701") == "0") {
			alert("ERROR");
			return false;
		}*/
		
		var u = Ajax.Get('http://www.tc-now.com/ajax/IsValidAuthId.asp?authid=' + s );
		if ( u == "true" ) t = true;
	}
	
	if(  t == true ) 
	{
		setTCNowCookieValue ( CookieKey, "true")
	}
	
	return t;
}

function getValidILok()
{	
	var CookieKey = "IsValidILok";
	
	var t = getTCNowCookieValue( CookieKey );
	if ( t != true ) 
	{
		var s = prompt("Please enter your iLok ID", "");
		var u = Ajax.Get('http://www.tc-now.com/ajax/IsValidILok.asp?ilok=' + s );
		if ( u == "true" ) t = true;
	}
	
	if(  t == true ) 
	{
		setTCNowCookieValue ( CookieKey, "true")
	}
	
	return t;
}

function Add( PrdId, Qntt, Msg, BrickId, SubType ){
    switch ( parseInt(SubType,10) )	
    {
        case 3:	// PowerCore plugins
            if(  !getValidAuthId() ) { alert("Invalid PowerCore authentication ID!"); return; }
            break;
        case 5: // TDM plug-ins
            if(  !getValidILok() ) { alert("Invalid iLok ID!"); return; }
            break;
        default:
        	if (!confirm(Msg)) return;
            break;
    }
    document.forms['AddToBasket'+BrickId].PrdId.value=PrdId;
    document.forms['AddToBasket'+BrickId].Qty.value=Qntt;
    document.forms['AddToBasket'+BrickId].submit();
 }
