function xmlhttpPost(strURL,qstr,id) {	
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            updatepage(self.xmlHttpReq.responseText,id);
        }
    }
    self.xmlHttpReq.send(qstr);
	/**/
}
function updatepage(str,id){
	//alert(str);
	if(id == 'divcheckstock'){
		//alert(str);
		if(str == '1'){
			//Goto confirm page
			//alert("Goto confirm page");
			window.location='confirm.php';
		}else{
			alert("Sorry, we don’t have enough stocks for your requested quantity");
			show_stock_notavailable();
		}
	}else if(id == 'addtransaction'){
		if(str == '1'){
			document.sendform.submit();
		}else{
			alert("Sorry, Create transaction fail");
		}
	}else if(id=='addlastaddress'){
		if(str == '1'){
			addtransaction();
		}else if(str == '-1'){
			alert("Local shipping is not available for your address, \n       Please select worldwide shipping.");
			window.location='summary.php?chgship=W';
		}else{
			alert("Sorry, Add delivery address fail");
		}
	}else if(id=='chgship'){
		window.location.reload();
	}else if(id=='selectSize'){
		var div = document.getElementById('listBoxQuantity');
		div.innerHTML = str;
		changeQuantity(1);
	}else{

		var div = document.getElementById(id);
		//alert(div);
		div.innerHTML = str;

		if(id == 'divsummary'){
			//updatebtnship();
			updateBusket_summary();
		}
		if(id == 'divhead'){
			if(document.getElementById('divsummary') != null){
				//alert(document.getElementById('divbag').innerHTML);
				window.location.reload(true);
			}
		}
	}
}
function xmlhttpPostAddBusket(strURL,qstr,id) {	
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            updateBusket(self.xmlHttpReq.responseText,id);
        }
    }
    self.xmlHttpReq.send(qstr);
	/**/
}
function updateBusket_summary(){
	var url = 'api/api.php';
	var qstr = 'cmd=updatebag';
	xmlhttpPost(url,qstr,'divhead');
}
function updateBusket(str,id){
	//alert(str);
	var div = document.getElementById(id);
	//alert(div);
	div.innerHTML = str;
	
	var divbtnaddbusket = document.getElementById('btnaddbag');
	var divbtncheckout = document.getElementById('btncheckout');
	divbtnaddbusket.style.display='none';
	divbtncheckout.style.display='block';
	//Update Busket
	var url = 'api/api.php';
	var qstr = 'cmd=updatebag';
	xmlhttpPost(url,qstr,'divhead');
}
function xmlhttpPostDelBusket(strURL,qstr) {	
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            updateDelBusket(self.xmlHttpReq.responseText);
        }
    }
    self.xmlHttpReq.send(qstr);
	/**/
}
function updateDelBusket(str){
	window.location=str;
}
function getProduct_detail(modelid,colorid){
	//alert("Test "+modelid+" "+colorid);
	var url = 'api/api.php';
	var qstr = 'cmd=pdetail&modelid='+modelid+'&cid='+colorid;
	//alert(qstr);
	xmlhttpPost(url,qstr,'divproductdetail');
}
function logout(){
	//alert("Logout");
	var url = 'api/api.php';
	var qstr = 'cmd=logout';
	//alert(qstr);
	xmlhttpPost(url,qstr,'divhead');
}
function clearbag(){
	//alert("Clear Busket!");
	if(confirm('Clear Busket ?')){
		var url = 'api/api.php';
		var qstr = 'cmd=clearbag';
		xmlhttpPost(url,qstr,'divhead');
	}else{
		return false;
	}
}
function delItem_Busket(id){
	//alert(id);
	var url = 'api/api.php';
	var qstr = 'cmd=delItem&id='+id;
	xmlhttpPostDelBusket(url,qstr);
}
function addBusket(){
	//alert("Add Busket");
	var productId = document.getElementById('hproductid').value;
	var size = document.getElementById('hsize').value;
	var ship = document.getElementById('hship').value;
	if(size == 'N'){
		alert('Please select size');
		return false;
	}
	if(ship == 'N'){
		alert('Please select Shiping');
		return false;
	}
	var url = 'api/api.php';
	var qstr = 'cmd=addbag&pid='+productId;
	var div = document.getElementById('divbag');
	//alert(div);
	div.innerHTML = 'Please wait';
	xmlhttpPostAddBusket(url,qstr,'divbag');
}
function selectSize(size){
	//alert("Select - "+size);
	var url = 'api/api.php';
	var qstr = 'cmd=selectsize&size='+size;
	//alert(qstr);
	xmlhttpPost(url,qstr,'selectSize');
	var hsize = document.getElementById('hsize');
	hsize.value = size;
}
function selectShip(ship){
	var url = 'api/api.php';
	var qstr = 'cmd=selectship&ship='+ship;
	//alert(qstr);
	xmlhttpPost(url,qstr,'chgship');
	//var hship = document.getElementById('hship');
	//hship.value = ship;
}
function changeQuantity(value){
	//alert(value);
	var url = 'api/api.php';
	var qstr = 'cmd=chgquantity&value='+value;
	//alert(qstr);
	xmlhttpPost(url,qstr,'divbag');
}
function selectShip_onSummary(ship){
	//alert("Change ship to :"+ship);
	var url = 'api/api.php';
	var qstr = 'cmd=chgship&ship='+ship;
	//alert(qstr);
	xmlhttpPost(url,qstr,'divsummary');
}
function updatebtnship(){
	var url = 'api/api.php';
	var qstr = 'cmd=upbtnship';
	//alert(qstr);
	xmlhttpPost(url,qstr,'divbtnship');
}
function changequantity_on_summary(index,value){
	//alert(index+' ,'+value);
	var url = 'api/api.php';
	var qstr = 'cmd=chgquantitysummary&id='+index+'&value='+value;
	//alert(qstr);
	xmlhttpPost(url,qstr,'divsummary');
}
function show_stock_notavailable(){
	var url = 'api/api.php';
	var qstr = 'cmd=stocknotavailable';
	//alert(qstr);
	xmlhttpPost(url,qstr,'divsummary');
}
function setsesstion_cnf(value){
	var url = 'api/api.php';
	var qstr = 'cmd=sessioncnf&value='+value;
	//alert(qstr);
	xmlhttp(url,qstr);
}
function setsesstion_pid(value){
	var url = 'api/api.php';
	var qstr = 'cmd=sessionpid&value='+value;
	//alert(qstr);
	xmlhttp(url,qstr);
}
function xmlhttp(strURL,qstr) {	
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            //
        }
    }
    self.xmlHttpReq.send(qstr);
	/**/
}



/**** Connection to swf ****/
function xmlhttpCheckLogin(strURL,qstr) {
	//alert("test");
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            updateswfsession(self.xmlHttpReq.responseText);
        }
    }
    self.xmlHttpReq.send(qstr);
	/**/
}
function xmlhttpCheckLoout(strURL,qstr) {
	//alert("test");
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            updatelogoutsuccess(self.xmlHttpReq.responseText);
        }
    }
    self.xmlHttpReq.send(qstr);
	/**/
}
function updateswfsession(str){
	//alert(str);
	sendToActionScript(str);
}
function updatelogoutsuccess(str){
	sendToJavaScript();
}
function thisMovie(movieName) {
	if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[movieName];
    } else {
		return document[movieName];
	}
}
function sendToActionScript(value) {
	thisMovie("main").sendToActionScript(value);
}
function sendToJavaScript(){
	var url = 'api/api.php';
	var qstr = 'cmd=swfsession';
	xmlhttpCheckLogin(url,qstr);
	//sendToActionScript("ccc");
}
function sendToJavaScriptLogout(){
	var url = 'api/api.php';
	var qstr = 'cmd=logoutswf';
	//alert(qstr);
	xmlhttpCheckLoout(url,qstr);
}
function sendToJavaScriptClearBag(){
	var url = 'api/api.php';
	var qstr = 'cmd=clearbagswf';
	//alert(qstr);
	xmlhttpCheckLoout(url,qstr);
}
function checkstock(){
	//alert("In Check stock");
	var url = 'api/api.php';
	var qstr = 'cmd=checkstock';
	xmlhttpPost(url,qstr,'divcheckstock');
}
function addtransaction(){
	var url = 'api/api.php';
	var qstr = 'cmd=addtransaction';
	xmlhttpPost(url,qstr,'addtransaction');
}

/*Page Confirm*/
function eventTrigger (e) {
    if (! e)
        e = event;
    return e.target || e.srcElement;
}

function radioClick (e) {
    var obj = eventTrigger (e);
    var notify = document.getElementById &&
                    document.getElementById ('h_delivery_type');
    if (notify)
        notify.value = obj.value;

	//alert(notify.value);
    return true;
}
function validateNewAddress()
{
	if (document.getElementById('ld_fname').value == '') {
		document.getElementById('ld_fname').focus();
		alert('First Name is required');
		return false;
	}
	if (document.getElementById('ld_lname').value == '') {
		document.getElementById('ld_lname').focus();
		alert('Surname is required');
		return false;
	}
	if (document.getElementById('ld_address1').value == '') {
		document.getElementById('ld_address1').focus();
		alert('Address Line 1 is required');
		return false;
	}
	/*
	if (document.getElementById('ld_address2').value == '') {
		document.getElementById('ld_address2').focus();
		alert('Address Line 2 is required');
		return false;
	}
	*/
	if (document.getElementById('ld_city').value == '') {
		document.getElementById('ld_city').focus();
		alert('City is required');
		return false;
	}
	if (document.getElementById('ld_state').value == '') {
		document.getElementById('ld_state').focus();
		alert('State/Province/Region is required');
		return false;
	}
	if (document.getElementById('ld_countryid').value == '') {
		alert('Country is required');
		return false;
	}
	if (document.getElementById('ld_zipcode').value == '') {
		document.getElementById('ld_zipcode').focus();
		alert('ZIP/Postal Code is required');
		return false;
	}
	if (document.getElementById('ld_phone').value == '') {
		document.getElementById('ld_phone').focus();
		alert('Phone Number is required');
		return false;
	}
	return true;
}
function add_lastdelivery_address(){
	//Pack data for add last delivery address
	var notify = document.getElementById &&
                    document.getElementById ('h_delivery_type');

	
	if(notify.value == 'new'){
		
		var fname = document.getElementById('ld_fname').value;
		var lname = document.getElementById('ld_lname').value;
		var address1 = document.getElementById('ld_address1').value;
		var address2 = document.getElementById('ld_address2').value;
		var city = document.getElementById('ld_city').value;
		var state = document.getElementById('ld_state').value;
		var countryid = document.getElementById('ld_countryid').value;
		var zipcode = document.getElementById('ld_zipcode').value;
		var phone = document.getElementById('ld_phone').value;
		//alert("xxxx"+countryid);
		
		var url = 'api/api.php';
		
		var data = "fname="+fname+"&lname="+lname+"&address1="+address1+"&address2="+address2+"&city="+city+"&state="+state+"&countryid="+countryid+"&zipcode="+zipcode+"&phone="+phone;
		var qstr = 'cmd=addlastaddress&type=new&'+data;
		//alert(qstr);
		xmlhttpPost(url,qstr,'addlastaddress');
		
	}else{
		var url = 'api/api.php';
		var qstr = 'cmd=addlastaddress&type=account';
		xmlhttpPost(url,qstr,'addlastaddress');
	}
}

function MessageBox(v){
	alert(v);
}