function preLoader()
{
	// Create an array for the image objects
	var preLoadImage = new Array();

    // Add required image source to the array
    // Note: only preload generic images
	var imageArray = new Array();
        imageArray[0] = "images/icon_external_link_on.gif";

	for (var i = 0; i < imageArray.length; i++)
	{
		preLoadImage[i] = new Image();
		preLoadImage[i].src = imageArray[i];
	}
}

function form_init() {
	// This function styles input and button elements
	// To be used in live version
	
	var fieldType = new Array();
		fieldType[0] = "input";
		fieldType[1] = "select";
		fieldType[2] = "textarea";
		fieldType[3] = "button";

	for (var x=0; x<fieldType.length; x++) {
		var tag = new Array();
		tag = document.getElementsByTagName(fieldType[x]);
		
		for (var i=0; i<tag.length; i++) {
			tag[i].style.fontFamily = "Verdana, Arial, sans-serif";
			tag[i].style.fontSize = "12px";
			switch (tag[i].type) {
				case "submit":
				case "reset":
				case "button":
					{	switch (tag[i].id) {
							case "newsletter-submit":
							case "track-order-submit":
							case "help-btn":
								// Special styled button - coded in HTML do nothing
								break;
							
							default:
								{	// Style as a button
									tag[i].onmousedown = function styleButtonOn() {this.style.backgroundColor = "#666666"; this.style.borderColor="#999999"; this.style.color="#ffffff";}
									tag[i].onmouseup = function styleButtonOn() {this.style.backgroundColor = "#dfdfdf"; this.style.borderColor="#666666"; this.style.color="#000000";}
									break;
								}
						}
						break;
					}
				
				case "radio":
				case "checkbox":
					tag[i].style.border = "none";
					break;
				
				default:
					{	tag[i].style.borderStyle = "solid";
						tag[i].style.borderWidth = "1px";
						tag[i].style.borderColor = "#999999";
					
						switch (tag[i].id) {
							case "login-id":
							case "newsletter-email-1":
								{	tag[i].onfocus = function styleElementOn() {this.style.backgroundColor="#dfdfdf"; this.style.borderColor="#999999"; if(this.value=='Enter your email address here') this.value='';};
									tag[i].onblur = function styleElementOff() {this.style.backgroundColor="#ffffff"; if(this.value=='') this.value='Enter your email address here';};
									break;
								}
							
							case "special-interests":
								{	tag[i].onfocus = function styleElementOn() {this.style.backgroundColor="#ffffff"; this.style.borderColor="#999999";};
									tag[i].onblur = function styleElementOff() {this.style.backgroundColor="#ffffff";};
									break;
								}

								default:
								{	tag[i].onfocus = function styleElementOn() {this.style.backgroundColor="#dfdfdf"; this.style.borderColor="#999999";};
									tag[i].onblur = function styleElementOff() {this.style.backgroundColor="#ffffff";};
									
									// To fix IE 6 bug, show Title dropdown
									if (tag[i].id=="title") tag[i].style.display = "block";
									break;
								}
						}
						
						if (tag[i].id.indexOf("notes[]") != -1) 
							{	tag[i].onfocus = function styleElementOn() {this.style.backgroundColor="#dfdfdf"; this.style.borderColor="#999999"; if(this.value=='Enter your notes here as required') this.value='';};
								tag[i].onblur = function styleElementOff() {this.style.backgroundColor="#ffffff"; if(this.value=='') this.value='Enter your notes here as required';};
							}
												
						break;
					}
			}
		}
	}
	params_init();
}

// Global variables for use with params_init()

var dataMsgId;

function params_init() {
	// This function simulates messages generated a result of database interaction
	// For illustrative purposes only and is to be removed from live version
	
	var urlParam = location.search;
	
	var params = new Array();
		params[0] = "messageid";

	// Take the search string from the URL and load into variables

	for (i=0; i<params.length; i++) {
		var startPos = 0;
		var endPos = 0;
		if (urlParam.indexOf(params[i]+"=") >= 0) 
		{	switch (params[i]) {
                case "messageid":
                    startPos = urlParam.indexOf(params[i]+"=") + (params[i].length + 1);
                    endPos = urlParam.substring(startPos, urlParam.length).indexOf("&");
                    if (endPos == -1) endPos = urlParam.length;
                    dataMsgId = urlParam.substring(startPos, startPos + endPos);
                    
                    break;
            }
		}
	}
	
	/* console.log("dataMsgId = " + dataMsgId);
	console.log("returnPath = " + returnPath);
	console.log("yesRoute = " + yesRoute);
	console.log("noRoute = " + noRoute);
	console.log("trade = " + trade);
	console.log("registerReturn = " + registerReturn); 
	console.log("credit = " + credit);
	console.log("modify = " + modify);  */

	if (dataMsgId != "none" && dataMsgId) {
		var msgString = "";
		var message = document.getElementById("user-message");
		message.innerHTML = "";
	
		switch (dataMsgId) {
			case "login_failure":
				msgString = "Login ID or Password are incorrect, please retry.";
				break;
			
			case "contact_update":
				msgString = "Contact Details updated successfully.";
				break;

			case "subscriptions_update":
				msgString = "Subscriptions updated successfully.";
				break;

			case "account_update":
				msgString = "Login Information updated successfully.";
				break;

			case "trade_update":
				msgString = "Trade Details updated successfully.";
				break;

			case "account_email_verified":
				msgString = "Your email address has been verified and updated successfully.";
				break;
				
			case "email_exists":
				msgString = "Email address has already been registered, please retry.";
				break;

			case "email_sent":
				msgString = "Email successfully sent.";
				break;
				
            case "already_registered_subscriber":
				msgString = "Your are already registered as a Subscriber, please login to upgrade.";
				break;

            case "already_registered_collector":
				msgString = "You are already registered as a Collector, please login to upgrade.";
				break;

            case "already_registered_collector2":
				msgString = "You are already registered as a Collector, please login.";
				break;

            case "already_registered_trade":
				msgString = "You are already registered as a Trade Customer, please login.";
				break;

            case "upgrade_complete":
				msgString = "You login has been upgraded successfully.";
				break;
			
			case "already_registered_login":
				msgString = "Email address has already been registered, please retry.";
				break;
			
			case "wrong_information":
				msgString = "Email address or password is wrong, please retry.";
				break;	
				
			case "unsubscribe":
				msgString = "You have successfully unsubscribed from our e-Newsletter.";
				break;

            case "resubscribe":
				msgString = "You have successfully re-subscribed to our e-Newsletter.";
				break;
				
			case "already_subscribed":
				msgString = "You are already subscribed to our e-Newsletter.";
				break;

			case "subscriber_not_found":
				msgString = "This address is not subscribed to our e-Newsletter.";
				break;	
				
			case "subscription_email_sent":
				msgString = "Email has been sent to you, Please confirm your subscription";
				break;
				
			case "subscription_email_empty":
				msgString = "Please enter email address for E-Newsletter subscription";
				break;

            case "wishlist_saved":
				msgString = "Your Wish List has been saved.";
				break;
				
			case "wishlist_exists":
				msgString = "Wish List already exists with this name.";
				break;
				
			case "wishlist_updated":
				msgString = "Your Wish List has been updated.";
				break;
				
			case "wishlist_renamed":
				msgString = "Your Wish List has been renamed.";
				break;
				
			case "wishlist_deleted":
				msgString = "Your Wish List has been deleted.";
				break;
				
			case "password_sent":
				msgString = "Your Password has been emailed to you.";
				break;
				
			case "user_not_found":
				msgString = "Email address not registered. Please <a href='http://www.urbangalleries.com/test/contactus' rel='section' rev='help' onclick='openDialog(this.href); return false;'>contact us</a> if your require assistance.";
				break;
			
			case "invalid_email_address":
				msgString = "Email address is invalid, please retry.";
				break;	
						
			case "mandatory_fields":
				msgString = "Required fields, please retry.";
				break;	

            case "hotmail_address":
				msgString = "Regrettably Hotmail addresses are not supported.";
				break;	
				
		}
		message.innerHTML = msgString;
	
	}

}

function javaScriptEnabled()  {
	document.getElementById("javascript-enabled").style.display="block";
}

function scrollToBottom(url)  {
	if (url.indexOf("messageid=email_sent") > 0) {
		var documentHeight;
		
		if (document.height)
			documentHeight = document.height;
		else
			{	if (document.body.scrollHeight)
					documentHeight = document.body.scrollHeight;
			}
		
		window.scrollTo(0,documentHeight);
	}
}

function openDialog (page, dialogWidth, dialogHeight, resizeable) {
	//opens a pop-up dialog and centers it on the screen
	// To be used in live version
	
	if(dialogWidth==null) dialogWidth = 601;
	if(dialogHeight==null) dialogHeight = 500;
	if(resizeable==null) dialogResize = 'no';
    else {dialogResize = resizeable;}
    var xPos = (screen.width - dialogWidth) / 2;
	var yPos = (screen.height - dialogHeight) / 2;
	var properties = "";
	properties = properties + "width=" + dialogWidth + ",";
	properties = properties + "height=" + dialogHeight + ",";
	properties = properties + "resizable=" + dialogResize +",";
	properties = properties + "menubar=no,";
	properties = properties + "toolbar=no,";
	properties = properties + "status=no,";
	properties = properties + "scrollbars=yes,";
	properties = properties + "left=" + xPos + ",";
	properties = properties + "top=" + yPos;
	
	window.open (page,'', properties);

}

function init_overlay () {
	// This function presents and resizes the transparent png overlay for javaScript enabled browsers
	// If Internet Explorer 6 (does not support .png)  the background colour is not changed to the transparency image
	// To be used in live version

	if (navigator.appVersion.indexOf("MSIE 6") == -1) {
		document.body.style.backgroundColor = "#ffffff";
		document.getElementById("maincontainer").style.display = "block";
		var x = document.getElementById("overlay");
		x.style.background = "url(images/transparency.png)";
		
		if (navigator.appVersion.indexOf("Safari") == -1) {
			var documentHeight = document.documentElement.scrollHeight;
			x.style.height = documentHeight + "px";
		}
	}

}
function getEmailAddress(addressName, preString, stringNumber, postString) {
	emailAddress = addressName + "@urbangalleries.com";
	var htmlString = preString + "<a class='email-link' href='mailto:" + emailAddress + "' rel='nofollow'>" + emailAddress + "</a>" + postString;
	document.getElementById("email-string-" + stringNumber).innerHTML = htmlString;;

}

function autoPopulateAlt(screenName) {
    switch (screenName) {
    
        case "product":
            var selected = document.getElementById("artist_id").selectedIndex;
            var selectedArtist = document.getElementById("artist_id").options[selected].text;
            document.getElementById("large_image_alt").value =  document.getElementById("title").value + ' by ' + selectedArtist;
            document.getElementById("thumbnail_image_alt").value = document.getElementById("title").value + ' by ' + selectedArtist;
            break;

        case "artist":
            document.getElementById("portrait_alt").value =  'Portrait of ' + document.getElementById("first_name").value + ' ' + document.getElementById("last_name").value;
            document.getElementById("page_title").value =  document.getElementById("first_name").value + ' ' + document.getElementById("last_name").value;
            document.getElementById("meta_description").value =  document.getElementById("first_name").value + ' ' + document.getElementById("last_name").value;
            
            break;
    }
}

function generateMetaKeywords(screenName) {
    
    if (confirm ("This will overwrite the current Meta Keywords.  Continue?")) 
        {
            var keywordString = "";
            var genericKeywords = "Limited Edition, Urban Galleries, Urban Fine Art Galleries Limited, Vancouver, Artwork, Art, Contemporary Art";
            genericKeywords = genericKeywords + ", Commercial Art, Commercial Artists, Best of British Artists, British Artists, Washington Green Fine Art, Washington Green, Demontfort Fine Art";
            var selected = 0;
            switch (screenName) {
            
                case "product":
                    selected = document.getElementById("artist_id").selectedIndex;
                    if (selected!=0)
                        {keywordString = document.getElementById("artist_id").options[selected].text;}
                    
                    if (document.getElementById("title").value!="")
                        {keywordString = addCommaCheck(keywordString) + document.getElementById("title").value;}

                    selected = document.getElementById("select").selectedIndex;
                    if (selected!=0)
                        {keywordString = addCommaCheck(keywordString) + document.getElementById("select").options[selected].text;}

                    selected = document.getElementById("art_medium_id").selectedIndex;
                    if (selected!=0)
                        {keywordString = addCommaCheck(keywordString) + document.getElementById("art_medium_id").options[selected].text;}

                    selected = document.getElementById("category_id").selectedIndex;
                    if (selected!=0)
                        {keywordString = addCommaCheck(keywordString) + document.getElementById("category_id").options[selected].text;}
                    
                    selected = document.getElementById("color_scheme_id").selectedIndex;
                    if (selected!=0)
                        {keywordString = addCommaCheck(keywordString) + document.getElementById("color_scheme_id").options[selected].text;}
                    
                    selected = document.getElementById("mood_id").selectedIndex;
                    if (selected!=0)
                        {keywordString = addCommaCheck(keywordString) + document.getElementById("mood_id").options[selected].text;}


                    break;
                    
                case "artist":
                    if (document.getElementById("first_name").value != "" && document.getElementById("last_name").value != "")
                        {keywordString = document.getElementById("first_name").value + " " + document.getElementById("last_name").value;}
                
                    selected = document.getElementById("country_domicile").selectedIndex;
					keywordString = addCommaCheck(keywordString) + document.getElementById("country_domicile").options[selected].text;

                    selected = document.getElementById("category_id").selectedIndex;
                    if (selected!=0)
                        {keywordString = addCommaCheck(keywordString) + document.getElementById("category_id").options[selected].text;}

                    break;
                
             }
            document.getElementById("meta_keywords").value = addCommaCheck(keywordString) + genericKeywords;
        }
}

function addCommaCheck(inputString) {

    if (inputString!="") 
        {return inputString + ", ";}
    else
        {return inputString;}
        
}

function checkProductStatus(submitType, rowId, siteUrl) {
    
    if (document.getElementById("status-tag").innerHTML.indexOf("Sold Out") >=0) 
        {
        alert ("Sorry, you cannot add this item to your " + submitType + " as it has sold out");
        }
    else if(document.getElementById("status-tag").innerHTML.indexOf("Contact Us") >=0 || document.getElementById("status-tag").innerHTML.indexOf("On Order") >=0)
        {
        alert ("Sorry, you cannot add this item to your " + submitType + ", please contact us to confirm availability");
        }
    else    
        {   switch (submitType) {
                case "order":
                    document.getElementById('artworkselection').submit();
                    break;
                    
                case "wish list":
                    window.top.location = siteUrl + "add2wishlist?product_id=" + rowId;
                    break;
            }
        }
}

function emailToSelf(obj) {
    if (obj.checked==true) {
        document.getElementById('recipient-name').value = document.getElementById('from-name').value;
        document.getElementById('recipient-email').value = document.getElementById('from-email').value;
    } else {
        document.getElementById('recipient-name').value = '';
        document.getElementById('recipient-email').value = '';
    }
}

function submitOrder() {
    if (document.getElementById("terms-accepted").checked == true) {
        //submit order
        document.getElementById("order-summary").submit();}
        
    else {
        alert ("To continue, please acknowledge that you accept our Terms & Conditions.");
    }

}