var testGroup = '';

function getIndex (selectBox, optionValue){
	for(var k=0; k<selectBox.length; k++)
		if (selectBox[k].value == optionValue)
			return k;
}

function ToggleTbody( objTBody , objCheckBox )
{
	if (objCheckBox.checked) 
		showtbody(objTBody);
	else 
		hidetbody(objTBody);
}

function hidetbody ( obj ) {document.getElementById(obj).style.display = "none" ;}
 
function showtbody ( obj ) 
{ 
	if(window.ActiveXObject) // ie is used
		document.getElementById(obj).style.display = 'block';
	else
		document.getElementById(obj).style.display = 'table-row-group';
}

function show_div(div_id) { 
	document.getElementById(div_id).style.visibility = 'visible'; 
} 
		 
function hide_div(div_id) { 
	document.getElementById(div_id).style.visibility = 'hidden'; 
} 

function searchKeyPress(input, searchID){
    if (input.keyCode == 13)
    { 
        input.returnValue=false; 
        input.cancel = true;
		executeSearch(searchID);
	}
}

function executeSearch(searchID){
	redirect('/searchLanding.aspx?searchTerm=' + URLEncode(document.getElementById(searchID).value));
}

function emailKeyPress(input){
    if (input.keyCode == 13)
    { 
        input.returnValue=false; 
        input.cancel = true;
		executeEmailManagement('signup');
	}
}

function executeEmailManagement(action){
	if (action == 'signup')
		redirect('/account.aspx?page=email&action=signup&email=' + URLEncode(document.getElementById("email_emailBox").value));
	else
		redirect('/account.aspx?page=email&action=manage&email=' + URLEncode(document.getElementById("email_emailBox").value));
}

function suggestionSubmit(){
	legacyPost('suggestionArea', '/controls/conAccount/suggestionSubmit.aspx?type=' + document.getElementById("suggestionType").value +  '&text=' + URLEncode(document.getElementById("suggestionText").value) + '&suggestionURL=' + URLEncode(document.getElementById("suggestionURL").value));
}

function redirect(url){
	window.location.href = url;
}

function googleCheckoutClick(){
	s.pageName="Google Checkout" 
	void(s.t()); 
}

function URLEncode( plaintext )
{
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	}

	return encoded;
}


// XML LIBRARY
function stripCDATA(xmlString){
var output = xmlString;
var ar = /<!\[CDATA\[(.*)\]\]>/g.exec(xmlString);
            if (ar != null){
                        output = ar[1];
            }
            return output;
}

function xmlValue(xmlString, xmlField){
var output = '';
var regPattern = '<' + xmlField + '>(.*)</' + xmlField + '>';
var reg = new RegExp(regPattern, "g");
var ar = reg.exec(xmlString);
            if (ar != null){
                        output = ar[1];
            }
            return stripCDATA(output);
}


// AJAX LIBRARY
var ajaxDebug = false;

function stripCDATA(xmlString){
var output = xmlString;
var ar = /<!\[CDATA\[(.*)\]\]>/g.exec(xmlString);
            if (ar != null){
                        output = ar[1];
            }
            return output;
}

function xmlValue(xmlString, xmlField){
var output = '';
var regPattern = '<' + xmlField + '>(.*)</' + xmlField + '>';
var reg = new RegExp(regPattern, "g");
var ar = reg.exec(xmlString);
            if (ar != null){
                        output = ar[1];
            }
            return stripCDATA(output);
}


function Map(){
    var len = 0;
    var keys = new Array();
    var values = new Array();

    this.get = function(key){
        var val = null;
        for(var i=0; i<len; i++){
            if(keys[i] == key){
                val = values[i];
                break;
            }//end if
        }//end for

        return val;
    }//end get()

    this.put = function(key, value){
        keys[len] = key;
        values[len++] = value;
    }//end put()

    this.length = function(){
        return len;
    }//end length()

    this.contains = function(key){
	var con = false;
        for(var i=0; i<len; i++){
            if(keys[i] == key){
                con = true;
                break;
            }//end if
        }//end for

	return con;
    }//end contains()

    this.remove = function(key){
        var keyArr = new Array();
        var valArr = new Array();
        var l = 0;
        for(var i=0; i<len; i++){
            if(keys[i] != key){
                keyArr[l] = keys[i];
                valArr[l++] = values[i];
            }//end if
        }//end for

        keys = keyArr;
        values = valArr;
	len = l;
    }//end remove()        

}//end Map

//AJAX main class
function AJAX(){

    var nameSpace = "http://tempuri.org/";
    var map = new Map();

    var getAJAXIdentity = function(){
        return "AJAX" + (AJAX.indentity++);
    }//end GetAJAXIdentity()
    
    //Overridden toString method.
    this.toString = function(){
        return "AJAX Framework Class";
    }//end toString()
    
    //Method for error handling.
    this.onError = function(error){
        if (ajaxDebug) alert("There was an error. The error description is: \n\n" + error);
    };//end onError()

    //Call a page with a callback function name.
    this.callPage = function(url, callbackFunction){
        var iframe = document.createElement("IFRAME");
        var IE = (navigator.appName.indexOf("Microsoft") >= 0);
        iframe.id = getAJAXIdentity();
        
        map.put(iframe.id, callbackFunction);
        
        iframe.style.display = "none";
        document.body.appendChild(iframe);
        
        if(IE){
            
            if(iframe.addEventListener){
                //Opera
                iframe.addEventListener("load", function(){
                    callbackFunction(document.frames[this.id].document.body.innerHTML);
                    this.removeNode();
                }, false);
            }
            else{
                //IE
                iframe.onreadystatechange = function(){
                    if(this.readyState == "complete"){
                        callbackFunction(document.frames[this.id].document.body.innerHTML);
                        this.removeNode();
                    }//end if
                };
            }//end tc
        }
        else{
			 //Mozilla
            iframe.addEventListener("load", function(){
                callbackFunction(document.getElementById(this.id).contentDocument.body.innerHTML);
                //this.removeNode();
            }, false);
        }//end if
        
        iframe.src = url;
    };//end callPage()
	
    //Call a web service, pass any additional aruments in as "key=value"
    this.callService = function(serviceUrl, soapMethod,parameters){
		var callbackFunction = soapMethod;
        var IE = (navigator.appName.indexOf("Microsoft") >= 0);
        var callServiceError = this.onError;
        
        if(serviceUrl.indexOf("http://") < 0){
			if (serviceUrl.indexOf("https://") < 0)
			{
                serviceUrl = "http://" + serviceUrl;
			}
		}
            //serviceUrl += "?WSDL";
            
            var soapEnvelope = new String();
            soapEnvelope += "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
            soapEnvelope += "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:tns=\"" + nameSpace + "\" xmlns:types=\"" + nameSpace + "/encodedTypes\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">";
            soapEnvelope += "<soap:Body  soap:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">";
            soapEnvelope += "<tns:" + soapMethod + ">"; // FUNCTION TO CALL
			            
			// BUILD THE PARAMETERS AND VALUES FOR THE SOAP REQUEST
            if(arguments.length > 2){
                for (var i = 2; i < arguments.length; i++)
                {
                    var params = arguments[i].split("=");                   
                    soapEnvelope += "<" + params[0] + ">";
                    soapEnvelope += arguments[i].substr(params[0].length+1,arguments[i].length-params[0].length).replace(/&/g, '&amp;');
                    soapEnvelope += "</" + params[0] + ">";
                }//end for
            }//end if

			if (testGroup != '')
			{
                    soapEnvelope += "<abTestGroup>";
                    soapEnvelope += testGroup.replace(/&/g, '&amp;');
                    soapEnvelope += "</abTestGroup>";
			}

			soapEnvelope += "</tns:" + soapMethod + ">";	// FUNCTION TO CALL
			soapEnvelope += "</soap:Body>";
            soapEnvelope += "</soap:Envelope>";
        
			if (ajaxDebug) alert(soapEnvelope);       
        if(IE){    
        	
            var xmlHttp = null;
         	 if(window.XMLHttpRequest)
			 {
			 	if(navigator.appVersion.indexOf("MSIE 7") >= 0) // They are using the new IE 7 and XMLHttpRequest is not fully supported.
					xmlHttp = new ActiveXObject("MSXML2.XMLHTTP");
				else
					xmlHttp = new XMLHttpRequest();
			  }
			  else
			  {
				xmlHttp = new ActiveXObject("MSXML2.XMLHTTP");
			  }
            
            xmlHttp.onreadystatechange = function(){
                if(xmlHttp.readyState == 4){                      
                    var cb = null;
                    if(xmlHttp.callbackFunction){
                        //opera
                        var response = xmlHttp.responseXML.getElementsByTagName(soapMethod + "Result")[0];
                        if(!response)
                            response = xmlHttp.responseXML.getElementsByTagName(soapMethod + "Response")[0];
                        if(!response){
                            callServiceError("WebService does not contain a Result/Response node");
                            return;
                        }//end if
                    }
                    else if(callbackFunction){
                    
                    if (ajaxDebug) alert(xmlHttp.responseText);
                        //IE
                        var responseXml = new ActiveXObject('Microsoft.XMLDOM');
                        responseXml.loadXML(xmlHttp.responseText);
                            
                        var responseNode = responseXml.selectSingleNode("//" + soapMethod + "Response");
                        if(!responseNode)
                            responseNode = responseXml.selectSingleNode("//" + soapMethod + "Result");
                        if(!responseNode)
                            callServiceError("Response/Result node not found.\n\nResponse:\n" + xmlHttp.responseText);
                            
                        var resultNode = responseNode.firstChild;
                        if (resultNode != null){
                            try{
								if (ajaxDebug) alert(resultNode.text.replace(/\'/g,"\\'"));
                                setTimeout('ajax' + callbackFunction + "('" + resultNode.text.replace(/\'/g,"\\'") + "')");
                            }
                            catch(ex){
                                callServiceError(ex);
                            }//end tc
                        }
                        else{
                            try{
                                callbackFunction();
                            }
                            catch(ex){
                                callServiceError(ex);
                            }//end tc
                        }//end if
                    }//end if
                }//end if
            };
       
            xmlHttp.open("POST", serviceUrl, true);			
            xmlHttp.setRequestHeader("Content-Type", "text/xml");
            xmlHttp.setRequestHeader("SOAPAction", nameSpace + "/" + soapMethod);       
            
            try 
			{
					xmlHttp.Send(soapEnvelope);            
			}
            catch(ex)
			{
					serviceCallError(ex);
			}
				}
        else{
              
			var req = new XMLHttpRequest();
			if (req) {
			req.onreadystatechange = function() {
			if (req.readyState == 4 && (req.status == 200 || req.status == 304)) {		

				if(navigator.vendor.indexOf("Apple") >= 0) // I assume they are using safari
				{
					    var responseXml;
						var str = req.responseText;
						var contentType = "text/xml";
						if (window.ActiveXObject) 
						{
							var d = new ActiveXObject("MSXML.DomDocument");
							d.loadXML(str);
							responseXml = d;
						} 
						else if (window.XMLHttpRequest) 
						{
							var req2 = new XMLHttpRequest;
							req2.open("GET", "data:" + (contentType || "application/xml") +
											";charset=utf-8," + encodeURIComponent(str), false);
							  
							if (req2.overrideMimeType) 
							{
								req2.overrideMimeType(contentType);
							}

							req2.send(null);
							responseXml = req2.responseXML;
						}

						req.responseXML.getElementsByTagName(soapMethod + "Result").item(0).normalize();
                        var responseNode = req.responseXML.getElementsByTagName(soapMethod + "Result").item(0).firstChild;
                        if(!responseNode)
                            responseNode = req.responseXML.getElementsByTagName(soapMethod + "Response").item(0).firstChild;
                        if(!responseNode)
                            callServiceError("Response/Result node not found.\n\nResponse:\n" + req.responseText);
                        
                        var resultNode = responseNode;
                        if (resultNode != null)
						{
							setTimeout('ajax' + callbackFunction + "('" + resultNode.nodeValue.replace(/\'/g,"\\'") + "')",0);
                        }
                        else
						{
                            setTimeout(callbackFunction + "('')",0);
                            
                        }//end if
				}
				else
				{
					
                        var responseXml;
                        var domparser = new DOMParser();
                        var xpath = new XPathEvaluator();
                        responseXml = domparser.parseFromString(req.responseText, "text/xml");

						req.responseXML.getElementsByTagName(soapMethod + "Result").item(0).normalize();
                        var responseNode = req.responseXML.getElementsByTagName(soapMethod + "Result").item(0).firstChild;
                        if(!responseNode)
                            responseNode = req.responseXML.getElementsByTagName(soapMethod + "Response").item(0).firstChild;
                        if(!responseNode)
                            callServiceError("Response/Result node not found.\n\nResponse:\n" + req.responseText);
                        
                        var resultNode = responseNode;
                        if (resultNode != null){
	                              setTimeout('ajax' + callbackFunction + "('" + resultNode.nodeValue.replace(/\'/g,"\\'") + "')");                      
                        }
                        else{
	                              setTimeout(callbackFunction + "('')");
                            
                        }//end if	
				}
			}
			};
			
			req.open('POST', serviceUrl);
			req.setRequestHeader("Content-Type", "text/xml");
            req.setRequestHeader("SOAPAction", nameSpace + "/" +  soapMethod);
			req.send(soapEnvelope);
			}
        }//end if      
		
    }//end callService()
	
    //Method for setting the namespace
    this.setNameSpace = function(ns){
        nameSpace = ns;
    }//end setNameSpace()
	
    //Method for returning the namespace
    this.getNameSpace = function(){
        return ns;
    }//end getNameSpace()
	
}//end AJAX()

AJAX.indentity = 0;

function legacyPost(id, url) {
  url = url + '&rnd2=' + Math.random()
  url = url + '&page=review'
  var req = false;
  // For Safari, Firefox, and other non-MS browsers
  if (window.XMLHttpRequest) {
    try {
      req = new XMLHttpRequest();
    } catch (e) {
      req = false;
    }
  } else if (window.ActiveXObject) {
    // For Internet Explorer on Windows
    try {
      req = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        req = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {
        req = false;
      }
    }
  }
 var element = document.getElementById(id);
 if (!element) {
  alert("Bad id " + id +
   "passed to clientSideInclude." +
   "You need a div or span element " +
   "with this id in your page.");
  return;
 }
  if (req) 
  {
	try
	{
		// Synchronous request, wait till we have it all
		req.open('GET', url, false);
		req.send(null);
		element.innerHTML = req.responseText;
	}
	catch(e)
	{
		window.location="https://www.golfballs.com/coCart.aspx"; 
	}
  } 
  else 
  {
    element.innerHTML =
   "Sorry, your browser does not support " +
      "XMLHTTPRequest objects. This page requires " +
      "Internet Explorer 5 or better for Windows, " +
      "or Firefox for any system, or Safari. Other " +
      "compatible browsers may also exist.";
  }
}

function parseFromString (str, contentType) 
{
	if (window.ActiveXObject) 
	{
		var d = new ActiveXObject("MSXML.DomDocument");
		d.loadXML(str);
		return d;
	} 
	else if (window.XMLHttpRequest) 
	{
		var req = new XMLHttpRequest;
		req.open("GET", "data:" + (contentType || "application/xml") +
						";charset=utf-8," + encodeURIComponent(str), false);
		if (req.overrideMimeType) {
			req.overrideMimeType(contentType);
		}
		req.send(null);
		return req.responseXML;
	}
}

function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}
