// functions that are not upload/cart/payment related nor contact detail related

function cartAdd(myform, imageID, imgName) {
	// imgName is no longer used
	var checkthis = "document." + myform + ".option.value";
	if (myform=="add000"){
		var option_val = 99;
		for (var i=0; i < document.add000.option.length; i++){
			if (document.add000.option[i].checked){
				option_val = document.add000.option[i].value;
			}
		}
		var wrap_val = 99;
		for (var i=0; i < document.add000.wrap.length; i++){
			if (document.add000.wrap[i].checked){
				wrap_val = document.add000.wrap[i].value;
			}
		}
		if (wrap_val == "gallery"){
			var wrap_descr = "Gallery wrap";
		}
		else if (wrap_val == "mirror"){
			var wrap_descr = "Mirror wrap";
		}
		else if (wrap_val == "white"){
			var wrap_descr = "White edge";
		}
		else if (wrap_val == "black"){
			var wrap_descr = "Black edge";
		}
		else {
			var wrap_descr = "Unknown wrap";
		}
	}
	else {
		// for non-uploaded ones, i.e. from gallery
		var wrap_descr = "Gallery wrap";
	}
	var submitthis = "document." + myform + ".";
	if (myform!="add000" && eval(checkthis) == null){
		alert('Please select canvas size');
		//return false;
	}
	else if (option_val == 99 || eval(checkthis) == 99){
		alert('Please select canvas size');
		//return false;
	}
	else if (wrap_val == 99){
		alert('Please select wrapping style');
		//return false;
	}
	else {
		// split string when come across "g". mySplitResult[0] = "im" and mySplitResult[1] = "123" (or whatever)
		var mySplitResult = imageID.split("g");
		if (imageID=="img000"){
			mcpNameValue = "Uploaded image [" + document.add000.uploadedJPG.value + "]";
			mcpNumberValue = "0000";
		} else if (imageID!="img000"){
			mcpNameValue = "Image ID: 0" + mySplitResult[1];
			mcpNumberValue = "0" + mySplitResult[1];
		} else {
			mcpNameValue = "Error!";
			mcpNumberValue = "9999";
		}
		if (myform!="add000"){
			var CanvasImgValue = eval("document." + myform + ".option.options[document." + myform + ".option.selectedIndex].value");
		}
		else {
			var CanvasImgValue = option_val;
		}
		var CanvasImgArray = new Array();
		CanvasImgArray = CanvasImgValue.split('x');
		var CanvasImgWidth = CanvasImgArray[0];
		var CanvasImgHeight = CanvasImgArray[1];
		var CanvasImgWidthNum = CanvasImgWidth;
		// make numerical by performing mathematical operation (except +)
		CanvasImgWidthNum = CanvasImgWidthNum - 0;
		var CanvasImgHeightNum = CanvasImgHeight;
		// make numerical by performing mathematical operation (except +)
		CanvasImgHeightNum = CanvasImgHeightNum - 0;
		if (CanvasImgWidthNum > CanvasImgHeightNum){
			var myPrice = eval("price" + CanvasImgHeight + "x" + CanvasImgWidth);
		} else {
			var myPrice = eval("price" + CanvasImgWidth + "x" + CanvasImgHeight);
		}
		var CanvasDescrMsg = 'Stretched canvas print - ' + CanvasImgWidth + 'x' + CanvasImgHeight + ' inches<br>' + wrap_descr + '<br>' + mcpNameValue;
		document.forms[myform].elements['desc'].value = CanvasDescrMsg;
		if (CanvasImgWidthNum < 10){
			CanvasImgWidth = 0 + CanvasImgWidth;
		}
		if (CanvasImgHeightNum < 10){
			CanvasImgHeight = 0 + CanvasImgHeight;
		}
		if (imageID=="img000"){
			document.forms[myform].elements['general_code'].value = CanvasImgWidth + CanvasImgHeight + mcpNumberValue;
		} else {
			document.forms[myform].elements['code'].value = CanvasImgWidth + CanvasImgHeight + mcpNumberValue;
		}
		document.forms[myform].elements['price'].value = myPrice;
		// use this for testing
		//document.forms[myform].elements['price'].value = 0.01;
		breakvar = 0; // reset variable
		if(breakvar == 1){  
		}			
		else {
			eval(submitthis + "submit()");
		}
	}
}

function Set_Cookie( name, value, expires, path, domain, secure ){
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );

	if ( expires ){
	expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	
	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
	( ( path ) ? ";path=" + path : "" ) + 
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}

function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f
	
	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );
		
		
		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
	
		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}

function Delete_Cookie( name, path, domain ) {
if ( Get_Cookie( name ) ) document.cookie = name + "=" +
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function isCookiesEnabled(){
	// remember, these are the possible parameters for Set_Cookie:
	// name, value, expires, path, domain, secure
	Set_Cookie( 'test', 'none', '', '/', '', '' );
	// if Get_Cookie succeeds, cookies are enabled, since 
	//the cookie was successfully created.
	if ( Get_Cookie( 'test' ) ){
		//document.write( 'cookies are currently enabled.' );
		cookie_set = true;
		// and these are the parameters for Delete_Cookie:
		// name, path, domain
		// make sure you use the same parameters in Set and Delete Cookie.
		Delete_Cookie('test', '/', '');
	}
	// if the Get_Cookie test fails, cookies 
	//are not enabled for this session.
	else {
		document.write( '<div id="no_js"><span class="bold">You have browser cookies disabled. This website depends on cookies being enabled. Please enable cookies in your browser\'s settings and refresh this page.</span><br>For Internet Explorer: Tools -> Internet Options -> Privacy -> Advanced -> First-party Cookies<br>For Firefox: Tools -> Options -> Privacy -> Accept cookies from sites</div>' );
		cookie_set = false;
	}
}


function textCounter(field,cntfield,maxlimit) {
	var TheLength = field.value.length;
	var CharLeft = maxlimit - field.value.length;
	if (field.value.length > maxlimit){
		document.getElementById("char_text").innerHTML = "You are using too many characters (maximum is 750). You have " + TheLength + " characters.";
	}
	else {
		document.getElementById("char_text").innerHTML = CharLeft + " characters left (maximum is 750).";
	}
}

