var http; var target_element_id; var processing_image_tag; var show_processing_image; var actionName = ""; target_element_id = ""; var processing_image_path = siteUrl + "/images/ajax-loader.gif"; show_processing_image = 1; var aVersions = ["MSXML2.XMLHttp.5.0", "MSXML2.XMLHttp.4.0", "MSXML2.XMLHttp.3.0", "MSXML2.XMLHttp", "Microsoft.XMLHttp"]; function createRequestObject() { var request_o; var browser = navigator.appName; if(browser == "Microsoft Internet Explorer") { for(var i = 0;i < aVersions.length;i++) { try { request_o = new ActiveXObject(aVersions[i]) }catch(err) { } } }else { try { request_o = new XMLHttpRequest }catch(err) { } } return request_o } function getData(method, target_file, parameters, target_element_id_param, myActionName, show_processing_image_param, process_image_url) { actionName = myActionName; try { http = createRequestObject() }catch(err) { txt = "Unable create request object!\n\n"; txt += "Error description: " + err.description + "\n\n"; alert(txt) } target_element_id = target_element_id_param; show_processing_image = show_processing_image_param; processing_image_path = process_image_url; if(method == "POST") { try { http.open("POST", target_file, true) }catch(err) { txt = "Unable to open the connection!\n\n"; txt += "Error description: " + err.description + "\n\n"; alert(txt) } http.onreadystatechange = handle_response; try { http.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); http.setRequestHeader("Content-length", parameters.length); http.setRequestHeader("Connection", "close") }catch(err) { txt = "Unable to set the request header!\n\n"; txt += "Error description: " + err.description + "\n\n"; alert(txt) } try { http.send(parameters) }catch(err) { txt = "Unable to set the send the request\n\n"; txt += "Error description: " + err.description + "\n\n"; alert(txt) } } if(method == "GET") { target_url = target_file + "?" + parameters; try { http.open("get", target_url) }catch(err) { txt = "Unable to open the connection!\n\n"; txt += "Error description: " + err.description + "\n\n"; alert(txt) } http.onreadystatechange = handle_response; try { http.send(null) }catch(err) { txt = "Unable to set the send the request\n\n"; txt += "Error description: " + err.description + "\n\n"; alert(txt) } } } function handle_response() { try { var readystate = http.readyState }catch(err) { txt = "Unable to get the ready state !\n\n"; txt += "Error description: " + err.description + "\n\n"; alert(txt) } if(readystate < 4) { if(processing_image_path != "" && show_processing_image == 1) { var response = "<img src='" + processing_image_path + "' />"; show_response(response) } } if(readystate == 4) { req_status = http.status; switch(req_status) { case 200: switch(actionName) { case "add_to_cart": var response = http.responseText; if(response.substring(0, 6) == "noroot") { alert("You can add related product after adding main product."); response = response.substring(6) }else { if(response.substring(0, 6) == "errpri") { alert("Sorry, Please try again."); response = response.substring(6) }else { if(response.substring(0, 6) == "errqty") { alert("Sorry, You can order maximum 10 products or avaliable instore Quantity."); response = response.substring(6) }else { alert("Product is added successfully in the cart.") } } } show_response(response); if(typeof myCart != "undefined") { if(confirm("Your cart has been modified. \nClick on 'OK' button to refresh your cart.")) { document.location = siteUrl+"/my_cart/" } } break; case "buy_it_now": var response = http.responseText; if(response.substring(0, 6) == "errqty") { alert("Sorry, You can order maximum 10 products or avaliable instore Quantity."); response = response.substring(6); show_response(response) }else { document.location = siteUrl+"/my_cart/" } break } break; break; case 400: alert("Bad Request"); break; case 401: alert("Unauthorised Request"); break; case 403: alert("Forbidden Request"); break; case 404: alert("File Not Found"); break; case 500: alert("Internal Server Error"); break; default: break } } } function show_response(response) { if(document.getElementById(target_element_id)) { document.getElementById(target_element_id).innerHTML = response } } ;
