
var debug = false;
/**
Browser Compatability function.
Returns the correct XMLHttpRequest depending on the current browser.
*/
function GetXmlHttp() {	
	var xmlhttp = false;
	if (window.XMLHttpRequest)
	{
		xmlhttp = new XMLHttpRequest();
  }
	else if (window.ActiveXObject)// code for IE
	{
		try 
		{
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) 
		{
			try 
			{
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (E) {
				xmlhttp=false;
			}
		}
	}
	return xmlhttp;
}


/**
<summary>
Gets the response stream from the passed url, and then calls the callbackFuntion passing the response and the div_ids.
</summary>
<param name="url">The url to make the request to get the response data.</param>
<param name="callbackFunction">The function to call after the response has been recieved. the response <b>must</b> always be the first argument to the function.</param>
<param name="params"> (optional) Any other parameters you want to pass to the functions. (Note: only constants/strings/globals can be passed as params, most variables will be out of scope.) </param>
</summary>
<example>
	<code>
PassAjaxResponseToFunction('?getsomehtml=1', 'FunctionToHandleTheResponse', "\'div1\',\'div2\',\'div3\'');

function FunctionToHandleTheResponse(response, d1, d2, d3){
	var data = response.split(';');
	document.getElementById(d1).innerHTML = data[0];
	document.getElementById(d2).innerHTML = data[1];
	document.getElementById(d3).innerHTML = data[2];
}
	</code>
</example>
*/
function PassAjaxResponseToFunction(url, callbackFunction, params)
{		
  var xmlhttp = new GetXmlHttp();
  //now we got the XmlHttpRequest object, send the request.
  if (xmlhttp)
  {
    xmlhttp.onreadystatechange = function () 
                                {
	                                if (xmlhttp && xmlhttp.readyState==4)
	                                {//we got something back..
		                                if (xmlhttp.status==200)
		                                {
			                                var response = xmlhttp.responseText;
			                                var functionToCall = callbackFunction+'(response,'+params+')';
			                                if(debug){
				                                //alert(response);
				                                //alert (functionToCall);
			                                }
			                                eval(functionToCall);
		                                } else if(debug){
			                                document.write(xmlhttp.responseText);
		                                }
	                                }
                                }
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);
  }
}
/**
///<summary>
///Sets the innerHTML property of obj_id with the response from the passed url./
///</summary>
///<param name="url">The url to make the request to get the response data.</param>
///<param name="obj_id">The object or the id of the object to set the innerHTML for.</param>
*/
function SetInnerHTMLFromAjaxResponseNew(qty,id,obj_id,ean)
{  
 validNumericMax(qty);
debugger;
 var url = "Cart.aspx?qty=" + qty + "&id=" + id + "&ean=" + ean;  
 var dtmNow = new Date();
 var strMS = dtmNow.getTime();
 url = url + "&ti=" + strMS;
 var xmlhttp = new GetXmlHttp();
 var strQty = "prd_qty" + id;
   
 var rowID = "rowid" + id;
 var divSubTotal = "divSubTotal" + id;
 var divCartTotal = "divCartTotal";
 var divldbVat7 = "divldbVat7";
 var divldbVat19 = "divldbVat19";
 var divVersandkoseten= "divVersandkoseten";
 var divWarenwart="divWarenwart";
 var divFinalAmount="divFinalAmount";

  //now we got the XmlHttpRequest object, send the request.
  if (xmlhttp)
  {
    xmlhttp.onreadystatechange = function () 
    {
        if (xmlhttp && xmlhttp.readyState==4)
        {//we got something back..
            if (xmlhttp.status==200)
            {
                if(debug)
                {
                    alert(xmlhttp.responseText);
                }
                if(typeof obj_id == 'object')
                {
                alert(xmlhttp.responseText);	
                    obj_id.innerHTML = xmlhttp.responseText;
                } 
                else 
                {
                      //This qunantity value need not be updates as the value is stored in the input variable
                      //document.getElementById(strQty).value = xmlhttp.responseText;
                      var strCartValues = xmlhttp.responseText;
                      var data = strCartValues.split('|');
                     
                     if(data[0]== "NA")
                     {
                         alert("Please choose a lesser quantity !");
                         document.getElementById(strQty).value= data[1];
                     }
                     else
                     {                 
                          if(data[0] == "0,00")
                          {                          
                               var rowToDelete = document.getElementById(rowID); 
                               rowToDelete.parentNode.removeChild(rowToDelete);        
                               document.getElementById(divCartTotal).innerHTML = data[1]+ " " + "\u20AC";;
                               if(document.getElementById(divVersandkoseten) != null)
                               {
                               document.getElementById(divVersandkoseten).innerHTML=data[4]+ " " + "\u20AC";;
                               }
                               if(document.getElementById(divldbVat7) != null)
                               {
	                            document.getElementById(divldbVat7).innerHTML = data[2]+ " " + "\u20AC";;
	                           }
	                           if(document.getElementById(divldbVat19) != null)
                               { 
	                            document.getElementById(divldbVat19).innerHTML = data[3]+ " " + "\u20AC";;
	                           }
	                           if(document.getElementById(divFinalAmount) != null)
                               { 
	                           document.getElementById(divFinalAmount).innerHTML = data[5]+ " " + "\u20AC";;
	                           }
	                      }
                          else
                          {
                              if(document.getElementById(divSubTotal)!=null)
                              {
	                            document.getElementById(divSubTotal).innerHTML =  data[0]+ " " + "\u20AC";
	                            }
	                            if(document.getElementById(divCartTotal)!=null)
	                            {
	                            document.getElementById(divCartTotal).innerHTML = data[1]+ " " + "\u20AC";
	                            }
	                            if(document.getElementById(divldbVat7) != null)
                               { 
	                            document.getElementById(divldbVat7).innerHTML = data[2]+ " " + "\u20AC";
	                           }
	                           if(document.getElementById(divldbVat19) != null)
                               { 
	                            document.getElementById(divldbVat19).innerHTML = data[3]+ " " + "\u20AC";
	                           }  
	                           if(document.getElementById(divVersandkoseten)!=null)
	                            {
                                document.getElementById(divVersandkoseten).innerHTML=data[4]+ " " + "\u20AC";
	                            }
	                           if(document.getElementById(divFinalAmount)!=null)
	                            {
	                            document.getElementById(divFinalAmount).innerHTML =  data[5]+" " + "\u20AC";
	                            }      
            	              	                      
	                            colorFade(rowID,'background','ffffff','d8e6ee');	                
	                            setTimeout("colorFade('"+rowID+"','background','d8e6ee','ffffff')", 2000);
	                      } 
	                  }       
	                SetInnerHTMLFromAjaxResponseonLoad();
                }
            }
            else if(debug)
            {
                document.Write(xmlhttp.responseText);
            }
        }
    }
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);
  }
}

//calling this function from checkquantity() for updating previous value in case of empty
function SetInnerHTMLFromAjaxResponseCart(qty,id,obj_id,ean)
{
 var url = "/partnerManagement/Cart.aspx?qty=" + qty + "&id=" + obj_id + "&ean=" + ean;
 var dtmNow = new Date();
 var strMS = dtmNow.getTime();
 url = url + "&ti=" + strMS;
 var xmlhttp = new GetXmlHttp();
 var strQty = "prd_qty" + id;

  var rowID = "rowid" + id;
 var divSubTotal = "divSubTotal" + obj_id;
 var divCartTotal = "divCartTotal";
 var divldbVat7 = "divldbVat7";
 var divldbVat19 = "divldbVat19";

  //now we got the XmlHttpRequest object, send the request.
  if (xmlhttp)
  {
    xmlhttp.onreadystatechange = function ()
    {
        if (xmlhttp && xmlhttp.readyState==4)
        {//we got something back..
            if (xmlhttp.status==200)
            {
                if(debug){
                    alert(xmlhttp.responseText);
                }
                if(typeof obj_id == 'object'){
                alert(xmlhttp.responseText);
                    obj_id.innerHTML = xmlhttp.responseText;
                }
                else{
                  //document.getElementById(obj_id).innerHTML = xmlhttp.responseText;
                  //This qunantity value need not be updates as the value is stored in the input variable
                  //document.getElementById(strQty).value = xmlhttp.responseText;
                  var strCartValues = xmlhttp.responseText;
                  var data = strCartValues.split('|');

                 if(data[0]== "NA")
                 {
                 alert("Der Artikel steht  nur in begrenzter Anzahl zur Verfügung.");
                 document.getElementById(strQty).value= data[1];
                 }
                 else
                 {

                  if(data[0] == "0,00")
                  {
                  var rowToDelete = document.getElementById(rowID);
                  rowToDelete.parentNode.removeChild(rowToDelete);
                  document.getElementById(divCartTotal).innerHTML = data[1]+ ' &#8364;';
	              if(document.getElementById(divldbVat7) != null)
	                {
	                  document.getElementById(divldbVat7).innerHTML = data[2]+ ' &#8364;';

	                }
	                if(document.getElementById(divldbVat19) != null)
	                {
	                  document.getElementById(divldbVat19).innerHTML = data[3]+ ' &#8364;';

	                }
                  }
                  else
                  {
                     if(document.getElementById(divSubTotal) != null)
	                {
	                document.getElementById(divSubTotal).innerHTML = data[0]+ ' &#8364;';
                 }
	                document.getElementById(divCartTotal).innerHTML = data[1]+ ' &#8364;';
	                if(document.getElementById(divldbVat7) != null)
	                {

	                   document.getElementById(divldbVat7).innerHTML = data[2]+ ' &#8364;';
	                }
	                if(document.getElementById(divldbVat19) != null)
	                {

	                  document.getElementById(divldbVat19).innerHTML = data[3]+ ' &#8364;';
	                }

	                 if(document.getElementById(strQty) != null)
                     {
	                   document.getElementById(strQty).value = data[4];
	                 }

	              }
	              }
	               // SetInnerHTMLFromAjaxResponseonLoad();
                }
            }
            else if(debug)
            {
                document.Write(xmlhttp.responseText);
            }
        }
    }
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);
  }
}

//Calling this function to update the cart (i.e. the cart at the top/body)
function SetInnerHTMLFromAjaxResponse(qty,ean,div_id)
{  
debugger;
    var imgdiv;
    imgdiv = "divIsEnable" + ean;
    var imgdivcontrol;
    var imgpath;
    if(document.getElementById(imgdiv)!=null)
    {
    imgpath  = document.getElementById(imgdiv).innerHTML;
    document.getElementById(imgdiv).innerHTML = "<img src='/images/animatedBar.gif' alt='' />";   
    }
    
    //new code added by Madhusudhan on 05-05-2009 for modal popup
                        ModalPopups.Indicator("idIndicator1",   " ",   "<br /><br /><center>Sie haben das Produkt zum Warenkorb hinzugef&uuml;gt </center><BR>" +    
                         "",    
                        {   
                             width: 350,   
                              height: 100   
                         }   
                        );   
               
                     setTimeout('ModalPopups.Close(\"idIndicator1\");', 2000);   
     //  
                        
    var url = "/partnerManagement/AddToCart.aspx?qty=" + qty + "&ean=" + ean + "&divid=" + div_id;
    var dtmNow = new Date();
    var strMS = dtmNow.getTime();
    url = url + "&ti=" + strMS;
    var xmlhttp = new GetXmlHttp();
    
    if (xmlhttp)
  {
    xmlhttp.onreadystatechange = function () 
    {
        if (xmlhttp && xmlhttp.readyState==4)
        {//we got something back..
            if (xmlhttp.status==200)
            {
                if(debug){
                }
                if(typeof div_id == 'object'){
                    div_id.innerHTML = xmlhttp.responseText;
                } else {
                
                var strCartValues = xmlhttp.responseText;
                var data = strCartValues.split('|');     
                
                if(data[0]== "NA")
                {
                }
                else
                {
                document.getElementById('divArticle').innerHTML = data[0];
                document.getElementById('divSum').innerHTML = data[1]+ ' &#8364;';
                }
                
                  //document.getElementById(div_id).innerHTML = xmlhttp.responseText ;            
                  document.getElementById(imgdiv).innerHTML = imgpath; //" <img src='/images/ich.gif' alt=''  onmouseover=\"this.style.cursor='pointer'\" onclick=\"javascript:SetInnerHTMLFromAjaxResponse('1','" + ean + "','waren');\" />";
//                  hideIt();
                }
            } else if(debug){
                document.Write(xmlhttp.responseText);
            }
        }
                                }
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);
    }
    
}

function SetInnerHTMLFromAjaxResponseAll(qty,ean,div_id,imgdiv)
{  
    var imgdivcontrol;
    if(document.getElementById(imgdiv)!=null)
    {
    document.getElementById(imgdiv).innerHTML = "<img src='/Images/animatedBar.gif' border='0' />";   
    }
    
    //new code added by Madhusudhan on 05-05-2009 for modal popup
                        ModalPopups.Indicator("idIndicator1",   " ",   "<br /><br /><center>Sie haben das Produkt zum Warenkorb hinzugef&uuml;gt </center><BR>" +    
                         "",    
                        {   
                             width: 350,   
                              height: 100   
                         }   
                        );   
               
                     setTimeout('ModalPopups.Close(\"idIndicator1\");', 2000);   
    //
                        
    var url = "/PartnerManagement/AddToCart.aspx?qty=" + qty + "&ean=" + ean + "&divid=" + div_id;
    var dtmNow = new Date();
    var strMS = dtmNow.getTime();
    url = url + "&ti=" + strMS;
    var xmlhttp = new GetXmlHttp();
    
    if (xmlhttp)
  {
    xmlhttp.onreadystatechange = function () 
    {
        if (xmlhttp && xmlhttp.readyState==4)
        {//we got something back..
            if (xmlhttp.status==200)
            {
                if(debug){
                }
                if(typeof div_id == 'object'){
                    div_id.innerHTML = xmlhttp.responseText;
                } else {
                
                var strCartValues = xmlhttp.responseText;
                var data = strCartValues.split('|');     
                
                if(data[0]== "NA")
                {
                }
                else
                {
                document.getElementById('divArticle').innerHTML = data[0];
	            document.getElementById('divSum').innerHTML = data[1]+ ' &#8364;';
                }
                  //document.getElementById(div_id).innerHTML = xmlhttp.responseText ;            
                  document.getElementById(imgdiv).innerHTML = " <img src='/Images/ich.gif' border='0' width='69' height='65' onmouseover=\"this.style.cursor='pointer'\" onclick=\"javascript:SetInnerHTMLFromAjaxResponse('1','" + ean + "','waren');\" />";
//                  hideIt();
                }
            } else if(debug){
                document.Write(xmlhttp.responseText);
            }
        }
                                }
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);
    }
    
}

//Calling this function to update the cart (i.e. the cart at the top/body)
function SetInnerHTMLFromAjaxResponseonLoad()
{  
    var url = "/PartnerManagement/AddToCart.aspx?ean=&divid=";
    var dtmNow = new Date();
    var strMS = dtmNow.getTime();
    url = url + "&ti=" + strMS;
    var xmlhttp = new GetXmlHttp();
    if (xmlhttp)
  {
    xmlhttp.onreadystatechange = function () 
    {
        if (xmlhttp && xmlhttp.readyState==4)
        {
            if (xmlhttp.status==200)
            {
                if(debug){
                }
                if(typeof div_id == 'object'){
                    div_id.innerHTML = xmlhttp.responseText;
                } else {           
                   var strCartValues = xmlhttp.responseText;
                var data = strCartValues.split('|');     
                
                   if(document.getElementById('divArticle') != null)
                   { 
	                document.getElementById('divArticle').innerHTML = data[0];
	               }
	               if(document.getElementById('divSum') != null)
                   { 
	                document.getElementById('divSum').innerHTML = data[1]+ ' &#8364;';
	               }
                }
            } else if(debug){
                document.Write(xmlhttp.responseText);
            }
        }
                                }
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);
    }
    
}
//Calling this function to move to Manage Cart.

function gotoManageCart()
{
window.location = "http://www.booksalltime.de/PartnerManagement/ManageCart.aspx";
}

//function to accept only Numeric characters  ** by suman 11-04-07
//i.e. other that numeric characters we schould raise an alert if entered
function validNumericMax(qty) {
	var valid = "0123456789"
	var ok = "yes";
	var number = qty;
	
	//Assumption: Maximum number of articles allowed is 999
		
	var temp1;
	var temp2;
	var temp3;
		temp1 = "" + number.substring(0, 1);
		temp2 = "" + number.substring(1, 2);
		temp3 = "" + number.substring(2, 3);
		if (valid.indexOf(temp1) == "-1")
		{
		 ok = "no";	
		}		
		if (valid.indexOf(temp2) == "-1")
		{
		 ok = "no";	
		}		
		if (valid.indexOf(temp3) == "-1")
		{
		 ok = "no";	
		}	
	if (ok == "no") 
	{
		//alert("Invalid entry!  Only Numeric Characters are accepted!");
		document.getElementById('divError').style.display='block';
        document.getElementById('lblMsg').innerText='Invalid entry!  Only Numeric Characters are accepted!';
        document.getElementById('lblMsg').textContent='Invalid entry!  Only Numeric Characters are accepted!';
		return false;
	}
	else
	{
	return true;
	}
	
}


// main function to process the fade request //
function colorFade(id,element,start,end,steps,speed) {
  var startrgb,endrgb,er,eg,eb,step,rint,gint,bint,step;
  var target = document.getElementById(id);
  steps = steps || 20;
  speed = speed || 20;
  clearInterval(target.timer);
  endrgb = colorConv(end);
  er = endrgb[0];
  eg = endrgb[1];
  eb = endrgb[2];
  if(!target.r) {
    startrgb = colorConv(start);
    r = startrgb[0];
    g = startrgb[1];
    b = startrgb[2];
    target.r = r;
    target.g = g;
    target.b = b;
  }
  rint = Math.round(Math.abs(target.r-er)/steps);
  gint = Math.round(Math.abs(target.g-eg)/steps);
  bint = Math.round(Math.abs(target.b-eb)/steps);
  if(rint == 0) { rint = 1 }
  if(gint == 0) { gint = 1 }
  if(bint == 0) { bint = 1 }
  target.step = 1;
  target.timer = setInterval( function() { animateColor(id,element,steps,er,eg,eb,rint,gint,bint) }, speed);
}

// incrementally close the gap between the two colors //
function animateColor(id,element,steps,er,eg,eb,rint,gint,bint) {
  var target = document.getElementById(id);
  var color;
  if(target.step <= steps) {
    var r = target.r;
    var g = target.g;
    var b = target.b;
    if(r >= er) {
      r = r - rint;
    } else {
      r = parseInt(r) + parseInt(rint);
    }
    if(g >= eg) {
      g = g - gint;
    } else {
      g = parseInt(g) + parseInt(gint);
    }
    if(b >= eb) {
      b = b - bint;
    } else {
      b = parseInt(b) + parseInt(bint);
    }
    color = 'rgb(' + r + ',' + g + ',' + b + ')';
    if(element == 'background') {
      target.style.backgroundColor = color;
    } else if(element == 'border') {
      target.style.borderColor = color;
    } else {
      target.style.color = color;
    }
    target.r = r;
    target.g = g;
    target.b = b;
    target.step = target.step + 1;
  } else {
    clearInterval(target.timer);
    color = 'rgb(' + er + ',' + eg + ',' + eb + ')';
    if(element == 'background') {
      target.style.backgroundColor = color;
    } else if(element == 'border') {
      target.style.borderColor = color;
    } else {
      target.style.color = color;
    }
  }
}

// convert the color to rgb from hex //
function colorConv(color) {
  var rgb = [parseInt(color.substring(0,2),16), 
    parseInt(color.substring(2,4),16), 
    parseInt(color.substring(4,6),16)];
  return rgb;
}


var y1 = 20;   // change the # on the left to adjuct the Y co-ordinate
(document.getElementById) ? dom = true : dom = false;

function hideIt() {
  if (dom) {document.getElementById("layer1").style.visibility='hidden';}
}

function showIt() {
  if (dom) {document.getElementById("layer1").style.visibility='visible';}
}

function placeIt() {
  if (dom && !document.all) {document.getElementById("layer1").style.top = window.pageYOffset + (window.innerHeight - (window.innerHeight-y1)) + "px";}
  if (document.all) {document.all["layer1"].style.top = document.documentElement.scrollTop + (document.documentElement.clientHeight - (document.documentElement.clientHeight-y1)) + "px";}
  window.setTimeout("placeIt()", 10); }
