﻿/*
File: KarsaForum.js
Author: Marek Skotnica(mskotnica@seznam.cz)
Copyright: Karsa Technologies, s.r.o.
Version: 1.0

This file contains: 
KarsaCaptcha.js                - 1.0
KarsaValidate.js               - 1.0.1 
KarsaKarsaProductForum.js      - 2.0

This file requies: 
KarsaFramework.js              - 1.0 or less 


*/

/********************************************************* KARSACAPTCHA.JS 1.0 **********************************************************/
var KarsaCaptcha = {
	captchaImgElementName : 'obrazekCAPTCHA',
	captchaBase : '', 
	captchaTimeCode : '', 
	Init : function(captchaBase, captchaTimeCode){
		this.captchaBase		= captchaBase;
		this.captchaTimeCode	= captchaTimeCode;
	} ,

	Get : function(code) {
		return KarsaAjax.DirectCall(this.captchaBase + '?code='+code, null, 'GET').replace(/^\s+|\s+$/g, '');
	} , 
	
	Refresh : function() {
		$(this.captchaImgElementName).src += "0";	
	}, 
	
	Show : function() {
		$(this.captchaImgElementName).src = this.captchaBase + '?time=' + this.captchaTimeCode;
	}
	
}	
/********************************************************* KARSAVALIDATE.JS 1.0.1 *******************************************************/


//////////////////////////////////////////////////////////////////////
//class for validation common formats
//Functions: 
//		KarsaValidate.Psc(psc)  - Validation PSC
//		KarsaValidate.CzePhone(phoneNumber) Validation czech phone number
//		KarsaValidate..Email(eMail) - Validation e-mail 
//////////////////////////////////////////////////////////////////////

var KarsaValidate = {
    ///<summary>This class contains common validation functions.</summary>
	Psc: function(psc){
	    ///<summary>Validate czech psc. Alowed PSC are "12345" and "123 45"</summary>
        ///<param name="psc">Psc we want to test in string. </param>
        ///<returns type="bool">True if psc is valid.</returns>
		var rx_psc = /^\d{3} ?\d{2}$/;
		return rx_psc.test(psc);
	} ,
	
	CzePhone: function (phone){
		///<summary>Validate czech phone.</summary>
        ///<param name="phone">Phone we want to test in string. </param>
        ///<returns type="bool">True if phone number is valid.</returns>
		var rx_cze_phone = /^(\+\d{3})? ?\d{3} ?\d{3} ?\d{3}$/;
		return rx_cze_phone.test(phone);
	} , 

	Email: function(email) {
		///<summary>validate email acording RFC2822 specification</summary>
        ///<param name="email">Email we want to test in string. </param>
        ///<returns type="bool">True if email is valid.</returns>
		var rx_local_strict = /^[a-zA-Z0-9\!\#\$\%\*\/\?\|\^\{\}\`\~\&\'\+\-\=\_\.]{1,64}@[^\@]+$/;
		var rx_local_quotes = /^\".{1,62}\"@[^\@]+$/;
		var rx_domain_named = /@[a-zA-Z0-9\-\.]{1,255}$/;
		var rx_domain_addr = /@\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\]$/;
		var rx_wrong1 = /(\.\.)|(^\.)|(\.@)|(@\.)/;
		var rx_wrong2 = /^[^\"].*@+.*[^\"]@[^\@]*$/;
		if ((rx_local_strict.test(email) || rx_local_quotes.test(email)) &&
		(rx_domain_named.test(email) || rx_domain_addr.test(email)) &&
		!rx_wrong1.test(email) &&
		!rx_wrong2.test(email)) {
			return true;
		}
		else {
			return false;
		}
	}, 
	
	TextField : function(field, minValue, maxValue){
		///<summary>Simple validate domElement value if match min and max length.</summary>
        ///<param name="field" value = "domElement" >field with value</param>
        ///<param name="minValue" value = "int" >Minimal value length.</param>
        ///<param name="maxValue" value = "int" >Maximum value length.</param>
        ///<returns type="bool">True if field value is in range. </returns>
		if(field.value.length > minValue && field.value.length < maxValue)
			return true;
		return false;
	}

}

/********************************************************* KARSAPRODUCTFORUM.JS 2.0 *******************************************************/

var KarsaProductForum = {
	
	//global settings
	//max length of comment text
	maxCommentLength	: 2000, 
	//max length of name
	maxNameLength		: 50, 
	//max length of subject
	maxSubjectLength	: 50, 
	//message to display when format is incorrect
	badFieldsFormatMsg  : 'Vsechny polozky musite vyplnit, email musi mit spravny format.', 
	//message to displaz when captcha is incorrect
	badCaptchaMsg		: 'Spatne vyplneny kontrolni kod',
	//private fields
	//url base path to ProductForumPage.aspx
	urlBase				: '', 
	//id of current selected input box(state whitch is holded on reply)
	forumID				: 0, 
	//id of parent object when reply, default is first div ID (under add new post button)
	currentContainerID	: 'content_formular', 
	//id of field name in html
	nameFieldName		: 'comment_jmeno', 
	//id of field subject in html
	nameFieldSubject	: 'comment_subject', 
	//id of field text in html
	nameFieldTxt		: 'comment_txt', 
	//id of field email in html
	nameFieldEmail		: 'comment_email', 
	//id of captcha img field
	nameFieldCode		: 'code', 
	
	Init : function(urlBase, captchaBase, captchaTimeCode){
		///<summary>Init function sets correct urls for ajax purposes. Requied params are two resolved urls and timeCode.</summary>
        ///<param name="urlBase" type = "string" >Url path to ProductForumPage.aspx (or similar aspx page) where are posted data. Ex. <%=ResolveUrl("~/ProductForumPage.aspx")%></param>
        ///<param name="captchaBase" type = "string" >Resolved url adress to captcha.ashx page. Ex. <%=ResolveUrl("~/captcha.ashx")%></param>
        ///<param name="captchaTimeCode" type = "string" >Code to protect javascript flying effect. Try to find aspx function FlyCode() .</param>
		this.urlBase = urlBase;
		KarsaCaptcha.Init(captchaBase, captchaTimeCode);
	},
	
	ShowForm : function(controlID, forumItemID){
	    ///<summary>Show product forum form in requied div and sets current formID.</summary>
        ///<param name="controlID" type = "string" >ajax call url</param>
        ///<param name="forumItemID" type = "id" >id of new active form.</param>
        //show captcha
        KarsaCaptcha.Show();
        //switch form to another div and set current forum id...
		var control = $(controlID);
		var current = $(this.currentContainerID);
		
		this.forumID = forumItemID;
		if (controlID!=this.currentContainerID) {
			control.innerHTML = current.innerHTML;
			current.innerHTML = "";
		}
		current.style.display='inline';
		this.currentContainerID = controlID;
	}, 
	
	HideForm : function(){
		///<summary>Action witch is performed after correct submit form. Refresh page. </summary>
		//THIS CAN BE REWRITED INTO FULL AJAX IF ANYONE WILL NEED IT...
		window.location.reload();
	}, 
	
	SendForm : function() {
		///<summary>Validate form data, captcha and submit.</summary>	
		if (this.ValidateAndSubmit()) {
			var valid = KarsaCaptcha.Get($(this.nameFieldCode).value);
			if (valid == '1') {
				this.AjaxCall($(this.nameFieldName).value, $(this.nameFieldSubject).value, $(this.nameFieldTxt).value, $(this.nameFieldEmail).value, this.forumID);
			} else {
				alert(this.badCaptchaMsg);
			}
		}
	}, 
	
	AjaxCall : function(name,subject,txt,email,forID) {
		///<summary>Ajax call to submit post into urlBase aspx page.</summary>
        ///<param name="name" type = "string" >user name</param>
        ///<param name="subject" type = "string" >subject</param>
        ///<param name="txt" type = "string" >text</param>
        ///<param name="email" type = "string" >email</param>
        ///<param name="forID" type = "int" >submited form id, to identify new or reply post</param>
		var DELIMITER = "*+*|_~_|-**";
		//gets the product value
		var productID =	$('productID').value;
		//submit product forum form by ajax and show server message
		alert(KarsaAjax.DirectCall(this.urlBase, name+DELIMITER+productID+DELIMITER+forID+DELIMITER+email+DELIMITER+subject+DELIMITER+txt));
		//call some refresh page
		this.HideForm();
	} , 
	
	AjaxCallScore : function(id, score, message){
		///<summary>Score function for rating posts. </summary>
        ///<param name="id" type = "string" >id of post</param>
        ///<param name="score" type = "string" >score diference</param>
        ///<param name="message" type = "string" >message to display after score is added</param>
		var url = urlBase+'?id='+id+'&score='+score;
		KarsaAjax.DirectCall(url, null, 'GET');
		HideScoreBox(id,message);
	} , 
	
	HideScoreBox : function(id, message){
		///<summary>Hide score box after score is added. </summary>
        ///<param name="id" type = "string" >id of post</param>
        ///<param name="message" type = "string" >message to display after score is added</param>
		var scoreBox = $('scoreBox'+id);
		if (scoreBox) {
			scoreBox.innerHTML = "("+message+")";
		}
	} , 
	
	SetTableColor : function(tdID) {
		///<summary>Some grafic dynamic stuff. </summary>
        ///<param name="tdId" type = "string" >Id of current td.</param>
		var obj =	$(tdID);
		var obj1 = $("td1");var obj2 = $("td2");var obj3 = $("td3");var obj4 = $("td4");
		obj1.style.backgroundColor = "#eeeeee";obj2.style.backgroundColor = "#eeeeee";obj3.style.backgroundColor = "#eeeeee";obj4.style.backgroundColor = "#eeeeee";
		obj.style.backgroundColor = "#FFCC99";
	}, 
	
	ValidateAndSubmit : function() {
		///<summary>Function to validate fields.</summary>
		if ( KarsaValidate.TextField($(this.nameFieldName), 1, this.maxNameLength) &&
			 KarsaValidate.TextField($(this.nameFieldSubject), 1, this.maxSubjectLength) &&
			 KarsaValidate.TextField($(this.nameFieldTxt), 1, this.maxCommentLength) &&
			 KarsaValidate.Email($(this.nameFieldEmail).value) )
			{
				return true;
			} else {
				alert(this.badFieldsFormatMsg);
				return false;
			}
	}
}
