// Product-info page function which opens new image window for large product shots
function getImageXtraLarge(pExistingImageID, ImageURL, imageWidth, imageHeight)
{
	$imageLink = document.getElementById(pExistingImageID).src; // Get image link using the img id (placeholder)
	
	$imageName=$imageLink.split("/"); // Because the link contains directorys, use split to create an array
	$arrayLength=$imageName.length-1; // We only need the last array value so obtain it using length minus 1.
	$imageNameNoId = $imageName[$arrayLength].split("%257E");
	// Open new window with the url created with [PRD_XTRA_LARGE_LINK] and the image name (taken from the created array)
	newwindow=window.open("/library/image_popup/popup.php?url="+ImageURL+rawurlencode($imageName[$arrayLength])+"&clTxt=Click on image to close window", "mywindow","menubar=1,resizable=1"); // ,'name','height='+imageWidth+', width='+imageHeight+', location=yes'

	if (window.focus) 
	{
		newwindow.focus()
	}
}

function rawurldecode( str ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Brett Zamir (http://brettz9.blogspot.com)
    // *     example 1: rawurldecode('Kevin+van+Zonneveld%21');
    // *     returns 1: 'Kevin+van+Zonneveld!'
    // *     example 2: rawurldecode('http%3A%2F%2Fkevin.vanzonneveld.net%2F');
    // *     returns 2: 'http://kevin.vanzonneveld.net/'
    // *     example 3: rawurldecode('http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a');
    // *     returns 3: 'http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a'
 
    var histogram = {};
    var ret = str.toString(); 
 
    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };
 
    // The histogram is identical to the one in urlencode.
    histogram["'"]   = '%27';
    histogram['(']   = '%28';
    histogram[')']   = '%29';
    histogram['*']   = '%2A';
    histogram['~']   = '%7E';
    histogram['!']   = '%21';
 
    for (replace in histogram) {
        search = histogram[replace]; // Switch order when decoding
        ret = replacer(search, replace, ret) // Custom replace. No regexing
    }
 
    // End with decodeURIComponent, which most resembles PHP's encoding functions
    ret = decodeURIComponent(ret);
 
    return ret;
}

function rawurlencode( str ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Brett Zamir (http://brettz9.blogspot.com)
    // *     example 1: rawurlencode('Kevin van Zonneveld!');
    // *     returns 1: 'Kevin van Zonneveld%21'
    // *     example 2: rawurlencode('http://kevin.vanzonneveld.net/');
    // *     returns 2: 'http%3A%2F%2Fkevin.vanzonneveld.net%2F'
    // *     example 3: rawurlencode('http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a');
    // *     returns 3: 'http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a'
 
    var histogram = {}, tmp_arr = [];
    var ret = str.toString();
 
    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };
 
    // The histogram is identical to the one in urldecode.
    histogram["'"]   = '%27';
    histogram['(']   = '%28';
    histogram[')']   = '%29';
    histogram['*']   = '%2A'; 
    histogram['~']   = '%7E';
    histogram['!']   = '%21';
 
    // Begin with encodeURIComponent, which most resembles PHP's encoding functions
    ret = encodeURIComponent(ret);
 
    // Restore spaces, converted by encodeURIComponent which is not rawurlencode compatible
    ret = replacer('%20', ' ', ret); // Custom replace. No regexing
 
    for (search in histogram) {
        replace = histogram[search];
        ret = replacer(search, replace, ret) // Custom replace. No regexing
    }
 
    // Uppercase for full PHP compatibility
    return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2) {
        return "%"+m2.toUpperCase();
    });
 
    return ret;
}

// Function to get medium image and display it on product info page.
function getImage(pExistingImageID, pImageURL) // Pass in div id (placeholder) and image url
{ 

	//var zoomimage = '/library/ZoomImage.php?src=';
	var zoomimage = '';
    var img = document.createElement('img'); // Build image element
    
    img.onload = function (evt) // Create a function which is run when the image loads and appends our existing page (without having to reload it).
    {
        document.getElementById(pExistingImageID).src=this.src;
        document.getElementById(pExistingImageID).width=this.width;
        document.getElementById(pExistingImageID).height=this.height;
    }
    img.src = zoomimage+pImageURL;
}

function checkTerms() { // Check T & C box is ticked before proceding to paypal
	missinginfo = '';
	if (!document.agreement_form.agreement.checked) 
	{
		missinginfo ='Required information is missing: \n' +
		missinginfo + '\n - You must agree to the Terms and Conditions';
		alert(missinginfo);
		return false;
	}
	else 
	{
		return true;
	}
}

function doNothing() {
}

function clearTextField(field_name) 
{	
	if(document.getElementById(field_name).value == 'Enter your search term here')
	{
		document.getElementById(field_name).value = '';
	}
	return true;
}

// for retreat summery
function CheckMessageLength() 
{
	var ARLineCount
	var maxLen = 250;                                // the maximum length of message allowed
	var maxWidth = 250;                                // the maximum width of message allowed
	var maxLine = 6;                                // the maximum line number of message allowed
	var MsgLen = document.add_retreat.retreat_summary.value.length;
	var strMessage= document.add_retreat.retreat_summary.value;
	var strNewMessage = new String()
	strMessage = document.add_retreat.retreat_summary.value.replace(/\r\n/g, "\r")
	strMessage = strMessage.replace(/\n\r/g, "\r")
	strMessage = strMessage.replace(/\r/g, "\r")
	strMessage = strMessage.replace(/\n/g, "\r")
	var stringArray = strMessage.replace(/\r/g, " ").split(" ");
	ARLineCount = strMessage.split("\r");
	
	if(MsgLen > maxLen) {
			document.add_retreat.retreat_summary.value = document.add_retreat.retreat_summary.value.substring(0, maxLen);
			alert("Please limit the word length of your summary to " + maxLen + " characters");
	}
	
	for (var i=0; i < stringArray.length; i++)
	{
		if(stringArray[i].length >= maxWidth){
			strNewMessage = strMessage.replace(stringArray[i], stringArray[i].substring(0, maxWidth-1))
			document.add_retreat.retreat_summary.value = strNewMessage
			alert("Please limit the word width of your summary to " + maxWidth + " characters");
			break;
		}
	}
	
	if(ARLineCount.length > maxLine) {
		alert("Please limit the number of lines in your summary to " + maxLine + " lines");
		strNewMessage = ""
		for(var i=0; i<(ARLineCount.length-1); i++) {
			if(i+1 == maxLine) {
				strNewMessage = strNewMessage + ARLineCount[i];
			} else {
				strNewMessage = strNewMessage + ARLineCount[i] + "\n";
			}
		}
		document.add_retreat.retreat_summary.value = strNewMessage
	}
	
	document.add_retreat.MessageLength.value = document.add_retreat.retreat_summary.value.length;
}

function CodeEmailAddress($email){
var outString = "";
var inString = $email;
for(i=0;i<inString.length;i++){
outString+="&#"+inString.charCodeAt(i)+";"
}

var firstPart="<script type=\"text/javascript\">\n";
firstPart+="<!--\n document.";
firstPart+="write(\"<li><a href='mailto:";
firstPart+=outString;
firstPart+="'>";
firstPart+=$email;
//firstPart+='contact us';
firstPart+="</a></li>\")\n";
firstPart+="//"
firstPart+=" -->"
firstPart+="\n<"
firstPart+="/script>";
outString=firstPart
return outString;
}
