/*
* Copyright 2008 - 2009 The Couponier Pro - By Vincent Creazzo 
* Copyright 2008 - 2009 coupons.js script - By Vincent Creazzo 
* Website: http://www.spads.com Email: vcreazzo@spads.com
* This Copyright Notice MUST stay intact for legal use, all rights reserved.
*/

var sURL = "";
var sACT = "";
var sRTN = "";
var sPST = "";

// use this method to post onload
function postaction(sURL, sACT, sRTN){
// You can edit or increase to suit

if (sURL == 1) {
// default postaction # 1 if you know what you want done onload

sURL = "/cgi-bin/cp/cp.pl";   // external script url.
sACT = "&action=view_cats";  // name and value pairs to pass to the external script.
sRTN = "result";            // location of where you want the results to be placed on the page.
sPST = "1";

var self = this;
    self.httpPost(sURL, sACT, sRTN);

} else if (sURL == 2) {
// default postaction # 2 if you know what you want done onload

sURL="/cgi-bin/cp/cp_mail.pl";  // external html page name and location.
sACT="&action=load";           // name and value pairs to pass to the external script.
sRTN="SecureIt";                // location of where you want the results to be placed on the page.
sPST = "1";

var self = this;
    self.httpPost(sURL, sACT, sRTN);

} else if (sURL == 3) {
// default postaction # 3 if you know what page you want loaded onload

sURL="/cgi-bin/cp/cp.pl";     // external html page name and location.
sACT="&action=get_cities";    // No need to pass any var's, we are just getting the page
sRTN="search1";   // location of where you want the results to be placed on the page.

var self = this;
    self.httpPost(sURL, sACT, sRTN);

} else if (sURL == "") {
   // warn that no url was passed to the script

  alert("Problem Requesting Server Data, No URL was passed?");
}

var self = this;
    self.httpPost(sURL, sACT, sRTN);
}
// using the POST method
function httpPost(strURL, strACT, strRTN) {
    var xmlHttpReq = false;
    var self = this;

    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
       try {
            self.xmlHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
       } catch (e) {
         try {
            self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (E) {
                xmlHttpReq = false;
		// There is an error creating the object, old browser?
		alert(E.description);
           }
       }
     }

    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) {
           if (self.xmlHttpReq.status == 200) {
              // ok the server responded.
              updatepage(self.xmlHttpReq.responseText, strRTN);
           } else {
              alert("Problem Requesting Server Data, Server Status: " + self.xmlHttpReq.status + " : " + self.xmlHttpReq.statusText + "! Check Program file!");
             }
        }
    }

   if (strACT == "f1") {
    self.xmlHttpReq.send(getquerystring1());
   }
   else if (strACT == "f2") {
    self.xmlHttpReq.send(getquerystring2());
   }
   else if (strACT == "f3") {
    self.xmlHttpReq.send(getquerystring3());
   }
   else if (strACT == "f4") {
    self.xmlHttpReq.send(getquerystring4());
   }
   else {
    self.xmlHttpReq.send(strACT);
   }
}
// using the xml POST method
function xmlhttpPost(strURL, strACT, strRTN) {
    var xmlHttpReq = false;
    var self = this;

    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
       try {
            self.xmlHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
       } catch (e) {
         try {
            self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (E) {
                xmlHttpReq = false;
		// There is an error creating the object, old browser?
		alert(E.description);
           }
       }
     }

    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) {
           if (self.xmlHttpReq.status == 200) {
              // ok the server responded.
              updatepage(self.xmlHttpReq.responseText, strRTN);
           } else {
              alert("Problem Requesting Server Data, Server Status: " + self.xmlHttpReq.status + " : " + self.xmlHttpReq.statusText + "! Check Program file!");
             }
        }
    }
    // if the strACT (name and value pairs) is empty, get them from the form
    if (strACT == "") { 
             self.xmlHttpReq.send(getquerystring());
    } else {
             self.xmlHttpReq.send(strACT);
      } 
}
// using the GET method
function httpGet(strURL, strACT, strRTN) {
    var xmlHttpReq = false;
    var self = this;

    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
       try {
            self.xmlHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
       } catch (e) {
         try {
            self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (E) {
                xmlHttpReq = false;
		// There is an error creating the object, old browser?
		alert(E.description);
           }
       }
     }

    self.xmlHttpReq.open('GET', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'text/html');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
           if (self.xmlHttpReq.status == 200) {
              // ok the server responded.
              updatepage(self.xmlHttpReq.responseText, strRTN);
           } else {
              alert("Problem Requesting Server Data, Server Status: " + self.xmlHttpReq.status + " : " + self.xmlHttpReq.statusText + "! Check Program file!");
             }
        }
    }
    self.xmlHttpReq.send(strACT);
}
// update the page
function updatepage(str, strRTN){

document.getElementById(strRTN).innerHTML = str;

if (sPST == 1) { 
sPST = "";
var self = this;
    self.httpPost("/cgi-bin/cp/cp.pl", "&action=get_cities", "search1");
}

}
// Get values from a form to pass along, form name=f1
function getquerystring1() {
    var form = document.forms['f1'];
    var action = form.action.value;
    var city = form.city.value;
    var zip = form.zip.value;

    qstr = '&action='+action+'&city='+city+'&zip='+zip+'';  // NOTE: use this when var is a single word
    return qstr;
}
// Get values from a form to pass along, form name=f2
function getquerystring2() {
    var form = document.forms['f2'];
    var action = form.action.value;
    var username = form.username.value;
    var national = form.national.value;

    qstr = '&action='+action+'&username='+username+'&national='+national+'';  // NOTE: use this when var is a single word
    return qstr;
}

// Get values from a form to pass along, form name=f2
function getquerystring3() {
    var form = document.forms['f3'];
    var action = form.action.value;
    var city = form.city.value;
    var zip = form.zip.value;
    var display = form.display.value;

    qstr = '&action='+action+'&city='+city+'&zip='+zip+'&display='+display+'&begin=1&end=5';  // NOTE: use this when var is a single word
    return qstr;
}
function getquerystring4() {
    var form     = document.forms['f3'];
    var category = form.category.value;

    qstr = '&action=get_subs&category='+category+'';  // NOTE: no '?' before querystring
    return qstr;
}
//document.onload=postaction('3');
