function GetXmlHttpObject() {
var xmlHttp = null;

try {
    // Firefox, Opera 8.0+, Safari
    xmlHttp = new XMLHttpRequest();
}
catch (e) {
    // Internet Explorer
    try {
        xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e) {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
}

return xmlHttp;
}

function submitPriceBeaterForm(priceOption, prodURL, prodName) {
    var xmlHttp = GetXmlHttpObject();
	
    if (xmlHttp == null) {
        alert("Your browser does not support AJAX!");
        return;
    }
    
    var name = document.getElementById('pbName').value;
    var email = document.getElementById('email').value;
    var telephone = document.getElementById('telephone').value
    var priceFound = document.getElementById('priceFound').value;
    var where = document.getElementById('where').value;
    var otherInfo = document.getElementById('otherInformation').value;
	
    var url = "/submitPriceBeaterForm.aspx?name=" + name + "&email=" + email + "&telephone=" + telephone + "&priceOption=" + priceOption + "&prodURL=" + prodURL + "&prodName=" + prodName + "&priceFound=" + priceFound + "&where=" + where + "&otherInfo=" + otherInfo;

    xmlHttp.onreadystatechange= function() { submitPriceBeaterFormFinished(xmlHttp); }

    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
}

function submitPriceBeaterFormFinished(xmlHttp) {
    if (xmlHttp.readyState == 4) {
        var rnObj = xmlHttp.responseText;
        if (rnObj == "ERROR")
        {
            document.getElementById('PricePromiseDivContent').innerHTML = "Sorry, there was an error with your request, please try again later";
        }
        else
        {
            document.getElementById('PricePromiseDivContent').innerHTML = document.getElementById('priceBeaterConfirm').innerHTML;
        }
    }
} 

function submitQuestionForm(prodID) {
    var xmlHttp = GetXmlHttpObject();
	
    if (xmlHttp == null) {
        alert("Your browser does not support AJAX!");
        return;
    }
    
    var email = document.getElementById('qEmail').value;
    var name = document.getElementById('qName').value;
    var telephone = document.getElementById('qTelephone').value;
    var question = document.getElementById('qQuestion').value;
	
	if ((email == "")&&(telephone == "")) {
		alert("You must provide either an email address or a telephone number!");
		return;
	}
	
    var url = "/submitQuestionForm.aspx?prodID=" + prodID + "&email=" + email + "&name=" + name + "&telephone=" + telephone + "&question=" + question;

    xmlHttp.onreadystatechange= function() { submitQuestionFormFinished(xmlHttp); }

    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
}

function submitQuestionFormFinished(xmlHttp) {
    if (xmlHttp.readyState == 4) {
        var rnObj = xmlHttp.responseText;
        if (rnObj == "ERROR")
        {
            document.getElementById('AskQuestionDivContent').innerHTML = "Sorry, there was an error with your request, please try again later";
        }
        else 
        {
            document.getElementById('AskQuestionDivContent').innerHTML = document.getElementById('questionConfirm').innerHTML;
        }
    }
}    

function submitCallbackForm() {
    var xmlHttp = GetXmlHttpObject();
	
    if (xmlHttp == null) {
        alert("Your browser does not support AJAX!");
        return;
    }
    
    //check the telephone number first... no point letting them ask for a callback if there's no number :-p
    var strTelephone = document.getElementById('cbTelephone').value;
    if (strTelephone == "")
    {
        alert("Please enter a telephone number");
        return;
    }
    var strName = document.getElementById('cbName').value;
    var strEmail = document.getElementById('cbEmail').value;
    var strProductID = document.getElementById('cbProdID').value;
    var requestReason = document.getElementById('RingbackReason').options[document.getElementById('RingbackReason').selectedIndex].value;
    var callbackTime = "";
    if (document.getElementById('CallbackTime') != null)
    {
        callbackTime = document.getElementById('CallbackTime').options[document.getElementById('CallbackTime').selectedIndex].value;
    }
    var storeID = getStoreID();
    
    var url = "/submitCallbackForm.aspx?name=" + strName + "&email=" + strEmail + "&telephone=" + strTelephone + "&productID=" + strProductID + "&reason=" + requestReason + "&time=" + callbackTime + "&storeID=" + storeID;

    xmlHttp.onreadystatechange= function() { submitCallbackFormFinished(xmlHttp); }

    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
}

function submitCallbackFormFinished(xmlHttp) {
    if (xmlHttp.readyState == 4) {
        var rnObj = xmlHttp.responseText;
        if (rnObj == "SUCCESSFUL")
        {
            document.getElementById('CallbackDivContent').innerHTML = document.getElementById('callbackConfirm').innerHTML;
        }
        else 
        {
            document.getElementById('CallbackDivContent').innerHTML = "Sorry, there was an error with your request, please try again later";
        }
    }
}

function updateReviewWeighting(reviewID, userVote, reviewBoxID) {
	var xmlHttp = GetXmlHttpObject();
	
	if (xmlHttp == null) {
		alert("Your browser does not support AJAX!");
		return;
	}
	
	var url = "/updateReviewWeighting.aspx?reviewID=" + reviewID + "&userVote=" + userVote + "&date=" + (new Date());

	xmlHttp.onreadystatechange= function() { updateReviewFinished(xmlHttp, reviewBoxID); }

	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}

function updateReviewFinished(xmlHttp, reviewBoxID) {
	if (xmlHttp.readyState == 4) {
		var rnObj = xmlHttp.responseText;
		
		if (reviewBoxID == 1)
		{
		    document.getElementById('helpful1').innerHTML = "Thankyou for your feedback";   
		}
		else if (reviewBoxID == 2)
		{
		    document.getElementById('helpful2').innerHTML = "Thankyou for your feedback";   
		}
		else if (reviewBoxID == 3)
		{
		    document.getElementById('helpful3').innerHTML = "Thankyou for your feedback";   
		}
		else if (reviewBoxID == 4)
		{
		    document.getElementById('helpful4').innerHTML = "Thankyou for your feedback";   
		}
	}
}

var prods = [];

function showOptions(idx) {
	document.getElementById('options' + idx).style.display = "block";
	
	checkSubOption1(idx);
}

function hideOptions(idx, prodID) {
	document.getElementById('options' + idx).style.display = "none";
	
	var ele = document.getElementsByTagName("input");
	
	for (var i = 0, len = ele.length; i < len; i++) {
		if (ele[i].className == "to" + prodID) {
			ele[i].checked = false;
		}
	}						
}

function hideShowOptions(idx, prodID) {
	if (document.getElementById('tick' + idx).checked == true) {
		showOptions(idx);
	}
	else {
		hideOptions(idx, prodID);
	}
}

function checkSubOption1(idx) {					
	document.getElementById('options' + idx).getElementsByTagName("input")[0].checked = true;
}

function checkItemsSelected() {
	var count = 0;
	var ele = document.getElementById('product-compare').getElementsByTagName("input");
	
	for (var idx = 0, len = ele.length; idx < len; idx++) {
		if (ele[idx].className.indexOf("to") != -1) {
			if (ele[idx].checked == true) {
				count++;
			}
		}
	}
	
	if (count == 0) {
		alert("No items selected");
		return false;
	}
	else if (count == 1) {
		alert("Only 1 item selected");
		return false;							
	}
	else if (count > 4) {
		alert("Please select only 4 items at once");
		return false;							
	}

	return true;
}

function CompareProducts(catID) {
	var aryProds = [];

	for (var idx = 0, len = document.getElementById('prodCompareBox_compareArea').getElementsByTagName("input").length; idx < len; idx++) {
		var ele = document.getElementById('prodCompareBox_compareArea').getElementsByTagName("input")[idx];
		
		if ((ele.checked == true)&&(ele.id.indexOf("_") != -1)) {
			var parts = ele.id.split("_");
			
			var prodID = parts[0];
			var optionID = parts[1];
			var price = parts[2];			
			
			aryProds.push(prodID + "|" + optionID + "|" + price);
		}
	}
	
	var xmlHttp = GetXmlHttpObject();
	
	if (xmlHttp == null) {
		alert("Your browser does not support AJAX!");
		return;
	}
	
	var url = "/Compare2UC.aspx?catID=" + catID + "&prods=" + aryProds.join(",") + "&date=" + (new Date());

	oldCompareArea = document.getElementById('product-compare').innerHTML;
	
	document.getElementById('CompareButton').value = "Loading....";
	document.getElementById('CompareButton').disabled = true;
	
	xmlHttp.onreadystatechange= function() { getCompareResult(xmlHttp); }

	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}

function getCompareResult(xmlHttp) {
	if (xmlHttp.readyState == 4) {
		document.getElementById('product-compare').innerHTML = xmlHttp.responseText;
		window.location.hash = "heading-product-compare";
	}
}

var oldCompareArea = "";

function submitCommentForm(threadId, storeId, prodId, name, email, subject, comment, date) {
	var xmlHttp = GetXmlHttpObject();
	
	if (xmlHttp == null) {
		alert("Your browser does not support AJAX!");
		return;
	}
	
	var url = "/submitCommentForm.aspx?threadId=" + threadId + "&storeid=" + storeId + "&productID=" + prodId + "&name=" + name + "&email=" + email + "&subject=" + subject + "&comment=" + comment + "&date=" + date + "&random=" + new Date();
    
	xmlHttp.onreadystatechange= function() { submitCommentFormFinished(xmlHttp); }

	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);

    //testing(threadId, storeId, "GBD", prodId); //tells customers that there have been replied
}

function submitCommentFormFinished(xmlHttp) {
	if (xmlHttp.readyState == 4) {
		var rnObj = xmlHttp.responseText;
		var msg = rnObj.split(":");
		
		//check for errors first
		if (msg[0] == "ERROR")
		{ //check for the second part of the message
		    if (msg[1] == "dbentry")
		    {
	            document.getElementById('add-comment').style.display = "none";
	            document.getElementById('comment-error').style.display = "block";
		    }
		    /*
		    else if (msg[1] == "retrieve email")
		    { //debugging COMMENT OUT FOR NORMAL USE
		        document.getElementById('add-comment').style.display = "none";
		        document.getElementById('comment-confirm').style.display = "block";
		        alert("error retrieving emails");
		    }
		    else if (msg[1] == "send email")
		    { //debugging COMMENT OUT FOR NORMAL USE
		        document.getElementById('add-comment').style.display = "none";
		        document.getElementById('comment-confirm').style.display = "block";
		        alert("error sending emails");
		    }
		    */
		}
		else //SUCCESS
		{
		    document.getElementById('add-comment').style.display = "none";
		    document.getElementById('comment-confirm').style.display = "block";
		}
	}
}

/*
function testing(threadID, storeID, storeName, productID) //REMOVE
{
    alert("here");
    var xmlHttp = GetXmlHttpObject();
	
	if (xmlHttp == null) {
		alert("Your browser does not support AJAX!");
		return;
	}
	
	var url = "/emailCommentReplies.aspx?threadId=" + threadID + "&storeId=" + storeID + "&storeName=" + storeName + "&productId=" + productID + "&random=" + new Date();

    xmlHttp.onreadystatechange= function() { testingFinished(xmlHttp); }

	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}

function testingFinished(xmlHttp) {
	if (xmlHttp.readyState == 4) {
		var rnObj = xmlHttp.responseText;
		alert(rnObj);
    }
}
*/

function prepareCommentForm(storeId, prodId, inDay, inMonth, inYear)
{ //prepare the form data for processing
    var comThreadId = document.getElementById('cmThread').value; //-1 if new thread
    var comStoreId = storeId;
    var comProdId = prodId;
    var comName = document.getElementById('cmName').value;
    var comEmail = document.getElementById('cmEmail').value;
    var comSubject = document.getElementById('cmSubject').value;
    var comComment = document.getElementById('cmComment').value;
    
    //get date numbers
    var day = inDay;
    var month = inMonth;
    var year = inYear;
    
    //format the day and month to be correct length
    if (day < 10) day = "0" + day;
    if (month < 10) month = "0" + month;
    
    //build date NOTE: uses mm-dd-yyyy format
    var comDate = month + "-" + day + "-" + year;
    
    submitCommentForm(comThreadId, comStoreId, comProdId, comName, comEmail, comSubject, comComment, comDate);
}

function newThread(threadId)
{ //set up form for a new thread
    document.getElementById('cmThread').value = threadId;
    document.getElementById('cmName').value = "";
    document.getElementById('cmEmail').value = "";
    document.getElementById('cmSubject').value = "";
    document.getElementById('cmComment').value = "";
    
    document.getElementById('comment-post-box').style.display = 'block'; 
    document.getElementById('add-comment').style.display = 'block'; 
    document.getElementById('comment-confirm').style.display = 'none'; 
    document.getElementById('comment-error').style.display = 'none';
}

function cancelThread()
{ //clear the form 
    document.getElementById('comment-post-box').style.display = 'none'; 
    document.getElementById('add-comment').style.display = 'none';
    
    document.getElementById('cmThread').value = -1;
    document.getElementById('cmName').value = "";
    document.getElementById('cmEmail').value = "";
    document.getElementById('cmSubject').value = "";
    document.getElementById('cmComment').value = "";
}

//Product Sizes Main
function showHideComments(elementId, expandId)	
{
    if (document.getElementById(elementId).style.display != "none")
    {
        document.getElementById(elementId).style.display = "none";
        document.getElementById(expandId).innerHTML = "<img src=\"/assets/images/product-page/expand.gif\" alt=\"\" />";
    }
    else 
    {
        document.getElementById(elementId).style.display = "block";
        document.getElementById(expandId).innerHTML = "<img src=\"/assets/images/product-page/collapse.gif\" alt=\"\" />";
    }
}

//compare products bits
function showHideCompareRows(numRows, numRowsToShow)
{
    var startRow = numRowsToShow + 1;
    for (var idx = startRow; idx < numRows; idx++)
    {
        if (document.getElementById('compareRow' + idx).style.display != "none")
        {
            document.getElementById('compareRow' + idx).style.display = "none";
            document.getElementById('showHideCompareRowsText').innerHTML = "Show all sheds <img src=\"/assets/images/product-page/expand.gif\" alt=\"\" />";
        }
        else 
        {
            document.getElementById('compareRow' + idx).style.display = "";
            document.getElementById('showHideCompareRowsText').innerHTML = "Hide extra sheds <img src=\"/assets/images/product-page/collapse.gif\" alt=\"\" />";
        }
    }
}
