// JavaScript Document
var http;
var idToDisplayMsg;
var urlForAjax;
var idToDisplayMsgVoucher;



// HTTP REQUEST Function Use this to call a HTTP request
function getHTTPObject() {
	var xmlhttp=null;
 	if(window.XMLHttpRequest){
    	xmlhttp = new XMLHttpRequest();
  	}
  	else if (window.ActiveXObject){
    	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    	if (!xmlhttp){
        	xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
    	}
    }
  	return xmlhttp;
}

// Call this function on state change
function stateChanged(){
		var docEle=eval("document.getElementById('"+idToDisplayMsg+"')");	
		if( http.readyState == 4){
			contentArray=http.responseText.split("|");
			document.getElementById('withvat').value=contentArray[2];
			document.getElementById('voucherId').value=contentArray[3];
			document.getElementById('voucherAmount').value=contentArray[4];
			document.getElementById('voucherType').value=contentArray[5];
			document.getElementById('voucher_code').value=contentArray[6];
			
			if(contentArray[1]=="1")
			{
				document.getElementById('hiddenDiv').style.display="block";
			}
			if(contentArray[6]!='0' || contentArray[6]!='1')
			{
				document.getElementById(idToDisplayMsg).style.background="#FFFFCC";
				
				
			}
			if(contentArray[7]=='refresh')
			{
				document.getElementById(idToDisplayMsg).style.background="#FFFFFF";
				
			}
			
			docEle.innerHTML=contentArray[0];
			
			
			//document.getElementById('hiddenDiv').style.display="block";
			var inner = document.getElementById(idToDisplayMsg).innerHTML;			 
		}
		else
		{
			document.getElementById(idToDisplayMsg).innerHTML = "<img src='images/loading_animation.gif />'";		 
		}
}
// end State Changed








function ajaxCall(productId,divId){
	alert('aaa');
	

	var xmlhttp=false; //Clear our fetching variable
	try {
			xmlhttp = new ActiveXObject('Msxml2.XMLHTTP'); //Try the first kind of active x object.
	} catch (e) {
			try {
					xmlhttp = new
					ActiveXObject('Microsoft.XMLHTTP'); //Try the second kind of active x object
		} catch (E) {
			xmlhttp = false;
					}
	}
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
			xmlhttp = new XMLHttpRequest(); //If we were able to get a working active x object, start an XMLHttpRequest
	}		
//var type=document.getElementById(txttype).value;
	var file = 'ajaxpage.php?productId='+productId+'&divId='+divId;
	//This is the path to the file we just finished making *
    //alert(file);
	xmlhttp.open('GET', file, true); //Open the file through GET, and add the page we want to retrieve as a GET variable **
    xmlhttp.onreadystatechange=function() {
		//alert(xmlhttp.readyState);
        if (xmlhttp.readyState==4) { //Check if it is ready to recieve data
                var content = xmlhttp.responseText; //The content data which has been retrieved ***
                if( content ){ 
				contentArray=content.split("|");
				
					$("#2").click(function () {  
					   $("#test1").fadeOut(500);
					   $("#test2").fadeIn(500);  
					 }); 
				
					document.getElementById('detail').innerHTML = contentArray[0];
					 
					//document.getElementById('quantity').value='0';
					//document.getElementById('cost').value=contentArray[2];
					//document.getElementById('minimum_pages').value=contentArray[1];
					//document.getElementById('load1').innerHTML = "";
					return;
                }
        }
		else{
				//document.getElementById(txtsubcat).innerHTML = "<img src='images/loading_animation.gif />'";
			
			}
		
        }
		
        xmlhttp.send(null) //Nullify the XMLHttpRequest
return;
}

function productDetails(productId,divId){	
	
	
	var xmlhttp=false; //Clear our fetching variable
	try {
			xmlhttp = new ActiveXObject('Msxml2.XMLHTTP'); //Try the first kind of active x object.
	} catch (e) {
			try {
					xmlhttp = new
					ActiveXObject('Microsoft.XMLHTTP'); //Try the second kind of active x object
		} catch (E) {
			xmlhttp = false;
					}
	}
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
			xmlhttp = new XMLHttpRequest(); //If we were able to get a working active x object, start an XMLHttpRequest
	}		
//var type=document.getElementById(txttype).value;
	var file = 'productDetail.php?productId='+productId+'&divId='+divId;
	//This is the path to the file we just finished making *
    //alert(file);
	xmlhttp.open('GET', file, true); //Open the file through GET, and add the page we want to retrieve as a GET variable **
    xmlhttp.onreadystatechange=function() {
		//alert(xmlhttp.readyState);
        if (xmlhttp.readyState==4) { //Check if it is ready to recieve data
                var content = xmlhttp.responseText; //The content data which has been retrieved ***
                if( content ){ 
				contentArray=content.split("|");		
					//alert(divId);
					
					document.getElementById(divId).innerHTML = contentArray[0];
					 
					//document.getElementById('quantity').value='0';
					//document.getElementById('cost').value=contentArray[2];
					//document.getElementById('minimum_pages').value=contentArray[1];
					//document.getElementById('load1').innerHTML = "";
					return;
                }
        }
		else{
				document.getElementById(divId).innerHTML = "<img src='images/loading_animation.gif'/>";
			
			}
		
        }
		
        xmlhttp.send(null) //Nullify the XMLHttpRequest
return;
}

function changeTotal(productsId,attrOptId, attrOptVal, price ){	
	
	var xmlhttp=false; //Clear our fetching variable
	try {
			xmlhttp = new ActiveXObject('Msxml2.XMLHTTP'); //Try the first kind of active x object.
	} catch (e) {
			try {
					xmlhttp = new
					ActiveXObject('Microsoft.XMLHTTP'); //Try the second kind of active x object
		} catch (E) {
			xmlhttp = false;
					}
	}
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
			xmlhttp = new XMLHttpRequest(); //If we were able to get a working active x object, start an XMLHttpRequest
	}		
//var type=document.getElementById(txttype).value;
	var file = 'change_total.php?attrOptId='+attrOptId+'&attrOptVal='+attrOptVal+'&price='+price+'&productsId='+productsId;
	//This is the path to the file we just finished making *
    //alert(file);
	xmlhttp.open('GET', file, true); //Open the file through GET, and add the page we want to retrieve as a GET variable **
    xmlhttp.onreadystatechange=function() {
		//alert(xmlhttp.readyState);
        if (xmlhttp.readyState==4) { //Check if it is ready to recieve data
                var content = xmlhttp.responseText; //The content data which has been retrieved ***
                if( content ){ 
				contentArray=content.split("|");					
					
					document.getElementById('totalId').innerHTML = contentArray[1];
					document.getElementById('overall_price').value=contentArray[0];
					 
					//document.getElementById('quantity').value='0';
					//document.getElementById('cost').value=contentArray[2];
					//document.getElementById('minimum_pages').value=contentArray[1];
					//document.getElementById('load1').innerHTML = "";
					return;
                }
        }
		else{
				//document.getElementById(divId).innerHTML = "<img src='images/loading_animation.gif />'";
			
			}
		
        }
		
        xmlhttp.send(null) //Nullify the XMLHttpRequest
return;
}

function getTotal(radioId, noData, indPrice, overallPrice,check){	


/*	
alert(radioId);
alert(noData);
alert(indPrice);
alert(overallPrice);
	

	var xmlhttp=false; //Clear our fetching variable
	try {
			xmlhttp = new ActiveXObject('Msxml2.XMLHTTP'); //Try the first kind of active x object.
	} catch (e) {
			try {
					xmlhttp = new
					ActiveXObject('Microsoft.XMLHTTP'); //Try the second kind of active x object
		} catch (E) {
			xmlhttp = false;
					}
	}
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
			xmlhttp = new XMLHttpRequest(); //If we were able to get a working active x object, start an XMLHttpRequest
	}*/		
//var type=document.getElementById(txttype).value;
	
		var requiredId=0;
		
		for(var r=1; r<=noData; r ++){
			
			//alert(document.getElementById('value'+r).value);
			requiredId=parseFloat(requiredId)+parseFloat(document.getElementById('value'+r).value);
			//overallPrice + = document.getElementById(requiredId).value;
		
		}
		
		document.getElementById('totalId').innerHTML = format_number(parseFloat(requiredId)+parseFloat(overallPrice),2);
	
	
		
	//document.getElementById('overall_price').value=contentArray[0];
	/*var file = 'get_total.php?requiredId='+requiredId+'&noData='+noData+'&indPrice='+indPrice+'&overallPrice='+overallPrice;
	//This is the path to the file we just finished making *
    //alert(file);
	xmlhttp.open('GET', file, true); //Open the file through GET, and add the page we want to retrieve as a GET variable **
    xmlhttp.onreadystatechange=function() {
		//alert(xmlhttp.readyState);
        if (xmlhttp.readyState==4) { //Check if it is ready to recieve data
                var content = xmlhttp.responseText; //The content data which has been retrieved ***
                if( content ){ 
				contentArray=content.split("|");					
					
					document.getElementById('totalId').innerHTML = contentArray[0];
					document.getElementById('overall_price').value=contentArray[0];
					 
					//document.getElementById('quantity').value='0';
					//document.getElementById('cost').value=contentArray[2];
					//document.getElementById('minimum_pages').value=contentArray[1];
					//document.getElementById('load1').innerHTML = "";
					return;
                }
        }
		else{
				//document.getElementById(divId).innerHTML = "<img src='images/loading_animation.gif />'";
			
			}
		
        }
		
        xmlhttp.send(null) //Nullify the XMLHttpRequest*/
return;
}

function addCommas(nStr)
{
nStr += '';
x = nStr.split('.');
x1 = x[0];
x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
return x1 + x2;
}

function format_number(pnumber,decimals){
if (isNaN(pnumber)) { return 0};
if (pnumber=='') { return 0};

var snum = new String(pnumber);
var sec = snum.split('.');
var whole = parseFloat(sec[0]);
var result = '';

if(sec.length > 1){
var dec = new String(sec[1]);
dec = String(parseFloat(sec[1])/Math.pow(10,(dec.length - decimals)));
dec = String(whole + Math.round(parseFloat(dec))/Math.pow(10,decimals));
var dot = dec.indexOf('.');
if(dot == -1){
dec += '.';
dot = dec.indexOf('.');
}
while(dec.length <= dot + decimals) { dec += '0'; }
result = dec;
} else{
var dot;
var dec = new String(whole);
dec += '.';
dot = dec.indexOf('.');
while(dec.length <= dot + decimals) { dec += '0'; }
result = dec;
}
//return result;
var final_result=addCommas(result);
return final_result;
}

function getModel(funcName,txttype,txtsubcat){

	var xmlhttp=false; //Clear our fetching variable
	try {
			xmlhttp = new ActiveXObject('Msxml2.XMLHTTP'); //Try the first kind of active x object.
	} catch (e) {
			try {
					xmlhttp = new
					ActiveXObject('Microsoft.XMLHTTP'); //Try the second kind of active x object
		} catch (E) {
			xmlhttp = false;
					}
	}
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
			xmlhttp = new XMLHttpRequest(); //If we were able to get a working active x object, start an XMLHttpRequest
	}		
var type=document.getElementById(txttype).value;
	var file = 'ajaxPage.php?funcName='+funcName+'&type='+type;
	//This is the path to the file we just finished making *
    //alert(file);
	xmlhttp.open('GET', file, true); //Open the file through GET, and add the page we want to retrieve as a GET variable **
    xmlhttp.onreadystatechange=function() {
		//alert(xmlhttp.readyState);
        if (xmlhttp.readyState==4) { //Check if it is ready to recieve data
                var content = xmlhttp.responseText; //The content data which has been retrieved ***
                if( content ){ 
				contentArray=content.split("|");
					
					document.getElementById(txtsubcat).innerHTML = contentArray[0];
					//document.getElementById('quantity').value='0';
					//document.getElementById('cost').value=contentArray[2];
					//document.getElementById('minimum_pages').value=contentArray[1];
					//document.getElementById('load1').innerHTML = "";
					return;
                }
        }
		else{
				document.getElementById(txtsubcat).innerHTML = "Loading...";
			
			}
		
        }
		
        xmlhttp.send(null) //Nullify the XMLHttpRequest
return;
}

